Skip to content

Commit 1b042eb

Browse files
committed
fix: support match_with_placeholder without perl
1 parent 7fd0056 commit 1b042eb

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

src/assert_snapshot.sh

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env bash
22

3-
43
function snapshot::match_with_placeholder() {
54
local actual="$1"
65
local snapshot="$2"
@@ -13,11 +12,26 @@ function snapshot::match_with_placeholder() {
1312
regex="${regex//$token/(.|\\n)*}"
1413
regex="^${regex}$"
1514

16-
if REGEX="$regex" perl -0 -e 'my $r=$ENV{REGEX}; exit((join("",<>)) =~ /$r/s ? 0 : 1);' <<< "$actual"; then
17-
return 0
15+
if command -v perl >/dev/null 2>&1; then
16+
if REGEX="$regex" perl -0 -e 'my $r=$ENV{REGEX}; exit((join("",<>)) =~ /$r/s ? 0 : 1);' <<< "$actual"; then
17+
return 0
18+
else
19+
return 1
20+
fi
21+
else
22+
# fallback: only supports single-line ignores
23+
local fallback_pattern
24+
fallback_pattern=$(printf '%s' "$snapshot" | sed "s|$placeholder|.*|g")
25+
# escape other special regex chars
26+
fallback_pattern=$(printf '%s' "$fallback_pattern" | sed -e 's/[][\.^$*+?{}|()]/\\&/g')
27+
fallback_pattern="^${fallback_pattern}$"
28+
29+
if printf '%s\n' "$actual" | grep -Eq "$fallback_pattern"; then
30+
return 0
31+
else
32+
return 1
33+
fi
1834
fi
19-
20-
return 1
2135
}
2236

2337
function assert_match_snapshot() {

0 commit comments

Comments
 (0)