Debian Codegraph @

analysis/lint/testutil/check.go

raw
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 // This file is a modified copy of x/tools/go/analysis/analysistest/analysistest.go
6
7 package testutil
8
9 import (
10 "bytes"
11 "fmt"
12 "go/format"
13 "go/token"
14 "os"
15 "path/filepath"
16 "regexp"
17 "slices"
18 "sort"
19 "strings"
20 "testing"
21
22 "honnef.co/go/tools/internal/diff/myers"
23 "honnef.co/go/tools/lintcmd/runner"
24
25 "golang.org/x/tools/go/expect"
26 "golang.org/x/tools/txtar"
27 )
28
29 func CheckSuggestedFixes(t *testing.T, diagnostics []runner.Diagnostic) {
30 // Process each result (package) separately, matching up the suggested
31 // fixes into a diff, which we will compare to the .golden file. We have
32 // to do this per-result in case a file appears in two packages, such as in
33 // packages with tests, where mypkg/a.go will appear in both mypkg and
34 // mypkg.test. In that case, the analyzer may suggest the same set of
35 // changes to a.go for each package. If we merge all the results, those
36 // changes get doubly applied, which will cause conflicts or mismatches.
37 // Validating the results separately means as long as the two analyses
38 // don't produce conflicting suggestions for a single file, everything
39 // should match up.
40 // file -> message -> edits
41 fileEdits := make(map[string]map[string][]runner.TextEdit)
42 fileContents := make(map[string][]byte)
43
44 // Validate edits, prepare the fileEdits map and read the file contents.
45 for _, diag := range diagnostics {
46 for _, sf := range diag.SuggestedFixes {
47 for _, edit := range sf.TextEdits {
48 // Validate the edit.
49 if edit.Position.Offset > edit.End.Offset {
50 t.Errorf(
51 "diagnostic for analysis %v contains Suggested Fix with malformed edit: pos (%v) > end (%v)",
52 diag.Category, edit.Position.Offset, edit.End.Offset)
53 continue
54 }
55 if edit.Position.Filename != edit.End.Filename {
56 t.Errorf(
57 "diagnostic for analysis %v contains Suggested Fix with malformed edit spanning files %v and %v",
58 diag.Category, edit.Position.Filename, edit.End.Filename)
59 continue
60 }
61 if _, ok := fileContents[edit.Position.Filename]; !ok {
62 contents, err := os.ReadFile(edit.Position.Filename)
63 if err != nil {
64 t.Errorf("error reading %s: %v", edit.Position.Filename, err)
65 }
66 fileContents[edit.Position.Filename] = contents
67 }
68
69 if _, ok := fileEdits[edit.Position.Filename]; !ok {
70 fileEdits[edit.Position.Filename] = make(map[string][]runner.TextEdit)
71 }
72 fileEdits[edit.Position.Filename][sf.Message] = append(fileEdits[edit.Position.Filename][sf.Message], edit)
73 }
74 }
75 }
76
77 for file, fixes := range fileEdits {
78 // Get the original file contents.
79 orig, ok := fileContents[file]
80 if !ok {
81 t.Errorf("could not find file contents for %s", file)
82 continue
83 }
84
85 // Get the golden file and read the contents.
86 ar, err := txtar.ParseFile(file + ".golden")
87 if err != nil {
88 t.Errorf("error reading %s.golden: %v", file, err)
89 continue
90 }
91
92 if len(ar.Files) > 0 {
93 // one virtual file per kind of suggested fix
94
95 if len(ar.Comment) != 0 {
96 // we allow either just the comment, or just virtual
97 // files, not both. it is not clear how "both" should
98 // behave.
99 t.Errorf("%s.golden has leading comment; we don't know what to do with it", file)
100 continue
101 }
102
103 var sfs []string
104 for sf := range fixes {
105 sfs = append(sfs, sf)
106 }
107 slices.Sort(sfs)
108 for _, sf := range sfs {
109 edits := fixes[sf]
110 found := false
111 for _, vf := range ar.Files {
112 if vf.Name == sf {
113 found = true
114 out := applyEdits(orig, edits)
115 // the file may contain multiple trailing
116 // newlines if the user places empty lines
117 // between files in the archive. normalize
118 // this to a single newline.
119 want := string(bytes.TrimRight(vf.Data, "\n")) + "\n"
120 formatted, err := format.Source([]byte(out))
121 if err != nil {
122 t.Errorf("%s: error formatting edited source: %v\n%s", file, err, out)
123 continue
124 }
125 if want != string(formatted) {
126 d := myers.ComputeEdits(want, string(formatted))
127 var diff strings.Builder
128 for _, op := range d {
129 diff.WriteString(op.String())
130 }
131 t.Errorf("suggested fixes failed for %s[%s]:\n%s", file, sf, diff.String())
132 }
133 break
134 }
135 }
136 if !found {
137 t.Errorf("no section for suggested fix %q in %s.golden", sf, file)
138 }
139 }
140 for _, vf := range ar.Files {
141 if _, ok := fixes[vf.Name]; !ok {
142 t.Errorf("%s.golden has section for suggested fix %q, but we didn't produce any fix by that name", file, vf.Name)
143 }
144 }
145 } else {
146 // all suggested fixes are represented by a single file
147
148 var catchallEdits []runner.TextEdit
149 for _, edits := range fixes {
150 catchallEdits = append(catchallEdits, edits...)
151 }
152
153 out := applyEdits(orig, catchallEdits)
154 want := string(ar.Comment)
155
156 formatted, err := format.Source([]byte(out))
157 if err != nil {
158 t.Errorf("%s: error formatting resulting source: %v\n%s", file, err, out)
159 continue
160 }
161 if want != string(formatted) {
162 d := myers.ComputeEdits(want, string(formatted))
163 var diff strings.Builder
164 for _, op := range d {
165 diff.WriteString(op.String())
166 }
167 t.Errorf("suggested fixes failed for %s:\n%s", file, diff.String())
168 }
169 }
170 }
171 }
172
173 func Check(t *testing.T, gopath string, files []string, diagnostics []runner.Diagnostic, facts []runner.TestFact) {
174 relativePath := func(path string) string {
175 cwd, err := os.Getwd()
176 if err != nil {
177 return path
178 }
179 rel, err := filepath.Rel(cwd, path)
180 if err != nil {
181 return path
182 }
183 return rel
184 }
185
186 type key struct {
187 file string
188 line int
189 }
190
191 // the 'files' argument contains a list of all files that were part of the tested package
192 want := make(map[key][]*expect.Note)
193
194 fset := token.NewFileSet()
195 seen := map[string]struct{}{}
196 for _, file := range files {
197 seen[file] = struct{}{}
198
199 notes, err := expect.Parse(fset, file, nil)
200 if err != nil {
201 t.Fatal(err)
202 }
203 for _, note := range notes {
204 k := key{
205 file: file,
206 line: fset.PositionFor(note.Pos, false).Line,
207 }
208 want[k] = append(want[k], note)
209 }
210 }
211
212 for _, diag := range diagnostics {
213 file := diag.Position.Filename
214 if _, ok := seen[file]; !ok {
215 t.Errorf("got diagnostic in file %q, but that file isn't part of the checked package", relativePath(file))
216 return
217 }
218 }
219
220 check := func(posn token.Position, message string, kind string, argIdx int, identifier string) {
221 k := key{posn.Filename, posn.Line}
222 expects := want[k]
223 var unmatched []string
224 for i, exp := range expects {
225 if exp.Name == kind {
226 if kind == "fact" && exp.Args[0] != expect.Identifier(identifier) {
227 continue
228 }
229 matched := false
230 switch arg := exp.Args[argIdx].(type) {
231 case string:
232 matched = strings.Contains(message, arg)
233 case *regexp.Regexp:
234 matched = arg.MatchString(message)
235 default:
236 t.Fatalf("unexpected argument type %T", arg)
237 }
238 if matched {
239 // matched: remove the expectation.
240 expects[i] = expects[len(expects)-1]
241 expects = expects[:len(expects)-1]
242 want[k] = expects
243 return
244 }
245 unmatched = append(unmatched, fmt.Sprintf("%q", exp.Args[argIdx]))
246 }
247 }
248 if unmatched == nil {
249 posn.Filename = relativePath(posn.Filename)
250 t.Errorf("%v: unexpected diag: %v", posn, message)
251 } else {
252 posn.Filename = relativePath(posn.Filename)
253 t.Errorf("%v: diag %q does not match pattern %s",
254 posn, message, strings.Join(unmatched, " or "))
255 }
256 }
257
258 checkDiag := func(posn token.Position, message string) {
259 check(posn, message, "diag", 0, "")
260 }
261
262 checkFact := func(posn token.Position, name, message string) {
263 check(posn, message, "fact", 1, name)
264 }
265
266 // Check the diagnostics match expectations.
267 for _, f := range diagnostics {
268 // TODO(matloob): Support ranges in analysistest.
269 posn := f.Position
270 checkDiag(posn, f.Message)
271 }
272
273 // Check the facts match expectations.
274 for _, fact := range facts {
275 name := fact.ObjectName
276 posn := fact.Position
277 if name == "" {
278 name = "package"
279 posn.Line = 1
280 }
281
282 checkFact(posn, name, fact.FactString)
283 }
284
285 // Reject surplus expectations.
286 //
287 // Sometimes an Analyzer reports two similar diagnostics on a
288 // line with only one expectation. The reader may be confused by
289 // the error message.
290 // TODO(adonovan): print a better error:
291 // "got 2 diagnostics here; each one needs its own expectation".
292 var surplus []string
293 for key, expects := range want {
294 for _, exp := range expects {
295 surplus = append(surplus, fmt.Sprintf("%s:%d: no %s was reported matching %q", relativePath(key.file), key.line, exp.Name, exp.Args))
296 }
297 }
298 sort.Strings(surplus)
299 for _, err := range surplus {
300 t.Errorf("%s", err)
301 }
302 }
303
304 func applyEdits(src []byte, edits []runner.TextEdit) []byte {
305 // This function isn't efficient, but it doesn't have to be.
306
307 edits = slices.Clone(edits)
308 sort.Slice(edits, func(i, j int) bool {
309 if edits[i].Position.Offset < edits[j].Position.Offset {
310 return true
311 }
312 if edits[i].Position.Offset == edits[j].Position.Offset {
313 return edits[i].End.Offset < edits[j].End.Offset
314 }
315 return false
316 })
317
318 out := bytes.Clone(src)
319 offset := 0
320 for _, edit := range edits {
321 start := edit.Position.Offset + offset
322 end := edit.End.Offset + offset
323 if edit.End == (token.Position{}) {
324 end = -1
325 }
326 if len(edit.NewText) == 0 {
327 // pure deletion
328 copy(out[start:], out[end:])
329 out = out[:len(out)-(end-start)]
330 offset -= end - start
331 } else if end == -1 || end == start {
332 // pure insertion
333 tmp := make([]byte, len(out)+len(edit.NewText))
334 copy(tmp, out[:start])
335 copy(tmp[start:], edit.NewText)
336 copy(tmp[start+len(edit.NewText):], out[start:])
337 offset += len(edit.NewText)
338 out = tmp
339 } else if end-start == len(edit.NewText) {
340 // exact replacement
341 copy(out[start:], edit.NewText)
342 } else if end-start < len(edit.NewText) {
343 // replace with longer string
344 growth := len(edit.NewText) - (end - start)
345 tmp := make([]byte, len(out)+growth)
346 copy(tmp, out[:start])
347 copy(tmp[start:], edit.NewText)
348 copy(tmp[start+len(edit.NewText):], out[end:])
349 offset += growth
350 out = tmp
351 } else if end-start > len(edit.NewText) {
352 // replace with shorter string
353 shrinkage := (end - start) - len(edit.NewText)
354
355 copy(out[start:], edit.NewText)
356 copy(out[start+len(edit.NewText):], out[end:])
357 out = out[:len(out)-shrinkage]
358 offset -= shrinkage
359 }
360 }
361
362 // Debug code
363 if false {
364 fmt.Println("input:")
365 fmt.Println(string(src))
366 fmt.Println()
367 fmt.Println("edits:")
368 for _, edit := range edits {
369 fmt.Printf("%d:%d - %d:%d <- %q\n", edit.Position.Line, edit.Position.Column, edit.End.Line, edit.End.Column, edit.NewText)
370 }
371 fmt.Println("output:")
372 fmt.Println(string(out))
373 panic("")
374 }
375
376 return out
377 }