Skip to content

Commit

Permalink
Add post-run-command hook
Browse files Browse the repository at this point in the history
Pass environment variables with some summary info
  • Loading branch information
dnephin committed May 16, 2020
1 parent bbd252b commit 8c90dec
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
22 changes: 22 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"os/exec"

"github.com/pkg/errors"
"gotest.tools/gotestsum/internal/junitxml"
Expand Down Expand Up @@ -86,3 +87,24 @@ func writeJUnitFile(opts *options, execution *testjson.Execution) error {
FormatTestCaseClassname: opts.junitTestCaseClassnameFormat.Value(),
})
}

func postRunHook(opts *options, execution *testjson.Execution) error {
if len(opts.postRunHookCmd) == 0 {
return nil
}

cmd := exec.Command(opts.postRunHookCmd[0], opts.postRunHookCmd[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = append(
os.Environ(),
"GOTESTSUM_JSONFILE="+opts.jsonFile,
"GOTESTSUM_JUNITFILE"+opts.junitFile,
fmt.Sprintf("TESTS_TOTAL=%d", execution.Total()),
fmt.Sprintf("TESTS_FAILED=%d", len(execution.Failed())),
fmt.Sprintf("TESTS_SKIPPED=%d", len(execution.Skipped())),
fmt.Sprintf("TESTS_ERRORS=%d", len(execution.Errors())),
)
// TODO: send a more detailed report to stdin?
return cmd.Run()
}
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ Formats:
"format the testsuite name field as: "+junitFieldFormatValues)
flags.Var(opts.junitTestCaseClassnameFormat, "junitfile-testcase-classname",
"format the testcase classname field as: "+junitFieldFormatValues)
flags.StringSliceVar(&opts.postRunHookCmd, "post-run-command",
nil, "run this command once the tests have completed")
flags.BoolVar(&opts.version, "version", false, "show version and exit")
return flags, opts
}
Expand All @@ -127,6 +129,7 @@ type options struct {
rawCommand bool
jsonFile string
junitFile string
postRunHookCmd []string
noColor bool
noSummary *noSummaryValue
junitTestSuiteNameFormat *junitFieldFormatValue
Expand Down Expand Up @@ -169,6 +172,9 @@ func run(opts *options) error {
if err := writeJUnitFile(opts, exec); err != nil {
return err
}
if err := postRunHook(opts, exec); err != nil {
return err
}
return goTestProc.cmd.Wait()
}

Expand Down

0 comments on commit 8c90dec

Please sign in to comment.