]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: clean up test support code and remove global variables
authorRob Findley <rfindley@google.com>
Tue, 11 Aug 2020 16:49:40 +0000 (12:49 -0400)
committerRobert Findley <rfindley@google.com>
Thu, 27 Aug 2020 18:33:37 +0000 (18:33 +0000)
This is a straightforward port of CL 244627.

Change-Id: Ide980957430b35e22a6e22818b0ce9de410988af
Reviewed-on: https://go-review.googlesource.com/c/go/+/247902
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/go/types/stdlib_test.go

index 51ee0b1c363c2b8932b5149ab10f1bea74937807..f5a3273fa1c1d8873c8fa06aa8f6e9a01e144bc9 100644 (file)
@@ -27,22 +27,21 @@ import (
        . "go/types"
 )
 
-var (
-       pkgCount int // number of packages processed
-       start    time.Time
-
-       // Use the same importer for all std lib tests to
-       // avoid repeated importing of the same packages.
-       stdLibImporter = importer.Default()
-)
+// Use the same importer for all std lib tests to
+// avoid repeated importing of the same packages.
+var stdLibImporter = importer.Default()
 
 func TestStdlib(t *testing.T) {
        testenv.MustHaveGoBuild(t)
 
-       start = time.Now()
-       walkDirs(t, filepath.Join(runtime.GOROOT(), "src"))
+       pkgCount := 0
+       duration := walkPkgDirs(filepath.Join(runtime.GOROOT(), "src"), func(dir string, filenames []string) {
+               typecheck(t, dir, filenames)
+               pkgCount++
+       }, t.Error)
+
        if testing.Verbose() {
-               fmt.Println(pkgCount, "packages typechecked in", time.Since(start))
+               fmt.Println(pkgCount, "packages typechecked in", duration)
        }
 }
 
@@ -235,7 +234,6 @@ func typecheck(t *testing.T, path string, filenames []string) {
        }
        info := Info{Uses: make(map[*ast.Ident]Object)}
        conf.Check(path, fset, files, &info)
-       pkgCount++
 
        // Perform checks of API invariants.
 
@@ -278,39 +276,48 @@ func pkgFilenames(dir string) ([]string, error) {
        return filenames, nil
 }
 
-// Note: Could use filepath.Walk instead of walkDirs but that wouldn't
-//       necessarily be shorter or clearer after adding the code to
-//       terminate early for -short tests.
+func walkPkgDirs(dir string, pkgh func(dir string, filenames []string), errh func(args ...interface{})) time.Duration {
+       w := walker{time.Now(), 10 * time.Millisecond, pkgh, errh}
+       w.walk(dir)
+       return time.Since(w.start)
+}
 
-func walkDirs(t *testing.T, dir string) {
+type walker struct {
+       start time.Time
+       dmax  time.Duration
+       pkgh  func(dir string, filenames []string)
+       errh  func(args ...interface{})
+}
+
+func (w *walker) walk(dir string) {
        // limit run time for short tests
-       if testing.Short() && time.Since(start) >= 10*time.Millisecond {
+       if testing.Short() && time.Since(w.start) >= w.dmax {
                return
        }
 
        fis, err := ioutil.ReadDir(dir)
        if err != nil {
-               t.Error(err)
+               w.errh(err)
                return
        }
 
-       // typecheck package in directory
+       // apply pkgh to the files in directory dir
        // but ignore files directly under $GOROOT/src (might be temporary test files).
        if dir != filepath.Join(runtime.GOROOT(), "src") {
                files, err := pkgFilenames(dir)
                if err != nil {
-                       t.Error(err)
+                       w.errh(err)
                        return
                }
                if files != nil {
-                       typecheck(t, dir, files)
+                       w.pkgh(dir, files)
                }
        }
 
        // traverse subdirectories, but don't walk into testdata
        for _, fi := range fis {
                if fi.IsDir() && fi.Name() != "testdata" {
-                       walkDirs(t, filepath.Join(dir, fi.Name()))
+                       w.walk(filepath.Join(dir, fi.Name()))
                }
        }
 }