From: Michael Matloob Date: Wed, 30 Nov 2022 17:53:13 +0000 (-0500) Subject: go/internal/gcimporter: fix TestImportStdLib X-Git-Tag: go1.20rc1~51 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=8ed74ee39afab11012460fa3e54dd6b83a6439c0;p=gostls13.git go/internal/gcimporter: fix TestImportStdLib The test attempted to find all stdlib packages by scanning pkg/$GOOS_$GOARCH for .a files and then tried to import all of them. Now that .a files are no longer being placed there, the test is a noop. Fix this by using go list std (and filtering out testonly packages) and trying to import all of those to recreate what the test intended to do. This also removes a dependency on the pkg/$GOOS_$GOARCH directory which will stop being produced by dist in CL 453496. For #47257 Change-Id: I7c1944a89db9da9269def3d64a11408a60d73d46 Reviewed-on: https://go-review.googlesource.com/c/go/+/453858 Reviewed-by: Michael Matloob Run-TryBot: Michael Matloob Reviewed-by: Bryan Mills TryBot-Result: Gopher Robot --- diff --git a/src/cmd/compile/internal/importer/gcimporter_test.go b/src/cmd/compile/internal/importer/gcimporter_test.go index 4f1ba41a1d..7aba193b37 100644 --- a/src/cmd/compile/internal/importer/gcimporter_test.go +++ b/src/cmd/compile/internal/importer/gcimporter_test.go @@ -12,6 +12,7 @@ import ( "internal/goexperiment" "internal/testenv" "os" + "os/exec" "path" "path/filepath" "runtime" @@ -60,37 +61,6 @@ func testPath(t *testing.T, path, srcDir string) *types2.Package { return pkg } -const maxTime = 30 * time.Second - -func testDir(t *testing.T, dir string, endTime time.Time) (nimports int) { - dirname := filepath.Join(testenv.GOROOT(t), "pkg", runtime.GOOS+"_"+runtime.GOARCH, dir) - list, err := os.ReadDir(dirname) - if err != nil { - t.Fatalf("testDir(%s): %s", dirname, err) - } - for _, f := range list { - if time.Now().After(endTime) { - t.Log("testing time used up") - return - } - switch { - case !f.IsDir(): - // try extensions - for _, ext := range pkgExts { - if strings.HasSuffix(f.Name(), ext) { - name := f.Name()[0 : len(f.Name())-len(ext)] // remove extension - if testPath(t, filepath.Join(dir, name), dir) != nil { - nimports++ - } - } - } - case f.IsDir(): - nimports += testDir(t, filepath.Join(dir, f.Name()), endTime) - } - } - return -} - func mktmpdir(t *testing.T) string { tmpdir, err := os.MkdirTemp("", "gcimporter_test") if err != nil { @@ -235,6 +205,9 @@ func TestVersionHandling(t *testing.T) { } func TestImportStdLib(t *testing.T) { + if testing.Short() { + t.Skip("the imports can be expensive, and this test is especially slow when the build cache is empty") + } testenv.MustHaveGoBuild(t) // This package only handles gc export data. @@ -242,11 +215,29 @@ func TestImportStdLib(t *testing.T) { t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler) } - dt := maxTime - if testing.Short() && testenv.Builder() == "" { - dt = 10 * time.Millisecond + // Get list of packages in stdlib. Filter out test-only packages with {{if .GoFiles}} check. + var stderr bytes.Buffer + cmd := exec.Command("go", "list", "-f", "{{if .GoFiles}}{{.ImportPath}}{{end}}", "std") + cmd.Stderr = &stderr + out, err := cmd.Output() + if err != nil { + t.Fatalf("failed to run go list to determine stdlib packages: %v\nstderr:\n%v", err, stderr.String()) } - nimports := testDir(t, "", time.Now().Add(dt)) // installed packages + pkgs := strings.Fields(string(out)) + + var nimports int + for _, pkg := range pkgs { + t.Run(pkg, func(t *testing.T) { + if testPath(t, pkg, filepath.Join(testenv.GOROOT(t), "src", path.Dir(pkg))) != nil { + nimports++ + } + }) + } + const minPkgs = 225 // 'GOOS=plan9 go1.18 list std | wc -l' reports 228; most other platforms have more. + if len(pkgs) < minPkgs { + t.Fatalf("too few packages (%d) were imported", nimports) + } + t.Logf("tested %d imports", nimports) } diff --git a/src/go/internal/gcimporter/gcimporter_test.go b/src/go/internal/gcimporter/gcimporter_test.go index af99e7a852..faf3bb0f0e 100644 --- a/src/go/internal/gcimporter/gcimporter_test.go +++ b/src/go/internal/gcimporter/gcimporter_test.go @@ -10,6 +10,7 @@ import ( "internal/goexperiment" "internal/testenv" "os" + "os/exec" "path" "path/filepath" "runtime" @@ -68,39 +69,8 @@ func testPath(t *testing.T, path, srcDir string) *types.Package { return pkg } -const maxTime = 30 * time.Second - var pkgExts = [...]string{".a", ".o"} // keep in sync with gcimporter.go -func testDir(t *testing.T, dir string, endTime time.Time) (nimports int) { - dirname := filepath.Join(testenv.GOROOT(t), "pkg", runtime.GOOS+"_"+runtime.GOARCH, dir) - list, err := os.ReadDir(dirname) - if err != nil { - t.Fatalf("testDir(%s): %s", dirname, err) - } - for _, f := range list { - if time.Now().After(endTime) { - t.Log("testing time used up") - return - } - switch { - case !f.IsDir(): - // try extensions - for _, ext := range pkgExts { - if strings.HasSuffix(f.Name(), ext) { - name := f.Name()[0 : len(f.Name())-len(ext)] // remove extension - if testPath(t, filepath.Join(dir, name), dir) != nil { - nimports++ - } - } - } - case f.IsDir(): - nimports += testDir(t, filepath.Join(dir, f.Name()), endTime) - } - } - return -} - func mktmpdir(t *testing.T) string { tmpdir, err := os.MkdirTemp("", "gcimporter_test") if err != nil { @@ -370,6 +340,9 @@ func TestVersionHandling(t *testing.T) { } func TestImportStdLib(t *testing.T) { + if testing.Short() { + t.Skip("the imports can be expensive, and this test is especially slow when the build cache is empty") + } testenv.MustHaveGoBuild(t) // This package only handles gc export data. @@ -377,11 +350,29 @@ func TestImportStdLib(t *testing.T) { t.Skipf("gc-built packages not available (compiler = %s)", runtime.Compiler) } - dt := maxTime - if testing.Short() && testenv.Builder() == "" { - dt = 10 * time.Millisecond + // Get list of packages in stdlib. Filter out test-only packages with {{if .GoFiles}} check. + var stderr bytes.Buffer + cmd := exec.Command("go", "list", "-f", "{{if .GoFiles}}{{.ImportPath}}{{end}}", "std") + cmd.Stderr = &stderr + out, err := cmd.Output() + if err != nil { + t.Fatalf("failed to run go list to determine stdlib packages: %v\nstderr:\n%v", err, stderr.String()) } - nimports := testDir(t, "", time.Now().Add(dt)) // installed packages + pkgs := strings.Fields(string(out)) + + var nimports int + for _, pkg := range pkgs { + t.Run(pkg, func(t *testing.T) { + if testPath(t, pkg, filepath.Join(testenv.GOROOT(t), "src", path.Dir(pkg))) != nil { + nimports++ + } + }) + } + const minPkgs = 225 // 'GOOS=plan9 go1.18 list std | wc -l' reports 228; most other platforms have more. + if len(pkgs) < minPkgs { + t.Fatalf("too few packages (%d) were imported", nimports) + } + t.Logf("tested %d imports", nimports) }