xerrors: fix TestErrorf false positive when top-level dir is renamed The rePath pattern will typically replace two matches with "/path.": one with the test's import path, like " /golang.org/x/xerrors_test.", and a file path like " /Users/gopher/golang.org/x/xerrors/fmt_test.". The former will always end with "/xerrors_test." as long as the module path is still "golang.org/x/xerrors" and the TestErrorf test is inside an xerrors_test package. The latter may not match xerrors.*test if the top-level directory isn't named "xerrors". But it will always end with "/fmt_test." as long as the TestErrorf test is still inside the file named "fmt_test.go". So look for those two patterns to fix false positive failures when the git repository is cloned to a custom path without any "xerrors" in it. Change-Id: I4d95121734337050309c2d989afd8f82430a2f31 Reviewed-on: https://go-review.googlesource.com/c/xerrors/+/609380 Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
diff --git a/fmt_test.go b/fmt_test.go index 99d945f..1ee7e6f 100644 --- a/fmt_test.go +++ b/fmt_test.go
@@ -555,14 +555,14 @@ func (panicValue) String() string { panic("panic") } -var rePath = regexp.MustCompile(`( [^ ]*)xerrors.*test\.`) -var reLine = regexp.MustCompile(":[0-9]*\n?$") +var rePath = regexp.MustCompile(`( [^ ]+)\/(xerrors_test|fmt_test)\.`) +var reLine = regexp.MustCompile(":[0-9]+\n?$") func cleanPath(s string) string { s = rePath.ReplaceAllString(s, "/path.") s = reLine.ReplaceAllString(s, ":xxx") - s = strings.Replace(s, "\n ", "", -1) - s = strings.Replace(s, " /", "/", -1) + s = strings.ReplaceAll(s, "\n ", "") + s = strings.ReplaceAll(s, " /", "/") return s }