Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions acceptance/bin/repls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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])
Expand Down
19 changes: 17 additions & 2 deletions libs/testdiff/replacement.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,31 @@ 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, `$`, `$$`),
order,
)
}

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)
}
Expand Down
22 changes: 22 additions & 0 deletions libs/testdiff/replacement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading