Skip to content

Commit c117233

Browse files
matloobrsc
authored andcommitted
[internal-branch.go1.20-vendor] 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. Updates golang/go#47257 Change-Id: Idfa0cbb21093776183ce193eb5363a9727bf77ef Reviewed-on: https://go-review.googlesource.com/c/tools/+/454118 Run-TryBot: Michael Matloob <[email protected]> Reviewed-by: Michael Matloob <[email protected]> TryBot-Result: Gopher Robot <[email protected]> gopls-CI: kokoro <[email protected]> Reviewed-by: Bryan Mills <[email protected]> (cherry picked from commit 0379b73) Reviewed-on: https://go-review.googlesource.com/c/tools/+/462636 Run-TryBot: Bryan Mills <[email protected]> Reviewed-by: Russ Cox <[email protected]>
1 parent 5861e20 commit c117233

File tree

2 files changed

+71
-35
lines changed

2 files changed

+71
-35
lines changed

internal/gcimporter/gcimporter_test.go

+26-35
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"io/ioutil"
2121
"os"
2222
"os/exec"
23+
"path"
2324
"path/filepath"
2425
"runtime"
2526
"strings"
@@ -88,37 +89,6 @@ func testPath(t *testing.T, path, srcDir string) *types.Package {
8889
return pkg
8990
}
9091

91-
const maxTime = 30 * time.Second
92-
93-
func testDir(t *testing.T, dir string, endTime time.Time) (nimports int) {
94-
dirname := filepath.Join(runtime.GOROOT(), "pkg", runtime.GOOS+"_"+runtime.GOARCH, dir)
95-
list, err := ioutil.ReadDir(dirname)
96-
if err != nil {
97-
t.Fatalf("testDir(%s): %s", dirname, err)
98-
}
99-
for _, f := range list {
100-
if time.Now().After(endTime) {
101-
t.Log("testing time used up")
102-
return
103-
}
104-
switch {
105-
case !f.IsDir():
106-
// try extensions
107-
for _, ext := range pkgExts {
108-
if strings.HasSuffix(f.Name(), ext) {
109-
name := f.Name()[0 : len(f.Name())-len(ext)] // remove extension
110-
if testPath(t, filepath.Join(dir, name), dir) != nil {
111-
nimports++
112-
}
113-
}
114-
}
115-
case f.IsDir():
116-
nimports += testDir(t, filepath.Join(dir, f.Name()), endTime)
117-
}
118-
}
119-
return
120-
}
121-
12292
func mktmpdir(t *testing.T) string {
12393
tmpdir, err := ioutil.TempDir("", "gcimporter_test")
12494
if err != nil {
@@ -371,15 +341,36 @@ func TestVersionHandling(t *testing.T) {
371341
}
372342

373343
func TestImportStdLib(t *testing.T) {
344+
if testing.Short() {
345+
t.Skip("the imports can be expensive, and this test is especially slow when the build cache is empty")
346+
}
374347
// This package only handles gc export data.
375348
needsCompiler(t, "gc")
376349
testenv.NeedsGoBuild(t) // to find stdlib export data in the build cache
377350

378-
dt := maxTime
379-
if testing.Short() && os.Getenv("GO_BUILDER_NAME") == "" {
380-
dt = 10 * time.Millisecond
351+
// Get list of packages in stdlib. Filter out test-only packages with {{if .GoFiles}} check.
352+
var stderr bytes.Buffer
353+
cmd := exec.Command("go", "list", "-f", "{{if .GoFiles}}{{.ImportPath}}{{end}}", "std")
354+
cmd.Stderr = &stderr
355+
out, err := cmd.Output()
356+
if err != nil {
357+
t.Fatalf("failed to run go list to determine stdlib packages: %v\nstderr:\n%v", err, stderr.String())
381358
}
382-
nimports := testDir(t, "", time.Now().Add(dt)) // installed packages
359+
pkgs := strings.Fields(string(out))
360+
361+
var nimports int
362+
for _, pkg := range pkgs {
363+
t.Run(pkg, func(t *testing.T) {
364+
if testPath(t, pkg, filepath.Join(testenv.GOROOT(t), "src", path.Dir(pkg))) != nil {
365+
nimports++
366+
}
367+
})
368+
}
369+
const minPkgs = 225 // 'GOOS=plan9 go1.18 list std | wc -l' reports 228; most other platforms have more.
370+
if len(pkgs) < minPkgs {
371+
t.Fatalf("too few packages (%d) were imported", nimports)
372+
}
373+
383374
t.Logf("tested %d imports", nimports)
384375
}
385376

internal/testenv/testenv.go

+45
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,48 @@ func WriteImportcfg(t testing.TB, dstPath string, additionalPackageFiles map[str
345345
t.Fatalf("writing the importcfg failed: %s", err)
346346
}
347347
}
348+
349+
var (
350+
gorootOnce sync.Once
351+
gorootPath string
352+
gorootErr error
353+
)
354+
355+
func findGOROOT() (string, error) {
356+
gorootOnce.Do(func() {
357+
gorootPath = runtime.GOROOT()
358+
if gorootPath != "" {
359+
// If runtime.GOROOT() is non-empty, assume that it is valid. (It might
360+
// not be: for example, the user may have explicitly set GOROOT
361+
// to the wrong directory.)
362+
return
363+
}
364+
365+
cmd := exec.Command("go", "env", "GOROOT")
366+
out, err := cmd.Output()
367+
if err != nil {
368+
gorootErr = fmt.Errorf("%v: %v", cmd, err)
369+
}
370+
gorootPath = strings.TrimSpace(string(out))
371+
})
372+
373+
return gorootPath, gorootErr
374+
}
375+
376+
// GOROOT reports the path to the directory containing the root of the Go
377+
// project source tree. This is normally equivalent to runtime.GOROOT, but
378+
// works even if the test binary was built with -trimpath.
379+
//
380+
// If GOROOT cannot be found, GOROOT skips t if t is non-nil,
381+
// or panics otherwise.
382+
func GOROOT(t testing.TB) string {
383+
path, err := findGOROOT()
384+
if err != nil {
385+
if t == nil {
386+
panic(err)
387+
}
388+
t.Helper()
389+
t.Skip(err)
390+
}
391+
return path
392+
}

0 commit comments

Comments
 (0)