Skip to content

Commit

Permalink
go/analysis/analysistest: give better hint in SuggestedFix assertion
Browse files Browse the repository at this point in the history
Also, better documentation on the underlying cause.

Change-Id: I0ee93b6e9f2ada52d9a32a322b77fda783ddf076
Reviewed-on: https://go-review.googlesource.com/c/tools/+/525215
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
  • Loading branch information
adonovan committed Sep 5, 2023
1 parent a807ccf commit b5e55d1
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions go/analysis/analysistest/analysistest.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,34 @@ type Testing interface {
// println()
// }
// }
//
// # Conflicts
//
// A single analysis pass may offer two or more suggested fixes that
// (1) conflict but are nonetheless logically composable, (e.g.
// because both update the import declaration), or (2) are
// fundamentally incompatible (e.g. alternative fixes to the same
// statement).
//
// It is up to the driver to decide how to apply such fixes. A
// sophisticated driver could attempt to resolve conflicts of the
// first kind, but this test driver simply reports the fact of the
// conflict with the expectation that the user will split their tests
// into nonconflicting parts.
//
// Conflicts of the second kind can be avoided by giving the
// alternative fixes different names (SuggestedFix.Message) and using
// a multi-section .txtar file with a named section for each
// alternative fix.
//
// Analyzers that compute fixes from a textual diff of the
// before/after file contents (instead of directly from syntax tree
// positions) may produce fixes that, although logically
// non-conflicting, nonetheless conflict due to the particulars of the
// diff algorithm. In such cases it may suffice to introduce
// sufficient separation of the statements in the test input so that
// the computed diffs do not overlap. If that fails, break the test
// into smaller parts.
func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns ...string) []*Result {
r := Run(t, dir, a, patterns...)

Expand Down Expand Up @@ -135,7 +163,7 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns
continue
}
if _, ok := fileContents[file]; !ok {
contents, err := ioutil.ReadFile(file.Name())
contents, err := os.ReadFile(file.Name())
if err != nil {
t.Errorf("error reading %s: %v", file.Name(), err)
}
Expand Down Expand Up @@ -186,7 +214,7 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns
found = true
out, err := diff.ApplyBytes(orig, edits)
if err != nil {
t.Errorf("%s: error applying fixes: %v", file.Name(), err)
t.Errorf("%s: error applying fixes: %v (see possible explanations at RunWithSuggestedFixes)", file.Name(), err)
continue
}
// the file may contain multiple trailing
Expand Down Expand Up @@ -220,7 +248,7 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns

out, err := diff.ApplyBytes(orig, catchallEdits)
if err != nil {
t.Errorf("%s: error applying fixes: %v", file.Name(), err)
t.Errorf("%s: error applying fixes: %v (see possible explanations at RunWithSuggestedFixes)", file.Name(), err)
continue
}
want := string(ar.Comment)
Expand Down

0 comments on commit b5e55d1

Please sign in to comment.