]> Cypherpunks repositories - gostls13.git/commitdiff
go/internal/gcimporter: fix TestImportStdLib
authorMichael Matloob <matloob@golang.org>
Wed, 30 Nov 2022 17:53:13 +0000 (12:53 -0500)
committerMichael Matloob <matloob@golang.org>
Thu, 1 Dec 2022 20:48:36 +0000 (20:48 +0000)
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 <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

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

index 4f1ba41a1d29ab1d319242f2bcbaf306f448b480..7aba193b37499d5edfc113d4de2421d5249fc023 100644 (file)
@@ -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)
 }
 
index af99e7a8522b265d1a24fbc1d59a55b38cd53826..faf3bb0f0ea83eeb910ab6d813705a0aa004eb05 100644 (file)
@@ -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)
 }