Skip to content

Commit

Permalink
refactor(test): included all run command flag test
Browse files Browse the repository at this point in the history
* Added all run command flag test in run_test.go
* Refactored root_test.go to use new methods in run_test.go
  • Loading branch information
squakez authored and nicolaferraro committed Jan 11, 2021
1 parent 2f6ed74 commit 26491cb
Show file tree
Hide file tree
Showing 2 changed files with 341 additions and 128 deletions.
46 changes: 4 additions & 42 deletions pkg/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,42 +46,16 @@ func kamelTestPreAddCommandInit() (*RootCmdOptions, *cobra.Command) {
return &options, rootCmd
}

func TestLoadFromCommandLine(t *testing.T) {
options, rootCmd := kamelTestPreAddCommandInit()

runCmdOptions := addTestRunCmd(options, rootCmd)

kamelTestPostAddCommandInit(t, rootCmd)

const VAR2 = "VAR2=value2"
_, err := test.ExecuteCommand(rootCmd, "run", "route.java", "--env", "VAR1=value,othervalue", "--env", VAR2)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

if len(runCmdOptions.EnvVars) != 2 {
t.Errorf("Properties expected to contain: \n %v elements\nGot:\n %v elemtns\n", 2, len(runCmdOptions.EnvVars))
}
if runCmdOptions.EnvVars[0] != "VAR1=value,othervalue" || runCmdOptions.EnvVars[1] != VAR2 {
t.Errorf("EnvVars expected to be: \n %v\nGot:\n %v\n", "[VAR1=value,othervalue VAR=value2]", runCmdOptions.EnvVars)
}
}

func TestLoadFromEnvVar(t *testing.T) {
//shows how to include a "," character inside an env value see VAR1 value
os.Setenv("KAMEL_RUN_ENVS", "\"VAR1=value,\"\"othervalue\"\"\",VAR2=value2")

options, rootCmd := kamelTestPreAddCommandInit()

runCmdOptions := addTestRunCmd(options, rootCmd)

kamelTestPostAddCommandInit(t, rootCmd)
runCmdOptions, rootCmd, _ := initializeRunCmdOptions(t)

_, err := test.ExecuteCommand(rootCmd, "run", "route.java")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

if len(runCmdOptions.EnvVars) != 2 {
t.Fatalf("Properties expected to contain: \n %v elements\nGot:\n %v elemtns\n", 2, len(runCmdOptions.EnvVars))
}
Expand All @@ -95,17 +69,13 @@ func TestLoadFromFile(t *testing.T) {
var propertiesFile = []byte(`kamel.run.envs: "VAR1=value,""othervalue""",VAR2=value2`)
viper.SetConfigType("properties")
readViperConfigFromBytes(propertiesFile, t)
options, rootCmd := kamelTestPreAddCommandInit()

runCmdOptions := addTestRunCmd(options, rootCmd)

kamelTestPostAddCommandInit(t, rootCmd)
runCmdOptions, rootCmd, _ := initializeRunCmdOptions(t)

_, err := test.ExecuteCommand(rootCmd, "run", "route.java")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

if len(runCmdOptions.EnvVars) != 2 {
t.Fatalf("Properties expected to contain: \n %v elements\nGot:\n %v elemtns\n", 2, len(runCmdOptions.EnvVars))
}
Expand All @@ -119,17 +89,13 @@ func TestPrecedenceEnvVarOverFile(t *testing.T) {
var propertiesFile = []byte(`kamel.run.envs: VAR2=file`)
viper.SetConfigType("properties")
readViperConfigFromBytes(propertiesFile, t)
options, rootCmd := kamelTestPreAddCommandInit()

runCmdOptions := addTestRunCmd(options, rootCmd)

kamelTestPostAddCommandInit(t, rootCmd)
runCmdOptions, rootCmd, _ := initializeRunCmdOptions(t)

_, err := test.ExecuteCommand(rootCmd, "run", "route.java")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

if len(runCmdOptions.EnvVars) != 1 {
t.Fatalf("Properties expected to contain: \n %v elements\nGot:\n %v elements\n", 1, len(runCmdOptions.EnvVars))
}
Expand All @@ -143,17 +109,13 @@ func TestPrecedenceCommandLineOverEverythingElse(t *testing.T) {
var propertiesFile = []byte(`kamel.run.envs: VAR2=file`)
viper.SetConfigType("properties")
readViperConfigFromBytes(propertiesFile, t)
options, rootCmd := kamelTestPreAddCommandInit()

runCmdOptions := addTestRunCmd(options, rootCmd)

kamelTestPostAddCommandInit(t, rootCmd)
runCmdOptions, rootCmd, _ := initializeRunCmdOptions(t)

_, err := test.ExecuteCommand(rootCmd, "run", "route.java", "--env", "VAR3=commandLine")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

if len(runCmdOptions.EnvVars) != 1 {
t.Fatalf("Properties expected to contain: \n %v elements\nGot:\n %v elements\n", 1, len(runCmdOptions.EnvVars))
}
Expand Down

0 comments on commit 26491cb

Please sign in to comment.