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

Commit 9a24cac

Browse files
carolynvssdboyer
authored andcommitted
Print the stack trace when the helper fails a test
1 parent 6694017 commit 9a24cac

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

test/test.go

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"strings"
2020
"sync"
2121
"testing"
22+
23+
"github.com/pkg/errors"
2224
)
2325

2426
var (
@@ -59,7 +61,7 @@ func NewHelper(t *testing.T) *Helper {
5961
// Must gives a fatal error if err is not nil.
6062
func (h *Helper) Must(err error) {
6163
if err != nil {
62-
h.t.Fatal(err)
64+
h.t.Fatalf("%+v", err)
6365
}
6466
}
6567

@@ -73,16 +75,16 @@ func (h *Helper) check(err error) {
7375
// parallel runs the test in parallel by calling t.Parallel.
7476
func (h *Helper) parallel() {
7577
if h.ran {
76-
h.t.Fatal("internal testsuite error: call to parallel after run")
78+
h.t.Fatalf("%+v", errors.New("internal testsuite error: call to parallel after run"))
7779
}
7880
if h.wd != "" {
79-
h.t.Fatal("internal testsuite error: call to parallel after cd")
81+
h.t.Fatalf("%+v", errors.New("internal testsuite error: call to parallel after cd"))
8082
}
8183
for _, e := range h.env {
8284
if strings.HasPrefix(e, "GOROOT=") || strings.HasPrefix(e, "GOPATH=") || strings.HasPrefix(e, "GOBIN=") {
8385
val := e[strings.Index(e, "=")+1:]
8486
if strings.HasPrefix(val, "testdata") || strings.HasPrefix(val, "./testdata") {
85-
h.t.Fatalf("internal testsuite error: call to parallel with testdata in environment (%s)", e)
87+
h.t.Fatalf("%+v", errors.Errorf("internal testsuite error: call to parallel with testdata in environment (%s)", e))
8688
}
8789
}
8890
}
@@ -94,7 +96,7 @@ func (h *Helper) parallel() {
9496
func (h *Helper) pwd() string {
9597
wd, err := os.Getwd()
9698
if err != nil {
97-
h.t.Fatalf("could not get working directory: %v", err)
99+
h.t.Fatalf("%+v", errors.Wrap(err, "could not get working directory"))
98100
}
99101
return wd
100102
}
@@ -104,7 +106,7 @@ func (h *Helper) pwd() string {
104106
// other tests.
105107
func (h *Helper) Cd(dir string) {
106108
if h.inParallel {
107-
h.t.Fatal("internal testsuite error: changing directory when running in parallel")
109+
h.t.Fatalf("%+v", errors.New("internal testsuite error: changing directory when running in parallel"))
108110
}
109111
if h.wd == "" {
110112
h.wd = h.pwd()
@@ -120,7 +122,7 @@ func (h *Helper) Cd(dir string) {
120122
// command.
121123
func (h *Helper) Setenv(name, val string) {
122124
if h.inParallel && (name == "GOROOT" || name == "GOPATH" || name == "GOBIN") && (strings.HasPrefix(val, "testdata") || strings.HasPrefix(val, "./testdata")) {
123-
h.t.Fatalf("internal testsuite error: call to setenv with testdata (%s=%s) after parallel", name, val)
125+
h.t.Fatalf("%+v", errors.Errorf("internal testsuite error: call to setenv with testdata (%s=%s) after parallel", name, val))
124126
}
125127
h.unsetenv(name)
126128
h.env = append(h.env, name+"="+val)
@@ -145,7 +147,7 @@ func (h *Helper) DoRun(args []string) error {
145147
if h.inParallel {
146148
for _, arg := range args {
147149
if strings.HasPrefix(arg, "testdata") || strings.HasPrefix(arg, "./testdata") {
148-
h.t.Fatal("internal testsuite error: parallel run using testdata")
150+
h.t.Fatalf("%+v", errors.New("internal testsuite error: parallel run using testdata"))
149151
}
150152
}
151153
}
@@ -191,7 +193,7 @@ func (h *Helper) Run(args ...string) {
191193
// runFail runs the test go command, and expects it to fail.
192194
func (h *Helper) runFail(args ...string) {
193195
if status := h.DoRun(args); status == nil {
194-
h.t.Fatal("testgo succeeded unexpectedly")
196+
h.t.Fatalf("%+v", errors.New("testgo succeeded unexpectedly"))
195197
} else {
196198
h.t.Log("testgo failed as expected:", status)
197199
}
@@ -264,15 +266,15 @@ func (h *Helper) RunGit(dir string, args ...string) {
264266
// getStdout returns standard output of the testgo run as a string.
265267
func (h *Helper) getStdout() string {
266268
if !h.ran {
267-
h.t.Fatal("internal testsuite error: stdout called before run")
269+
h.t.Fatalf("%+v", errors.New("internal testsuite error: stdout called before run"))
268270
}
269271
return h.stdout.String()
270272
}
271273

272274
// getStderr returns standard error of the testgo run as a string.
273275
func (h *Helper) getStderr() string {
274276
if !h.ran {
275-
h.t.Fatal("internal testsuite error: stdout called before run")
277+
h.t.Fatalf("%+v", errors.New("internal testsuite error: stdout called before run"))
276278
}
277279
return h.stderr.String()
278280
}
@@ -282,7 +284,7 @@ func (h *Helper) getStderr() string {
282284
// each line separately, as with the grep command.
283285
func (h *Helper) doGrepMatch(match string, b *bytes.Buffer) bool {
284286
if !h.ran {
285-
h.t.Fatal("internal testsuite error: grep called before run")
287+
h.t.Fatalf("%+v", errors.New("internal testsuite error: grep called before run"))
286288
}
287289
re := regexp.MustCompile(match)
288290
for _, ln := range bytes.Split(b.Bytes(), []byte{'\n'}) {
@@ -355,14 +357,14 @@ func (h *Helper) grepStderrNot(match, msg string) {
355357
func (h *Helper) grepBothNot(match, msg string) {
356358
if h.doGrepMatch(match, &h.stdout) || h.doGrepMatch(match, &h.stderr) {
357359
h.t.Log(msg)
358-
h.t.Fatalf("pattern %v found unexpectedly in standard output or standard error", match)
360+
h.t.Fatalf("%+v", errors.Errorf("pattern %v found unexpectedly in standard output or standard error", match))
359361
}
360362
}
361363

362364
// doGrepCount counts the number of times a regexp is seen in a buffer.
363365
func (h *Helper) doGrepCount(match string, b *bytes.Buffer) int {
364366
if !h.ran {
365-
h.t.Fatal("internal testsuite error: doGrepCount called before run")
367+
h.t.Fatalf("%+v", errors.New("internal testsuite error: doGrepCount called before run"))
366368
}
367369
re := regexp.MustCompile(match)
368370
c := 0
@@ -386,7 +388,7 @@ func (h *Helper) grepCountBoth(match string) int {
386388
// removed if it exists.
387389
func (h *Helper) creatingTemp(path string) {
388390
if filepath.IsAbs(path) && !strings.HasPrefix(path, h.tempdir) {
389-
h.t.Fatalf("internal testsuite error: creatingTemp(%q) with absolute path not in temporary directory", path)
391+
h.t.Fatalf("%+v", errors.Errorf("internal testsuite error: creatingTemp(%q) with absolute path not in temporary directory", path))
390392
}
391393
// If we have changed the working directory, make sure we have
392394
// an absolute path, because we are going to change directory
@@ -467,16 +469,17 @@ func (h *Helper) TempCopy(dest, src string) {
467469
// TempDir adds a temporary directory for a run of testgo.
468470
func (h *Helper) TempDir(path string) {
469471
h.makeTempdir()
470-
if err := os.MkdirAll(filepath.Join(h.tempdir, path), 0755); err != nil && !os.IsExist(err) {
471-
h.t.Fatal(err)
472+
fullPath := filepath.Join(h.tempdir, path)
473+
if err := os.MkdirAll(fullPath, 0755); err != nil && !os.IsExist(err) {
474+
h.t.Fatalf("%+v", errors.Errorf("Unable to create temp directory: %s", fullPath))
472475
}
473476
}
474477

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

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

497500
// MustExist fails if path does not exist.
498501
func (h *Helper) MustExist(path string) {
502+
if !h.Exist(path) {
503+
h.t.Fatalf("%+v", errors.Errorf("%s does not exist but should", path))
504+
}
505+
}
506+
507+
// Exist returns whether or not a path exists
508+
func (h *Helper) Exist(path string) bool {
499509
if _, err := os.Stat(path); err != nil {
500510
if os.IsNotExist(err) {
501-
h.t.Fatalf("%s does not exist but should", path)
511+
return false
502512
}
503-
h.t.Fatalf("%s stat failed: %v", path, err)
513+
h.t.Fatalf("%+v", errors.Wrapf(err, "Error checking if path exists: %s", path))
504514
}
515+
516+
return true
505517
}
506518

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

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

0 commit comments

Comments
 (0)