Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding test in the root package in order to test the run command #733

Merged
merged 3 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
35 changes: 35 additions & 0 deletions cmd/venom/root/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package root

import (
"os/exec"
"path/filepath"
"strings"
"testing"

"github.com/ovh/venom"
"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)
venom.IsTest = "test"
assert.Equal(t, 3, len(rootCmd.Commands()))
err := rootCmd.Execute()
assert.NoError(t, err)
rootCmd.Execute()

}
1 change: 1 addition & 0 deletions cmd/venom/run/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ func initFromEnv(environ []string) ([]string, error) {
v := strings.Split(os.Getenv("VENOM_VAR"), " ")
variables = v
}

if os.Getenv("VENOM_VAR_FROM_FILE") != "" {
varFiles = strings.Split(os.Getenv("VENOM_VAR_FROM_FILE"), " ")
}
Expand Down
1 change: 1 addition & 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 Down