Skip to content

Commit

Permalink
go/internal/gcimporter: in short tests, avoid creating export data fo…
Browse files Browse the repository at this point in the history
…r 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 <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
  • Loading branch information
Bryan C. Mills authored and gopherbot committed Dec 2, 2022
1 parent 3e2ab20 commit a79b55b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 9 deletions.
39 changes: 35 additions & 4 deletions src/cmd/compile/internal/importer/gcimporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
49 changes: 44 additions & 5 deletions src/go/internal/gcimporter/gcimporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"fmt"
"internal/goexperiment"
"internal/goroot"
"internal/testenv"
"os"
"os/exec"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

0 comments on commit a79b55b

Please sign in to comment.