Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 4947a40

Browse files
committed
remove unresolved vars from env set by exec
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 2605aae commit 4947a40

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

local/compose/exec.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ func (s *composeService) Exec(ctx context.Context, project *types.Project, opts
5050
container := containers[0]
5151

5252
var env []string
53-
projectEnv := types.NewMappingWithEquals(opts.Environment).Resolve(func(s string) (string, bool) {
54-
v, ok := project.Environment[s]
55-
return v, ok
56-
})
57-
for k, v := range service.Environment.OverrideBy(projectEnv) {
53+
for k, v := range service.Environment.OverrideBy(types.NewMappingWithEquals(opts.Environment)).
54+
Resolve(func(s string) (string, bool) {
55+
v, ok := project.Environment[s]
56+
return v, ok
57+
}).
58+
RemoveEmpty() {
5859
env = append(env, fmt.Sprintf("%s=%s", k, *v))
5960
}
6061

local/e2e/compose/compose_exec_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
package e2e
1818

1919
import (
20+
"strings"
2021
"testing"
2122

23+
"gotest.tools/v3/assert"
2224
"gotest.tools/v3/icmd"
2325

2426
. "github.com/docker/compose-cli/utils/e2e"
@@ -40,4 +42,18 @@ func TestLocalComposeExec(t *testing.T) {
4042
res := c.RunDockerOrExitError("exec", "compose-e2e-exec_simple_1", "/bin/false")
4143
res.Assert(t, icmd.Expected{ExitCode: 1})
4244
})
45+
46+
t.Run("exec with env set", func(t *testing.T) {
47+
res := icmd.RunCmd(c.NewDockerCmd("exec", "-e", "FOO", "compose-e2e-exec_simple_1", "/usr/bin/env"),
48+
func(cmd *icmd.Cmd) {
49+
cmd.Env = append(cmd.Env, "FOO=BAR")
50+
})
51+
res.Assert(t, icmd.Expected{Out: "FOO=BAR"})
52+
})
53+
54+
t.Run("exec without env set", func(t *testing.T) {
55+
res := c.RunDockerOrExitError("exec", "-e", "FOO", "compose-e2e-exec_simple_1", "/usr/bin/env")
56+
res.Assert(t, icmd.Expected{ExitCode: 0})
57+
assert.Check(t, !strings.Contains(res.Stdout(), "FOO="))
58+
})
4359
}

0 commit comments

Comments
 (0)