aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/cover/cfg_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/cover/cfg_test.go')
-rw-r--r--src/cmd/cover/cfg_test.go159
1 files changed, 121 insertions, 38 deletions
diff --git a/src/cmd/cover/cfg_test.go b/src/cmd/cover/cfg_test.go
index 6782ec89a4..701de615d0 100644
--- a/src/cmd/cover/cfg_test.go
+++ b/src/cmd/cover/cfg_test.go
@@ -5,9 +5,9 @@
package main_test
import (
+ "cmd/internal/cov/covcmd"
"encoding/json"
"fmt"
- "internal/coverage"
"internal/testenv"
"os"
"path/filepath"
@@ -21,14 +21,15 @@ func writeFile(t *testing.T, path string, contents []byte) {
}
}
-func writePkgConfig(t *testing.T, outdir, tag, ppath, pname string, gran string) string {
+func writePkgConfig(t *testing.T, outdir, tag, ppath, pname string, gran string, mpath string) string {
incfg := filepath.Join(outdir, tag+"incfg.txt")
outcfg := filepath.Join(outdir, "outcfg.txt")
- p := coverage.CoverPkgConfig{
- PkgPath: ppath,
- PkgName: pname,
- Granularity: gran,
- OutConfig: outcfg,
+ p := covcmd.CoverPkgConfig{
+ PkgPath: ppath,
+ PkgName: pname,
+ Granularity: gran,
+ OutConfig: outcfg,
+ EmitMetaFile: mpath,
}
data, err := json.Marshal(p)
if err != nil {
@@ -74,10 +75,6 @@ func runPkgCover(t *testing.T, outdir string, tag string, incfg string, mode str
}
}
-// Set to true when debugging unit test (to inspect debris, etc).
-// Note that this functionality does not work on windows.
-const debugWorkDir = false
-
func TestCoverWithCfg(t *testing.T) {
testenv.MustHaveGoRun(t)
@@ -85,29 +82,7 @@ func TestCoverWithCfg(t *testing.T) {
// Subdir in testdata that has our input files of interest.
tpath := filepath.Join("testdata", "pkgcfg")
-
- // Helper to collect input paths (go files) for a subdir in 'pkgcfg'
- pfiles := func(subdir string) []string {
- de, err := os.ReadDir(filepath.Join(tpath, subdir))
- if err != nil {
- t.Fatalf("reading subdir %s: %v", subdir, err)
- }
- paths := []string{}
- for _, e := range de {
- if !strings.HasSuffix(e.Name(), ".go") || strings.HasSuffix(e.Name(), "_test.go") {
- continue
- }
- paths = append(paths, filepath.Join(tpath, subdir, e.Name()))
- }
- return paths
- }
-
dir := tempDir(t)
- if debugWorkDir {
- dir = "/tmp/qqq"
- os.RemoveAll(dir)
- os.Mkdir(dir, 0777)
- }
instdira := filepath.Join(dir, "insta")
if err := os.Mkdir(instdira, 0777); err != nil {
t.Fatal(err)
@@ -131,6 +106,7 @@ func TestCoverWithCfg(t *testing.T) {
}
var incfg string
+ apkgfiles := []string{filepath.Join(tpath, "a", "a.go")}
for _, scenario := range scenarios {
// Instrument package "a", producing a set of instrumented output
// files and an 'output config' file to pass on to the compiler.
@@ -139,9 +115,9 @@ func TestCoverWithCfg(t *testing.T) {
mode := scenario.mode
gran := scenario.gran
tag := mode + "_" + gran
- incfg = writePkgConfig(t, instdira, tag, ppath, pname, gran)
+ incfg = writePkgConfig(t, instdira, tag, ppath, pname, gran, "")
ofs, outcfg, _ := runPkgCover(t, instdira, tag, incfg, mode,
- pfiles("a"), false)
+ apkgfiles, false)
t.Logf("outfiles: %+v\n", ofs)
// Run the compiler on the files to make sure the result is
@@ -161,7 +137,7 @@ func TestCoverWithCfg(t *testing.T) {
errExpected := true
tag := "errors"
_, _, errmsg := runPkgCover(t, instdira, tag, "/not/a/file", mode,
- pfiles("a"), errExpected)
+ apkgfiles, errExpected)
want := "error reading pkgconfig file"
if !strings.Contains(errmsg, want) {
t.Errorf("'bad config file' test: wanted %s got %s", want, errmsg)
@@ -171,7 +147,7 @@ func TestCoverWithCfg(t *testing.T) {
t.Logf("mangling in config")
writeFile(t, incfg, []byte("blah=foo\n"))
_, _, errmsg = runPkgCover(t, instdira, tag, incfg, mode,
- pfiles("a"), errExpected)
+ apkgfiles, errExpected)
want = "error reading pkgconfig file"
if !strings.Contains(errmsg, want) {
t.Errorf("'bad config file' test: wanted %s got %s", want, errmsg)
@@ -181,8 +157,115 @@ func TestCoverWithCfg(t *testing.T) {
t.Logf("writing empty config")
writeFile(t, incfg, []byte("\n"))
_, _, errmsg = runPkgCover(t, instdira, tag, incfg, mode,
- pfiles("a"), errExpected)
+ apkgfiles, errExpected)
if !strings.Contains(errmsg, want) {
t.Errorf("'bad config file' test: wanted %s got %s", want, errmsg)
}
}
+
+func TestCoverOnPackageWithNoTestFiles(t *testing.T) {
+ testenv.MustHaveGoRun(t)
+
+ // For packages with no test files, the new "go test -cover"
+ // strategy is to run cmd/cover on the package in a special
+ // "EmitMetaFile" mode. When running in this mode, cmd/cover walks
+ // the package doing instrumention, but when finished, instead of
+ // writing out instrumented source files, it directly emits a
+ // meta-data file for the package in question, essentially
+ // simulating the effect that you would get if you added a dummy
+ // "no-op" x_test.go file and then did a build and run of the test.
+
+ t.Run("YesFuncsNoTests", func(t *testing.T) {
+ testCoverNoTestsYesFuncs(t)
+ })
+ t.Run("NoFuncsNoTests", func(t *testing.T) {
+ testCoverNoTestsNoFuncs(t)
+ })
+}
+
+func testCoverNoTestsYesFuncs(t *testing.T) {
+ t.Parallel()
+ dir := tempDir(t)
+
+ // Run the cover command with "emit meta" enabled on a package
+ // with functions but no test files.
+ tpath := filepath.Join("testdata", "pkgcfg")
+ pkg1files := []string{filepath.Join(tpath, "yesFuncsNoTests", "yfnt.go")}
+ ppath := "cfg/yesFuncsNoTests"
+ pname := "yesFuncsNoTests"
+ mode := "count"
+ gran := "perblock"
+ tag := mode + "_" + gran
+ instdir := filepath.Join(dir, "inst")
+ if err := os.Mkdir(instdir, 0777); err != nil {
+ t.Fatal(err)
+ }
+ mdir := filepath.Join(dir, "meta")
+ if err := os.Mkdir(mdir, 0777); err != nil {
+ t.Fatal(err)
+ }
+ mpath := filepath.Join(mdir, "covmeta.xxx")
+ incfg := writePkgConfig(t, instdir, tag, ppath, pname, gran, mpath)
+ _, _, errmsg := runPkgCover(t, instdir, tag, incfg, mode,
+ pkg1files, false)
+ if errmsg != "" {
+ t.Fatalf("runPkgCover err: %q", errmsg)
+ }
+
+ // Check for existence of meta-data file.
+ if inf, err := os.Open(mpath); err != nil {
+ t.Fatalf("meta-data file not created: %v", err)
+ } else {
+ inf.Close()
+ }
+
+ // Make sure it is digestible.
+ cdargs := []string{"tool", "covdata", "percent", "-i", mdir}
+ cmd := testenv.Command(t, testenv.GoToolPath(t), cdargs...)
+ run(cmd, t)
+}
+
+func testCoverNoTestsNoFuncs(t *testing.T) {
+ t.Parallel()
+ dir := tempDir(t)
+
+ // Run the cover command with "emit meta" enabled on a package
+ // with no functions and no test files.
+ tpath := filepath.Join("testdata", "pkgcfg")
+ pkgfiles := []string{filepath.Join(tpath, "noFuncsNoTests", "nfnt.go")}
+ pname := "noFuncsNoTests"
+ mode := "count"
+ gran := "perblock"
+ ppath := "cfg/" + pname
+ tag := mode + "_" + gran
+ instdir := filepath.Join(dir, "inst2")
+ if err := os.Mkdir(instdir, 0777); err != nil {
+ t.Fatal(err)
+ }
+ mdir := filepath.Join(dir, "meta2")
+ if err := os.Mkdir(mdir, 0777); err != nil {
+ t.Fatal(err)
+ }
+ mpath := filepath.Join(mdir, "covmeta.yyy")
+ incfg := writePkgConfig(t, instdir, tag, ppath, pname, gran, mpath)
+ _, _, errmsg := runPkgCover(t, instdir, tag, incfg, mode,
+ pkgfiles, false)
+ if errmsg != "" {
+ t.Fatalf("runPkgCover err: %q", errmsg)
+ }
+
+ // We expect to see an empty meta-data file in this case.
+ if inf, err := os.Open(mpath); err != nil {
+ t.Fatalf("opening meta-data file: error %v", err)
+ } else {
+ defer inf.Close()
+ fi, err := inf.Stat()
+ if err != nil {
+ t.Fatalf("stat meta-data file: %v", err)
+ }
+ if fi.Size() != 0 {
+ t.Fatalf("want zero-sized meta-data file got size %d",
+ fi.Size())
+ }
+ }
+}