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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
|
// 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. We need two copies of the harness, one built
// with -covermode=atomic and one built non-atomic.
bdir1 := mkdir(t, filepath.Join(dir, "build1"))
hargs1 := []string{"-covermode=atomic", "-coverpkg=all"}
atomicHarnessPath := buildHarness(t, bdir1, hargs1)
nonAtomicMode := testing.CoverMode()
if testing.CoverMode() == "atomic" {
nonAtomicMode = "set"
}
bdir2 := mkdir(t, filepath.Join(dir, "build2"))
hargs2 := []string{"-coverpkg=all", "-covermode=" + nonAtomicMode}
nonAtomicHarnessPath := buildHarness(t, bdir2, hargs2)
t.Logf("atomic harness path is %s", atomicHarnessPath)
t.Logf("non-atomic harness path is %s", nonAtomicHarnessPath)
// 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, atomicHarnessPath, dir)
})
t.Run("emitToWriter", func(t *testing.T) {
t.Parallel()
testEmitToWriter(t, atomicHarnessPath, dir)
})
t.Run("emitToNonexistentDir", func(t *testing.T) {
t.Parallel()
testEmitToNonexistentDir(t, atomicHarnessPath, dir)
})
t.Run("emitToNilWriter", func(t *testing.T) {
t.Parallel()
testEmitToNilWriter(t, atomicHarnessPath, dir)
})
t.Run("emitToFailingWriter", func(t *testing.T) {
t.Parallel()
testEmitToFailingWriter(t, atomicHarnessPath, dir)
})
t.Run("emitWithCounterClear", func(t *testing.T) {
t.Parallel()
testEmitWithCounterClear(t, atomicHarnessPath, dir)
})
t.Run("emitToDirNonAtomic", func(t *testing.T) {
t.Parallel()
testEmitToDirNonAtomic(t, nonAtomicHarnessPath, nonAtomicMode, dir)
})
t.Run("emitToWriterNonAtomic", func(t *testing.T) {
t.Parallel()
testEmitToWriterNonAtomic(t, nonAtomicHarnessPath, nonAtomicMode, dir)
})
t.Run("emitWithCounterClearNonAtomic", func(t *testing.T) {
t.Parallel()
testEmitWithCounterClearNonAtomic(t, nonAtomicHarnessPath, nonAtomicMode, 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, mode string) {
if testing.CoverMode() != mode {
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, "atomic")
upmergeCoverData(t, rdir, "atomic")
})
}
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, "atomic")
upmergeCoverData(t, rdir, "atomic")
})
}
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, "atomic")
upmergeCoverData(t, rdir, "atomic")
})
}
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, "atomic")
upmergeCoverData(t, rdir, "atomic")
})
}
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, "atomic")
upmergeCoverData(t, rdir, "atomic")
})
}
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, "atomic")
upmergeCoverData(t, rdir, "atomic")
})
}
func testEmitWithCounterClear(t *testing.T, harnessPath string, dir string) {
withAndWithoutRunner(func(setGoCoverDir bool, tag string) {
tp := "emitWithCounterClear"
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{tp, "postClear"}
avoid := []string{"preClear", "main", "final"}
if msg := testForSpecificFunctions(t, edir, want, avoid); msg != "" {
t.Logf("%s", output)
t.Errorf("coverage data from %q output match failed: %s", tp, msg)
}
upmergeCoverData(t, edir, "atomic")
upmergeCoverData(t, rdir, "atomic")
})
}
func testEmitToDirNonAtomic(t *testing.T, harnessPath string, naMode string, dir string) {
tp := "emitToDir"
tag := "nonatomdir"
rdir, edir := mktestdirs(t, tag, tp, dir)
output, err := runHarness(t, harnessPath, tp,
true, rdir, edir)
// We expect an error here.
if err == nil {
t.Logf("%s", output)
t.Fatalf("running 'harness -tp %s': did not get expected error", tp)
}
got := strings.TrimSpace(string(output))
want := "WriteCountersDir invoked for program built"
if !strings.Contains(got, want) {
t.Errorf("running 'harness -tp %s': got:\n%s\nwant: %s",
tp, got, want)
}
upmergeCoverData(t, edir, naMode)
upmergeCoverData(t, rdir, naMode)
}
func testEmitToWriterNonAtomic(t *testing.T, harnessPath string, naMode string, dir string) {
tp := "emitToWriter"
tag := "nonatomw"
rdir, edir := mktestdirs(t, tag, tp, dir)
output, err := runHarness(t, harnessPath, tp,
true, rdir, edir)
// We expect an error here.
if err == nil {
t.Logf("%s", output)
t.Fatalf("running 'harness -tp %s': did not get expected error", tp)
}
got := strings.TrimSpace(string(output))
want := "WriteCounters invoked for program built"
if !strings.Contains(got, want) {
t.Errorf("running 'harness -tp %s': got:\n%s\nwant: %s",
tp, got, want)
}
upmergeCoverData(t, edir, naMode)
upmergeCoverData(t, rdir, naMode)
}
func testEmitWithCounterClearNonAtomic(t *testing.T, harnessPath string, naMode string, dir string) {
tp := "emitWithCounterClear"
tag := "cclear"
rdir, edir := mktestdirs(t, tag, tp, dir)
output, err := runHarness(t, harnessPath, tp,
true, rdir, edir)
// We expect an error here.
if err == nil {
t.Logf("%s", output)
t.Fatalf("running 'harness -tp %s' nonatomic: did not get expected error", tp)
}
got := strings.TrimSpace(string(output))
want := "ClearCounters invoked for program built"
if !strings.Contains(got, want) {
t.Errorf("running 'harness -tp %s': got:\n%s\nwant: %s",
tp, got, want)
}
upmergeCoverData(t, edir, naMode)
upmergeCoverData(t, rdir, naMode)
}
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)
}
}
}
func TestIssue59563TruncatedCoverPkgAll(t *testing.T) {
if testing.Short() {
t.Skipf("skipping test: too long for short mode")
}
testenv.MustHaveGoRun(t)
tmpdir := t.TempDir()
ppath := filepath.Join(tmpdir, "foo.cov")
cmd := exec.Command(testenv.GoToolPath(t), "test", "-coverpkg=all", "-coverprofile="+ppath)
cmd.Dir = filepath.Join("testdata", "issue59563")
b, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("go test -cover failed: %v", err)
}
cmd = exec.Command(testenv.GoToolPath(t), "tool", "cover", "-func="+ppath)
b, err = cmd.CombinedOutput()
if err != nil {
t.Fatalf("go tool cover -func failed: %v", err)
}
lines := strings.Split(string(b), "\n")
nfound := 0
bad := false
for _, line := range lines {
f := strings.Fields(line)
if len(f) == 0 {
continue
}
// We're only interested in the specific function "large" for
// the testcase being built. See the #59563 for details on why
// size matters.
if !(strings.HasPrefix(f[0], "runtime/coverage/testdata/issue59563/repro.go") && strings.Contains(line, "large")) {
continue
}
nfound++
want := "100.0%"
if f[len(f)-1] != want {
t.Errorf("wanted %s got: %q\n", want, line)
bad = true
}
}
if nfound != 1 {
t.Errorf("wanted 1 found, got %d\n", nfound)
bad = true
}
if bad {
t.Logf("func output:\n%s\n", string(b))
}
}
|