From ab42c546fbc1d3009dfe73b8fd19dc12bf44413a Mon Sep 17 00:00:00 2001 From: Hayato Kiwata Date: Tue, 25 Jun 2024 01:08:07 +0900 Subject: [PATCH] test: Add an e2e test for the COMPOSE_FILE environment variable in finch compose command Based on the review, the UpdateEnv function has been modified to pass the environment variable key and value as arguments. Also, the DeleteEnv function has been modified to accept environment variable key. Signed-off-by: Hayato Kiwata --- option/option.go | 16 ++++++++-------- tests/compose_build.go | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/option/option.go b/option/option.go index a6c78b3..d2bee36 100644 --- a/option/option.go +++ b/option/option.go @@ -6,6 +6,7 @@ package option import ( "errors" + "fmt" "os" "os/exec" "strings" @@ -53,8 +54,9 @@ func (o *Option) NewCmd(args ...string) *exec.Cmd { } // UpdateEnv updates the environment variable for the key name of the input. -func (o *Option) UpdateEnv(env string) { - if i, exists := containsEnv(o.env, env); exists { +func (o *Option) UpdateEnv(envKey, envValue string) { + env := fmt.Sprintf("%s=%s", envKey, envValue) + if i, exists := containsEnv(o.env, envKey); exists { o.env[i] = env } else { o.env = append(o.env, env) @@ -62,18 +64,16 @@ func (o *Option) UpdateEnv(env string) { } // DeleteEnv deletes the environment variable for the key name of the input. -func (o *Option) DeleteEnv(env string) { - if i, exists := containsEnv(o.env, env); exists { +func (o *Option) DeleteEnv(envKey string) { + if i, exists := containsEnv(o.env, envKey); exists { o.env = append(o.env[:i], o.env[i+1:]...) } } // containsEnv determines whether an environment variable exists. -func containsEnv(envs []string, targetEnv string) (int, bool) { +func containsEnv(envs []string, targetEnvKey string) (int, bool) { for i, env := range envs { - envKey := strings.Split(env, "=")[0] - targetEnvKey := strings.Split(targetEnv, "=")[0] - if envKey == targetEnvKey { + if strings.Split(env, "=")[0] == targetEnvKey { return i, true } } diff --git a/tests/compose_build.go b/tests/compose_build.go index 6d2a280..effe1af 100644 --- a/tests/compose_build.go +++ b/tests/compose_build.go @@ -45,8 +45,8 @@ func ComposeBuild(o *option.Option) { }) ginkgo.It("should build services defined in the compose file specified by the COMPOSE_FILE environment variable", func() { - envValue := fmt.Sprintf("COMPOSE_FILE=%s", composeFilePath) - o.UpdateEnv(envValue) + envKey := "COMPOSE_FILE" + o.UpdateEnv(envKey, composeFilePath) command.Run(o, "compose", "build") @@ -57,7 +57,7 @@ func ComposeBuild(o *option.Option) { output := command.StdoutStr(o, "run", localImages[defaultImage]) gomega.Expect(output).Should(gomega.Equal("Compose build test")) - o.DeleteEnv(envValue) + o.DeleteEnv(envKey) }) ginkgo.It("should output progress in plain text format", func() {