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

test: Add an e2e test for the COMPOSE_FILE environment variable in fi… #172

Merged
merged 2 commits into from
Jun 24, 2024
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
30 changes: 30 additions & 0 deletions option/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ package option

import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
)

// Option customizes how tests are run.
Expand Down Expand Up @@ -50,3 +52,31 @@ 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(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)
}
}

// DeleteEnv deletes the environment variable for the key name of the input.
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, targetEnvKey string) (int, bool) {
for i, env := range envs {
if strings.Split(env, "=")[0] == 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() {
envKey := "COMPOSE_FILE"
o.UpdateEnv(envKey, composeFilePath)

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(envKey)
})

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