tools: Improve fix-tests

Handle cases where the expected and got strings are both substrings of the test source. In this situation its better to use the longer option.

Fixes cases where fix-tests would keep on adding junk to an EXPECT_EQ string

Change-Id: I800d0d08178d01743b3587527830aefce3f3152e
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/54240
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
This commit is contained in:
Ben Clayton 2021-06-15 10:22:27 +00:00 committed by Tint LUCI CQ
parent 1a14f2093c
commit d80bb9d997
1 changed files with 8 additions and 4 deletions

View File

@ -173,19 +173,23 @@ func processFailure(test, wd, failure string) error {
fix = func(testSource string) (string, error) { fix = func(testSource string) (string, error) {
// We don't know if a or b is the expected, so just try flipping the string // We don't know if a or b is the expected, so just try flipping the string
// to the other form. // to the other form.
if len(b) > len(a) { // Go with the longer match, in case both are found
a, b = b, a
}
switch { switch {
case strings.Contains(testSource, a): case strings.Contains(testSource, a):
testSource = strings.Replace(testSource, a, b, -1) testSource = strings.ReplaceAll(testSource, a, b)
case strings.Contains(testSource, b): case strings.Contains(testSource, b):
testSource = strings.Replace(testSource, b, a, -1) testSource = strings.ReplaceAll(testSource, b, a)
default: default:
// Try escaping for R"(...)" strings // Try escaping for R"(...)" strings
a, b = escape(a), escape(b) a, b = escape(a), escape(b)
switch { switch {
case strings.Contains(testSource, a): case strings.Contains(testSource, a):
testSource = strings.Replace(testSource, a, b, -1) testSource = strings.ReplaceAll(testSource, a, b)
case strings.Contains(testSource, b): case strings.Contains(testSource, b):
testSource = strings.Replace(testSource, b, a, -1) testSource = strings.ReplaceAll(testSource, b, a)
default: default:
return "", fmt.Errorf("Could not fix 'EXPECT_EQ' pattern in '%v'", file) return "", fmt.Errorf("Could not fix 'EXPECT_EQ' pattern in '%v'", file)
} }