Skip to content

Commit

Permalink
adding test in the root package in order to test the run command
Browse files Browse the repository at this point in the history
Signed-off-by: Fokion Sotiropoulos <[email protected]>
  • Loading branch information
fokion committed Oct 21, 2023
1 parent 57972a6 commit 5eb3b6a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/venom/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions cmd/venom/root/root_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
28 changes: 16 additions & 12 deletions cmd/venom/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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"), " ")
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
},
Expand Down
11 changes: 11 additions & 0 deletions venom.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
IsTest = ""
)

// OSExit is a wrapper for os.Exit
func OSExit(exitCode int) {
if IsTest != "" {
bincover.ExitCode = exitCode
Expand All @@ -35,6 +36,15 @@ func OSExit(exitCode int) {
}
}

// OSExit is a wrapper for os.Exit
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

Expand Down Expand Up @@ -73,6 +83,7 @@ type Venom struct {
StopOnFailure bool
HtmlReport bool
Verbose int
InTestMode bool
}

var trace = color.New(color.Attribute(90)).SprintFunc()
Expand Down

0 comments on commit 5eb3b6a

Please sign in to comment.