1
0
mirror of https://github.com/golang/go synced 2024-11-18 18:34:40 -07:00

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

View File

@ -79,7 +79,7 @@ func Run(t Testing, dir string, a *analysis.Analyzer, pkgnames ...string) {
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,
// which must have been parsed with comments enabled. Surplus diagnostics
// 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.
type key struct {
file string
@ -133,7 +133,7 @@ func checkDiagnostics(t Testing, pass *analysis.Pass, diagnostics []analysis.Dia
for _, f := range pass.Files {
for _, c := range f.Comments {
posn := pass.Fset.Position(c.Pos())
sanitize(&posn)
sanitize(gopath, &posn)
text := strings.TrimSpace(c.Text())
if !strings.HasPrefix(text, "want") {
continue
@ -156,7 +156,7 @@ func checkDiagnostics(t Testing, pass *analysis.Pass, diagnostics []analysis.Dia
// Check the diagnostics match expectations.
for _, f := range diagnostics {
posn := pass.Fset.Position(f.Pos)
sanitize(&posn)
sanitize(gopath, &posn)
rx, ok := wantErrs[key{posn.Filename, posn.Line}]
if !ok {
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,
// typically a gnarly /tmp directory.
func sanitize(posn *token.Position) {
// TODO(adonovan): port to windows.
if strings.HasPrefix(posn.Filename, "/tmp/") {
if i := strings.Index(posn.Filename, "/src/"); i > 0 {
posn.Filename = posn.Filename[i+len("/src/"):]
}
}
func sanitize(gopath string, posn *token.Position) {
prefix := gopath + string(os.PathSeparator) + "src" + string(os.PathSeparator)
posn.Filename = strings.TrimPrefix(posn.Filename, prefix)
}