@@ -19,6 +19,8 @@ import (
19
19
"strings"
20
20
"sync"
21
21
"testing"
22
+
23
+ "github.com/pkg/errors"
22
24
)
23
25
24
26
var (
@@ -59,7 +61,7 @@ func NewHelper(t *testing.T) *Helper {
59
61
// Must gives a fatal error if err is not nil.
60
62
func (h * Helper ) Must (err error ) {
61
63
if err != nil {
62
- h .t .Fatal ( err )
64
+ h .t .Fatalf ( "%+v" , err )
63
65
}
64
66
}
65
67
@@ -73,16 +75,16 @@ func (h *Helper) check(err error) {
73
75
// parallel runs the test in parallel by calling t.Parallel.
74
76
func (h * Helper ) parallel () {
75
77
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") )
77
79
}
78
80
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") )
80
82
}
81
83
for _ , e := range h .env {
82
84
if strings .HasPrefix (e , "GOROOT=" ) || strings .HasPrefix (e , "GOPATH=" ) || strings .HasPrefix (e , "GOBIN=" ) {
83
85
val := e [strings .Index (e , "=" )+ 1 :]
84
86
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 ) )
86
88
}
87
89
}
88
90
}
@@ -94,7 +96,7 @@ func (h *Helper) parallel() {
94
96
func (h * Helper ) pwd () string {
95
97
wd , err := os .Getwd ()
96
98
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" ) )
98
100
}
99
101
return wd
100
102
}
@@ -104,7 +106,7 @@ func (h *Helper) pwd() string {
104
106
// other tests.
105
107
func (h * Helper ) Cd (dir string ) {
106
108
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") )
108
110
}
109
111
if h .wd == "" {
110
112
h .wd = h .pwd ()
@@ -120,7 +122,7 @@ func (h *Helper) Cd(dir string) {
120
122
// command.
121
123
func (h * Helper ) Setenv (name , val string ) {
122
124
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 ) )
124
126
}
125
127
h .unsetenv (name )
126
128
h .env = append (h .env , name + "=" + val )
@@ -145,7 +147,7 @@ func (h *Helper) DoRun(args []string) error {
145
147
if h .inParallel {
146
148
for _ , arg := range args {
147
149
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") )
149
151
}
150
152
}
151
153
}
@@ -191,7 +193,7 @@ func (h *Helper) Run(args ...string) {
191
193
// runFail runs the test go command, and expects it to fail.
192
194
func (h * Helper ) runFail (args ... string ) {
193
195
if status := h .DoRun (args ); status == nil {
194
- h .t .Fatal ( " testgo succeeded unexpectedly" )
196
+ h .t .Fatalf ( "%+v" , errors . New ( " testgo succeeded unexpectedly") )
195
197
} else {
196
198
h .t .Log ("testgo failed as expected:" , status )
197
199
}
@@ -264,15 +266,15 @@ func (h *Helper) RunGit(dir string, args ...string) {
264
266
// getStdout returns standard output of the testgo run as a string.
265
267
func (h * Helper ) getStdout () string {
266
268
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") )
268
270
}
269
271
return h .stdout .String ()
270
272
}
271
273
272
274
// getStderr returns standard error of the testgo run as a string.
273
275
func (h * Helper ) getStderr () string {
274
276
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") )
276
278
}
277
279
return h .stderr .String ()
278
280
}
@@ -282,7 +284,7 @@ func (h *Helper) getStderr() string {
282
284
// each line separately, as with the grep command.
283
285
func (h * Helper ) doGrepMatch (match string , b * bytes.Buffer ) bool {
284
286
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") )
286
288
}
287
289
re := regexp .MustCompile (match )
288
290
for _ , ln := range bytes .Split (b .Bytes (), []byte {'\n' }) {
@@ -355,14 +357,14 @@ func (h *Helper) grepStderrNot(match, msg string) {
355
357
func (h * Helper ) grepBothNot (match , msg string ) {
356
358
if h .doGrepMatch (match , & h .stdout ) || h .doGrepMatch (match , & h .stderr ) {
357
359
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 ) )
359
361
}
360
362
}
361
363
362
364
// doGrepCount counts the number of times a regexp is seen in a buffer.
363
365
func (h * Helper ) doGrepCount (match string , b * bytes.Buffer ) int {
364
366
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") )
366
368
}
367
369
re := regexp .MustCompile (match )
368
370
c := 0
@@ -386,7 +388,7 @@ func (h *Helper) grepCountBoth(match string) int {
386
388
// removed if it exists.
387
389
func (h * Helper ) creatingTemp (path string ) {
388
390
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 ) )
390
392
}
391
393
// If we have changed the working directory, make sure we have
392
394
// an absolute path, because we are going to change directory
@@ -467,16 +469,17 @@ func (h *Helper) TempCopy(dest, src string) {
467
469
// TempDir adds a temporary directory for a run of testgo.
468
470
func (h * Helper ) TempDir (path string ) {
469
471
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 ))
472
475
}
473
476
}
474
477
475
478
// Path returns the absolute pathname to file with the temporary
476
479
// directory.
477
480
func (h * Helper ) Path (name string ) string {
478
481
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 ) )
480
483
}
481
484
482
485
var joined string
@@ -496,18 +499,27 @@ func (h *Helper) Path(name string) string {
496
499
497
500
// MustExist fails if path does not exist.
498
501
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 {
499
509
if _ , err := os .Stat (path ); err != nil {
500
510
if os .IsNotExist (err ) {
501
- h . t . Fatalf ( "%s does not exist but should" , path )
511
+ return false
502
512
}
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 ) )
504
514
}
515
+
516
+ return true
505
517
}
506
518
507
519
// MustNotExist fails if path exists.
508
520
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 ) )
511
523
}
512
524
}
513
525
@@ -561,7 +573,7 @@ func (h *Helper) GetCommit(repo string) string {
561
573
cmd .Dir = repoPath
562
574
out , err := cmd .CombinedOutput ()
563
575
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 )) )
565
577
}
566
578
return strings .TrimSpace (string (out ))
567
579
}
0 commit comments