Skip to content

Commit ebb8a1f

Browse files
kyroyianlancetaylor
authored andcommitted
cmd/go: output coverage report even if there are no test files
When using test -cover or -coverprofile the output for "no test files" is the same format as for "no tests to run". Fixes #24570 Change-Id: If05609411676d42d94c1feac4bc839974fae2cc1 Reviewed-on: https://go-review.googlesource.com/115095 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent d159d61 commit ebb8a1f

File tree

9 files changed

+74
-27
lines changed

9 files changed

+74
-27
lines changed

src/cmd/go/go_test.go

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

3206+
// issue 24570
3207+
func TestGoTestCoverMultiPackage(t *testing.T) {
3208+
tg := testgo(t)
3209+
defer tg.cleanup()
3210+
tg.run("test", "-cover", "./testdata/testcover/...")
3211+
tg.grepStdout(`\?.*testdata/testcover/pkg1.*\d\.\d\d\ds.*coverage:.*0\.0% of statements \[no test files\]`, "expected [no test files] for pkg1")
3212+
tg.grepStdout(`ok.*testdata/testcover/pkg2.*\d\.\d\d\ds.*coverage:.*0\.0% of statements \[no tests to run\]`, "expected [no tests to run] for pkg2")
3213+
tg.grepStdout(`ok.*testdata/testcover/pkg3.*\d\.\d\d\ds.*coverage:.*100\.0% of statements`, "expected 100% coverage for pkg3")
3214+
}
3215+
3216+
// issue 24570
3217+
func TestGoTestCoverprofileMultiPackage(t *testing.T) {
3218+
tg := testgo(t)
3219+
defer tg.cleanup()
3220+
tg.creatingTemp("testdata/cover.out")
3221+
tg.run("test", "-coverprofile=testdata/cover.out", "./testdata/testcover/...")
3222+
tg.grepStdout(`\?.*testdata/testcover/pkg1.*\d\.\d\d\ds.*coverage:.*0\.0% of statements \[no test files\]`, "expected [no test files] for pkg1")
3223+
tg.grepStdout(`ok.*testdata/testcover/pkg2.*\d\.\d\d\ds.*coverage:.*0\.0% of statements \[no tests to run\]`, "expected [no tests to run] for pkg2")
3224+
tg.grepStdout(`ok.*testdata/testcover/pkg3.*\d\.\d\d\ds.*coverage:.*100\.0% of statements`, "expected 100% coverage for pkg3")
3225+
if out, err := ioutil.ReadFile("testdata/cover.out"); err != nil {
3226+
t.Error(err)
3227+
} else {
3228+
if !bytes.Contains(out, []byte("mode: set")) {
3229+
t.Errorf(`missing "mode: set" in %s`, out)
3230+
}
3231+
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`)) {
3232+
t.Errorf(`missing "pkg1/a.go:5.10,7.2 1 0" in %s`, out)
3233+
}
3234+
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`)) {
3235+
t.Errorf(`missing "pkg2/a.go:5.10,7.2 1 0" in %s`, out)
3236+
}
3237+
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`)) {
3238+
t.Errorf(`missing "pkg3/a.go:5.10,7.2 1 1" in %s`, out)
3239+
}
3240+
}
3241+
}
3242+
32063243
func TestGoGenerateHandlesSimpleCommand(t *testing.T) {
32073244
if runtime.GOOS == "windows" {
32083245
t.Skip("skipping because windows has no echo command")

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ 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.
5552
func TestPackagesFor(p *Package, cover *TestCover) (pmain, ptest, pxtest *Package, err error) {
5653
var imports, ximports []*Package
5754
var stk ImportStack

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

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -781,14 +781,6 @@ 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-
792784
// Build Package structs describing:
793785
// pmain - pkg.test binary
794786
// ptest - package + test files
@@ -1176,13 +1168,17 @@ func (c *runCache) builderRunTest(b *work.Builder, a *work.Action) error {
11761168

11771169
if err == nil {
11781170
norun := ""
1171+
res := "ok"
11791172
if !testShowPass && !testJSON {
11801173
buf.Reset()
11811174
}
1182-
if bytes.HasPrefix(out, noTestsToRun[1:]) || bytes.Contains(out, noTestsToRun) {
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) {
11831179
norun = " [no tests to run]"
11841180
}
1185-
fmt.Fprintf(cmd.Stdout, "ok \t%s\t%s%s%s\n", a.Package.ImportPath, t, coveragePercentage(out), norun)
1181+
fmt.Fprintf(cmd.Stdout, "%s \t%s\t%s%s%s\n", res, a.Package.ImportPath, t, coveragePercentage(out), norun)
11861182
c.saveOutput(a)
11871183
} else {
11881184
base.SetExitStatus(1)
@@ -1596,15 +1592,3 @@ func builderPrintTest(b *work.Builder, a *work.Action) error {
15961592
}
15971593
return nil
15981594
}
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-
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package pkg1
2+
3+
import "fmt"
4+
5+
func F() {
6+
fmt.Println("pkg1")
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package pkg2
2+
3+
import "fmt"
4+
5+
func F() {
6+
fmt.Println("pkg2")
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package pkg2
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package pkg3
2+
3+
import "fmt"
4+
5+
func F() {
6+
fmt.Println("pkg3")
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package pkg3
2+
3+
import "testing"
4+
5+
func TestF(t *testing.T) {
6+
F()
7+
}

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("\t[no test files]\n")
150+
skipLineSuffix = []byte(" [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\t[no test files]\n" is only line.
169+
// Special case for entirely skipped test binary: "? \tpkgname\t0.001s [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)