File size: 7,961 Bytes
61bba11 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | // Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main_test
import (
"cmd/internal/cov/covcmd"
"encoding/json"
"fmt"
"internal/testenv"
"os"
"path/filepath"
"strings"
"testing"
)
func writeFile(t *testing.T, path string, contents []byte) {
if err := os.WriteFile(path, contents, 0666); err != nil {
t.Fatalf("os.WriteFile(%s) failed: %v", path, err)
}
}
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 := covcmd.CoverPkgConfig{
PkgPath: ppath,
PkgName: pname,
Granularity: gran,
OutConfig: outcfg,
EmitMetaFile: mpath,
}
data, err := json.Marshal(p)
if err != nil {
t.Fatalf("json.Marshal failed: %v", err)
}
writeFile(t, incfg, data)
return incfg
}
func writeOutFileList(t *testing.T, infiles []string, outdir, tag string) ([]string, string) {
outfilelist := filepath.Join(outdir, tag+"outfilelist.txt")
var sb strings.Builder
cv := filepath.Join(outdir, "covervars.go")
outfs := []string{cv}
fmt.Fprintf(&sb, "%s\n", cv)
for _, inf := range infiles {
base := filepath.Base(inf)
of := filepath.Join(outdir, tag+".cov."+base)
outfs = append(outfs, of)
fmt.Fprintf(&sb, "%s\n", of)
}
if err := os.WriteFile(outfilelist, []byte(sb.String()), 0666); err != nil {
t.Fatalf("writing %s: %v", outfilelist, err)
}
return outfs, outfilelist
}
func runPkgCover(t *testing.T, outdir string, tag string, incfg string, mode string, infiles []string, errExpected bool) ([]string, string, string) {
// Write the pkgcfg file.
outcfg := filepath.Join(outdir, "outcfg.txt")
// Form up the arguments and run the tool.
outfiles, outfilelist := writeOutFileList(t, infiles, outdir, tag)
args := []string{"-pkgcfg", incfg, "-mode=" + mode, "-var=var" + tag, "-outfilelist", outfilelist}
args = append(args, infiles...)
cmd := testenv.Command(t, testcover(t), args...)
if errExpected {
errmsg := runExpectingError(cmd, t)
return nil, "", errmsg
} else {
run(cmd, t)
return outfiles, outcfg, ""
}
}
func TestCoverWithCfg(t *testing.T) {
testenv.MustHaveGoRun(t)
t.Parallel()
// Subdir in testdata that has our input files of interest.
tpath := filepath.Join("testdata", "pkgcfg")
dir := tempDir(t)
instdira := filepath.Join(dir, "insta")
if err := os.Mkdir(instdira, 0777); err != nil {
t.Fatal(err)
}
scenarios := []struct {
mode, gran string
}{
{
mode: "count",
gran: "perblock",
},
{
mode: "set",
gran: "perfunc",
},
{
mode: "regonly",
gran: "perblock",
},
}
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.
ppath := "cfg/a"
pname := "a"
mode := scenario.mode
gran := scenario.gran
tag := mode + "_" + gran
incfg = writePkgConfig(t, instdira, tag, ppath, pname, gran, "")
ofs, outcfg, _ := runPkgCover(t, instdira, tag, incfg, mode,
apkgfiles, false)
t.Logf("outfiles: %+v\n", ofs)
// Run the compiler on the files to make sure the result is
// buildable.
bargs := []string{"tool", "compile", "-p", "a", "-coveragecfg", outcfg}
bargs = append(bargs, ofs...)
cmd := testenv.Command(t, testenv.GoToolPath(t), bargs...)
cmd.Dir = instdira
run(cmd, t)
}
// Do some error testing to ensure that various bad options and
// combinations are properly rejected.
// Expect error if config file inaccessible/unreadable.
mode := "atomic"
errExpected := true
tag := "errors"
_, _, errmsg := runPkgCover(t, instdira, tag, "/not/a/file", mode,
apkgfiles, errExpected)
want := "error reading pkgconfig file"
if !strings.Contains(errmsg, want) {
t.Errorf("'bad config file' test: wanted %s got %s", want, errmsg)
}
// Expect err if config file contains unknown stuff.
t.Logf("mangling in config")
writeFile(t, incfg, []byte("blah=foo\n"))
_, _, errmsg = runPkgCover(t, instdira, tag, incfg, mode,
apkgfiles, errExpected)
want = "error reading pkgconfig file"
if !strings.Contains(errmsg, want) {
t.Errorf("'bad config file' test: wanted %s got %s", want, errmsg)
}
// Expect error on empty config file.
t.Logf("writing empty config")
writeFile(t, incfg, []byte("\n"))
_, _, errmsg = runPkgCover(t, instdira, tag, incfg, mode,
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 instrumentation, 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())
}
}
}
|