Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
Print the stack trace when the helper fails a test
Browse files Browse the repository at this point in the history
  • Loading branch information
carolynvs authored and sdboyer committed Feb 24, 2017
1 parent 6694017 commit 9a24cac
Showing 1 changed file with 35 additions and 23 deletions.
58 changes: 35 additions & 23 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"strings"
"sync"
"testing"

"github.com/pkg/errors"
)

var (
Expand Down Expand Up @@ -59,7 +61,7 @@ func NewHelper(t *testing.T) *Helper {
// Must gives a fatal error if err is not nil.
func (h *Helper) Must(err error) {
if err != nil {
h.t.Fatal(err)
h.t.Fatalf("%+v", err)
}
}

Expand All @@ -73,16 +75,16 @@ func (h *Helper) check(err error) {
// parallel runs the test in parallel by calling t.Parallel.
func (h *Helper) parallel() {
if h.ran {
h.t.Fatal("internal testsuite error: call to parallel after run")
h.t.Fatalf("%+v", errors.New("internal testsuite error: call to parallel after run"))
}
if h.wd != "" {
h.t.Fatal("internal testsuite error: call to parallel after cd")
h.t.Fatalf("%+v", errors.New("internal testsuite error: call to parallel after cd"))
}
for _, e := range h.env {
if strings.HasPrefix(e, "GOROOT=") || strings.HasPrefix(e, "GOPATH=") || strings.HasPrefix(e, "GOBIN=") {
val := e[strings.Index(e, "=")+1:]
if strings.HasPrefix(val, "testdata") || strings.HasPrefix(val, "./testdata") {
h.t.Fatalf("internal testsuite error: call to parallel with testdata in environment (%s)", e)
h.t.Fatalf("%+v", errors.Errorf("internal testsuite error: call to parallel with testdata in environment (%s)", e))
}
}
}
Expand All @@ -94,7 +96,7 @@ func (h *Helper) parallel() {
func (h *Helper) pwd() string {
wd, err := os.Getwd()
if err != nil {
h.t.Fatalf("could not get working directory: %v", err)
h.t.Fatalf("%+v", errors.Wrap(err, "could not get working directory"))
}
return wd
}
Expand All @@ -104,7 +106,7 @@ func (h *Helper) pwd() string {
// other tests.
func (h *Helper) Cd(dir string) {
if h.inParallel {
h.t.Fatal("internal testsuite error: changing directory when running in parallel")
h.t.Fatalf("%+v", errors.New("internal testsuite error: changing directory when running in parallel"))
}
if h.wd == "" {
h.wd = h.pwd()
Expand All @@ -120,7 +122,7 @@ func (h *Helper) Cd(dir string) {
// command.
func (h *Helper) Setenv(name, val string) {
if h.inParallel && (name == "GOROOT" || name == "GOPATH" || name == "GOBIN") && (strings.HasPrefix(val, "testdata") || strings.HasPrefix(val, "./testdata")) {
h.t.Fatalf("internal testsuite error: call to setenv with testdata (%s=%s) after parallel", name, val)
h.t.Fatalf("%+v", errors.Errorf("internal testsuite error: call to setenv with testdata (%s=%s) after parallel", name, val))
}
h.unsetenv(name)
h.env = append(h.env, name+"="+val)
Expand All @@ -145,7 +147,7 @@ func (h *Helper) DoRun(args []string) error {
if h.inParallel {
for _, arg := range args {
if strings.HasPrefix(arg, "testdata") || strings.HasPrefix(arg, "./testdata") {
h.t.Fatal("internal testsuite error: parallel run using testdata")
h.t.Fatalf("%+v", errors.New("internal testsuite error: parallel run using testdata"))
}
}
}
Expand Down Expand Up @@ -191,7 +193,7 @@ func (h *Helper) Run(args ...string) {
// runFail runs the test go command, and expects it to fail.
func (h *Helper) runFail(args ...string) {
if status := h.DoRun(args); status == nil {
h.t.Fatal("testgo succeeded unexpectedly")
h.t.Fatalf("%+v", errors.New("testgo succeeded unexpectedly"))
} else {
h.t.Log("testgo failed as expected:", status)
}
Expand Down Expand Up @@ -264,15 +266,15 @@ func (h *Helper) RunGit(dir string, args ...string) {
// getStdout returns standard output of the testgo run as a string.
func (h *Helper) getStdout() string {
if !h.ran {
h.t.Fatal("internal testsuite error: stdout called before run")
h.t.Fatalf("%+v", errors.New("internal testsuite error: stdout called before run"))
}
return h.stdout.String()
}

// getStderr returns standard error of the testgo run as a string.
func (h *Helper) getStderr() string {
if !h.ran {
h.t.Fatal("internal testsuite error: stdout called before run")
h.t.Fatalf("%+v", errors.New("internal testsuite error: stdout called before run"))
}
return h.stderr.String()
}
Expand All @@ -282,7 +284,7 @@ func (h *Helper) getStderr() string {
// each line separately, as with the grep command.
func (h *Helper) doGrepMatch(match string, b *bytes.Buffer) bool {
if !h.ran {
h.t.Fatal("internal testsuite error: grep called before run")
h.t.Fatalf("%+v", errors.New("internal testsuite error: grep called before run"))
}
re := regexp.MustCompile(match)
for _, ln := range bytes.Split(b.Bytes(), []byte{'\n'}) {
Expand Down Expand Up @@ -355,14 +357,14 @@ func (h *Helper) grepStderrNot(match, msg string) {
func (h *Helper) grepBothNot(match, msg string) {
if h.doGrepMatch(match, &h.stdout) || h.doGrepMatch(match, &h.stderr) {
h.t.Log(msg)
h.t.Fatalf("pattern %v found unexpectedly in standard output or standard error", match)
h.t.Fatalf("%+v", errors.Errorf("pattern %v found unexpectedly in standard output or standard error", match))
}
}

// doGrepCount counts the number of times a regexp is seen in a buffer.
func (h *Helper) doGrepCount(match string, b *bytes.Buffer) int {
if !h.ran {
h.t.Fatal("internal testsuite error: doGrepCount called before run")
h.t.Fatalf("%+v", errors.New("internal testsuite error: doGrepCount called before run"))
}
re := regexp.MustCompile(match)
c := 0
Expand All @@ -386,7 +388,7 @@ func (h *Helper) grepCountBoth(match string) int {
// removed if it exists.
func (h *Helper) creatingTemp(path string) {
if filepath.IsAbs(path) && !strings.HasPrefix(path, h.tempdir) {
h.t.Fatalf("internal testsuite error: creatingTemp(%q) with absolute path not in temporary directory", path)
h.t.Fatalf("%+v", errors.Errorf("internal testsuite error: creatingTemp(%q) with absolute path not in temporary directory", path))
}
// If we have changed the working directory, make sure we have
// an absolute path, because we are going to change directory
Expand Down Expand Up @@ -467,16 +469,17 @@ func (h *Helper) TempCopy(dest, src string) {
// TempDir adds a temporary directory for a run of testgo.
func (h *Helper) TempDir(path string) {
h.makeTempdir()
if err := os.MkdirAll(filepath.Join(h.tempdir, path), 0755); err != nil && !os.IsExist(err) {
h.t.Fatal(err)
fullPath := filepath.Join(h.tempdir, path)
if err := os.MkdirAll(fullPath, 0755); err != nil && !os.IsExist(err) {
h.t.Fatalf("%+v", errors.Errorf("Unable to create temp directory: %s", fullPath))
}
}

// Path returns the absolute pathname to file with the temporary
// directory.
func (h *Helper) Path(name string) string {
if h.tempdir == "" {
h.t.Fatalf("internal testsuite error: path(%q) with no tempdir", name)
h.t.Fatalf("%+v", errors.Errorf("internal testsuite error: path(%q) with no tempdir", name))
}

var joined string
Expand All @@ -496,18 +499,27 @@ func (h *Helper) Path(name string) string {

// MustExist fails if path does not exist.
func (h *Helper) MustExist(path string) {
if !h.Exist(path) {
h.t.Fatalf("%+v", errors.Errorf("%s does not exist but should", path))
}
}

// Exist returns whether or not a path exists
func (h *Helper) Exist(path string) bool {
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
h.t.Fatalf("%s does not exist but should", path)
return false
}
h.t.Fatalf("%s stat failed: %v", path, err)
h.t.Fatalf("%+v", errors.Wrapf(err, "Error checking if path exists: %s", path))
}

return true
}

// MustNotExist fails if path exists.
func (h *Helper) MustNotExist(path string) {
if _, err := os.Stat(path); err == nil || !os.IsNotExist(err) {
h.t.Fatalf("%s exists but should not (%v)", path, err)
if h.Exist(path) {
h.t.Fatalf("%+v", errors.Errorf("%s exists but should not", path))
}
}

Expand Down Expand Up @@ -561,7 +573,7 @@ func (h *Helper) GetCommit(repo string) string {
cmd.Dir = repoPath
out, err := cmd.CombinedOutput()
if err != nil {
h.t.Fatalf("git commit failed: out -> %s err -> %v", string(out), err)
h.t.Fatalf("%+v", errors.Wrapf(err, "git commit failed: out -> %s", string(out)))
}
return strings.TrimSpace(string(out))
}

0 comments on commit 9a24cac

Please sign in to comment.