Skip to content

Commit 7254cfc

Browse files
ianlancetaylorrsc
authored andcommitted
cmd/go: revert "output coverage report even if there are no test files"
Original CL description: When using test -cover or -coverprofile the output for "no test files" is the same format as for "no tests to run". Reverting because this CL changed cmd/go to build test binaries for packages that have no tests, leading to extra work and confusion. Updates #24570 Fixes #25789 Fixes #26157 Fixes #26242 Change-Id: Ibab1307d39dfaec0de9359d6d96706e3910c8efd Reviewed-on: https://go-review.googlesource.com/122518 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Russ Cox <[email protected]>
1 parent b56e247 commit 7254cfc

File tree

9 files changed

+27
-74
lines changed

9 files changed

+27
-74
lines changed

src/cmd/go/go_test.go

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3262,43 +3262,6 @@ func TestGoTestBuildsAnXtestContainingOnlyNonRunnableExamples(t *testing.T) {
32623262
tg.grepStdout("File with non-runnable example was built.", "file with non-runnable example was not built")
32633263
}
32643264

3265-
// issue 24570
3266-
func TestGoTestCoverMultiPackage(t *testing.T) {
3267-
tg := testgo(t)
3268-
defer tg.cleanup()
3269-
tg.run("test", "-cover", "./testdata/testcover/...")
3270-
tg.grepStdout(`\?.*testdata/testcover/pkg1.*(\d\.\d\d\ds|cached).*coverage:.*0\.0% of statements \[no test files\]`, "expected [no test files] for pkg1")
3271-
tg.grepStdout(`ok.*testdata/testcover/pkg2.*(\d\.\d\d\ds|cached).*coverage:.*0\.0% of statements \[no tests to run\]`, "expected [no tests to run] for pkg2")
3272-
tg.grepStdout(`ok.*testdata/testcover/pkg3.*(\d\.\d\d\ds|cached).*coverage:.*100\.0% of statements`, "expected 100% coverage for pkg3")
3273-
}
3274-
3275-
// issue 24570
3276-
func TestGoTestCoverprofileMultiPackage(t *testing.T) {
3277-
tg := testgo(t)
3278-
defer tg.cleanup()
3279-
tg.creatingTemp("testdata/cover.out")
3280-
tg.run("test", "-coverprofile=testdata/cover.out", "./testdata/testcover/...")
3281-
tg.grepStdout(`\?.*testdata/testcover/pkg1.*(\d\.\d\d\ds|cached).*coverage:.*0\.0% of statements \[no test files\]`, "expected [no test files] for pkg1")
3282-
tg.grepStdout(`ok.*testdata/testcover/pkg2.*(\d\.\d\d\ds|cached).*coverage:.*0\.0% of statements \[no tests to run\]`, "expected [no tests to run] for pkg2")
3283-
tg.grepStdout(`ok.*testdata/testcover/pkg3.*(\d\.\d\d\ds|cached).*coverage:.*100\.0% of statements`, "expected 100% coverage for pkg3")
3284-
if out, err := ioutil.ReadFile("testdata/cover.out"); err != nil {
3285-
t.Error(err)
3286-
} else {
3287-
if !bytes.Contains(out, []byte("mode: set")) {
3288-
t.Errorf(`missing "mode: set" in %s`, out)
3289-
}
3290-
if !bytes.Contains(out, []byte(`pkg1/a.go:5.10,7.2 1 0`)) && !bytes.Contains(out, []byte(`pkg1\a.go:5.10,7.2 1 0`)) {
3291-
t.Errorf(`missing "pkg1/a.go:5.10,7.2 1 0" in %s`, out)
3292-
}
3293-
if !bytes.Contains(out, []byte(`pkg2/a.go:5.10,7.2 1 0`)) && !bytes.Contains(out, []byte(`pkg2\a.go:5.10,7.2 1 0`)) {
3294-
t.Errorf(`missing "pkg2/a.go:5.10,7.2 1 0" in %s`, out)
3295-
}
3296-
if !bytes.Contains(out, []byte(`pkg3/a.go:5.10,7.2 1 1`)) && !bytes.Contains(out, []byte(`pkg3\a.go:5.10,7.2 1 1`)) {
3297-
t.Errorf(`missing "pkg3/a.go:5.10,7.2 1 1" in %s`, out)
3298-
}
3299-
}
3300-
}
3301-
33023265
func TestGoGenerateHandlesSimpleCommand(t *testing.T) {
33033266
if runtime.GOOS == "windows" {
33043267
t.Skip("skipping because windows has no echo command")

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ type TestCover struct {
4949
// (for example, if there are no "package p" test files and
5050
// package p need not be instrumented for coverage or any other reason),
5151
// then the returned ptest == p.
52+
//
53+
// The caller is expected to have checked that len(p.TestGoFiles)+len(p.XTestGoFiles) > 0,
54+
// or else there's no point in any of this.
5255
func TestPackagesFor(p *Package, cover *TestCover) (pmain, ptest, pxtest *Package, err error) {
5356
var imports, ximports []*Package
5457
var stk ImportStack

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,14 @@ var windowsBadWords = []string{
781781
}
782782

783783
func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, printAction *work.Action, err error) {
784+
if len(p.TestGoFiles)+len(p.XTestGoFiles) == 0 {
785+
build := b.CompileAction(work.ModeBuild, work.ModeBuild, p)
786+
run := &work.Action{Mode: "test run", Package: p, Deps: []*work.Action{build}}
787+
addTestVet(b, p, run, nil)
788+
print := &work.Action{Mode: "test print", Func: builderNoTest, Package: p, Deps: []*work.Action{run}}
789+
return build, run, print, nil
790+
}
791+
784792
// Build Package structs describing:
785793
// pmain - pkg.test binary
786794
// ptest - package + test files
@@ -1168,17 +1176,13 @@ func (c *runCache) builderRunTest(b *work.Builder, a *work.Action) error {
11681176

11691177
if err == nil {
11701178
norun := ""
1171-
res := "ok"
11721179
if !testShowPass && !testJSON {
11731180
buf.Reset()
11741181
}
1175-
if len(a.Package.TestGoFiles)+len(a.Package.XTestGoFiles) == 0 {
1176-
res = "? "
1177-
norun = " [no test files]"
1178-
} else if bytes.HasPrefix(out, noTestsToRun[1:]) || bytes.Contains(out, noTestsToRun) {
1182+
if bytes.HasPrefix(out, noTestsToRun[1:]) || bytes.Contains(out, noTestsToRun) {
11791183
norun = " [no tests to run]"
11801184
}
1181-
fmt.Fprintf(cmd.Stdout, "%s \t%s\t%s%s%s\n", res, a.Package.ImportPath, t, coveragePercentage(out), norun)
1185+
fmt.Fprintf(cmd.Stdout, "ok \t%s\t%s%s%s\n", a.Package.ImportPath, t, coveragePercentage(out), norun)
11821186
c.saveOutput(a)
11831187
} else {
11841188
base.SetExitStatus(1)
@@ -1592,3 +1596,15 @@ func builderPrintTest(b *work.Builder, a *work.Action) error {
15921596
}
15931597
return nil
15941598
}
1599+
1600+
// builderNoTest is the action for testing a package with no test files.
1601+
func builderNoTest(b *work.Builder, a *work.Action) error {
1602+
var stdout io.Writer = os.Stdout
1603+
if testJSON {
1604+
json := test2json.NewConverter(lockedStdout{}, a.Package.ImportPath, test2json.Timestamp)
1605+
defer json.Close()
1606+
stdout = json
1607+
}
1608+
fmt.Fprintf(stdout, "? \t%s\t[no test files]\n", a.Package.ImportPath)
1609+
return nil
1610+
}

src/cmd/go/testdata/testcover/pkg1/a.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/cmd/go/testdata/testcover/pkg2/a.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/cmd/go/testdata/testcover/pkg2/a_test.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/cmd/go/testdata/testcover/pkg3/a.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/cmd/go/testdata/testcover/pkg3/a_test.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/cmd/internal/test2json/test2json.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ var (
147147
fourSpace = []byte(" ")
148148

149149
skipLinePrefix = []byte("? \t")
150-
skipLineSuffix = []byte(" [no test files]\n")
150+
skipLineSuffix = []byte("\t[no test files]\n")
151151
)
152152

153153
// handleInputLine handles a single whole test output line.
@@ -166,7 +166,7 @@ func (c *converter) handleInputLine(line []byte) {
166166
return
167167
}
168168

169-
// Special case for entirely skipped test binary: "? \tpkgname\t0.001s [no test files]\n" is only line.
169+
// Special case for entirely skipped test binary: "? \tpkgname\t[no test files]\n" is only line.
170170
// Report it as plain output but remember to say skip in the final summary.
171171
if bytes.HasPrefix(line, skipLinePrefix) && bytes.HasSuffix(line, skipLineSuffix) && len(c.report) == 0 {
172172
c.result = "skip"

0 commit comments

Comments
 (0)