From 93d122afeb732bd949f38bc838045a2439b1b75d Mon Sep 17 00:00:00 2001 From: Fokion Sotiropoulos Date: Thu, 10 Aug 2023 14:13:52 +0100 Subject: [PATCH] adding test in the root package in order to test the run command Signed-off-by: Fokion Sotiropoulos --- cmd/venom/root/root.go | 2 +- cmd/venom/root/root_test.go | 36 ++++++++++++++++++++++++++++++++++++ cmd/venom/run/cmd.go | 28 ++++++++++++++++------------ venom.go | 9 +++++++++ 4 files changed, 62 insertions(+), 13 deletions(-) create mode 100644 cmd/venom/root/root_test.go diff --git a/cmd/venom/root/root.go b/cmd/venom/root/root.go index 36d90914..c34c4540 100644 --- a/cmd/venom/root/root.go +++ b/cmd/venom/root/root.go @@ -18,7 +18,7 @@ func New() *cobra.Command { return rootCmd } -//AddCommands adds child commands to the root command rootCmd. +// AddCommands adds child commands to the root command rootCmd. func addCommands(cmd *cobra.Command) { cmd.AddCommand(run.Cmd) cmd.AddCommand(version.Cmd) diff --git a/cmd/venom/root/root_test.go b/cmd/venom/root/root_test.go new file mode 100644 index 00000000..ee858a6e --- /dev/null +++ b/cmd/venom/root/root_test.go @@ -0,0 +1,36 @@ +package root + +import ( + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +// getTopLevelFolder returns the top level folder of the project +func getTopLevelFolder() string { + out, err := exec.Command("go", "list", "-m", "-f", "{{.Dir}}").Output() + if err != nil { + panic(err) + } + return strings.TrimSpace(string(out)) +} + +// TestRunCmd tests the run command +func TestRunCmd(t *testing.T) { + var validArgs []string + + validArgs = append(validArgs, "run", filepath.Join(getTopLevelFolder(), "tests", "assertions")) + + rootCmd := New() + rootCmd.SetArgs(validArgs) + os.Setenv("VENOM_TEST_MODE", "true") + assert.Equal(t, 3, len(rootCmd.Commands())) + err := rootCmd.Execute() + assert.NoError(t, err) + rootCmd.Execute() + os.Unsetenv("VENOM_TEST_MODE") +} diff --git a/cmd/venom/run/cmd.go b/cmd/venom/run/cmd.go index 5900b300..d3e63363 100644 --- a/cmd/venom/run/cmd.go +++ b/cmd/venom/run/cmd.go @@ -61,12 +61,12 @@ func initArgs(cmd *cobra.Command) { // Configuration file overrides the environment variables. if _, err := initFromEnv(os.Environ()); err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) - venom.OSExit(2) + v.OSExit(2) } if err := initFromConfigFile(); err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) - venom.OSExit(2) + v.OSExit(2) } cmd.LocalFlags().VisitAll(initFromCommandArguments) } @@ -244,6 +244,10 @@ func initFromEnv(environ []string) ([]string, error) { v := strings.Split(os.Getenv("VENOM_VAR"), " ") variables = v } + // VENOM_TEST_MODE is used to set the venom not calling os.Exit() at the end of the run + if os.Getenv("VENOM_TEST_MODE") != "" { + v.InTestMode = os.Getenv("VENOM_TEST_MODE") == "true" + } if os.Getenv("VENOM_VAR_FROM_FILE") != "" { varFiles = strings.Split(os.Getenv("VENOM_VAR_FROM_FILE"), " ") } @@ -346,19 +350,19 @@ var Cmd = &cobra.Command{ if err := v.InitLogger(); err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) - venom.OSExit(2) + v.OSExit(2) } if v.Verbose == 3 { fCPU, err := os.Create(filepath.Join(v.OutputDir, "pprof_cpu_profile.prof")) if err != nil { fmt.Fprintf(os.Stderr, "error while create profile file %v\n", err) - venom.OSExit(2) + v.OSExit(2) } fMem, err := os.Create(filepath.Join(v.OutputDir, "pprof_mem_profile.prof")) if err != nil { fmt.Fprintf(os.Stderr, "error while create profile file %v\n", err) - venom.OSExit(2) + v.OSExit(2) } if fCPU != nil && fMem != nil { pprof.StartCPUProfile(fCPU) //nolint @@ -379,7 +383,7 @@ var Cmd = &cobra.Command{ fi, err := os.Open(f) if err != nil { fmt.Fprintf(os.Stderr, "unable to open var-from-file %s: %v\n", f, err) - venom.OSExit(2) + v.OSExit(2) } defer fi.Close() readers = append(readers, fi) @@ -388,31 +392,31 @@ var Cmd = &cobra.Command{ mapvars, err := readInitialVariables(context.Background(), variables, readers, os.Environ()) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) - venom.OSExit(2) + v.OSExit(2) } v.AddVariables(mapvars) if err := v.Parse(context.Background(), path); err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) - venom.OSExit(2) + v.OSExit(2) } if err := v.Process(context.Background(), path); err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) - venom.OSExit(2) + v.OSExit(2) } if err := v.OutputResult(); err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) - venom.OSExit(2) + v.OSExit(2) } if v.Tests.Status == venom.StatusPass { fmt.Fprintf(os.Stdout, "final status: %v\n", venom.Green(v.Tests.Status)) - venom.OSExit(0) + v.OSExit(0) } fmt.Fprintf(os.Stdout, "final status: %v\n", venom.Red(v.Tests.Status)) - venom.OSExit(2) + v.OSExit(2) return nil }, diff --git a/venom.go b/venom.go index 6b48a1d6..f929471c 100644 --- a/venom.go +++ b/venom.go @@ -35,6 +35,14 @@ func OSExit(exitCode int) { } } +func (v *Venom) OSExit(exitCode int) { + if v.InTestMode { + bincover.ExitCode = exitCode + } else { + OSExit(exitCode) + } +} + // ContextKey can be added in context to store contextual infos. Also used by logger. type ContextKey string @@ -73,6 +81,7 @@ type Venom struct { StopOnFailure bool HtmlReport bool Verbose int + InTestMode bool } var trace = color.New(color.Attribute(90)).SprintFunc()