Debian Codegraph @

go/analysis/analysistest/analysistest_test.go

raw symbols SCIP
1 // Copyright 2018 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 analysistest_test
6
7 import (
8 "fmt"
9 "go/token"
10 "log"
11 "os"
12 "reflect"
13 "strings"
14 "testing"
15
16 "golang.org/x/tools/go/analysis"
17 "golang.org/x/tools/go/analysis/analysistest"
18 "golang.org/x/tools/go/analysis/passes/findcall"
19 "golang.org/x/tools/internal/testenv"
20 "golang.org/x/tools/internal/testfiles"
21 "golang.org/x/tools/txtar"
22 )
23
24 func init() {
25 // Run() decides when tests use GOPATH mode or modules.
26 // We turn off GOPROXY just for good measure.
27 if err := os.Setenv("GOPROXY", "off"); err != nil {
28 log.Fatal(err)
29 }
30 }
31
32 // TestTheTest tests the analysistest testing infrastructure.
33 func TestTheTest(t *testing.T) {
34 testenv.NeedsTool(t, "go")
35
36 // We'll simulate a partly failing test of the findcall analysis,
37 // which (by default) reports calls to functions named 'println'.
38 findcall.Analyzer.Flags.Set("name", "println")
39
40 filemap := map[string]string{
41 "a/b.go": `package main // want package:"found"
42
43 func main() {
44 // The expectation is ill-formed:
45 print() // want: "diagnostic"
46 print() // want foo"fact"
47 print() // want foo:
48 print() // want "\xZZ scan error"
49
50 // A diagnostic is reported at this line, but the expectation doesn't match:
51 println("hello, world") // want "wrong expectation text"
52
53 // An unexpected diagnostic is reported at this line:
54 println() // trigger an unexpected diagnostic
55
56 // No diagnostic is reported at this line:
57 print() // want "unsatisfied expectation"
58
59 // OK
60 println("hello, world") // want "call of println"
61
62 // OK /* */-form.
63 println("안녕, 세계") /* want "call of println" */
64
65 // OK (nested comment)
66 println("Γειά σου, Κόσμε") // some comment // want "call of println"
67
68 // OK (nested comment in /**/)
69 println("你好,世界") /* some comment // want "call of println" */
70
71 // OK (multiple expectations on same line)
72 println(); println() // want "call of println(...)" "call of println(...)"
73
74 // A Line that is not formatted correctly in the golden file.
75 }
76
77 // OK (facts and diagnostics on same line)
78 func println(...interface{}) { println() } // want println:"found" "call of println(...)"
79
80 `,
81 "a/b.go.golden": `package main // want package:"found"
82
83 func main() {
84 // The expectation is ill-formed:
85 print() // want: "diagnostic"
86 print() // want foo"fact"
87 print() // want foo:
88 print() // want "\xZZ scan error"
89
90 // A diagnostic is reported at this line, but the expectation doesn't match:
91 println_TEST_("hello, world") // want "wrong expectation text"
92
93 // An unexpected diagnostic is reported at this line:
94 println_TEST_() // trigger an unexpected diagnostic
95
96 // No diagnostic is reported at this line:
97 print() // want "unsatisfied expectation"
98
99 // OK
100 println_TEST_("hello, world") // want "call of println"
101
102 // OK /* */-form.
103 println_TEST_("안녕, 세계") /* want "call of println" */
104
105 // OK (nested comment)
106 println_TEST_("Γειά σου, Κόσμε") // some comment // want "call of println"
107
108 // OK (nested comment in /**/)
109 println_TEST_("你好,世界") /* some comment // want "call of println" */
110
111 // OK (multiple expectations on same line)
112 println_TEST_()
113 println_TEST_() // want "call of println(...)" "call of println(...)"
114
115 // A Line that is not formatted correctly in the golden file.
116 }
117
118 // OK (facts and diagnostics on same line)
119 func println(...interface{}) { println_TEST_() } // want println:"found" "call of println(...)"
120 `,
121 "a/b_test.go": `package main
122
123 // Test file shouldn't mess with things (issue #40574)
124 `,
125 }
126 dir, cleanup, err := analysistest.WriteFiles(filemap)
127 if err != nil {
128 t.Fatal(err)
129 }
130 defer cleanup()
131
132 var got []string
133 t2 := errorfunc(func(s string) { got = append(got, s) }) // a fake *testing.T
134 analysistest.RunWithSuggestedFixes(t2, dir, findcall.Analyzer, "a")
135
136 want := []string{
137 `a/b.go:5: in 'want' comment: unexpected ":"`,
138 `a/b.go:6: in 'want' comment: got String after foo, want ':'`,
139 `a/b.go:7: in 'want' comment: got EOF, want regular expression`,
140 `a/b.go:8: in 'want' comment: invalid char escape`,
141 "a/b.go:11:9: diagnostic \"call of println(...)\" does not match pattern `wrong expectation text`",
142 `a/b.go:14:9: unexpected diagnostic: call of println(...)`,
143 "a/b.go:11: no diagnostic was reported matching `wrong expectation text`",
144 "a/b.go:17: no diagnostic was reported matching `unsatisfied expectation`",
145 // duplicate copies of each message from the test package (see issue #40574)
146 `a/b.go:5: in 'want' comment: unexpected ":"`,
147 `a/b.go:6: in 'want' comment: got String after foo, want ':'`,
148 `a/b.go:7: in 'want' comment: got EOF, want regular expression`,
149 `a/b.go:8: in 'want' comment: invalid char escape`,
150 "a/b.go:11:9: diagnostic \"call of println(...)\" does not match pattern `wrong expectation text`",
151 `a/b.go:14:9: unexpected diagnostic: call of println(...)`,
152 "a/b.go:11: no diagnostic was reported matching `wrong expectation text`",
153 "a/b.go:17: no diagnostic was reported matching `unsatisfied expectation`",
154 }
155 if !reflect.DeepEqual(got, want) {
156 t.Errorf("got:\n%s\nwant:\n%s",
157 strings.Join(got, "\n"),
158 strings.Join(want, "\n"))
159 }
160 }
161
162 // TestNoEnd tests that a missing SuggestedFix.End position is
163 // correctly interpreted as if equal to SuggestedFix.Pos (see issue #64199).
164 func TestNoEnd(t *testing.T) {
165 noend := &analysis.Analyzer{
166 Name: "noend",
167 Doc: "inserts /*hello*/ before first decl",
168 Run: func(pass *analysis.Pass) (any, error) {
169 decl := pass.Files[0].Decls[0]
170 pass.Report(analysis.Diagnostic{
171 Pos: decl.Pos(),
172 End: token.NoPos,
173 Message: "say hello",
174 SuggestedFixes: []analysis.SuggestedFix{{
175 Message: "say hello",
176 TextEdits: []analysis.TextEdit{
177 {
178 Pos: decl.Pos(),
179 End: token.NoPos,
180 NewText: []byte("/*hello*/"),
181 },
182 },
183 }},
184 })
185 return nil, nil
186 },
187 }
188
189 filemap := map[string]string{
190 "a/a.go": `package a
191
192 func F() {} // want "say hello"`,
193 "a/a.go.golden": `package a
194
195 /*hello*/
196 func F() {} // want "say hello"`,
197 }
198 dir, cleanup, err := analysistest.WriteFiles(filemap)
199 if err != nil {
200 t.Fatal(err)
201 }
202 defer cleanup()
203
204 analysistest.RunWithSuggestedFixes(t, dir, noend, "a")
205 }
206
207 func TestModule(t *testing.T) {
208 const content = `
209 Test that analysis.pass.Module is populated.
210
211 -- go.mod --
212 module golang.org/fake/mod
213
214 go 1.21
215
216 require golang.org/xyz/fake v0.12.34
217
218 -- mod.go --
219 // We expect a module.Path and a module.GoVersion, but an empty module.Version.
220
221 package mod // want "golang.org/fake/mod,,1.21"
222
223 import "golang.org/xyz/fake/ver"
224
225 var _ ver.T
226
227 -- vendor/modules.txt --
228 # golang.org/xyz/fake v0.12.34
229 ## explicit; go 1.18
230 golang.org/xyz/fake/ver
231
232 -- vendor/golang.org/xyz/fake/ver/ver.go --
233 // This package is vendored so that we can populate a non-empty
234 // Pass.Module.Version is in a test.
235
236 package ver //want "golang.org/xyz/fake,v0.12.34,1.18"
237
238 type T string
239 `
240 fs, err := txtar.FS(txtar.Parse([]byte(content)))
241 if err != nil {
242 t.Fatal(err)
243 }
244 dir := testfiles.CopyToTmp(t, fs)
245
246 filever := &analysis.Analyzer{
247 Name: "mod",
248 Doc: "reports module information",
249 Run: func(pass *analysis.Pass) (any, error) {
250 msg := "no module info"
251 if m := pass.Module; m != nil {
252 msg = fmt.Sprintf("%s,%s,%s", m.Path, m.Version, m.GoVersion)
253 }
254 for _, file := range pass.Files {
255 pass.Reportf(file.Package, "%s", msg)
256 }
257 return nil, nil
258 },
259 }
260 analysistest.Run(t, dir, filever, "golang.org/fake/mod", "golang.org/xyz/fake/ver")
261 }
262
263 type errorfunc func(string)
264
265 func (f errorfunc) Errorf(format string, args ...any) {
266 f(fmt.Sprintf(format, args...))
267 }