@@ -27,6 +27,7 @@ import (
27
27
"testing"
28
28
"time"
29
29
30
+ "golang.org/x/tools/internal/goroot"
30
31
"golang.org/x/tools/internal/testenv"
31
32
)
32
33
@@ -65,8 +66,20 @@ func compilePkg(t *testing.T, dirname, filename, outdirname string, packagefiles
65
66
}
66
67
objname := basename + ".o"
67
68
outname := filepath .Join (outdirname , objname )
68
- importcfgfile := filepath .Join (outdirname , basename ) + ".importcfg"
69
- testenv .WriteImportcfg (t , importcfgfile , packagefiles )
69
+
70
+ importcfgfile := os .DevNull
71
+ if len (packagefiles ) > 0 {
72
+ importcfgfile = filepath .Join (outdirname , basename ) + ".importcfg"
73
+ importcfg := new (bytes.Buffer )
74
+ fmt .Fprintf (importcfg , "# import config" )
75
+ for k , v := range packagefiles {
76
+ fmt .Fprintf (importcfg , "\n packagefile %s=%s\n " , k , v )
77
+ }
78
+ if err := os .WriteFile (importcfgfile , importcfg .Bytes (), 0655 ); err != nil {
79
+ t .Fatal (err )
80
+ }
81
+ }
82
+
70
83
importreldir := strings .ReplaceAll (outdirname , string (os .PathSeparator ), "/" )
71
84
cmd := exec .Command ("go" , "tool" , "compile" , "-p" , pkg , "-D" , importreldir , "-importcfg" , importcfgfile , "-o" , outname , filename )
72
85
cmd .Dir = dirname
@@ -109,7 +122,16 @@ func TestImportTestdata(t *testing.T) {
109
122
tmpdir := mktmpdir (t )
110
123
defer os .RemoveAll (tmpdir )
111
124
112
- compile (t , "testdata" , testfile , filepath .Join (tmpdir , "testdata" ), nil )
125
+ packageFiles := map [string ]string {}
126
+ for _ , pkg := range []string {"go/ast" , "go/token" } {
127
+ export , _ := FindPkg (pkg , "testdata" )
128
+ if export == "" {
129
+ t .Fatalf ("no export data found for %s" , pkg )
130
+ }
131
+ packageFiles [pkg ] = export
132
+ }
133
+
134
+ compile (t , "testdata" , testfile , filepath .Join (tmpdir , "testdata" ), packageFiles )
113
135
114
136
// filename should end with ".go"
115
137
filename := testfile [:len (testfile )- 3 ]
@@ -137,6 +159,10 @@ func TestImportTestdata(t *testing.T) {
137
159
}
138
160
139
161
func TestImportTypeparamTests (t * testing.T ) {
162
+ if testing .Short () {
163
+ t .Skipf ("in short mode, skipping test that requires export data for all of std" )
164
+ }
165
+
140
166
testenv .NeedsGo1Point (t , 18 ) // requires generics
141
167
142
168
// This package only handles gc export data.
@@ -191,7 +217,11 @@ func TestImportTypeparamTests(t *testing.T) {
191
217
192
218
// Compile and import, and compare the resulting package with the package
193
219
// that was type-checked directly.
194
- compile (t , rootDir , entry .Name (), filepath .Join (tmpdir , "testdata" ), nil )
220
+ pkgFiles , err := goroot .PkgfileMap ()
221
+ if err != nil {
222
+ t .Fatal (err )
223
+ }
224
+ compile (t , rootDir , entry .Name (), filepath .Join (tmpdir , "testdata" ), pkgFiles )
195
225
pkgName := strings .TrimSuffix (entry .Name (), ".go" )
196
226
imported := importPkg (t , "./testdata/" + pkgName , tmpdir )
197
227
checked := checkFile (t , filename , src )
@@ -589,7 +619,13 @@ func TestIssue13566(t *testing.T) {
589
619
if err != nil {
590
620
t .Fatal (err )
591
621
}
592
- compilePkg (t , "testdata" , "a.go" , testoutdir , nil , apkg (testoutdir ))
622
+
623
+ jsonExport , _ := FindPkg ("encoding/json" , "testdata" )
624
+ if jsonExport == "" {
625
+ t .Fatalf ("no export data found for encoding/json" )
626
+ }
627
+
628
+ compilePkg (t , "testdata" , "a.go" , testoutdir , map [string ]string {"encoding/json" : jsonExport }, apkg (testoutdir ))
593
629
compile (t , testoutdir , bpath , testoutdir , map [string ]string {apkg (testoutdir ): filepath .Join (testoutdir , "a.o" )})
594
630
595
631
// import must succeed (test for issue at hand)
0 commit comments