From f3343096b916c23ef279a0dca7a02f709db0fcea Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Wed, 18 Nov 2026 12:19:00 +0000 Subject: [PATCH 1/2] acc: word-bound all-digit literal replacements A script registers a resource id as a literal replacement, applied before the harness patterns. A short id (a LITE workspace fake allocates two-digit ones) then also matches inside UUIDs and timestamps and cuts them up. Match an all-digit literal only at word boundaries, in libs/testdiff and its repls.py mirror. Co-authored-by: Isaac --- acceptance/bin/repls.py | 10 ++++++++-- libs/testdiff/replacement.go | 19 +++++++++++++++++-- libs/testdiff/replacement_test.go | 7 +++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/acceptance/bin/repls.py b/acceptance/bin/repls.py index f24da6ce638..08bb3b1612e 100644 --- a/acceptance/bin/repls.py +++ b/acceptance/bin/repls.py @@ -28,6 +28,12 @@ def read_entries(): return result +def literal_pattern(value): + """Mirror appendLiteral in libs/testdiff: a short numeric id must not match inside UUIDs.""" + pattern = re.escape(value) + return rf"\b{pattern}\b" if value.isdigit() else pattern + + def read_repls(): """Return (pattern, replacement, distinct) tuples in application order.""" entries = [] @@ -45,8 +51,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..b4ae901d36e 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 + // UUIDs, timestamps and ports and cut them 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..95836cf843e 100644 --- a/libs/testdiff/replacement_test.go +++ b/libs/testdiff/replacement_test.go @@ -14,6 +14,13 @@ func TestReplacement_Literal(t *testing.T) { assert.Equal(t, `[replacement]`, repls.Replace(`foobar`)) } +func TestReplacement_NumericLiteralIsWordBounded(t *testing.T) { + var repls ReplacementsContext + + repls.Set(`21`, `[DEPLOYMENT_ID]`) + assert.Equal(t, `deployments/[DEPLOYMENT_ID] "[DEPLOYMENT_ID]" 1d2f21aa 3210`, repls.Replace(`deployments/21 "21" 1d2f21aa 3210`)) +} + func TestReplacement_Encoded(t *testing.T) { var repls ReplacementsContext From e8094cbbd1f47f73f2a98668b5d235e2f98a8f4e Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Wed, 18 Nov 2026 13:07:57 +0000 Subject: [PATCH 2/2] Address review: narrow the comment, table-driven test, ASCII-only digit check Co-authored-by: Isaac --- acceptance/bin/repls.py | 10 ++++++++-- libs/testdiff/replacement.go | 4 ++-- libs/testdiff/replacement_test.go | 23 +++++++++++++++++++---- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/acceptance/bin/repls.py b/acceptance/bin/repls.py index 08bb3b1612e..75daf33536c 100644 --- a/acceptance/bin/repls.py +++ b/acceptance/bin/repls.py @@ -29,9 +29,15 @@ def read_entries(): def literal_pattern(value): - """Mirror appendLiteral in libs/testdiff: a short numeric id must not match inside UUIDs.""" + 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 value.isdigit() else pattern + return rf"\b{pattern}\b" if re.fullmatch("[0-9]+", value) else pattern def read_repls(): diff --git a/libs/testdiff/replacement.go b/libs/testdiff/replacement.go index b4ae901d36e..26cd0338a86 100644 --- a/libs/testdiff/replacement.go +++ b/libs/testdiff/replacement.go @@ -93,8 +93,8 @@ 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 - // UUIDs, timestamps and ports and cut them up. + // 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` } diff --git a/libs/testdiff/replacement_test.go b/libs/testdiff/replacement_test.go index 95836cf843e..2d323e8ab0c 100644 --- a/libs/testdiff/replacement_test.go +++ b/libs/testdiff/replacement_test.go @@ -15,10 +15,25 @@ func TestReplacement_Literal(t *testing.T) { } func TestReplacement_NumericLiteralIsWordBounded(t *testing.T) { - var repls ReplacementsContext - - repls.Set(`21`, `[DEPLOYMENT_ID]`) - assert.Equal(t, `deployments/[DEPLOYMENT_ID] "[DEPLOYMENT_ID]" 1d2f21aa 3210`, repls.Replace(`deployments/21 "21" 1d2f21aa 3210`)) + 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) {