@@ -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}
@@ -229,6 +230,18 @@ func WithGoCode(modulePath, mainPkg string) Opt {
229230 }
230231
231232 c .cmd = exec .Command (target )
233+ c .cmd .Stdout = os .Stdout
234+ c .cmd .Stderr = os .Stderr
235+ return nil
236+ }
237+ }
238+
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 )
232245 return nil
233246 }
234247}
@@ -259,6 +272,7 @@ func MatchingLine(exp string, cmd *exec.Cmd) (func(*exec.Cmd) error, error) {
259272 return nil , fmt .Errorf ("%w: %w" , ErrBadRegexp , err )
260273 }
261274
275+ cmd .Stdout = nil
262276 stdout , err := cmd .StdoutPipe ()
263277 if err != nil {
264278 return nil , fmt .Errorf ("%w: %w" , ErrOutputPipe , err )
0 commit comments