@@ -34,16 +34,23 @@ func skipSpecialPlatforms(t *testing.T) {
34
34
}
35
35
}
36
36
37
- func compile (t * testing.T , dirname , filename string ) string {
38
- cmd := exec .Command (testenv .GoToolPath (t ), "tool" , "compile" , filename )
37
+ // compile runs the compiler on filename, with dirname as the working directory,
38
+ // and writes the output file to outdirname.
39
+ func compile (t * testing.T , dirname , filename , outdirname string ) string {
40
+ // filename must end with ".go"
41
+ if ! strings .HasSuffix (filename , ".go" ) {
42
+ t .Fatalf ("filename doesn't end in .go: %s" , filename )
43
+ }
44
+ basename := filepath .Base (filename )
45
+ outname := filepath .Join (outdirname , basename [:len (basename )- 2 ]+ "o" )
46
+ cmd := exec .Command (testenv .GoToolPath (t ), "tool" , "compile" , "-o" , outname , filename )
39
47
cmd .Dir = dirname
40
48
out , err := cmd .CombinedOutput ()
41
49
if err != nil {
42
50
t .Logf ("%s" , out )
43
51
t .Fatalf ("go tool compile %s failed: %s" , filename , err )
44
52
}
45
- // filename should end with ".go"
46
- return filepath .Join (dirname , filename [:len (filename )- 2 ]+ "o" )
53
+ return outname
47
54
}
48
55
49
56
func testPath (t * testing.T , path , srcDir string ) * types.Package {
@@ -88,17 +95,30 @@ func testDir(t *testing.T, dir string, endTime time.Time) (nimports int) {
88
95
return
89
96
}
90
97
98
+ func mktmpdir (t * testing.T ) string {
99
+ tmpdir , err := ioutil .TempDir ("" , "gcimporter_test" )
100
+ if err != nil {
101
+ t .Fatal ("mktmpdir:" , err )
102
+ }
103
+ if err := os .Mkdir (filepath .Join (tmpdir , "testdata" ), 0700 ); err != nil {
104
+ os .RemoveAll (tmpdir )
105
+ t .Fatal ("mktmpdir:" , err )
106
+ }
107
+ return tmpdir
108
+ }
109
+
91
110
func TestImportTestdata (t * testing.T ) {
92
111
// This package only handles gc export data.
93
112
if runtime .Compiler != "gc" {
94
113
t .Skipf ("gc-built packages not available (compiler = %s)" , runtime .Compiler )
95
114
}
96
115
97
- if outFn := compile (t , "testdata" , "exports.go" ); outFn != "" {
98
- defer os .Remove (outFn )
99
- }
116
+ tmpdir := mktmpdir (t )
117
+ defer os .RemoveAll (tmpdir )
118
+
119
+ compile (t , "testdata" , "exports.go" , filepath .Join (tmpdir , "testdata" ))
100
120
101
- if pkg := testPath (t , "./testdata/exports" , "." ); pkg != nil {
121
+ if pkg := testPath (t , "./testdata/exports" , tmpdir ); pkg != nil {
102
122
// The package's Imports list must include all packages
103
123
// explicitly imported by exports.go, plus all packages
104
124
// referenced indirectly via exported objects in exports.go.
@@ -131,6 +151,13 @@ func TestVersionHandling(t *testing.T) {
131
151
t .Fatal (err )
132
152
}
133
153
154
+ tmpdir := mktmpdir (t )
155
+ defer os .RemoveAll (tmpdir )
156
+ corruptdir := filepath .Join (tmpdir , "testdata" , "versions" )
157
+ if err := os .Mkdir (corruptdir , 0700 ); err != nil {
158
+ t .Fatal (err )
159
+ }
160
+
134
161
for _ , f := range list {
135
162
name := f .Name ()
136
163
if ! strings .HasSuffix (name , ".a" ) {
@@ -178,12 +205,11 @@ func TestVersionHandling(t *testing.T) {
178
205
}
179
206
// 4) write the file
180
207
pkgpath += "_corrupted"
181
- filename := filepath .Join (dir , pkgpath ) + ".a"
208
+ filename := filepath .Join (corruptdir , pkgpath ) + ".a"
182
209
ioutil .WriteFile (filename , data , 0666 )
183
- defer os .Remove (filename )
184
210
185
211
// test that importing the corrupted file results in an error
186
- _ , err = Import (make (map [string ]* types.Package ), pkgpath , dir , nil )
212
+ _ , err = Import (make (map [string ]* types.Package ), pkgpath , corruptdir , nil )
187
213
if err == nil {
188
214
t .Errorf ("import corrupted %q succeeded" , pkgpath )
189
215
} else if msg := err .Error (); ! strings .Contains (msg , "version skew" ) {
@@ -315,7 +341,7 @@ func TestIssue5815(t *testing.T) {
315
341
t .Skipf ("gc-built packages not available (compiler = %s)" , runtime .Compiler )
316
342
}
317
343
318
- pkg := importPkg (t , "strings" )
344
+ pkg := importPkg (t , "strings" , "." )
319
345
320
346
scope := pkg .Scope ()
321
347
for _ , name := range scope .Names () {
@@ -373,15 +399,22 @@ func TestIssue13566(t *testing.T) {
373
399
t .Skip ("avoid dealing with relative paths/drive letters on windows" )
374
400
}
375
401
376
- if f := compile (t , "testdata" , "a.go" ); f != "" {
377
- defer os .Remove (f )
378
- }
379
- if f := compile (t , "testdata" , "b.go" ); f != "" {
380
- defer os .Remove (f )
402
+ tmpdir := mktmpdir (t )
403
+ defer os .RemoveAll (tmpdir )
404
+ testoutdir := filepath .Join (tmpdir , "testdata" )
405
+
406
+ // b.go needs to be compiled from the output directory so that the compiler can
407
+ // find the compiled package a. We pass the full path to compile() so that we
408
+ // don't have to copy the file to that directory.
409
+ bpath , err := filepath .Abs (filepath .Join ("testdata" , "b.go" ))
410
+ if err != nil {
411
+ t .Fatal (err )
381
412
}
413
+ compile (t , "testdata" , "a.go" , testoutdir )
414
+ compile (t , testoutdir , bpath , testoutdir )
382
415
383
416
// import must succeed (test for issue at hand)
384
- pkg := importPkg (t , "./testdata/b" )
417
+ pkg := importPkg (t , "./testdata/b" , tmpdir )
385
418
386
419
// make sure all indirectly imported packages have names
387
420
for _ , imp := range pkg .Imports () {
@@ -451,9 +484,10 @@ func TestIssue15517(t *testing.T) {
451
484
t .Skip ("avoid dealing with relative paths/drive letters on windows" )
452
485
}
453
486
454
- if f := compile (t , "testdata" , "p.go" ); f != "" {
455
- defer os .Remove (f )
456
- }
487
+ tmpdir := mktmpdir (t )
488
+ defer os .RemoveAll (tmpdir )
489
+
490
+ compile (t , "testdata" , "p.go" , filepath .Join (tmpdir , "testdata" ))
457
491
458
492
// Multiple imports of p must succeed without redeclaration errors.
459
493
// We use an import path that's not cleaned up so that the eventual
@@ -469,7 +503,7 @@ func TestIssue15517(t *testing.T) {
469
503
// The same issue occurs with vendoring.)
470
504
imports := make (map [string ]* types.Package )
471
505
for i := 0 ; i < 3 ; i ++ {
472
- if _ , err := Import (imports , "./././testdata/p" , "." , nil ); err != nil {
506
+ if _ , err := Import (imports , "./././testdata/p" , tmpdir , nil ); err != nil {
473
507
t .Fatal (err )
474
508
}
475
509
}
@@ -489,11 +523,7 @@ func TestIssue15920(t *testing.T) {
489
523
t .Skip ("avoid dealing with relative paths/drive letters on windows" )
490
524
}
491
525
492
- if f := compile (t , "testdata" , "issue15920.go" ); f != "" {
493
- defer os .Remove (f )
494
- }
495
-
496
- importPkg (t , "./testdata/issue15920" )
526
+ compileAndImportPkg (t , "issue15920" )
497
527
}
498
528
499
529
func TestIssue20046 (t * testing.T ) {
@@ -510,12 +540,8 @@ func TestIssue20046(t *testing.T) {
510
540
t .Skip ("avoid dealing with relative paths/drive letters on windows" )
511
541
}
512
542
513
- if f := compile (t , "testdata" , "issue20046.go" ); f != "" {
514
- defer os .Remove (f )
515
- }
516
-
517
543
// "./issue20046".V.M must exist
518
- pkg := importPkg (t , "./testdata/ issue20046" )
544
+ pkg := compileAndImportPkg (t , "issue20046" )
519
545
obj := lookupObj (t , pkg .Scope (), "V" )
520
546
if m , index , indirect := types .LookupFieldOrMethod (obj .Type (), false , nil , "M" ); m == nil {
521
547
t .Fatalf ("V.M not found (index = %v, indirect = %v)" , index , indirect )
@@ -535,11 +561,7 @@ func TestIssue25301(t *testing.T) {
535
561
t .Skip ("avoid dealing with relative paths/drive letters on windows" )
536
562
}
537
563
538
- if f := compile (t , "testdata" , "issue25301.go" ); f != "" {
539
- defer os .Remove (f )
540
- }
541
-
542
- importPkg (t , "./testdata/issue25301" )
564
+ compileAndImportPkg (t , "issue25301" )
543
565
}
544
566
545
567
func TestIssue25596 (t * testing.T ) {
@@ -556,21 +578,24 @@ func TestIssue25596(t *testing.T) {
556
578
t .Skip ("avoid dealing with relative paths/drive letters on windows" )
557
579
}
558
580
559
- if f := compile (t , "testdata" , "issue25596.go" ); f != "" {
560
- defer os .Remove (f )
561
- }
562
-
563
- importPkg (t , "./testdata/issue25596" )
581
+ compileAndImportPkg (t , "issue25596" )
564
582
}
565
583
566
- func importPkg (t * testing.T , path string ) * types.Package {
567
- pkg , err := Import (make (map [string ]* types.Package ), path , "." , nil )
584
+ func importPkg (t * testing.T , path , srcDir string ) * types.Package {
585
+ pkg , err := Import (make (map [string ]* types.Package ), path , srcDir , nil )
568
586
if err != nil {
569
587
t .Fatal (err )
570
588
}
571
589
return pkg
572
590
}
573
591
592
+ func compileAndImportPkg (t * testing.T , name string ) * types.Package {
593
+ tmpdir := mktmpdir (t )
594
+ defer os .RemoveAll (tmpdir )
595
+ compile (t , "testdata" , name + ".go" , filepath .Join (tmpdir , "testdata" ))
596
+ return importPkg (t , "./testdata/" + name , tmpdir )
597
+ }
598
+
574
599
func lookupObj (t * testing.T , scope * types.Scope , name string ) types.Object {
575
600
if obj := scope .Lookup (name ); obj != nil {
576
601
return obj
0 commit comments