From a79b55bb9a5f459fd8d518223022a6d307354a27 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Thu, 1 Dec 2022 15:37:50 -0500 Subject: [PATCH] go/internal/gcimporter: in short tests, avoid creating export data for all of std MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 TryBot-Result: Gopher Robot Auto-Submit: Bryan Mills Reviewed-by: Michael Matloob --- .../internal/importer/gcimporter_test.go | 39 +++++++++++++-- src/go/internal/gcimporter/gcimporter_test.go | 49 +++++++++++++++++-- 2 files changed, 79 insertions(+), 9 deletions(-) diff --git a/src/cmd/compile/internal/importer/gcimporter_test.go b/src/cmd/compile/internal/importer/gcimporter_test.go index 7aba193b37..387c7c03fe 100644 --- a/src/cmd/compile/internal/importer/gcimporter_test.go +++ b/src/cmd/compile/internal/importer/gcimporter_test.go @@ -33,17 +33,30 @@ func compile(t *testing.T, dirname, filename, outdirname string, packagefiles ma // filename must end with ".go" basename, ok := strings.CutSuffix(filepath.Base(filename), ".go") if !ok { + t.Helper() t.Fatalf("filename doesn't end in .go: %s", filename) } objname := basename + ".o" outname := filepath.Join(outdirname, objname) - importcfgfile := filepath.Join(outdirname, basename) + ".importcfg" - testenv.WriteImportcfg(t, importcfgfile, packagefiles) pkgpath := path.Join("testdata", basename) + + importcfgfile := os.DevNull + if len(packagefiles) > 0 { + importcfgfile = filepath.Join(outdirname, basename) + ".importcfg" + importcfg := new(bytes.Buffer) + for k, v := range packagefiles { + fmt.Fprintf(importcfg, "packagefile %s=%s\n", k, v) + } + if err := os.WriteFile(importcfgfile, importcfg.Bytes(), 0655); err != nil { + t.Fatal(err) + } + } + cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-p", pkgpath, "-D", "testdata", "-importcfg", importcfgfile, "-o", outname, filename) cmd.Dir = dirname out, err := cmd.CombinedOutput() if err != nil { + t.Helper() t.Logf("%s", out) t.Fatalf("go tool compile %s failed: %s", filename, err) } @@ -96,7 +109,16 @@ func TestImportTestdata(t *testing.T) { tmpdir := mktmpdir(t) defer os.RemoveAll(tmpdir) - compile(t, "testdata", testfile, filepath.Join(tmpdir, "testdata"), nil) + importMap := map[string]string{} + for _, pkg := range wantImports { + export, _ := FindPkg(pkg, "testdata") + if export == "" { + t.Fatalf("no export data found for %s", pkg) + } + importMap[pkg] = export + } + + compile(t, "testdata", testfile, filepath.Join(tmpdir, "testdata"), importMap) path := "./testdata/" + strings.TrimSuffix(testfile, ".go") if pkg := testPath(t, path, tmpdir); pkg != nil { @@ -424,7 +446,13 @@ func TestIssue13566(t *testing.T) { if err != nil { t.Fatal(err) } - compile(t, "testdata", "a.go", testoutdir, nil) + + jsonExport, _ := FindPkg("encoding/json", "testdata") + if jsonExport == "" { + t.Fatalf("no export data found for encoding/json") + } + + compile(t, "testdata", "a.go", testoutdir, map[string]string{"encoding/json": jsonExport}) compile(t, testoutdir, bpath, testoutdir, map[string]string{"testdata/a": filepath.Join(testoutdir, "a.o")}) // import must succeed (test for issue at hand) @@ -598,12 +626,14 @@ func TestIssue25596(t *testing.T) { func importPkg(t *testing.T, path, srcDir string) *types2.Package { pkg, err := Import(make(map[string]*types2.Package), path, srcDir, nil) if err != nil { + t.Helper() t.Fatal(err) } return pkg } func compileAndImportPkg(t *testing.T, name string) *types2.Package { + t.Helper() tmpdir := mktmpdir(t) defer os.RemoveAll(tmpdir) 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 { if obj := scope.Lookup(name); obj != nil { return obj } + t.Helper() t.Fatalf("%s not found", name) return nil } diff --git a/src/go/internal/gcimporter/gcimporter_test.go b/src/go/internal/gcimporter/gcimporter_test.go index faf3bb0f0e..675bf222ce 100644 --- a/src/go/internal/gcimporter/gcimporter_test.go +++ b/src/go/internal/gcimporter/gcimporter_test.go @@ -8,6 +8,7 @@ import ( "bytes" "fmt" "internal/goexperiment" + "internal/goroot" "internal/testenv" "os" "os/exec" @@ -44,8 +45,20 @@ func compile(t *testing.T, dirname, filename, outdirname string, packagefiles ma } objname := basename + ".o" outname := filepath.Join(outdirname, objname) - importcfgfile := filepath.Join(outdirname, basename) + ".importcfg" - testenv.WriteImportcfg(t, importcfgfile, packagefiles) + + importcfgfile := os.DevNull + if len(packagefiles) > 0 { + importcfgfile = filepath.Join(outdirname, basename) + ".importcfg" + importcfg := new(bytes.Buffer) + fmt.Fprintf(importcfg, "# import config") + for k, v := range packagefiles { + fmt.Fprintf(importcfg, "\npackagefile %s=%s\n", k, v) + } + if err := os.WriteFile(importcfgfile, importcfg.Bytes(), 0655); err != nil { + t.Fatal(err) + } + } + pkgpath := path.Join("testdata", basename) cmd := testenv.Command(t, testenv.GoToolPath(t), "tool", "compile", "-p", pkgpath, "-D", "testdata", "-importcfg", importcfgfile, "-o", outname, filename) cmd.Dir = dirname @@ -106,7 +119,16 @@ func TestImportTestdata(t *testing.T) { tmpdir := mktmpdir(t) defer os.RemoveAll(tmpdir) - compile(t, "testdata", testfile, filepath.Join(tmpdir, "testdata"), nil) + packageFiles := map[string]string{} + for _, pkg := range wantImports { + export, _ := FindPkg(pkg, "testdata") + if export == "" { + t.Fatalf("no export data found for %s", pkg) + } + packageFiles[pkg] = export + } + + compile(t, "testdata", testfile, filepath.Join(tmpdir, "testdata"), packageFiles) path := "./testdata/" + strings.TrimSuffix(testfile, ".go") if pkg := testPath(t, path, tmpdir); pkg != nil { @@ -124,6 +146,10 @@ func TestImportTestdata(t *testing.T) { } func TestImportTypeparamTests(t *testing.T) { + if testing.Short() { + t.Skipf("in short mode, skipping test that requires export data for all of std") + } + // This package only handles gc export data. if runtime.Compiler != "gc" { t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler) @@ -178,7 +204,11 @@ func TestImportTypeparamTests(t *testing.T) { // Compile and import, and compare the resulting package with the package // that was type-checked directly. - compile(t, rootDir, entry.Name(), filepath.Join(tmpdir, "testdata"), nil) + pkgFiles, err := goroot.PkgfileMap() + if err != nil { + t.Fatal(err) + } + compile(t, rootDir, entry.Name(), filepath.Join(tmpdir, "testdata"), pkgFiles) pkgName := strings.TrimSuffix(entry.Name(), ".go") imported := importPkg(t, "./testdata/"+pkgName, tmpdir) checked := checkFile(t, filename, src) @@ -554,7 +584,13 @@ func TestIssue13566(t *testing.T) { if err != nil { t.Fatal(err) } - compile(t, "testdata", "a.go", testoutdir, nil) + + jsonExport, _ := FindPkg("encoding/json", "testdata") + if jsonExport == "" { + t.Fatalf("no export data found for encoding/json") + } + + compile(t, "testdata", "a.go", testoutdir, map[string]string{"encoding/json": jsonExport}) compile(t, testoutdir, bpath, testoutdir, map[string]string{"testdata/a": filepath.Join(testoutdir, "a.o")}) // import must succeed (test for issue at hand) @@ -755,12 +791,14 @@ func importPkg(t *testing.T, path, srcDir string) *types.Package { fset := token.NewFileSet() pkg, err := Import(fset, make(map[string]*types.Package), path, srcDir, nil) if err != nil { + t.Helper() t.Fatal(err) } return pkg } func compileAndImportPkg(t *testing.T, name string) *types.Package { + t.Helper() tmpdir := mktmpdir(t) defer os.RemoveAll(tmpdir) 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 { if obj := scope.Lookup(name); obj != nil { return obj } + t.Helper() t.Fatalf("%s not found", name) return nil } -- 2.48.1