]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: output coverage report even if there are no test files
authorDennis Kuhnert <mail.kuhnert@gmail.com>
Tue, 29 May 2018 15:39:00 +0000 (17:39 +0200)
committerIan Lance Taylor <iant@golang.org>
Wed, 6 Jun 2018 01:10:20 +0000 (01:10 +0000)
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 <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/go/go_test.go
src/cmd/go/internal/load/test.go
src/cmd/go/internal/test/test.go
src/cmd/go/testdata/testcover/pkg1/a.go [new file with mode: 0644]
src/cmd/go/testdata/testcover/pkg2/a.go [new file with mode: 0644]
src/cmd/go/testdata/testcover/pkg2/a_test.go [new file with mode: 0644]
src/cmd/go/testdata/testcover/pkg3/a.go [new file with mode: 0644]
src/cmd/go/testdata/testcover/pkg3/a_test.go [new file with mode: 0644]
src/cmd/internal/test2json/test2json.go

index 9b9df3cbc3ea0f305c68f64fa17463453902240d..ba5bba6123deb1c1a9d4489e6786ef74662d945b 100644 (file)
@@ -3203,6 +3203,43 @@ func TestGoTestBuildsAnXtestContainingOnlyNonRunnableExamples(t *testing.T) {
        tg.grepStdout("File with non-runnable example was built.", "file with non-runnable example was not built")
 }
 
+// issue 24570
+func TestGoTestCoverMultiPackage(t *testing.T) {
+       tg := testgo(t)
+       defer tg.cleanup()
+       tg.run("test", "-cover", "./testdata/testcover/...")
+       tg.grepStdout(`\?.*testdata/testcover/pkg1.*\d\.\d\d\ds.*coverage:.*0\.0% of statements \[no test files\]`, "expected [no test files] for pkg1")
+       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")
+       tg.grepStdout(`ok.*testdata/testcover/pkg3.*\d\.\d\d\ds.*coverage:.*100\.0% of statements`, "expected 100% coverage for pkg3")
+}
+
+// issue 24570
+func TestGoTestCoverprofileMultiPackage(t *testing.T) {
+       tg := testgo(t)
+       defer tg.cleanup()
+       tg.creatingTemp("testdata/cover.out")
+       tg.run("test", "-coverprofile=testdata/cover.out", "./testdata/testcover/...")
+       tg.grepStdout(`\?.*testdata/testcover/pkg1.*\d\.\d\d\ds.*coverage:.*0\.0% of statements \[no test files\]`, "expected [no test files] for pkg1")
+       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")
+       tg.grepStdout(`ok.*testdata/testcover/pkg3.*\d\.\d\d\ds.*coverage:.*100\.0% of statements`, "expected 100% coverage for pkg3")
+       if out, err := ioutil.ReadFile("testdata/cover.out"); err != nil {
+               t.Error(err)
+       } else {
+               if !bytes.Contains(out, []byte("mode: set")) {
+                       t.Errorf(`missing "mode: set" in %s`, out)
+               }
+               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`)) {
+                       t.Errorf(`missing "pkg1/a.go:5.10,7.2 1 0" in %s`, out)
+               }
+               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`)) {
+                       t.Errorf(`missing "pkg2/a.go:5.10,7.2 1 0" in %s`, out)
+               }
+               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`)) {
+                       t.Errorf(`missing "pkg3/a.go:5.10,7.2 1 1" in %s`, out)
+               }
+       }
+}
+
 func TestGoGenerateHandlesSimpleCommand(t *testing.T) {
        if runtime.GOOS == "windows" {
                t.Skip("skipping because windows has no echo command")
index 1444ddb58a40f2bc2418491b24dcdceed4dfd32d..0a13dfc1b7a9ca2110b8007823e9273edcb630c6 100644 (file)
@@ -49,9 +49,6 @@ type TestCover struct {
 // (for example, if there are no "package p" test files and
 // package p need not be instrumented for coverage or any other reason),
 // then the returned ptest == p.
-//
-// The caller is expected to have checked that len(p.TestGoFiles)+len(p.XTestGoFiles) > 0,
-// or else there's no point in any of this.
 func TestPackagesFor(p *Package, cover *TestCover) (pmain, ptest, pxtest *Package, err error) {
        var imports, ximports []*Package
        var stk ImportStack
index 585481b6b717dbdf22cd7d93ef740a8b4d23f325..aff5ff2c9d3edfc2fadcb787e3f42c1bff54fd78 100644 (file)
@@ -781,14 +781,6 @@ var windowsBadWords = []string{
 }
 
 func builderTest(b *work.Builder, 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}}
-               addTestVet(b, p, run, nil)
-               print := &work.Action{Mode: "test print", Func: builderNoTest, Package: p, Deps: []*work.Action{run}}
-               return build, run, print, nil
-       }
-
        // Build Package structs describing:
        //      pmain - pkg.test binary
        //      ptest - package + test files
@@ -1176,13 +1168,17 @@ func (c *runCache) builderRunTest(b *work.Builder, a *work.Action) error {
 
        if err == nil {
                norun := ""
+               res := "ok"
                if !testShowPass && !testJSON {
                        buf.Reset()
                }
-               if bytes.HasPrefix(out, noTestsToRun[1:]) || bytes.Contains(out, noTestsToRun) {
+               if len(a.Package.TestGoFiles)+len(a.Package.XTestGoFiles) == 0 {
+                       res = "? "
+                       norun = " [no test files]"
+               } else if bytes.HasPrefix(out, noTestsToRun[1:]) || bytes.Contains(out, noTestsToRun) {
                        norun = " [no tests to run]"
                }
-               fmt.Fprintf(cmd.Stdout, "ok  \t%s\t%s%s%s\n", a.Package.ImportPath, t, coveragePercentage(out), norun)
+               fmt.Fprintf(cmd.Stdout, "%s  \t%s\t%s%s%s\n", res, a.Package.ImportPath, t, coveragePercentage(out), norun)
                c.saveOutput(a)
        } else {
                base.SetExitStatus(1)
@@ -1596,15 +1592,3 @@ func builderPrintTest(b *work.Builder, a *work.Action) error {
        }
        return nil
 }
-
-// builderNoTest is the action for testing a package with no test files.
-func builderNoTest(b *work.Builder, a *work.Action) error {
-       var stdout io.Writer = os.Stdout
-       if testJSON {
-               json := test2json.NewConverter(lockedStdout{}, a.Package.ImportPath, test2json.Timestamp)
-               defer json.Close()
-               stdout = json
-       }
-       fmt.Fprintf(stdout, "?   \t%s\t[no test files]\n", a.Package.ImportPath)
-       return nil
-}
diff --git a/src/cmd/go/testdata/testcover/pkg1/a.go b/src/cmd/go/testdata/testcover/pkg1/a.go
new file mode 100644 (file)
index 0000000..e291611
--- /dev/null
@@ -0,0 +1,7 @@
+package pkg1
+
+import "fmt"
+
+func F() {
+       fmt.Println("pkg1")
+}
diff --git a/src/cmd/go/testdata/testcover/pkg2/a.go b/src/cmd/go/testdata/testcover/pkg2/a.go
new file mode 100644 (file)
index 0000000..7bd9bd4
--- /dev/null
@@ -0,0 +1,7 @@
+package pkg2
+
+import "fmt"
+
+func F() {
+       fmt.Println("pkg2")
+}
diff --git a/src/cmd/go/testdata/testcover/pkg2/a_test.go b/src/cmd/go/testdata/testcover/pkg2/a_test.go
new file mode 100644 (file)
index 0000000..4f791ad
--- /dev/null
@@ -0,0 +1 @@
+package pkg2
diff --git a/src/cmd/go/testdata/testcover/pkg3/a.go b/src/cmd/go/testdata/testcover/pkg3/a.go
new file mode 100644 (file)
index 0000000..bf86ed8
--- /dev/null
@@ -0,0 +1,7 @@
+package pkg3
+
+import "fmt"
+
+func F() {
+       fmt.Println("pkg3")
+}
diff --git a/src/cmd/go/testdata/testcover/pkg3/a_test.go b/src/cmd/go/testdata/testcover/pkg3/a_test.go
new file mode 100644 (file)
index 0000000..39c2c5a
--- /dev/null
@@ -0,0 +1,7 @@
+package pkg3
+
+import "testing"
+
+func TestF(t *testing.T) {
+       F()
+}
index f8052136be625025bd47f8e2d771e837c44794e6..1a54a1c3bb4b57549d11d7941d551e776f1449ae 100644 (file)
@@ -147,7 +147,7 @@ var (
        fourSpace = []byte("    ")
 
        skipLinePrefix = []byte("?   \t")
-       skipLineSuffix = []byte("\t[no test files]\n")
+       skipLineSuffix = []byte(" [no test files]\n")
 )
 
 // handleInputLine handles a single whole test output line.
@@ -166,7 +166,7 @@ func (c *converter) handleInputLine(line []byte) {
                return
        }
 
-       // Special case for entirely skipped test binary: "?   \tpkgname\t[no test files]\n" is only line.
+       // Special case for entirely skipped test binary: "?   \tpkgname\t0.001s [no test files]\n" is only line.
        // Report it as plain output but remember to say skip in the final summary.
        if bytes.HasPrefix(line, skipLinePrefix) && bytes.HasSuffix(line, skipLineSuffix) && len(c.report) == 0 {
                c.result = "skip"