1
2
3
4
5 package cfile
6
7 import (
8 "encoding/json"
9 "flag"
10 "internal/coverage"
11 "internal/goexperiment"
12 "internal/testenv"
13 "os"
14 "os/exec"
15 "path/filepath"
16 "strings"
17 "testing"
18 _ "unsafe"
19 )
20
21 func testGoCoverDir(t *testing.T) string {
22 if f := flag.Lookup("test.gocoverdir"); f != nil {
23 if dir := f.Value.String(); dir != "" {
24 return dir
25 }
26 }
27 return t.TempDir()
28 }
29
30
31
32
33
34 func TestTestSupport(t *testing.T) {
35 if !goexperiment.CoverageRedesign {
36 return
37 }
38 if testing.CoverMode() == "" {
39 return
40 }
41 tgcd := testGoCoverDir(t)
42 t.Logf("testing.testGoCoverDir() returns %s mode=%s\n",
43 tgcd, testing.CoverMode())
44
45 textfile := filepath.Join(t.TempDir(), "file.txt")
46 var sb strings.Builder
47 err := ProcessCoverTestDir(tgcd, textfile,
48 testing.CoverMode(), "", &sb, nil)
49 if err != nil {
50 t.Fatalf("bad: %v", err)
51 }
52
53
54 if inf, err := os.Open(textfile); err != nil {
55 t.Fatalf("problems opening text file %s: %v", textfile, err)
56 } else {
57 inf.Close()
58 }
59
60
61 strout := sb.String()
62 want := "of statements"
63 if !strings.Contains(strout, want) {
64 t.Logf("output from run: %s\n", strout)
65 t.Fatalf("percent output missing token: %q", want)
66 }
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 func TestCoverageSnapshot(t *testing.T) {
83 testenv.MustHaveGoRun(t)
84 args := []string{"test", "-tags", "SELECT_USING_THIS_TAG",
85 "-cover", "-run=TestCoverageSnapshotImpl", "internal/coverage/cfile"}
86 cmd := exec.Command(testenv.GoToolPath(t), args...)
87 if b, err := cmd.CombinedOutput(); err != nil {
88 t.Fatalf("go test failed (%v): %s", err, b)
89 }
90 }
91
92 const hellogo = `
93 package main
94
95 func main() {
96 println("hello")
97 }
98 `
99
100
101
102
103 func genAuxMeta(t *testing.T, dstdir string) (string, string) {
104
105 src := filepath.Join(dstdir, "hello.go")
106 if err := os.WriteFile(src, []byte(hellogo), 0777); err != nil {
107 t.Fatalf("write failed: %v", err)
108 }
109 args := []string{"run", "-covermode=" + testing.CoverMode(), src}
110 cmd := exec.Command(testenv.GoToolPath(t), args...)
111 cmd.Env = updateGoCoverDir(os.Environ(), dstdir, true)
112 if b, err := cmd.CombinedOutput(); err != nil {
113 t.Fatalf("go run failed (%v): %s", err, b)
114 }
115
116
117 files, err := os.ReadDir(dstdir)
118 if err != nil {
119 t.Fatalf("reading %s: %v", dstdir, err)
120 }
121 for _, f := range files {
122 if strings.HasPrefix(f.Name(), "covmeta") {
123 return filepath.Join(dstdir, f.Name()), "hello.go:"
124 }
125 }
126 t.Fatalf("could not locate generated meta-data file")
127 return "", ""
128 }
129
130 func TestAuxMetaDataFiles(t *testing.T) {
131 if !goexperiment.CoverageRedesign {
132 return
133 }
134 if testing.CoverMode() == "" {
135 return
136 }
137 testenv.MustHaveGoRun(t)
138 tgcd := testGoCoverDir(t)
139 t.Logf("testing.testGoCoverDir() returns %s mode=%s\n",
140 tgcd, testing.CoverMode())
141
142 td := t.TempDir()
143
144
145
146
147 othermetadir := filepath.Join(td, "othermeta")
148 if err := os.Mkdir(othermetadir, 0777); err != nil {
149 t.Fatalf("mkdir failed: %v", err)
150 }
151 mfile, token := genAuxMeta(t, othermetadir)
152
153
154 metafiles := filepath.Join(tgcd, coverage.MetaFilesFileName)
155 mfc := coverage.MetaFileCollection{
156 ImportPaths: []string{"command-line-arguments"},
157 MetaFileFragments: []string{mfile},
158 }
159 jdata, err := json.Marshal(mfc)
160 if err != nil {
161 t.Fatalf("marshal MetaFileCollection: %v", err)
162 }
163 if err := os.WriteFile(metafiles, jdata, 0666); err != nil {
164 t.Fatalf("write failed: %v", err)
165 }
166
167
168 var sb strings.Builder
169 textfile := filepath.Join(td, "file2.txt")
170 err = ProcessCoverTestDir(tgcd, textfile,
171 testing.CoverMode(), "", &sb, nil)
172 if err != nil {
173 t.Fatalf("bad: %v", err)
174 }
175 if err = os.Remove(metafiles); err != nil {
176 t.Fatalf("removing metafiles file: %v", err)
177 }
178
179
180 contents, err := os.ReadFile(textfile)
181 strc := string(contents)
182 if err != nil {
183 t.Fatalf("problems reading text file %s: %v", textfile, err)
184 }
185 if !strings.Contains(strc, token) {
186 t.Logf("content: %s\n", string(contents))
187 t.Fatalf("cov profile does not contain aux meta content %q", token)
188 }
189 }
190
View as plain text