Skip to content

Commit b5e55d1

Browse files
committed
go/analysis/analysistest: give better hint in SuggestedFix assertion
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>
1 parent a807ccf commit b5e55d1

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

go/analysis/analysistest/analysistest.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,34 @@ type Testing interface {
9898
// println()
9999
// }
100100
// }
101+
//
102+
// # Conflicts
103+
//
104+
// A single analysis pass may offer two or more suggested fixes that
105+
// (1) conflict but are nonetheless logically composable, (e.g.
106+
// because both update the import declaration), or (2) are
107+
// fundamentally incompatible (e.g. alternative fixes to the same
108+
// statement).
109+
//
110+
// It is up to the driver to decide how to apply such fixes. A
111+
// sophisticated driver could attempt to resolve conflicts of the
112+
// first kind, but this test driver simply reports the fact of the
113+
// conflict with the expectation that the user will split their tests
114+
// into nonconflicting parts.
115+
//
116+
// Conflicts of the second kind can be avoided by giving the
117+
// alternative fixes different names (SuggestedFix.Message) and using
118+
// a multi-section .txtar file with a named section for each
119+
// alternative fix.
120+
//
121+
// Analyzers that compute fixes from a textual diff of the
122+
// before/after file contents (instead of directly from syntax tree
123+
// positions) may produce fixes that, although logically
124+
// non-conflicting, nonetheless conflict due to the particulars of the
125+
// diff algorithm. In such cases it may suffice to introduce
126+
// sufficient separation of the statements in the test input so that
127+
// the computed diffs do not overlap. If that fails, break the test
128+
// into smaller parts.
101129
func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns ...string) []*Result {
102130
r := Run(t, dir, a, patterns...)
103131

@@ -135,7 +163,7 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns
135163
continue
136164
}
137165
if _, ok := fileContents[file]; !ok {
138-
contents, err := ioutil.ReadFile(file.Name())
166+
contents, err := os.ReadFile(file.Name())
139167
if err != nil {
140168
t.Errorf("error reading %s: %v", file.Name(), err)
141169
}
@@ -186,7 +214,7 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns
186214
found = true
187215
out, err := diff.ApplyBytes(orig, edits)
188216
if err != nil {
189-
t.Errorf("%s: error applying fixes: %v", file.Name(), err)
217+
t.Errorf("%s: error applying fixes: %v (see possible explanations at RunWithSuggestedFixes)", file.Name(), err)
190218
continue
191219
}
192220
// the file may contain multiple trailing
@@ -220,7 +248,7 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns
220248

221249
out, err := diff.ApplyBytes(orig, catchallEdits)
222250
if err != nil {
223-
t.Errorf("%s: error applying fixes: %v", file.Name(), err)
251+
t.Errorf("%s: error applying fixes: %v (see possible explanations at RunWithSuggestedFixes)", file.Name(), err)
224252
continue
225253
}
226254
want := string(ar.Comment)

0 commit comments

Comments
 (0)