Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1c4462a

Browse files
fixed a bug with environment vars being passed to targets (#54)
1 parent cb33c8d commit 1c4462a

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

‎executor/cmd.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (e *CommandExecutor) execute(
103103
zap.Strings("cmd", target.Up),
104104
zap.String("url", target.RepoURL),
105105
zap.String("dir", path),
106-
zap.Int("env", len(ex.env)),
106+
zap.Any("env", ex.env),
107107
zap.Bool("passthrough", e.passEnvironment))
108108

109109
return target.Execute(ex.path, ex.env, ex.shutdown, ex.passEnvironment)

‎executor/cmd_test.go‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,15 @@ func TestCommandPrepareWithoutPassthrough(t *testing.T) {
6767
},
6868
}, false, "pico", "GLOBAL_")
6969

70-
ex, err := ce.prepare("test", "./", false, nil)
70+
ex, err := ce.prepare("test", "./", false, map[string]string{
71+
"DATA_DIR": "/data/shared",
72+
})
7173
assert.NoError(t, err)
7274
assert.Equal(t, exec{
7375
path: "./",
7476
env: map[string]string{
7577
"SOME_SECRET": "123",
78+
"DATA_DIR": "/data/shared",
7679
},
7780
shutdown: false,
7881
passEnvironment: false,
@@ -92,13 +95,16 @@ func TestCommandPrepareWithGlobal(t *testing.T) {
9295
},
9396
}, false, "pico", "GLOBAL_")
9497

95-
ex, err := ce.prepare("test", "./", false, nil)
98+
ex, err := ce.prepare("test", "./", false, map[string]string{
99+
"DATA_DIR": "/data/shared",
100+
})
96101
assert.NoError(t, err)
97102
assert.Equal(t, exec{
98103
path: "./",
99104
env: map[string]string{
100105
"SOME_SECRET": "123",
101106
"SECRET": "456",
107+
"DATA_DIR": "/data/shared",
102108
},
103109
shutdown: false,
104110
passEnvironment: false,

‎task/target.go‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package task
22

33
import (
4-
"errors"
54
"fmt"
65
"os"
76
"os/exec"
7+
8+
"github.com/pkg/errors"
89
)
910

1011
// ExecutionTask encodes a Target with additional execution-time information.
@@ -70,15 +71,20 @@ func (t *Target) Execute(dir string, env map[string]string, shutdown bool, inher
7071
command = t.Up
7172
}
7273

73-
return execute(dir, env, command, inheritEnv)
74+
c, err := prepare(dir, env, command, inheritEnv)
75+
if err != nil {
76+
return errors.Wrap(err, "failed to prepare command for execution")
77+
}
78+
79+
return c.Run()
7480
}
7581

76-
func execute(dir string, env map[string]string, command []string, inheritEnv bool) (err error) {
82+
func prepare(dir string, env map[string]string, command []string, inheritEnv bool) (cmd*exec.Cmd, err error) {
7783
if len(command) == 0 {
78-
return errors.New("attempt to execute target with empty command")
84+
return nil, errors.New("attempt to execute target with empty command")
7985
}
8086

81-
cmd := exec.Command(command[0])
87+
cmd = exec.Command(command[0])
8288
if len(command) > 1 {
8389
cmd.Args = append(cmd.Args, command[1:]...)
8490
}
@@ -91,9 +97,9 @@ func execute(dir string, env map[string]string, command []string, inheritEnv boo
9197
cmdEnv = os.Environ()
9298
}
9399
for k, v := range env {
94-
cmdEnv = append(cmd.Env, fmt.Sprintf("%s=%s", k, v))
100+
cmdEnv = append(cmdEnv, fmt.Sprintf("%s=%s", k, v))
95101
}
96102
cmd.Env = cmdEnv
97103

98-
return cmd.Run()
104+
return cmd, nil
99105
}

‎task/target_test.go‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package task
2+
3+
import (
4+
"sort"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestPrepareTargetExecution(t *testing.T) {
11+
c, err := prepare(".", map[string]string{
12+
"VAR_1": "one",
13+
"VAR_2": "two",
14+
"VAR_3": "three",
15+
"VAR_4": "four",
16+
}, []string{"docker-compose", "up", "-d"}, false)
17+
assert.NoError(t, err)
18+
19+
assert.Equal(t, []string{"docker-compose", "up", "-d"}, c.Args)
20+
want := []string{
21+
"VAR_1=one",
22+
"VAR_2=two",
23+
"VAR_3=three",
24+
"VAR_4=four",
25+
}
26+
got := c.Env
27+
sort.Strings(want)
28+
sort.Strings(got)
29+
assert.Equal(t, want, got)
30+
assert.Equal(t, ".", c.Dir)
31+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /