@@ -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
3031type Cmd struct {
@@ -49,7 +50,7 @@ func New(opts ...Opt) *Cmd {
4950func (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.
147148func 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.
240251func StopWithSignal (s os.Signal ) func (* exec.Cmd ) error {
0 commit comments