Skip to content

Commit

Permalink
cmd/fiximports: fix 'go list' error formatting
Browse files Browse the repository at this point in the history
Error positions should be printed, when specified.

Also, made main_test less picky about whitespace before and after
error output.

After this change, the test for cmd/fiximports should pass before and
after CL 210938.

Updates golang/go#36087

Change-Id: I681d1ee07f7f19a0d9716b88678e2737f4c691de
Reviewed-on: https://go-review.googlesource.com/c/tools/+/211337
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
  • Loading branch information
Jay Conrod committed Dec 17, 2019
1 parent 38570b7 commit 210e553
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
9 changes: 8 additions & 1 deletion cmd/fiximports/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func fiximports(packages ...string) bool {
strings.Contains(msg, "expects import") {
// don't show the very errors we're trying to fix
} else {
fmt.Fprintln(stderr, msg)
fmt.Fprintln(stderr, p.Error)
}
}

Expand Down Expand Up @@ -467,6 +467,13 @@ type packageError struct {
Err string // the error itself
}

func (e packageError) Error() string {
if e.Pos != "" {
return e.Pos + ": " + e.Err
}
return e.Err
}

// list runs 'go list' with the specified arguments and returns the
// metadata for matching packages.
func list(args ...string) ([]*listPackage, error) {
Expand Down
7 changes: 4 additions & 3 deletions cmd/fiximports/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,20 @@ import (
test.wantStderr = strings.Replace(test.wantStderr, `testdata/src/old.com/bad/bad.go`, `testdata\src\old.com\bad\bad.go`, -1)
test.wantStderr = strings.Replace(test.wantStderr, `testdata/src/fruit.io/banana/banana.go`, `testdata\src\fruit.io\banana\banana.go`, -1)
}
test.wantStderr = strings.TrimSpace(test.wantStderr)

// Check status code.
if fiximports(test.packages...) != test.wantOK {
t.Errorf("#%d. fiximports() = %t", i, !test.wantOK)
}

// Compare stderr output.
if got := stderr.(*bytes.Buffer).String(); got != test.wantStderr {
if got := strings.TrimSpace(stderr.(*bytes.Buffer).String()); got != test.wantStderr {
if strings.Contains(got, "vendor/golang_org/x/text/unicode/norm") {
t.Skip("skipping known-broken test; see golang.org/issue/17417")
}
t.Errorf("#%d. stderr: got <<%s>>, want <<%s>>",
i, stderr, test.wantStderr)
t.Errorf("#%d. stderr: got <<\n%s\n>>, want <<\n%s\n>>",
i, got, test.wantStderr)
}

// Compare rewrites.
Expand Down

0 comments on commit 210e553

Please sign in to comment.