Skip to content

Commit

Permalink
Use the new ExitError.ExitCode
Browse files Browse the repository at this point in the history
To make it easier to fake os/exec.Cmd
  • Loading branch information
dnephin committed Sep 1, 2020
1 parent 5d0850c commit cd995f8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 51 deletions.
35 changes: 0 additions & 35 deletions exitcode.go

This file was deleted.

45 changes: 33 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func main() {
case *exec.ExitError:
// go test should already report the error to stderr, exit with
// the same status code
os.Exit(ExitCodeWithDefault(err))
os.Exit(exitCodeWithDefault(err))
default:
log.Error(err.Error())
os.Exit(3)
Expand Down Expand Up @@ -199,7 +199,7 @@ func run(opts *options) error {

goTestProc, err := startGoTest(ctx, goTestCmdArgs(opts, rerunOpts{}))
if err != nil {
return errors.Wrapf(err, "failed to run %s", strings.Join(goTestProc.cmd.Args, " "))
return err
}

handler, err := newEventHandler(opts)
Expand Down Expand Up @@ -327,32 +327,53 @@ func findPkgArgPosition(args []string) int {
}

type proc struct {
cmd *exec.Cmd
cmd waiter
stdout io.Reader
stderr io.Reader
}

type waiter interface {
Wait() error
}

func startGoTest(ctx context.Context, args []string) (proc, error) {
if len(args) == 0 {
return proc{}, errors.New("missing command to run")
}

p := proc{
cmd: exec.CommandContext(ctx, args[0], args[1:]...),
}
log.Debugf("exec: %s", p.cmd.Args)
cmd := exec.CommandContext(ctx, args[0], args[1:]...)
p := proc{cmd: cmd}
log.Debugf("exec: %s", cmd.Args)
var err error
p.stdout, err = p.cmd.StdoutPipe()
p.stdout, err = cmd.StdoutPipe()
if err != nil {
return p, err
}
p.stderr, err = p.cmd.StderrPipe()
p.stderr, err = cmd.StderrPipe()
if err != nil {
return p, err
}
err = p.cmd.Start()
if err := cmd.Start(); err != nil {
return p, errors.Wrapf(err, "failed to run %s", strings.Join(cmd.Args, " "))
}
log.Debugf("go test pid: %d", cmd.Process.Pid)
return p, nil
}

// GetExitCode returns the ExitStatus of a process from the error returned by
// exec.Run(). If the exit status is not available an error is returned.
func exitCodeWithDefault(err error) int {
if err == nil {
log.Debugf("go test pid: %d", p.cmd.Process.Pid)
return 0
}
if exiterr, ok := err.(exitCoder); ok {
if code := exiterr.ExitCode(); code != -1 {
return code
}
}
return p, err
return 127
}

type exitCoder interface {
ExitCode() int
}
6 changes: 2 additions & 4 deletions rerunfails.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"fmt"
"os"
"sort"
"strings"

"github.com/pkg/errors"
"gotest.tools/gotestsum/testjson"
)

Expand Down Expand Up @@ -64,7 +62,7 @@ func rerunFailed(ctx context.Context, opts *options, scanConfig testjson.ScanCon
for _, tc := range tcFilter(rec.failures) {
goTestProc, err := startGoTest(ctx, goTestCmdArgs(opts, newRerunOptsFromTestCase(tc)))
if err != nil {
return errors.Wrapf(err, "failed to run %s", strings.Join(goTestProc.cmd.Args, " "))
return err
}

cfg := testjson.ScanConfig{
Expand Down Expand Up @@ -92,7 +90,7 @@ func hasErrors(err error, exec *testjson.Execution) error {
case len(exec.Errors()) > 0:
return fmt.Errorf("rerun aborted because previous run had errors")
// Exit code 0 and 1 are expected.
case ExitCodeWithDefault(err) > 1:
case exitCodeWithDefault(err) > 1:
return fmt.Errorf("unexpected go test exit code: %v", err)
default:
return nil
Expand Down

0 comments on commit cd995f8

Please sign in to comment.