"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"
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
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.
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
}
func runTest(ctx context.Context, cmd *base.Command, args []string) {
- load.ModResolveTests = true
-
pkgArgs, testArgs = testFlags(args)
if cfg.DebugTrace != "" {
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")
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 {
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) {
}
}
+ // 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
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")
"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}}
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
}
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 {
return false
}
switch arg[:i] {
- case "-test.cpu",
+ case "-test.benchtime",
+ "-test.cpu",
"-test.list",
"-test.parallel",
"-test.run",
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
}
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
}
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)