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>
diff --git a/tools/src/cmd/fix-tests/main.go b/tools/src/cmd/fix-tests/main.go
index 86cb459..026556a 100644
--- a/tools/src/cmd/fix-tests/main.go
+++ b/tools/src/cmd/fix-tests/main.go
@@ -173,19 +173,23 @@
fix = func(testSource string) (string, error) {
// We don't know if a or b is the expected, so just try flipping the string
// to the other form.
+
+ if len(b) > len(a) { // Go with the longer match, in case both are found
+ a, b = b, a
+ }
switch {
case strings.Contains(testSource, a):
- testSource = strings.Replace(testSource, a, b, -1)
+ testSource = strings.ReplaceAll(testSource, a, b)
case strings.Contains(testSource, b):
- testSource = strings.Replace(testSource, b, a, -1)
+ testSource = strings.ReplaceAll(testSource, b, a)
default:
// Try escaping for R"(...)" strings
a, b = escape(a), escape(b)
switch {
case strings.Contains(testSource, a):
- testSource = strings.Replace(testSource, a, b, -1)
+ testSource = strings.ReplaceAll(testSource, a, b)
case strings.Contains(testSource, b):
- testSource = strings.Replace(testSource, b, a, -1)
+ testSource = strings.ReplaceAll(testSource, b, a)
default:
return "", fmt.Errorf("Could not fix 'EXPECT_EQ' pattern in '%v'", file)
}