go/analysis/analysistest/analysistest: fix test on non-Linux

The test's pathname sanitization heuristically assumed that $TMPDIR
contained /tmp, which is not the case on Darwin or Windows. Now we
pass it the precise directory prefix to strip off.

Fixes golang/go#27877

Change-Id: I85167d721ebb9c4f6d74016a00025fd726939e47
Reviewed-on: https://go-review.googlesource.com/137735
Run-TryBot: Andrew Bonventre <andybons@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
Alan Donovan 2018-09-26 12:43:23 -04:00
parent c756801b01
commit 34cd4017e8
1 changed files with 7 additions and 11 deletions

View File

@ -79,7 +79,7 @@ func Run(t Testing, dir string, a *analysis.Analyzer, pkgnames ...string) {
continue continue
} }
checkDiagnostics(t, pass, diagnostics) checkDiagnostics(t, dir, pass, diagnostics)
} }
} }
@ -123,7 +123,7 @@ func loadPackage(dir, pkgpath string) (*packages.Package, error) {
// specified by 'want "..."' comments in the package's source files, // specified by 'want "..."' comments in the package's source files,
// which must have been parsed with comments enabled. Surplus diagnostics // which must have been parsed with comments enabled. Surplus diagnostics
// and unmatched expectations are reported as errors to the Testing. // and unmatched expectations are reported as errors to the Testing.
func checkDiagnostics(t Testing, pass *analysis.Pass, diagnostics []analysis.Diagnostic) { func checkDiagnostics(t Testing, gopath string, pass *analysis.Pass, diagnostics []analysis.Diagnostic) {
// Read expectations out of comments. // Read expectations out of comments.
type key struct { type key struct {
file string file string
@ -133,7 +133,7 @@ func checkDiagnostics(t Testing, pass *analysis.Pass, diagnostics []analysis.Dia
for _, f := range pass.Files { for _, f := range pass.Files {
for _, c := range f.Comments { for _, c := range f.Comments {
posn := pass.Fset.Position(c.Pos()) posn := pass.Fset.Position(c.Pos())
sanitize(&posn) sanitize(gopath, &posn)
text := strings.TrimSpace(c.Text()) text := strings.TrimSpace(c.Text())
if !strings.HasPrefix(text, "want") { if !strings.HasPrefix(text, "want") {
continue continue
@ -156,7 +156,7 @@ func checkDiagnostics(t Testing, pass *analysis.Pass, diagnostics []analysis.Dia
// Check the diagnostics match expectations. // Check the diagnostics match expectations.
for _, f := range diagnostics { for _, f := range diagnostics {
posn := pass.Fset.Position(f.Pos) posn := pass.Fset.Position(f.Pos)
sanitize(&posn) sanitize(gopath, &posn)
rx, ok := wantErrs[key{posn.Filename, posn.Line}] rx, ok := wantErrs[key{posn.Filename, posn.Line}]
if !ok { if !ok {
t.Errorf("%v: unexpected diagnostic: %v", posn, f.Message) t.Errorf("%v: unexpected diagnostic: %v", posn, f.Message)
@ -174,11 +174,7 @@ func checkDiagnostics(t Testing, pass *analysis.Pass, diagnostics []analysis.Dia
// sanitize removes the GOPATH portion of the filename, // sanitize removes the GOPATH portion of the filename,
// typically a gnarly /tmp directory. // typically a gnarly /tmp directory.
func sanitize(posn *token.Position) { func sanitize(gopath string, posn *token.Position) {
// TODO(adonovan): port to windows. prefix := gopath + string(os.PathSeparator) + "src" + string(os.PathSeparator)
if strings.HasPrefix(posn.Filename, "/tmp/") { posn.Filename = strings.TrimPrefix(posn.Filename, prefix)
if i := strings.Index(posn.Filename, "/src/"); i > 0 {
posn.Filename = posn.Filename[i+len("/src/"):]
}
}
} }