Skip to content

Commit 4f09fe5

Browse files
committed
go/analysis/analysistest: Ignore error when...
...changed file cannot be formatted There are instances when the diagnostics are reported correctly by the analyzer, but the suggested fixes are wrong, and result in code that cannot be formatted by `format.Source` (maybe due to syntax errors in the edited code). Right now, in those cases, the test runner ignores the failed tests and moves on to the next test, giving an impression that the tests have passed. This commit fixes that. Signed-off-by: Sourya Vatsyayan <[email protected]>
1 parent 2caf765 commit 4f09fe5

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

go/analysis/analysistest/analysistest.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ var TestData = func() string {
6565
// Testing is an abstraction of a *testing.T.
6666
type Testing interface {
6767
Errorf(format string, args ...interface{})
68+
Skipf(format string, args ...interface{})
6869
}
6970

7071
// RunWithSuggestedFixes behaves like Run, but additionally verifies suggested fixes.
@@ -183,7 +184,7 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns
183184
want := string(bytes.TrimRight(vf.Data, "\n")) + "\n"
184185
formatted, err := format.Source([]byte(out))
185186
if err != nil {
186-
continue
187+
t.Skipf("Could not format edited file %s. Error: %s. Continuing nevertheless\n", file.Name(), err)
187188
}
188189
if want != string(formatted) {
189190
d := myers.ComputeEdits("", want, string(formatted))
@@ -209,6 +210,7 @@ func RunWithSuggestedFixes(t Testing, dir string, a *analysis.Analyzer, patterns
209210

210211
formatted, err := format.Source([]byte(out))
211212
if err != nil {
213+
t.Errorf("Could not format edited file %s. Error: %s", file.Name(), err)
212214
continue
213215
}
214216
if want != string(formatted) {

go/analysis/analysistest/analysistest_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,7 @@ type errorfunc func(string)
141141
func (f errorfunc) Errorf(format string, args ...interface{}) {
142142
f(fmt.Sprintf(format, args...))
143143
}
144+
145+
func (f errorfunc) Skipf(format string, args ...interface{}) {
146+
f(fmt.Sprintf(format, args...))
147+
}

0 commit comments

Comments
 (0)