Skip to content

Commit a79b55b

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
go/internal/gcimporter: in short tests, avoid creating export data for all of std
gcimporter.TestImportTypeparamTests still needs to create full export data because it loads lots of source files from GOROOT/test that expect to be able to import arbitrary subsets of the standard library, so we now skip it in short mode. On a clean build cache, this reduces 'go test -short cmd/compile/internal/importer go/internal/gcimporter' on my machine from 21–28s per test to <6s per test. Updates #56967. Updates #47257. Change-Id: I8fd80293ab135e0d2d213529b74e0ca6429cdfc7 Reviewed-on: https://go-review.googlesource.com/c/go/+/454498 Run-TryBot: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Bryan Mills <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent 3e2ab20 commit a79b55b

File tree

2 files changed

+79
-9
lines changed

2 files changed

+79
-9
lines changed

src/cmd/compile/internal/importer/gcimporter_test.go

+35-4
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,30 @@ func compile(t *testing.T, dirname, filename, outdirname string, packagefiles ma
3333
// filename must end with ".go"
3434
basename, ok := strings.CutSuffix(filepath.Base(filename), ".go")
3535
if !ok {
36+
t.Helper()
3637
t.Fatalf("filename doesn't end in .go: %s", filename)
3738
}
3839
objname := basename + ".o"
3940
outname := filepath.Join(outdirname, objname)
40-
importcfgfile := filepath.Join(outdirname, basename) + ".importcfg"
41-
testenv.WriteImportcfg(t, importcfgfile, packagefiles)
4241
pkgpath := path.Join("testdata", basename)
42+
43+
importcfgfile := os.DevNull
44+
if len(packagefiles) > 0 {
45+
importcfgfile = filepath.Join(outdirname, basename) + ".importcfg"
46+
importcfg := new(bytes.Buffer)
47+
for k, v := range packagefiles {
48+
fmt.Fprintf(importcfg, "packagefile %s=%s\n", k, v)
49+
}
50+
if err := os.WriteFile(importcfgfile, importcfg.Bytes(), 0655); err != nil {
51+
t.Fatal(err)
52+
}
53+
}
54+
4355
cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-p", pkgpath, "-D", "testdata", "-importcfg", importcfgfile, "-o", outname, filename)
4456
cmd.Dir = dirname
4557
out, err := cmd.CombinedOutput()
4658
if err != nil {
59+
t.Helper()
4760
t.Logf("%s", out)
4861
t.Fatalf("go tool compile %s failed: %s", filename, err)
4962
}
@@ -96,7 +109,16 @@ func TestImportTestdata(t *testing.T) {
96109
tmpdir := mktmpdir(t)
97110
defer os.RemoveAll(tmpdir)
98111

99-
compile(t, "testdata", testfile, filepath.Join(tmpdir, "testdata"), nil)
112+
importMap := map[string]string{}
113+
for _, pkg := range wantImports {
114+
export, _ := FindPkg(pkg, "testdata")
115+
if export == "" {
116+
t.Fatalf("no export data found for %s", pkg)
117+
}
118+
importMap[pkg] = export
119+
}
120+
121+
compile(t, "testdata", testfile, filepath.Join(tmpdir, "testdata"), importMap)
100122
path := "./testdata/" + strings.TrimSuffix(testfile, ".go")
101123

102124
if pkg := testPath(t, path, tmpdir); pkg != nil {
@@ -424,7 +446,13 @@ func TestIssue13566(t *testing.T) {
424446
if err != nil {
425447
t.Fatal(err)
426448
}
427-
compile(t, "testdata", "a.go", testoutdir, nil)
449+
450+
jsonExport, _ := FindPkg("encoding/json", "testdata")
451+
if jsonExport == "" {
452+
t.Fatalf("no export data found for encoding/json")
453+
}
454+
455+
compile(t, "testdata", "a.go", testoutdir, map[string]string{"encoding/json": jsonExport})
428456
compile(t, testoutdir, bpath, testoutdir, map[string]string{"testdata/a": filepath.Join(testoutdir, "a.o")})
429457

430458
// import must succeed (test for issue at hand)
@@ -598,12 +626,14 @@ func TestIssue25596(t *testing.T) {
598626
func importPkg(t *testing.T, path, srcDir string) *types2.Package {
599627
pkg, err := Import(make(map[string]*types2.Package), path, srcDir, nil)
600628
if err != nil {
629+
t.Helper()
601630
t.Fatal(err)
602631
}
603632
return pkg
604633
}
605634

606635
func compileAndImportPkg(t *testing.T, name string) *types2.Package {
636+
t.Helper()
607637
tmpdir := mktmpdir(t)
608638
defer os.RemoveAll(tmpdir)
609639
compile(t, "testdata", name+".go", filepath.Join(tmpdir, "testdata"), nil)
@@ -614,6 +644,7 @@ func lookupObj(t *testing.T, scope *types2.Scope, name string) types2.Object {
614644
if obj := scope.Lookup(name); obj != nil {
615645
return obj
616646
}
647+
t.Helper()
617648
t.Fatalf("%s not found", name)
618649
return nil
619650
}

src/go/internal/gcimporter/gcimporter_test.go

+44-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bytes"
99
"fmt"
1010
"internal/goexperiment"
11+
"internal/goroot"
1112
"internal/testenv"
1213
"os"
1314
"os/exec"
@@ -44,8 +45,20 @@ func compile(t *testing.T, dirname, filename, outdirname string, packagefiles ma
4445
}
4546
objname := basename + ".o"
4647
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, "\npackagefile %s=%s\n", k, v)
56+
}
57+
if err := os.WriteFile(importcfgfile, importcfg.Bytes(), 0655); err != nil {
58+
t.Fatal(err)
59+
}
60+
}
61+
4962
pkgpath := path.Join("testdata", basename)
5063
cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-p", pkgpath, "-D", "testdata", "-importcfg", importcfgfile, "-o", outname, filename)
5164
cmd.Dir = dirname
@@ -106,7 +119,16 @@ func TestImportTestdata(t *testing.T) {
106119
tmpdir := mktmpdir(t)
107120
defer os.RemoveAll(tmpdir)
108121

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)
110132
path := "./testdata/" + strings.TrimSuffix(testfile, ".go")
111133

112134
if pkg := testPath(t, path, tmpdir); pkg != nil {
@@ -124,6 +146,10 @@ func TestImportTestdata(t *testing.T) {
124146
}
125147

126148
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+
127153
// This package only handles gc export data.
128154
if runtime.Compiler != "gc" {
129155
t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler)
@@ -178,7 +204,11 @@ func TestImportTypeparamTests(t *testing.T) {
178204

179205
// Compile and import, and compare the resulting package with the package
180206
// 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)
182212
pkgName := strings.TrimSuffix(entry.Name(), ".go")
183213
imported := importPkg(t, "./testdata/"+pkgName, tmpdir)
184214
checked := checkFile(t, filename, src)
@@ -554,7 +584,13 @@ func TestIssue13566(t *testing.T) {
554584
if err != nil {
555585
t.Fatal(err)
556586
}
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})
558594
compile(t, testoutdir, bpath, testoutdir, map[string]string{"testdata/a": filepath.Join(testoutdir, "a.o")})
559595

560596
// import must succeed (test for issue at hand)
@@ -755,12 +791,14 @@ func importPkg(t *testing.T, path, srcDir string) *types.Package {
755791
fset := token.NewFileSet()
756792
pkg, err := Import(fset, make(map[string]*types.Package), path, srcDir, nil)
757793
if err != nil {
794+
t.Helper()
758795
t.Fatal(err)
759796
}
760797
return pkg
761798
}
762799

763800
func compileAndImportPkg(t *testing.T, name string) *types.Package {
801+
t.Helper()
764802
tmpdir := mktmpdir(t)
765803
defer os.RemoveAll(tmpdir)
766804
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 {
771809
if obj := scope.Lookup(name); obj != nil {
772810
return obj
773811
}
812+
t.Helper()
774813
t.Fatalf("%s not found", name)
775814
return nil
776815
}

0 commit comments

Comments
 (0)