summaryrefslogtreecommitdiffstats
path: root/src/runtime/coverage/ts_test.go
blob: b4c6e9716cbd307ced5f900a3dfd675c37a4ef33 (plain)
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
// 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 (
	"encoding/json"
	"internal/coverage"
	"internal/goexperiment"
	"internal/testenv"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"
	_ "unsafe"
)

//go:linkname testing_testGoCoverDir testing.testGoCoverDir
func testing_testGoCoverDir() string

func testGoCoverDir(t *testing.T) string {
	tgcd := testing_testGoCoverDir()
	if tgcd != "" {
		return tgcd
	}
	return t.TempDir()
}

// TestTestSupport does a basic verification of the functionality in
// runtime/coverage.processCoverTestDir (doing this here as opposed to
// relying on other test paths will provide a better signal when
// running "go test -cover" for this package).
func TestTestSupport(t *testing.T) {
	if !goexperiment.CoverageRedesign {
		return
	}
	if testing.CoverMode() == "" {
		return
	}
	tgcd := testGoCoverDir(t)
	t.Logf("testing.testGoCoverDir() returns %s mode=%s\n",
		tgcd, testing.CoverMode())

	textfile := filepath.Join(t.TempDir(), "file.txt")
	var sb strings.Builder
	err := processCoverTestDirInternal(tgcd, textfile,
		testing.CoverMode(), "", &sb)
	if err != nil {
		t.Fatalf("bad: %v", err)
	}

	// Check for existence of text file.
	if inf, err := os.Open(textfile); err != nil {
		t.Fatalf("problems opening text file %s: %v", textfile, err)
	} else {
		inf.Close()
	}

	// Check for percent output with expected tokens.
	strout := sb.String()
	want := "of statements"
	if !strings.Contains(strout, want) {
		t.Logf("output from run: %s\n", strout)
		t.Fatalf("percent output missing token: %q", want)
	}
}

var funcInvoked bool

//go:noinline
func thisFunctionOnlyCalledFromSnapshotTest(n int) int {
	if funcInvoked {
		panic("bad")
	}
	funcInvoked = true

	// Contents here not especially important, just so long as we
	// have some statements.
	t := 0
	for i := 0; i < n; i++ {
		for j := 0; j < i; j++ {
			t += i ^ j
		}
	}
	return t
}

// Tests runtime/coverage.snapshot() directly. Note that if
// coverage is not enabled, the hook is designed to just return
// zero.
func TestCoverageSnapshot(t *testing.T) {
	C1 := snapshot()
	thisFunctionOnlyCalledFromSnapshotTest(15)
	C2 := snapshot()
	cond := "C1 > C2"
	val := C1 > C2
	if testing.CoverMode() != "" {
		cond = "C1 >= C2"
		val = C1 >= C2
	}
	t.Logf("%f %f\n", C1, C2)
	if val {
		t.Errorf("erroneous snapshots, %s = true C1=%f C2=%f",
			cond, C1, C2)
	}
}

const hellogo = `
package main

func main() {
  println("hello")
}
`

// Returns a pair F,T where F is a meta-data file generated from
// "hello.go" above, and T is a token to look for that should be
// present in the coverage report from F.
func genAuxMeta(t *testing.T, dstdir string) (string, string) {
	// Do a GOCOVERDIR=<tmp> go run hello.go
	src := filepath.Join(dstdir, "hello.go")
	if err := os.WriteFile(src, []byte(hellogo), 0777); err != nil {
		t.Fatalf("write failed: %v", err)
	}
	args := []string{"run", "-covermode=" + testing.CoverMode(), src}
	cmd := exec.Command(testenv.GoToolPath(t), args...)
	cmd.Env = updateGoCoverDir(os.Environ(), dstdir, true)
	if b, err := cmd.CombinedOutput(); err != nil {
		t.Fatalf("go run failed (%v): %s", err, b)
	}

	// Pick out the generated meta-data file.
	files, err := os.ReadDir(dstdir)
	if err != nil {
		t.Fatalf("reading %s: %v", dstdir, err)
	}
	for _, f := range files {
		if strings.HasPrefix(f.Name(), "covmeta") {
			return filepath.Join(dstdir, f.Name()), "hello.go:"
		}
	}
	t.Fatalf("could not locate generated meta-data file")
	return "", ""
}

func TestAuxMetaDataFiles(t *testing.T) {
	if !goexperiment.CoverageRedesign {
		return
	}
	if testing.CoverMode() == "" {
		return
	}
	testenv.MustHaveGoRun(t)
	tgcd := testGoCoverDir(t)
	t.Logf("testing.testGoCoverDir() returns %s mode=%s\n",
		tgcd, testing.CoverMode())

	td := t.TempDir()

	// Manufacture a new, separate meta-data file not related to this
	// test. Contents are not important, just so long as the
	// packages/paths are different.
	othermetadir := filepath.Join(td, "othermeta")
	if err := os.Mkdir(othermetadir, 0777); err != nil {
		t.Fatalf("mkdir failed: %v", err)
	}
	mfile, token := genAuxMeta(t, othermetadir)

	// Write a metafiles file.
	metafiles := filepath.Join(tgcd, coverage.MetaFilesFileName)
	mfc := coverage.MetaFileCollection{
		ImportPaths:       []string{"command-line-arguments"},
		MetaFileFragments: []string{mfile},
	}
	jdata, err := json.Marshal(mfc)
	if err != nil {
		t.Fatalf("marshal MetaFileCollection: %v", err)
	}
	if err := os.WriteFile(metafiles, jdata, 0666); err != nil {
		t.Fatalf("write failed: %v", err)
	}

	// Kick off guts of test.
	var sb strings.Builder
	textfile := filepath.Join(td, "file2.txt")
	err = processCoverTestDirInternal(tgcd, textfile,
		testing.CoverMode(), "", &sb)
	if err != nil {
		t.Fatalf("bad: %v", err)
	}
	if err = os.Remove(metafiles); err != nil {
		t.Fatalf("removing metafiles file: %v", err)
	}

	// Look for the expected things in the coverage profile.
	contents, err := os.ReadFile(textfile)
	strc := string(contents)
	if err != nil {
		t.Fatalf("problems reading text file %s: %v", textfile, err)
	}
	if !strings.Contains(strc, token) {
		t.Logf("content: %s\n", string(contents))
		t.Fatalf("cov profile does not contain aux meta content %q", token)
	}
}