Skip to content

Commit e33794f

Browse files
committed
cmd/go: redefine -coverpkg to be a pattern list
If you run go test -coverpkg=all fmt one possible interpretation is that you want coverage for all the packages involved in the fmt test, not all the packages in the world. Because coverpkg was previously defined as a list of packages to be loaded, however, it meant all packages in the world. Now that the go command has a concept of package notation being used as a matching filter instead of a direct enumeration, apply that to -coverpkg, so that -coverpkg=all now has the more useful filter interpretation. Fixes #10271. Fixes #21283. Change-Id: Iddb77b21ba286d3dd65b62507af27e244865072d Reviewed-on: https://go-review.googlesource.com/76876 Run-TryBot: Russ Cox <[email protected]> Reviewed-by: David Crawshaw <[email protected]>
1 parent 283558e commit e33794f

File tree

6 files changed

+66
-32
lines changed

6 files changed

+66
-32
lines changed

src/cmd/go/alldocs.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@
576576
//
577577
// Usage:
578578
//
579-
// go list [-e] [-f format] [-json] [build flags] [packages]
579+
// go list [-deps] [-e] [-f format] [-json] [build flags] [packages]
580580
//
581581
// List lists the packages named by the import paths, one per line.
582582
//
@@ -680,6 +680,9 @@
680680
// The -json flag causes the package data to be printed in JSON format
681681
// instead of using the template format.
682682
//
683+
// The -deps flag causes list to add to its output all the dependencies of
684+
// the packages named on the command line.
685+
//
683686
// The -e flag changes the handling of erroneous packages, those that
684687
// cannot be found or are malformed. By default, the list command
685688
// prints an error to standard error for each erroneous package and
@@ -761,20 +764,21 @@
761764
//
762765
// In local directory mode, go test compiles and tests the package sources
763766
// found in the current directory and then runs the resulting test binary.
764-
// In this mode, the test binary runs with standard output and standard error
765-
// connected directly to the go command's own standard output and standard
766-
// error, and test result caching (discussed below) is disabled.
767-
// After the package test finishes, go test prints to standard output a
768-
// summary line showing the test status ('ok' or 'FAIL'), package name,
769-
// and elapsed time.
767+
// In this mode, caching (discussed below) is disabled. After the package test
768+
// finishes, go test prints a summary line showing the test status ('ok' or 'FAIL'),
769+
// package name, and elapsed time.
770770
//
771771
// In package list mode, go test compiles and tests each of the packages
772772
// listed on the command line. If a package test passes, go test prints only
773773
// the final 'ok' summary line. If a package test fails, go test prints the
774774
// full test output. If invoked with the -bench or -v flag, go test prints
775775
// the full output even for passing package tests, in order to display the
776-
// requested benchmark results or verbose logging. In package list mode,
777-
// go test prints all test output and summary lines to standard output.
776+
// requested benchmark results or verbose logging.
777+
//
778+
// All test output and summary lines are printed to the go command's standard
779+
// output, even if the test printed them to its own standard error.
780+
// (The go command's standard error is reserved for printing errors building
781+
// the tests.)
778782
//
779783
// In package list mode, go test also caches successful package test results.
780784
// If go test has cached a previous test run using the same test binary and
@@ -784,7 +788,9 @@
784788
// binary again. In the summary line, go test prints '(cached)' in place of
785789
// the elapsed time. To disable test caching, use any test flag or argument
786790
// other than the cacheable flags. The idiomatic way to disable test caching
787-
// explicitly is to use -count=1.
791+
// explicitly is to use -count=1. A cached result is treated as executing in
792+
// no time at all, so a successful package test result will be cached and reused
793+
// regardless of -timeout setting.
788794
//
789795
// In addition to the build flags, the flags handled by 'go test' itself are:
790796
//
@@ -1517,10 +1523,10 @@
15171523
// significantly more expensive.
15181524
// Sets -cover.
15191525
//
1520-
// -coverpkg pkg1,pkg2,pkg3
1521-
// Apply coverage analysis in each test to the given list of packages.
1526+
// -coverpkg pattern1,pattern2,pattern3
1527+
// Apply coverage analysis in each test to packages matching the patterns.
15221528
// The default is for each test to analyze only the package being tested.
1523-
// Packages are specified as import paths.
1529+
// See 'go help packages' for a description of package patterns.
15241530
// Sets -cover.
15251531
//
15261532
// -cpu 1,2,4

src/cmd/go/go_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2416,6 +2416,20 @@ func TestCoverageImportMainLoop(t *testing.T) {
24162416
tg.grepStderr("not an importable package", "did not detect import main")
24172417
}
24182418

2419+
func TestCoveragePattern(t *testing.T) {
2420+
tg := testgo(t)
2421+
defer tg.cleanup()
2422+
tg.parallel()
2423+
tg.makeTempdir()
2424+
tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
2425+
2426+
// If coverpkg=sleepy... expands by package loading
2427+
// (as opposed to pattern matching on deps)
2428+
// then it will try to load sleepybad, which does not compile,
2429+
// and the test command will fail.
2430+
tg.run("test", "-coverprofile="+filepath.Join(tg.tempdir, "cover.out"), "-coverpkg=sleepy...", "-run=^$", "sleepy1")
2431+
}
2432+
24192433
func TestPluginNonMain(t *testing.T) {
24202434
wd, err := os.Getwd()
24212435
if err != nil {

src/cmd/go/internal/load/flag.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (f *PerPackageFlag) set(v, cwd string) error {
5656
return fmt.Errorf("missing <pattern> in <pattern>=<value>")
5757
}
5858
pattern := v[:i]
59-
match = matchPackage(pattern, cwd)
59+
match = MatchPackage(pattern, cwd)
6060
v = v[i+1:]
6161
}
6262
flags, err := str.SplitQuotedFields(v)
@@ -104,7 +104,7 @@ func setCmdlinePatterns(args []string, cwd string) {
104104
}
105105
cmdlineMatchers = nil // allow reset for testing
106106
for _, arg := range args {
107-
cmdlineMatchers = append(cmdlineMatchers, matchPackage(arg, cwd))
107+
cmdlineMatchers = append(cmdlineMatchers, MatchPackage(arg, cwd))
108108
}
109109
}
110110

src/cmd/go/internal/load/search.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ func matchPattern(pattern string) func(name string) bool {
266266
}
267267
}
268268

269-
// matchPackage(pattern, cwd)(p) reports whether package p matches pattern in the working directory cwd.
270-
func matchPackage(pattern, cwd string) func(*Package) bool {
269+
// MatchPackage(pattern, cwd)(p) reports whether package p matches pattern in the working directory cwd.
270+
func MatchPackage(pattern, cwd string) func(*Package) bool {
271271
switch {
272272
case strings.HasPrefix(pattern, "./") || strings.HasPrefix(pattern, "../") || pattern == "." || pattern == "..":
273273
// Split pattern into leading pattern-free directory path

src/cmd/go/internal/test/test.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,10 @@ const testFlag2 = `
220220
significantly more expensive.
221221
Sets -cover.
222222
223-
-coverpkg pkg1,pkg2,pkg3
224-
Apply coverage analysis in each test to the given list of packages.
223+
-coverpkg pattern1,pattern2,pattern3
224+
Apply coverage analysis in each test to packages matching the patterns.
225225
The default is for each test to analyze only the package being tested.
226-
Packages are specified as import paths.
226+
See 'go help packages' for a description of package patterns.
227227
Sets -cover.
228228
229229
-cpu 1,2,4
@@ -604,21 +604,30 @@ func runTest(cmd *base.Command, args []string) {
604604
var builds, runs, prints []*work.Action
605605

606606
if testCoverPaths != nil {
607-
// Load packages that were asked about for coverage.
608-
// packagesForBuild exits if the packages cannot be loaded.
609-
testCoverPkgs = load.PackagesForBuild(testCoverPaths)
607+
match := make([]func(*load.Package) bool, len(testCoverPaths))
608+
matched := make([]bool, len(testCoverPaths))
609+
for i := range testCoverPaths {
610+
match[i] = load.MatchPackage(testCoverPaths[i], base.Cwd)
611+
}
610612

611-
// Warn about -coverpkg arguments that are not actually used.
612-
used := make(map[string]bool)
613-
for _, p := range pkgs {
614-
used[p.ImportPath] = true
615-
for _, dep := range p.Deps {
616-
used[dep] = true
613+
// Select for coverage all dependencies matching the testCoverPaths patterns.
614+
for _, p := range load.PackageList(pkgs) {
615+
haveMatch := false
616+
for i := range testCoverPaths {
617+
if match[i](p) {
618+
matched[i] = true
619+
haveMatch = true
620+
}
621+
}
622+
if haveMatch {
623+
testCoverPkgs = append(testCoverPkgs, p)
617624
}
618625
}
619-
for _, p := range testCoverPkgs {
620-
if !used[p.ImportPath] {
621-
fmt.Fprintf(os.Stderr, "warning: no packages being tested depend on %s\n", p.ImportPath)
626+
627+
// Warn about -coverpkg arguments that are not actually used.
628+
for i := range testCoverPaths {
629+
if !matched[i] {
630+
fmt.Fprintf(os.Stderr, "warning: no packages being tested depend on matches for pattern %s\n", testCoverPaths[i])
622631
}
623632
}
624633

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package p
2+
3+
// missing import
4+
5+
var _ = io.DoesNotExist

0 commit comments

Comments
 (0)