Skip to content

Commit 34f0d6d

Browse files
committed
Fix env
1 parent 683ac37 commit 34f0d6d

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

dep/cmd/cmd.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const (
2525
ErrBadRegexp = strerr.Error("bad regular expression for matching line")
2626
ErrOutputPipe = strerr.Error("failed to aquire output pipe for command")
2727
ErrBuildFailed = strerr.Error("failed to build go binary")
28+
ErrCreateCoverDir = strerr.Error("failed create coverage dir")
2829
)
2930

3031
type Cmd struct {
@@ -49,7 +50,7 @@ func New(opts ...Opt) *Cmd {
4950
func (c *Cmd) Start() error {
5051
for _, opt := range c.opts {
5152
if err := opt(c); err != nil {
52-
return fmt.Errorf("failed to apply option: %w", err)
53+
return fmt.Errorf("%w: %w", ErrOptApply, err)
5354
}
5455
}
5556

@@ -133,20 +134,20 @@ func WithStopFn(fn func(*exec.Cmd) error) Opt {
133134
}
134135
}
135136

136-
// WithEnv sets environment variables for the command.
137-
// By default, the command inherits the environment of the current process and setting this option will override it.
138-
func WithEnv(env ...string) Opt {
137+
// WithEnvSet sets environment variables for the command.
138+
// By default the command inherits the environment of the current process and setting this option will override it.
139+
func WithEnvSet(env ...string) Opt {
139140
return func(c *Cmd) error {
140141
c.cmd.Env = env
141142
return nil
142143
}
143144
}
144145

145146
// WithEnvAppend adds environment variables to commands current env.
146-
// By default, the command inherits the environment of the current process and setting this option will override it.
147+
// By default the command inherits the environment of the current process and setting this option will override it.
147148
func WithEnvAppend(env ...string) Opt {
148149
return func(c *Cmd) error {
149-
c.cmd.Env = env
150+
c.cmd.Env = append(c.cmd.Env, env...)
150151
return nil
151152
}
152153
}
@@ -235,6 +236,16 @@ func WithGoCode(modulePath, mainPkg string) Opt {
235236
}
236237
}
237238

239+
func WithGoCoverDir(dir string) Opt {
240+
return func(c *Cmd) error {
241+
if err := os.MkdirAll(dir, 0o755); err != nil {
242+
return fmt.Errorf("%w: %w", ErrCreateCoverDir, err)
243+
}
244+
c.cmd.Env = append(c.cmd.Env, "GOCOVERDIR="+dir)
245+
return nil
246+
}
247+
}
248+
238249
// StopWithSignal returns a stop function that sends the given signal to the command and waits for it to exit.
239250
// This can be used with WithStopFn to stop the command with a specific signal.
240251
func StopWithSignal(s os.Signal) func(*exec.Cmd) error {

dep/cmd/cmd_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func TestCmd(t *testing.T) {
9696
name: "WithEnv",
9797
cmd: cmd.New(
9898
cmd.WithCommand("go", "env", "GOPRIVATE"),
99-
cmd.WithEnv("GOPRIVATE=foo"),
99+
cmd.WithEnvSet("GOPRIVATE=foo"),
100100
cmd.WithWaitMatchingLine("foo"),
101101
cmd.WithStopFn(func(c *exec.Cmd) error { return nil }),
102102
),

0 commit comments

Comments
 (0)