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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
// 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 coverage
import (
"fmt"
"internal/coverage"
"internal/goexperiment"
"internal/platform"
"internal/testenv"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
)
// Set to true for debugging (linux only).
const fixedTestDir = false
func TestCoverageApis(t *testing.T) {
if testing.Short() {
t.Skipf("skipping test: too long for short mode")
}
if !goexperiment.CoverageRedesign {
t.Skipf("skipping new coverage tests (experiment not enabled)")
}
testenv.MustHaveGoBuild(t)
dir := t.TempDir()
if fixedTestDir {
dir = "/tmp/qqqzzz"
os.RemoveAll(dir)
mkdir(t, dir)
}
// Build harness.
bdir := mkdir(t, filepath.Join(dir, "build"))
hargs := []string{"-cover", "-coverpkg=all"}
if testing.CoverMode() != "" {
hargs = append(hargs, "-covermode="+testing.CoverMode())
}
harnessPath := buildHarness(t, bdir, hargs)
t.Logf("harness path is %s", harnessPath)
// Sub-tests for each API we want to inspect, plus
// extras for error testing.
t.Run("emitToDir", func(t *testing.T) {
t.Parallel()
testEmitToDir(t, harnessPath, dir)
})
t.Run("emitToWriter", func(t *testing.T) {
t.Parallel()
testEmitToWriter(t, harnessPath, dir)
})
t.Run("emitToNonexistentDir", func(t *testing.T) {
t.Parallel()
testEmitToNonexistentDir(t, harnessPath, dir)
})
t.Run("emitToNilWriter", func(t *testing.T) {
t.Parallel()
testEmitToNilWriter(t, harnessPath, dir)
})
t.Run("emitToFailingWriter", func(t *testing.T) {
t.Parallel()
testEmitToFailingWriter(t, harnessPath, dir)
})
t.Run("emitWithCounterClear", func(t *testing.T) {
t.Parallel()
testEmitWithCounterClear(t, harnessPath, dir)
})
}
// upmergeCoverData helps improve coverage data for this package
// itself. If this test itself is being invoked with "-cover", then
// what we'd like is for package coverage data (that is, coverage for
// routines in "runtime/coverage") to be incorporated into the test
// run from the "harness.exe" runs we've just done. We can accomplish
// this by doing a merge from the harness gocoverdir's to the test
// gocoverdir.
func upmergeCoverData(t *testing.T, gocoverdir string) {
if testing.CoverMode() == "" {
return
}
testGoCoverDir := os.Getenv("GOCOVERDIR")
if testGoCoverDir == "" {
return
}
args := []string{"tool", "covdata", "merge", "-pkg=runtime/coverage",
"-o", testGoCoverDir, "-i", gocoverdir}
t.Logf("up-merge of covdata from %s to %s", gocoverdir, testGoCoverDir)
t.Logf("executing: go %+v", args)
cmd := exec.Command(testenv.GoToolPath(t), args...)
if b, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("covdata merge failed (%v): %s", err, b)
}
}
// buildHarness builds the helper program "harness.exe".
func buildHarness(t *testing.T, dir string, opts []string) string {
harnessPath := filepath.Join(dir, "harness.exe")
harnessSrc := filepath.Join("testdata", "harness.go")
args := []string{"build", "-o", harnessPath}
args = append(args, opts...)
args = append(args, harnessSrc)
//t.Logf("harness build: go %+v\n", args)
cmd := exec.Command(testenv.GoToolPath(t), args...)
if b, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("build failed (%v): %s", err, b)
}
return harnessPath
}
func mkdir(t *testing.T, d string) string {
t.Helper()
if err := os.Mkdir(d, 0777); err != nil {
t.Fatalf("mkdir failed: %v", err)
}
return d
}
// updateGoCoverDir updates the specified environment 'env' to set
// GOCOVERDIR to 'gcd' (if setGoCoverDir is TRUE) or removes
// GOCOVERDIR from the environment (if setGoCoverDir is false).
func updateGoCoverDir(env []string, gcd string, setGoCoverDir bool) []string {
rv := []string{}
found := false
for _, v := range env {
if strings.HasPrefix(v, "GOCOVERDIR=") {
if !setGoCoverDir {
continue
}
v = "GOCOVERDIR=" + gcd
found = true
}
rv = append(rv, v)
}
if !found && setGoCoverDir {
rv = append(rv, "GOCOVERDIR="+gcd)
}
return rv
}
func runHarness(t *testing.T, harnessPath string, tp string, setGoCoverDir bool, rdir, edir string) (string, error) {
t.Logf("running: %s -tp %s -o %s with rdir=%s and GOCOVERDIR=%v", harnessPath, tp, edir, rdir, setGoCoverDir)
cmd := exec.Command(harnessPath, "-tp", tp, "-o", edir)
cmd.Dir = rdir
cmd.Env = updateGoCoverDir(os.Environ(), rdir, setGoCoverDir)
b, err := cmd.CombinedOutput()
//t.Logf("harness run output: %s\n", string(b))
return string(b), err
}
func testForSpecificFunctions(t *testing.T, dir string, want []string, avoid []string) string {
args := []string{"tool", "covdata", "debugdump",
"-live", "-pkg=command-line-arguments", "-i=" + dir}
t.Logf("running: go %v\n", args)
cmd := exec.Command(testenv.GoToolPath(t), args...)
b, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("'go tool covdata failed (%v): %s", err, b)
}
output := string(b)
rval := ""
for _, f := range want {
wf := "Func: " + f + "\n"
if strings.Contains(output, wf) {
continue
}
rval += fmt.Sprintf("error: output should contain %q but does not\n", wf)
}
for _, f := range avoid {
wf := "Func: " + f + "\n"
if strings.Contains(output, wf) {
rval += fmt.Sprintf("error: output should not contain %q but does\n", wf)
}
}
if rval != "" {
t.Logf("=-= begin output:\n" + output + "\n=-= end output\n")
}
return rval
}
func withAndWithoutRunner(f func(setit bool, tag string)) {
// Run 'f' with and without GOCOVERDIR set.
for i := 0; i < 2; i++ {
tag := "x"
setGoCoverDir := true
if i == 0 {
setGoCoverDir = false
tag = "y"
}
f(setGoCoverDir, tag)
}
}
func mktestdirs(t *testing.T, tag, tp, dir string) (string, string) {
t.Helper()
rdir := mkdir(t, filepath.Join(dir, tp+"-rdir-"+tag))
edir := mkdir(t, filepath.Join(dir, tp+"-edir-"+tag))
return rdir, edir
}
func testEmitToDir(t *testing.T, harnessPath string, dir string) {
withAndWithoutRunner(func(setGoCoverDir bool, tag string) {
tp := "emitToDir"
rdir, edir := mktestdirs(t, tag, tp, dir)
output, err := runHarness(t, harnessPath, tp,
setGoCoverDir, rdir, edir)
if err != nil {
t.Logf("%s", output)
t.Fatalf("running 'harness -tp emitDir': %v", err)
}
// Just check to make sure meta-data file and counter data file were
// written. Another alternative would be to run "go tool covdata"
// or equivalent, but for now, this is what we've got.
dents, err := os.ReadDir(edir)
if err != nil {
t.Fatalf("os.ReadDir(%s) failed: %v", edir, err)
}
mfc := 0
cdc := 0
for _, e := range dents {
if e.IsDir() {
continue
}
if strings.HasPrefix(e.Name(), coverage.MetaFilePref) {
mfc++
} else if strings.HasPrefix(e.Name(), coverage.CounterFilePref) {
cdc++
}
}
wantmf := 1
wantcf := 1
if mfc != wantmf {
t.Errorf("EmitToDir: want %d meta-data files, got %d\n", wantmf, mfc)
}
if cdc != wantcf {
t.Errorf("EmitToDir: want %d counter-data files, got %d\n", wantcf, cdc)
}
upmergeCoverData(t, edir)
upmergeCoverData(t, rdir)
})
}
func testEmitToWriter(t *testing.T, harnessPath string, dir string) {
withAndWithoutRunner(func(setGoCoverDir bool, tag string) {
tp := "emitToWriter"
rdir, edir := mktestdirs(t, tag, tp, dir)
output, err := runHarness(t, harnessPath, tp, setGoCoverDir, rdir, edir)
if err != nil {
t.Logf("%s", output)
t.Fatalf("running 'harness -tp %s': %v", tp, err)
}
want := []string{"main", tp}
avoid := []string{"final"}
if msg := testForSpecificFunctions(t, edir, want, avoid); msg != "" {
t.Errorf("coverage data from %q output match failed: %s", tp, msg)
}
upmergeCoverData(t, edir)
upmergeCoverData(t, rdir)
})
}
func testEmitToNonexistentDir(t *testing.T, harnessPath string, dir string) {
withAndWithoutRunner(func(setGoCoverDir bool, tag string) {
tp := "emitToNonexistentDir"
rdir, edir := mktestdirs(t, tag, tp, dir)
output, err := runHarness(t, harnessPath, tp, setGoCoverDir, rdir, edir)
if err != nil {
t.Logf("%s", output)
t.Fatalf("running 'harness -tp %s': %v", tp, err)
}
upmergeCoverData(t, edir)
upmergeCoverData(t, rdir)
})
}
func testEmitToUnwritableDir(t *testing.T, harnessPath string, dir string) {
withAndWithoutRunner(func(setGoCoverDir bool, tag string) {
tp := "emitToUnwritableDir"
rdir, edir := mktestdirs(t, tag, tp, dir)
// Make edir unwritable.
if err := os.Chmod(edir, 0555); err != nil {
t.Fatalf("chmod failed: %v", err)
}
defer os.Chmod(edir, 0777)
output, err := runHarness(t, harnessPath, tp, setGoCoverDir, rdir, edir)
if err != nil {
t.Logf("%s", output)
t.Fatalf("running 'harness -tp %s': %v", tp, err)
}
upmergeCoverData(t, edir)
upmergeCoverData(t, rdir)
})
}
func testEmitToNilWriter(t *testing.T, harnessPath string, dir string) {
withAndWithoutRunner(func(setGoCoverDir bool, tag string) {
tp := "emitToNilWriter"
rdir, edir := mktestdirs(t, tag, tp, dir)
output, err := runHarness(t, harnessPath, tp, setGoCoverDir, rdir, edir)
if err != nil {
t.Logf("%s", output)
t.Fatalf("running 'harness -tp %s': %v", tp, err)
}
upmergeCoverData(t, edir)
upmergeCoverData(t, rdir)
})
}
func testEmitToFailingWriter(t *testing.T, harnessPath string, dir string) {
withAndWithoutRunner(func(setGoCoverDir bool, tag string) {
tp := "emitToFailingWriter"
rdir, edir := mktestdirs(t, tag, tp, dir)
output, err := runHarness(t, harnessPath, tp, setGoCoverDir, rdir, edir)
if err != nil {
t.Logf("%s", output)
t.Fatalf("running 'harness -tp %s': %v", tp, err)
}
upmergeCoverData(t, edir)
upmergeCoverData(t, rdir)
})
}
func testEmitWithCounterClear(t *testing.T, harnessPath string, dir string) {
// Ensure that we have two versions of the harness: one built with
// -covermode=atomic and one built with -covermode=set (we need
// both modes to test all of the functionality).
var nonatomicHarnessPath, atomicHarnessPath string
if testing.CoverMode() != "atomic" {
nonatomicHarnessPath = harnessPath
bdir2 := mkdir(t, filepath.Join(dir, "build2"))
hargs := []string{"-covermode=atomic", "-coverpkg=all"}
atomicHarnessPath = buildHarness(t, bdir2, hargs)
} else {
atomicHarnessPath = harnessPath
mode := "set"
if testing.CoverMode() != "" && testing.CoverMode() != "atomic" {
mode = testing.CoverMode()
}
// Build a special nonatomic covermode version of the harness
// (we need both modes to test all of the functionality).
bdir2 := mkdir(t, filepath.Join(dir, "build2"))
hargs := []string{"-covermode=" + mode, "-coverpkg=all"}
nonatomicHarnessPath = buildHarness(t, bdir2, hargs)
}
withAndWithoutRunner(func(setGoCoverDir bool, tag string) {
// First a run with the nonatomic harness path, which we
// expect to fail.
tp := "emitWithCounterClear"
rdir1, edir1 := mktestdirs(t, tag, tp+"1", dir)
output, err := runHarness(t, nonatomicHarnessPath, tp,
setGoCoverDir, rdir1, edir1)
if err == nil {
t.Logf("%s", output)
t.Fatalf("running '%s -tp %s': unexpected success",
nonatomicHarnessPath, tp)
}
// Next a run with the atomic harness path, which we
// expect to succeed.
rdir2, edir2 := mktestdirs(t, tag, tp+"2", dir)
output, err = runHarness(t, atomicHarnessPath, tp,
setGoCoverDir, rdir2, edir2)
if err != nil {
t.Logf("%s", output)
t.Fatalf("running 'harness -tp %s': %v", tp, err)
}
want := []string{tp, "postClear"}
avoid := []string{"preClear", "main", "final"}
if msg := testForSpecificFunctions(t, edir2, want, avoid); msg != "" {
t.Logf("%s", output)
t.Errorf("coverage data from %q output match failed: %s", tp, msg)
}
if testing.CoverMode() == "atomic" {
upmergeCoverData(t, edir2)
upmergeCoverData(t, rdir2)
} else {
upmergeCoverData(t, edir1)
upmergeCoverData(t, rdir1)
}
})
}
func TestApisOnNocoverBinary(t *testing.T) {
if testing.Short() {
t.Skipf("skipping test: too long for short mode")
}
testenv.MustHaveGoBuild(t)
dir := t.TempDir()
// Build harness with no -cover.
bdir := mkdir(t, filepath.Join(dir, "nocover"))
edir := mkdir(t, filepath.Join(dir, "emitDirNo"))
harnessPath := buildHarness(t, bdir, nil)
output, err := runHarness(t, harnessPath, "emitToDir", false, edir, edir)
if err == nil {
t.Fatalf("expected error on TestApisOnNocoverBinary harness run")
}
const want = "not built with -cover"
if !strings.Contains(output, want) {
t.Errorf("error output does not contain %q: %s", want, output)
}
}
func TestIssue56006EmitDataRaceCoverRunningGoroutine(t *testing.T) {
if testing.Short() {
t.Skipf("skipping test: too long for short mode")
}
if !goexperiment.CoverageRedesign {
t.Skipf("skipping new coverage tests (experiment not enabled)")
}
// This test requires "go test -race -cover", meaning that we need
// go build, go run, and "-race" support.
testenv.MustHaveGoRun(t)
if !platform.RaceDetectorSupported(runtime.GOOS, runtime.GOARCH) ||
!testenv.HasCGO() {
t.Skip("skipped due to lack of race detector support / CGO")
}
// This will run a program with -cover and -race where we have a
// goroutine still running (and updating counters) at the point where
// the test runtime is trying to write out counter data.
cmd := exec.Command(testenv.GoToolPath(t), "test", "-cover", "-race")
cmd.Dir = filepath.Join("testdata", "issue56006")
b, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("go test -cover -race failed: %v", err)
}
// Don't want to see any data races in output.
avoid := []string{"DATA RACE"}
for _, no := range avoid {
if strings.Contains(string(b), no) {
t.Logf("%s\n", string(b))
t.Fatalf("found %s in test output, not permitted", no)
}
}
}
|