@@ -47,7 +47,10 @@ type Helper struct {
47
47
48
48
// NewHelper initializes a new helper for testing.
49
49
func NewHelper (t * testing.T ) * Helper {
50
- wd , _ := os .Getwd ()
50
+ wd , err := os .Getwd ()
51
+ if err != nil {
52
+ panic (err )
53
+ }
51
54
return & Helper {t : t , origWd : wd }
52
55
}
53
56
@@ -417,45 +420,39 @@ func (h *Helper) TempFile(path, contents string) {
417
420
h .Must (ioutil .WriteFile (filepath .Join (h .tempdir , path ), bytes , 0644 ))
418
421
}
419
422
420
- // GetTestFileBytes reads a file form the testdata directory into memory. src is
421
- // relative to ./testdata. Assumes tests take place starting in the cmd/dep
422
- // directory.
423
- func (h * Helper ) GetTestFileBytes (src string ) []byte {
424
- content , err := ioutil .ReadFile (filepath .Join (h .origWd , "_testdata" , src ))
423
+ // GetTestFile reads a file from the testdata directory into memory. src is
424
+ // relative to ./_testdata.
425
+ func (h * Helper ) GetTestFile (src string ) io.ReadCloser {
426
+ content , err := os .Open (filepath .Join (h .origWd , "_testdata" , src ))
425
427
if err != nil {
426
428
panic (err )
427
429
}
428
- if strings .HasSuffix (src , ".go" ) {
429
- formatted , err := format .Source (content )
430
- if err == nil {
431
- content = formatted
432
- }
433
- }
434
430
return content
435
431
}
436
432
437
- // GetTestFileString reads a file form the testdata directory into memory. src is
438
- // relative to ./testdata. Assumes tests take place starting in the cmd/dep
439
- // directory.
433
+ // GetTestFileString reads a file from the testdata directory into memory. src is
434
+ // relative to ./_testdata.
440
435
func (h * Helper ) GetTestFileString (src string ) string {
441
- return string (h .GetTestFileBytes (src ))
442
- }
443
-
444
- // GetTestFileReader reads a file form the testdata directory into memory. src is
445
- // relative to ./testdata. Assumes tests take place starting in the cmd/dep
446
- // directory.
447
- func (h * Helper ) GetTestFileReader (src string ) io.Reader {
448
- return strings .NewReader (h .GetTestFileString (src ))
436
+ content , err := ioutil .ReadAll (h .GetTestFile (src ))
437
+ if err != nil {
438
+ panic (err )
439
+ }
440
+ return string (content )
449
441
}
450
442
451
443
// TempCopy copies a temporary file from testdata into the temporary directory.
452
444
// dest is relative to the temp directory location, and src is relative to
453
- // ./testdata. Assumes tests take place starting in the cmd/dep directory .
445
+ // ./_testdata .
454
446
func (h * Helper ) TempCopy (dest , src string ) {
455
- content := h .GetTestFileBytes (src )
456
- h .makeTempdir ()
457
- h .Must (os .MkdirAll (filepath .Join (h .tempdir , filepath .Dir (dest )), 0755 ))
458
- h .Must (ioutil .WriteFile (filepath .Join (h .tempdir , dest ), content , 0644 ))
447
+ in := h .GetTestFile (src )
448
+ defer in .Close ()
449
+ h .TempDir (filepath .Dir (dest ))
450
+ out , err := os .Create (filepath .Join (h .tempdir , dest ))
451
+ if err != nil {
452
+ panic (err )
453
+ }
454
+ defer out .Close ()
455
+ io .Copy (out , in )
459
456
}
460
457
461
458
// TempDir adds a temporary directory for a run of testgo.
0 commit comments