diff --git a/acceptance/bin/repls.py b/acceptance/bin/repls.py index f24da6ce638..75daf33536c 100644 --- a/acceptance/bin/repls.py +++ b/acceptance/bin/repls.py @@ -28,6 +28,18 @@ def read_entries(): return result +def literal_pattern(value): + r"""Mirror appendLiteral in libs/testdiff: a short numeric id must not match inside UUIDs. + + >>> literal_pattern("21") + '\\b21\\b' + >>> literal_pattern("a.1") + 'a\\.1' + """ + pattern = re.escape(value) + return rf"\b{pattern}\b" if re.fullmatch("[0-9]+", value) else pattern + + def read_repls(): """Return (pattern, replacement, distinct) tuples in application order.""" entries = [] @@ -45,8 +57,8 @@ def read_repls(): # values with quotes or backslashes are replaced inside JSON output as well. encoded = json.dumps(literal, ensure_ascii=False)[1:-1] if encoded != literal: - entries.append((order, re.escape(encoded), new, False)) - entries.append((order, re.escape(literal), new, False)) + entries.append((order, literal_pattern(encoded), new, False)) + entries.append((order, literal_pattern(literal), new, False)) # Stable sort: replacements with the same order are applied in the order they were added. entries.sort(key=lambda entry: entry[0]) diff --git a/libs/testdiff/replacement.go b/libs/testdiff/replacement.go index 6df2ade8434..26cd0338a86 100644 --- a/libs/testdiff/replacement.go +++ b/libs/testdiff/replacement.go @@ -91,9 +91,15 @@ func (r *ReplacementsContext) append(pattern *regexp.Regexp, replacement string, } func (r *ReplacementsContext) appendLiteral(old, new string, order int) { + // Transform the input strings such that they can be used as literal strings in regular expressions. + pattern := regexp.QuoteMeta(old) + // A short numeric id (Litebox allocates two-digit ones) would otherwise also match inside a + // longer alphanumeric run, such as a UUID segment, and cut it up. + if isAllDigits(old) { + pattern = `\b` + pattern + `\b` + } r.append( - // Transform the input strings such that they can be used as literal strings in regular expressions. - regexp.MustCompile(regexp.QuoteMeta(old)), + regexp.MustCompile(pattern), // Transform the replacement string such that `$` is interpreted as a literal dollar sign. // For more information about how the replacement string is used, see [regexp.Regexp.Expand]. strings.ReplaceAll(new, `$`, `$$`), @@ -101,6 +107,15 @@ func (r *ReplacementsContext) appendLiteral(old, new string, order int) { ) } +func isAllDigits(s string) bool { + for _, c := range s { + if c < '0' || c > '9' { + return false + } + } + return s != "" +} + func (r *ReplacementsContext) Set(old, new string) { r.SetWithOrder(old, new, 0) } diff --git a/libs/testdiff/replacement_test.go b/libs/testdiff/replacement_test.go index 53e025b4ff5..2d323e8ab0c 100644 --- a/libs/testdiff/replacement_test.go +++ b/libs/testdiff/replacement_test.go @@ -14,6 +14,28 @@ func TestReplacement_Literal(t *testing.T) { assert.Equal(t, `[replacement]`, repls.Replace(`foobar`)) } +func TestReplacement_NumericLiteralIsWordBounded(t *testing.T) { + tests := []struct { + name, literal, input, want string + }{ + {"path segment", "21", "deployments/21", "deployments/[ID]"}, + {"quoted", "21", `"21"`, `"[ID]"`}, + {"inside a uuid", "21", "842521b7-a159-4c21-849b-24f82c5ce190", "[UUID]"}, + {"inside a longer number", "21", "3210 1213", "3210 1213"}, + {"next to a word character", "21", "job_21 v21", "job_21 v21"}, + {"timestamp field still matches", "21", "10:21:05", "10:[ID]:05"}, + {"non-numeric literal matches anywhere", "ab", "cabd", "c[ID]d"}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + var repls ReplacementsContext + repls.SetWithOrder(tc.literal, "[ID]", -100) + PrepareReplacementsUUID(t, &repls) + assert.Equal(t, tc.want, repls.Replace(tc.input)) + }) + } +} + func TestReplacement_Encoded(t *testing.T) { var repls ReplacementsContext