From c811f0f0f3bc8737c8c46368f53a75b439a842e5 Mon Sep 17 00:00:00 2001 From: nxyt Date: Thu, 22 Jun 2023 13:25:43 +0200 Subject: [PATCH] added some basic tests and fixed config marchaling issues --- README.md | 3 ++- cmd/command_test.go | 28 ++++++++++++++++++++++++++++ cmd/printConfig_test.go | 10 ++++++++++ config/command.go | 24 ++++++++++++++++++++++++ config/config_test.go | 4 ++++ start/start.go | 16 ---------------- test/countsErrors_test.go | 7 +++++++ utils/project_root.go | 2 +- utils/project_root_test.go | 14 ++++++++++++++ 9 files changed, 90 insertions(+), 18 deletions(-) create mode 100644 cmd/command_test.go create mode 100644 cmd/printConfig_test.go delete mode 100644 start/start.go create mode 100644 test/countsErrors_test.go create mode 100644 utils/project_root_test.go diff --git a/README.md b/README.md index 57e53a1..d2484d2 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,8 @@ Monorepo management tool that's extensible and will fit any workflow. - Extensible using any shell that's available on the system. ☑ - Discoverable: allows newcomers to easily find out all parts of the system they can interact with. ☑ - Smart testing makes CI pipelines faster. ☒ -- Integrates with other tools like telepresence. ☒ +- Post command hooks allow you to run script after specific command. ☒ +- Integrates with other tools like telepresence. ☑ - Works with Docker Compose, Kubernetes and standalone apps. ☑ - Composable: configuration can be split into many files. ☑ - Commands work from any directory within repository. ☑ diff --git a/cmd/command_test.go b/cmd/command_test.go new file mode 100644 index 0000000..94783ea --- /dev/null +++ b/cmd/command_test.go @@ -0,0 +1,28 @@ +package cmd + +import ( + "encoding/json" + + "optimus/config" + "testing" +) + +func TestCommandCanBeMarchaled(t *testing.T) { + c := &config.Cmd{ + Run: "", + Path: "", + Name: "", + Description: "", + File: "", + Shell: "", + CommandFunc: func() error { + println("elo co tam") + return nil + }, + } + + _, err := json.MarshalIndent(c, "", " ") + if err != nil { + panic(err) + } +} diff --git a/cmd/printConfig_test.go b/cmd/printConfig_test.go new file mode 100644 index 0000000..cc4d9b3 --- /dev/null +++ b/cmd/printConfig_test.go @@ -0,0 +1,10 @@ +package cmd + +import ( + "testing" +) + +func TestConfigCanBeMarchaled(t *testing.T) { + // Execute() + printConfigCmd.Run(nil, []string{""}) +} diff --git a/config/command.go b/config/command.go index 0aa1996..74f9676 100644 --- a/config/command.go +++ b/config/command.go @@ -1,12 +1,15 @@ package config import ( + "encoding/json" "fmt" "log" + // "optimus/utils" "os" "os/exec" + // "strings" "github.com/spf13/cobra" @@ -22,6 +25,27 @@ type Cmd struct { CommandFunc func() error } +func (c Cmd) MarshalJSON() ([]byte, error) { + fmt.Println("elo") + return json.Marshal(&struct { + Run string + Path string + Name string + Description string + File string + Shell string + CommandFunc string + }{ + Run: c.Run, + Path: c.Path, + Name: c.Name, + Description: c.Description, + File: c.File, + Shell: c.Shell, + CommandFunc: "Function", + }) +} + func ParseCmd(name string, root string, a any) Cmd { command := Cmd{ Run: "", diff --git a/config/config_test.go b/config/config_test.go index 5b7b700..c2d5c9f 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -14,3 +14,7 @@ func TestLoadConfig(t *testing.T) { } fmt.Printf("c: %v\n", string(j)) } + +func TestConfigCanBeMarchaled(t *testing.T) { + +} diff --git a/start/start.go b/start/start.go deleted file mode 100644 index 54067c6..0000000 --- a/start/start.go +++ /dev/null @@ -1,16 +0,0 @@ -package start - -import "github.com/spf13/viper" - -func Start(dirPath string) { - viper.SetConfigType("yaml") - viper.SetConfigName("optimus") - viper.AddConfigPath(dirPath) - - err := viper.ReadInConfig() - if err != nil { - println("command failed: ", err) - } - z := viper.GetString("start") - println(z) -} diff --git a/test/countsErrors_test.go b/test/countsErrors_test.go new file mode 100644 index 0000000..c256508 --- /dev/null +++ b/test/countsErrors_test.go @@ -0,0 +1,7 @@ +package test + +import "testing" + +func TestCountsErrorsProperly(t *testing.T) { + +} diff --git a/utils/project_root.go b/utils/project_root.go index 6b0d47e..01215d3 100644 --- a/utils/project_root.go +++ b/utils/project_root.go @@ -31,7 +31,7 @@ func ProjectRoot() string { return "" })() if p == "" { - panic("no path found") + panic("Not a git repository") } return p diff --git a/utils/project_root_test.go b/utils/project_root_test.go new file mode 100644 index 0000000..c5b4e39 --- /dev/null +++ b/utils/project_root_test.go @@ -0,0 +1,14 @@ +package utils + +import ( + "os" + "testing" +) + +func TestReturnsPath(t *testing.T) { + p := ProjectRoot() + _, err := os.ReadDir(p) + if err != nil { + t.Error(err) + } +}