remove unresolved vars from env set by `exec`

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-05-27 14:49:59 +02:00
parent 2605aae33f
commit 4947a4037f
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
2 changed files with 22 additions and 5 deletions

View File

@ -50,11 +50,12 @@ func (s *composeService) Exec(ctx context.Context, project *types.Project, opts
container := containers[0]
var env []string
projectEnv := types.NewMappingWithEquals(opts.Environment).Resolve(func(s string) (string, bool) {
v, ok := project.Environment[s]
return v, ok
})
for k, v := range service.Environment.OverrideBy(projectEnv) {
for k, v := range service.Environment.OverrideBy(types.NewMappingWithEquals(opts.Environment)).
Resolve(func(s string) (string, bool) {
v, ok := project.Environment[s]
return v, ok
}).
RemoveEmpty() {
env = append(env, fmt.Sprintf("%s=%s", k, *v))
}

View File

@ -17,8 +17,10 @@
package e2e
import (
"strings"
"testing"
"gotest.tools/v3/assert"
"gotest.tools/v3/icmd"
. "github.com/docker/compose-cli/utils/e2e"
@ -40,4 +42,18 @@ func TestLocalComposeExec(t *testing.T) {
res := c.RunDockerOrExitError("exec", "compose-e2e-exec_simple_1", "/bin/false")
res.Assert(t, icmd.Expected{ExitCode: 1})
})
t.Run("exec with env set", func(t *testing.T) {
res := icmd.RunCmd(c.NewDockerCmd("exec", "-e", "FOO", "compose-e2e-exec_simple_1", "/usr/bin/env"),
func(cmd *icmd.Cmd) {
cmd.Env = append(cmd.Env, "FOO=BAR")
})
res.Assert(t, icmd.Expected{Out: "FOO=BAR"})
})
t.Run("exec without env set", func(t *testing.T) {
res := c.RunDockerOrExitError("exec", "-e", "FOO", "compose-e2e-exec_simple_1", "/usr/bin/env")
res.Assert(t, icmd.Expected{ExitCode: 0})
assert.Check(t, !strings.Contains(res.Stdout(), "FOO="))
})
}