|
5 | 5 | "fmt"
|
6 | 6 | "os"
|
7 | 7 | "path/filepath"
|
8 |
| - "runtime" |
| 8 | + "regexp" |
9 | 9 | "strings"
|
10 | 10 | "testing"
|
11 | 11 | "time"
|
@@ -62,20 +62,40 @@ func testErrorMessages(t *testing.T, filename string) {
|
62 | 62 | actual := strings.TrimRight(buf.String(), "\n")
|
63 | 63 |
|
64 | 64 | // Check whether the error is as expected.
|
65 |
| - if canonicalizeErrors(actual) != canonicalizeErrors(expected) { |
| 65 | + if !matchErrors(t, expected, actual) { |
66 | 66 | t.Errorf("expected error:\n%s\ngot:\n%s", indentText(expected, "> "), indentText(actual, "> "))
|
67 | 67 | }
|
68 | 68 | }
|
69 | 69 |
|
70 |
| -func canonicalizeErrors(text string) string { |
71 |
| - // Fix for Windows: replace all backslashes with forward slashes so that |
72 |
| - // paths will be the same as on POSIX systems. |
73 |
| - // (It may also change some other backslashes, but since this is only for |
74 |
| - // comparing text it should be fine). |
75 |
| - if runtime.GOOS == "windows" { |
76 |
| - text = strings.ReplaceAll(text, "\\", "/") |
| 70 | +func matchErrors(t *testing.T, pattern, actual string) bool { |
| 71 | + patternLines := strings.Split(pattern, "\n") |
| 72 | + actualLines := strings.Split(actual, "\n") |
| 73 | + if len(patternLines) != len(actualLines) { |
| 74 | + return false |
77 | 75 | }
|
78 |
| - return text |
| 76 | + for i, patternLine := range patternLines { |
| 77 | + indices := regexp.MustCompile(`\{\{.*?\}\}`).FindAllStringIndex(patternLine, -1) |
| 78 | + patternParts := []string{"^"} |
| 79 | + lastStop := 0 |
| 80 | + for _, startstop := range indices { |
| 81 | + start := startstop[0] |
| 82 | + stop := startstop[1] |
| 83 | + patternParts = append(patternParts, |
| 84 | + regexp.QuoteMeta(patternLine[lastStop:start]), |
| 85 | + patternLine[start+2:stop-2]) |
| 86 | + lastStop = stop |
| 87 | + } |
| 88 | + patternParts = append(patternParts, regexp.QuoteMeta(patternLine[lastStop:]), "$") |
| 89 | + pattern := strings.Join(patternParts, "") |
| 90 | + re, err := regexp.Compile(pattern) |
| 91 | + if err != nil { |
| 92 | + t.Fatalf("could not compile regexp for %#v: %v", patternLine, err) |
| 93 | + } |
| 94 | + if !re.MatchString(actualLines[i]) { |
| 95 | + return false |
| 96 | + } |
| 97 | + } |
| 98 | + return true |
79 | 99 | }
|
80 | 100 |
|
81 | 101 | // Indent the given text with a given indentation string.
|
|
0 commit comments