Skip to content

Commit

Permalink
test: Add an e2e test for the COMPOSE_FILE environment variable in fi…
Browse files Browse the repository at this point in the history
…nch compose command

The following pull request attempts to modify finch compose command to
allow the COMPOSE_FILE environment variable.

- runfinch/finch#994

Therefore, this fix adds an e2e test to run finch compose command with the
COMPOSE_FILE environment variable.

Signed-off-by: Hayato Kiwata <[email protected]>
  • Loading branch information
haytok committed Jun 24, 2024
1 parent 3749402 commit 514ada5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
30 changes: 30 additions & 0 deletions option/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"os"
"os/exec"
"strings"
)

// Option customizes how tests are run.
Expand Down Expand Up @@ -50,3 +51,32 @@ func (o *Option) NewCmd(args ...string) *exec.Cmd {
cmd.Env = append(os.Environ(), o.env...)
return 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 {
o.env[i] = env
} else {
o.env = append(o.env, env)
}
}

// 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 {
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) {
for i, env := range envs {
envKey := strings.Split(env, "=")[0]
targetEnvKey := strings.Split(targetEnv, "=")[0]
if envKey == targetEnvKey {
return i, true
}
}

return -1, false
}
16 changes: 16 additions & 0 deletions tests/compose_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ func ComposeBuild(o *option.Option) {
gomega.Expect(output).Should(gomega.Equal("Compose build test"))
})

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)

command.Run(o, "compose", "build")

imageList := command.GetAllImageNames(o)
gomega.Expect(imageList).Should(gomega.ContainElement(gomega.HaveSuffix(imageSuffix[0])))
gomega.Expect(imageList).Should(gomega.ContainElement(gomega.HaveSuffix(imageSuffix[1])))
// The built image should print 'Compose build test' when run.
output := command.StdoutStr(o, "run", localImages[defaultImage])
gomega.Expect(output).Should(gomega.Equal("Compose build test"))

o.DeleteEnv(envValue)
})

ginkgo.It("should output progress in plain text format", func() {
composeBuildOutput := command.StderrStr(o, "compose", "build", "--progress",
"plain", "--no-cache", "--file", composeFilePath)
Expand Down

0 comments on commit 514ada5

Please sign in to comment.