This is an automated email from the ASF dual-hosted git repository. pcongiusti pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 64714bf001d260708738623f50376ae9825f418b Author: Pasquale Congiusti <[email protected]> AuthorDate: Wed Mar 2 16:32:51 2022 +0100 chore(cmd): teardown global settings We must clean those settings that may influence other unit tests --- pkg/cmd/root_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkg/cmd/root_test.go b/pkg/cmd/root_test.go index 2189ed5..8fe884d 100644 --- a/pkg/cmd/root_test.go +++ b/pkg/cmd/root_test.go @@ -49,6 +49,7 @@ func kamelTestPreAddCommandInit() (*RootCmdOptions, *cobra.Command) { } func TestLoadFromEnvVar(t *testing.T) { + defer teardown(t) // shows how to include a "," character inside an env value see VAR1 value if err := os.Setenv("KAMEL_RUN_ENVS", "\"VAR1=value,\"\"othervalue\"\"\",VAR2=value2"); err != nil { t.Fatalf("Unexpected error: %v", err) @@ -89,6 +90,7 @@ func TestLoadFromFile(t *testing.T) { } func TestPrecedenceEnvVarOverFile(t *testing.T) { + defer teardown(t) if err := os.Setenv("KAMEL_RUN_ENVS", "VAR1=envVar"); err != nil { t.Fatalf("Unexpected error: %v", err) } @@ -112,6 +114,7 @@ func TestPrecedenceEnvVarOverFile(t *testing.T) { } func TestPrecedenceCommandLineOverEverythingElse(t *testing.T) { + defer teardown(t) if err := os.Setenv("KAMEL_RUN_ENVS", "VAR1=envVar"); err != nil { t.Fatalf("Unexpected error: %v", err) } @@ -142,3 +145,12 @@ func readViperConfigFromBytes(t *testing.T, propertiesFile []byte) { t.Fatalf("Unexpected error: %v", unexpectedErr) } } + +// We must ALWAYS clean the environment variables and viper library properties to avoid mess up with the rest of the tests. +func teardown(t *testing.T) { + t.Helper() + if err := os.Setenv("KAMEL_RUN_ENVS", ""); err != nil { + t.Fatalf("Unexpected error: %v", err) + } + readViperConfigFromBytes(t, make([]byte, 0)) +}
