]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/cmd/go/internal/test/test.go
[dev.fuzz] all: merge master (d137b74) into dev.fuzz
[gostls13.git] / src / cmd / go / internal / test / test.go
index 076a7e0807fbf85121a8f6f8588726188cf06e4e..290f6fd3481876d3e80deb46d24db709648d8114 100644 (file)
@@ -29,6 +29,7 @@ import (
        "cmd/go/internal/cfg"
        "cmd/go/internal/load"
        "cmd/go/internal/lockedfile"
+       "cmd/go/internal/search"
        "cmd/go/internal/str"
        "cmd/go/internal/trace"
        "cmd/go/internal/work"
@@ -117,8 +118,8 @@ elapsed time in the summary line.
 
 The rule for a match in the cache is that the run involves the same
 test binary and the flags on the command line come entirely from a
-restricted set of 'cacheable' test flags, defined as -cpu, -list,
--parallel, -run, -short, and -v. If a run of go test has any test
+restricted set of 'cacheable' test flags, defined as -benchtime, -cpu,
+-list, -parallel, -run, -short, and -v. If a run of go test has any test
 or non-test flags outside this set, the result is not cached. To
 disable test caching, use any test flag or argument other than the
 cacheable flags. The idiomatic way to disable test caching explicitly
@@ -271,6 +272,13 @@ control the execution of any test:
            the Go tree can run a sanity check but not spend time running
            exhaustive tests.
 
+       -shuffle off,on,N
+               Randomize the execution order of tests and benchmarks.
+               It is off by default. If -shuffle is set to on, then it will seed
+               the randomizer using the system clock. If -shuffle is set to an
+               integer N, then N will be used as the seed value. In both cases,
+               the seed will be reported for reproducibility.
+
        -timeout d
            If a test binary runs longer than duration d, panic.
            If d is 0, the timeout is disabled.
@@ -479,7 +487,8 @@ var (
        testJSON         bool                              // -json flag
        testList         string                            // -list flag
        testO            string                            // -o flag
-       testOutputDir    = base.Cwd                        // -outputdir flag
+       testOutputDir    outputdirFlag                     // -outputdir flag
+       testShuffle      shuffleFlag                       // -shuffle flag
        testTimeout      time.Duration                     // -timeout flag
        testV            bool                              // -v flag
        testVet          = vetFlag{flags: defaultVetFlags} // -vet flag
@@ -569,8 +578,6 @@ var defaultVetFlags = []string{
 }
 
 func runTest(ctx context.Context, cmd *base.Command, args []string) {
-       load.ModResolveTests = true
-
        pkgArgs, testArgs = testFlags(args)
 
        if cfg.DebugTrace != "" {
@@ -596,7 +603,8 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
        work.VetFlags = testVet.flags
        work.VetExplicit = testVet.explicit
 
-       pkgs = load.PackagesAndErrors(ctx, pkgArgs)
+       pkgOpts := load.PackageOpts{ModResolveTests: true}
+       pkgs = load.PackagesAndErrors(ctx, pkgOpts, pkgArgs)
        load.CheckPackageErrors(pkgs)
        if len(pkgs) == 0 {
                base.Fatalf("no packages to test")
@@ -682,7 +690,7 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
                sort.Strings(all)
 
                a := &work.Action{Mode: "go test -i"}
-               pkgs := load.PackagesAndErrors(ctx, all)
+               pkgs := load.PackagesAndErrors(ctx, pkgOpts, all)
                load.CheckPackageErrors(pkgs)
                for _, p := range pkgs {
                        if cfg.BuildToolchainName == "gccgo" && p.Standard {
@@ -705,11 +713,11 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
                match := make([]func(*load.Package) bool, len(testCoverPaths))
                matched := make([]bool, len(testCoverPaths))
                for i := range testCoverPaths {
-                       match[i] = load.MatchPackage(testCoverPaths[i], base.Cwd)
+                       match[i] = load.MatchPackage(testCoverPaths[i], base.Cwd())
                }
 
                // Select for coverage all dependencies matching the testCoverPaths patterns.
-               for _, p := range load.TestPackageList(ctx, pkgs) {
+               for _, p := range load.TestPackageList(ctx, pkgOpts, pkgs) {
                        haveMatch := false
                        for i := range testCoverPaths {
                                if match[i](p) {
@@ -718,6 +726,12 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
                                }
                        }
 
+                       // A package which only has test files can't be imported
+                       // as a dependency, nor can it be instrumented for coverage.
+                       if len(p.GoFiles)+len(p.CgoFiles) == 0 {
+                               continue
+                       }
+
                        // Silently ignore attempts to run coverage on
                        // sync/atomic when using atomic coverage mode.
                        // Atomic coverage mode uses sync/atomic, so
@@ -780,7 +794,7 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
                        ensureImport(p, "sync/atomic")
                }
 
-               buildTest, runTest, printTest, err := builderTest(&b, ctx, p)
+               buildTest, runTest, printTest, err := builderTest(&b, ctx, pkgOpts, p)
                if err != nil {
                        str := err.Error()
                        str = strings.TrimPrefix(str, "\n")
@@ -847,7 +861,7 @@ var windowsBadWords = []string{
        "update",
 }
 
-func builderTest(b *work.Builder, ctx context.Context, p *load.Package) (buildAction, runAction, printAction *work.Action, err error) {
+func builderTest(b *work.Builder, ctx context.Context, pkgOpts load.PackageOpts, p *load.Package) (buildAction, runAction, printAction *work.Action, err error) {
        if len(p.TestGoFiles)+len(p.XTestGoFiles) == 0 {
                build := b.CompileAction(work.ModeBuild, work.ModeBuild, p)
                run := &work.Action{Mode: "test run", Package: p, Deps: []*work.Action{build}}
@@ -870,7 +884,7 @@ func builderTest(b *work.Builder, ctx context.Context, p *load.Package) (buildAc
                        DeclVars: declareCoverVars,
                }
        }
-       pmain, ptest, pxtest, err := load.TestPackagesFor(ctx, p, cover)
+       pmain, ptest, pxtest, err := load.TestPackagesFor(ctx, pkgOpts, p, cover)
        if err != nil {
                return nil, nil, nil, err
        }
@@ -943,11 +957,11 @@ func builderTest(b *work.Builder, ctx context.Context, p *load.Package) (buildAc
        var installAction, cleanAction *work.Action
        if testC || testNeedBinary() {
                // -c or profiling flag: create action to copy binary to ./test.out.
-               target := filepath.Join(base.Cwd, testBinary+cfg.ExeSuffix)
+               target := filepath.Join(base.Cwd(), testBinary+cfg.ExeSuffix)
                if testO != "" {
                        target = testO
                        if !filepath.IsAbs(target) {
-                               target = filepath.Join(base.Cwd, target)
+                               target = filepath.Join(base.Cwd(), target)
                        }
                }
                if target == os.DevNull {
@@ -1351,7 +1365,8 @@ func (c *runCache) tryCacheWithID(b *work.Builder, a *work.Action, id string) bo
                        return false
                }
                switch arg[:i] {
-               case "-test.cpu",
+               case "-test.benchtime",
+                       "-test.cpu",
                        "-test.list",
                        "-test.parallel",
                        "-test.run",
@@ -1524,7 +1539,7 @@ func computeTestInputsID(a *work.Action, testlog []byte) (cache.ActionID, error)
                        if !filepath.IsAbs(name) {
                                name = filepath.Join(pwd, name)
                        }
-                       if a.Package.Root == "" || !inDir(name, a.Package.Root) {
+                       if a.Package.Root == "" || search.InDir(name, a.Package.Root) == "" {
                                // Do not recheck files outside the module, GOPATH, or GOROOT root.
                                break
                        }
@@ -1533,7 +1548,7 @@ func computeTestInputsID(a *work.Action, testlog []byte) (cache.ActionID, error)
                        if !filepath.IsAbs(name) {
                                name = filepath.Join(pwd, name)
                        }
-                       if a.Package.Root == "" || !inDir(name, a.Package.Root) {
+                       if a.Package.Root == "" || search.InDir(name, a.Package.Root) == "" {
                                // Do not recheck files outside the module, GOPATH, or GOROOT root.
                                break
                        }
@@ -1551,18 +1566,6 @@ func computeTestInputsID(a *work.Action, testlog []byte) (cache.ActionID, error)
        return sum, nil
 }
 
-func inDir(path, dir string) bool {
-       if str.HasFilePathPrefix(path, dir) {
-               return true
-       }
-       xpath, err1 := filepath.EvalSymlinks(path)
-       xdir, err2 := filepath.EvalSymlinks(dir)
-       if err1 == nil && err2 == nil && str.HasFilePathPrefix(xpath, xdir) {
-               return true
-       }
-       return false
-}
-
 func hashGetenv(name string) cache.ActionID {
        h := cache.NewHash("getenv")
        v, ok := os.LookupEnv(name)