8
8
"bytes"
9
9
"fmt"
10
10
"internal/goexperiment"
11
+ "internal/goroot"
11
12
"internal/testenv"
12
13
"os"
13
14
"os/exec"
@@ -44,8 +45,20 @@ func compile(t *testing.T, dirname, filename, outdirname string, packagefiles ma
44
45
}
45
46
objname := basename + ".o"
46
47
outname := filepath .Join (outdirname , objname )
47
- importcfgfile := filepath .Join (outdirname , basename ) + ".importcfg"
48
- testenv .WriteImportcfg (t , importcfgfile , packagefiles )
48
+
49
+ importcfgfile := os .DevNull
50
+ if len (packagefiles ) > 0 {
51
+ importcfgfile = filepath .Join (outdirname , basename ) + ".importcfg"
52
+ importcfg := new (bytes.Buffer )
53
+ fmt .Fprintf (importcfg , "# import config" )
54
+ for k , v := range packagefiles {
55
+ fmt .Fprintf (importcfg , "\n packagefile %s=%s\n " , k , v )
56
+ }
57
+ if err := os .WriteFile (importcfgfile , importcfg .Bytes (), 0655 ); err != nil {
58
+ t .Fatal (err )
59
+ }
60
+ }
61
+
49
62
pkgpath := path .Join ("testdata" , basename )
50
63
cmd := testenv .Command (t , testenv .GoToolPath (t ), "tool" , "compile" , "-p" , pkgpath , "-D" , "testdata" , "-importcfg" , importcfgfile , "-o" , outname , filename )
51
64
cmd .Dir = dirname
@@ -106,7 +119,16 @@ func TestImportTestdata(t *testing.T) {
106
119
tmpdir := mktmpdir (t )
107
120
defer os .RemoveAll (tmpdir )
108
121
109
- compile (t , "testdata" , testfile , filepath .Join (tmpdir , "testdata" ), nil )
122
+ packageFiles := map [string ]string {}
123
+ for _ , pkg := range wantImports {
124
+ export , _ := FindPkg (pkg , "testdata" )
125
+ if export == "" {
126
+ t .Fatalf ("no export data found for %s" , pkg )
127
+ }
128
+ packageFiles [pkg ] = export
129
+ }
130
+
131
+ compile (t , "testdata" , testfile , filepath .Join (tmpdir , "testdata" ), packageFiles )
110
132
path := "./testdata/" + strings .TrimSuffix (testfile , ".go" )
111
133
112
134
if pkg := testPath (t , path , tmpdir ); pkg != nil {
@@ -124,6 +146,10 @@ func TestImportTestdata(t *testing.T) {
124
146
}
125
147
126
148
func TestImportTypeparamTests (t * testing.T ) {
149
+ if testing .Short () {
150
+ t .Skipf ("in short mode, skipping test that requires export data for all of std" )
151
+ }
152
+
127
153
// This package only handles gc export data.
128
154
if runtime .Compiler != "gc" {
129
155
t .Skipf ("gc-built packages not available (compiler = %s)" , runtime .Compiler )
@@ -178,7 +204,11 @@ func TestImportTypeparamTests(t *testing.T) {
178
204
179
205
// Compile and import, and compare the resulting package with the package
180
206
// that was type-checked directly.
181
- compile (t , rootDir , entry .Name (), filepath .Join (tmpdir , "testdata" ), nil )
207
+ pkgFiles , err := goroot .PkgfileMap ()
208
+ if err != nil {
209
+ t .Fatal (err )
210
+ }
211
+ compile (t , rootDir , entry .Name (), filepath .Join (tmpdir , "testdata" ), pkgFiles )
182
212
pkgName := strings .TrimSuffix (entry .Name (), ".go" )
183
213
imported := importPkg (t , "./testdata/" + pkgName , tmpdir )
184
214
checked := checkFile (t , filename , src )
@@ -554,7 +584,13 @@ func TestIssue13566(t *testing.T) {
554
584
if err != nil {
555
585
t .Fatal (err )
556
586
}
557
- compile (t , "testdata" , "a.go" , testoutdir , nil )
587
+
588
+ jsonExport , _ := FindPkg ("encoding/json" , "testdata" )
589
+ if jsonExport == "" {
590
+ t .Fatalf ("no export data found for encoding/json" )
591
+ }
592
+
593
+ compile (t , "testdata" , "a.go" , testoutdir , map [string ]string {"encoding/json" : jsonExport })
558
594
compile (t , testoutdir , bpath , testoutdir , map [string ]string {"testdata/a" : filepath .Join (testoutdir , "a.o" )})
559
595
560
596
// import must succeed (test for issue at hand)
@@ -755,12 +791,14 @@ func importPkg(t *testing.T, path, srcDir string) *types.Package {
755
791
fset := token .NewFileSet ()
756
792
pkg , err := Import (fset , make (map [string ]* types.Package ), path , srcDir , nil )
757
793
if err != nil {
794
+ t .Helper ()
758
795
t .Fatal (err )
759
796
}
760
797
return pkg
761
798
}
762
799
763
800
func compileAndImportPkg (t * testing.T , name string ) * types.Package {
801
+ t .Helper ()
764
802
tmpdir := mktmpdir (t )
765
803
defer os .RemoveAll (tmpdir )
766
804
compile (t , "testdata" , name + ".go" , filepath .Join (tmpdir , "testdata" ), nil )
@@ -771,6 +809,7 @@ func lookupObj(t *testing.T, scope *types.Scope, name string) types.Object {
771
809
if obj := scope .Lookup (name ); obj != nil {
772
810
return obj
773
811
}
812
+ t .Helper ()
774
813
t .Fatalf ("%s not found" , name )
775
814
return nil
776
815
}
0 commit comments