| 1 |
// Copyright 2022 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package cov_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"cmd/internal/cov" |
| 9 |
"fmt" |
| 10 |
"internal/coverage" |
| 11 |
"internal/coverage/decodecounter" |
| 12 |
"internal/coverage/decodemeta" |
| 13 |
"internal/coverage/pods" |
| 14 |
"internal/testenv" |
| 15 |
"os" |
| 16 |
"path/filepath" |
| 17 |
"testing" |
| 18 |
) |
| 19 |
|
| 20 |
// visitor implements the CovDataVisitor interface in a very stripped |
| 21 |
// down way, just keeps track of interesting events. |
| 22 |
type visitor struct { |
| 23 |
metaFileCount int |
| 24 |
counterFileCount int |
| 25 |
funcCounterData int |
| 26 |
metaFuncCount int |
| 27 |
} |
| 28 |
|
| 29 |
func (v *visitor) BeginPod(p pods.Pod) {} |
| 30 |
func (v *visitor) EndPod(p pods.Pod) {} |
| 31 |
func (v *visitor) VisitMetaDataFile(mdf string, mfr *decodemeta.CoverageMetaFileReader) { |
| 32 |
v.metaFileCount++ |
| 33 |
} |
| 34 |
func (v *visitor) BeginCounterDataFile(cdf string, cdr *decodecounter.CounterDataReader, dirIdx int) { |
| 35 |
v.counterFileCount++ |
| 36 |
} |
| 37 |
func (v *visitor) EndCounterDataFile(cdf string, cdr *decodecounter.CounterDataReader, dirIdx int) {} |
| 38 |
func (v *visitor) VisitFuncCounterData(payload decodecounter.FuncPayload) { v.funcCounterData++ } |
| 39 |
func (v *visitor) EndCounters() {} |
| 40 |
func (v *visitor) BeginPackage(pd *decodemeta.CoverageMetaDataDecoder, pkgIdx uint32) {} |
| 41 |
func (v *visitor) EndPackage(pd *decodemeta.CoverageMetaDataDecoder, pkgIdx uint32) {} |
| 42 |
func (v *visitor) VisitFunc(pkgIdx uint32, fnIdx uint32, fd *coverage.FuncDesc) { v.metaFuncCount++ } |
| 43 |
func (v *visitor) Finish() {} |
| 44 |
|
| 45 |
func TestIssue58411(t *testing.T) { |
| 46 |
testenv.MustHaveGoBuild(t) |
| 47 |
|
| 48 |
// Build a tiny test program with -cover. Smallness is important; |
| 49 |
// it is one of the factors that triggers issue 58411. |
| 50 |
d := t.TempDir() |
| 51 |
exepath := filepath.Join(d, "small.exe") |
| 52 |
path := filepath.Join("testdata", "small.go") |
| 53 |
cmd := testenv.Command(t, testenv.GoToolPath(t), "build", |
| 54 |
"-o", exepath, "-cover", path) |
| 55 |
b, err := cmd.CombinedOutput() |
| 56 |
if len(b) != 0 { |
| 57 |
t.Logf("## build output:\n%s", b) |
| 58 |
} |
| 59 |
if err != nil { |
| 60 |
t.Fatalf("build error: %v", err) |
| 61 |
} |
| 62 |
|
| 63 |
// Run to produce coverage data. Note the large argument; we need a large |
| 64 |
// argument (more than 4k) to trigger the bug, but the overall file |
| 65 |
// has to remain small (since large files will be read with mmap). |
| 66 |
covdir := filepath.Join(d, "covdata") |
| 67 |
if err = os.Mkdir(covdir, 0777); err != nil { |
| 68 |
t.Fatalf("creating covdir: %v", err) |
| 69 |
} |
| 70 |
large := fmt.Sprintf("%07999d", 0) |
| 71 |
cmd = testenv.Command(t, exepath, "1", "2", "3", large) |
| 72 |
cmd.Dir = covdir |
| 73 |
cmd.Env = append(os.Environ(), "GOCOVERDIR="+covdir) |
| 74 |
b, err = cmd.CombinedOutput() |
| 75 |
if err != nil { |
| 76 |
t.Logf("## run output:\n%s", b) |
| 77 |
t.Fatalf("build error: %v", err) |
| 78 |
} |
| 79 |
|
| 80 |
vis := &visitor{} |
| 81 |
|
| 82 |
// Read resulting coverage data. Without the fix, this would |
| 83 |
// yield a "short read" error. |
| 84 |
const verbosityLevel = 0 |
| 85 |
const flags = 0 |
| 86 |
cdr := cov.MakeCovDataReader(vis, []string{covdir}, verbosityLevel, flags, nil) |
| 87 |
err = cdr.Visit() |
| 88 |
if err != nil { |
| 89 |
t.Fatalf("visit failed: %v", err) |
| 90 |
} |
| 91 |
|
| 92 |
// make sure we saw a few things just for grins |
| 93 |
const want = "{metaFileCount:1 counterFileCount:1 funcCounterData:1 metaFuncCount:1}" |
| 94 |
got := fmt.Sprintf("%+v", *vis) |
| 95 |
if want != got { |
| 96 |
t.Errorf("visitor contents: want %v got %v\n", want, got) |
| 97 |
} |
| 98 |
} |