Skip to content

Commit 6b94272

Browse files
committed
rollback: src/assert_snapshot.sh
1 parent a39c96f commit 6b94272

File tree

1 file changed

+53
-42
lines changed

1 file changed

+53
-42
lines changed

src/assert_snapshot.sh

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,91 +6,102 @@ function snapshot::match_with_placeholder() {
66
local placeholder="${BASHUNIT_SNAPSHOT_PLACEHOLDER:-::ignore::}"
77
local token="__BASHUNIT_IGNORE__"
88

9-
local normalized="${snapshot//$placeholder/$token}"
10-
local escaped
11-
escaped=$(printf '%s' "$normalized" | sed -e 's/[.[\\^$*+?{}()|]/\\&/g')
12-
local regex="^${escaped//$token/(.|\\n)*}$"
9+
local sanitized_snapshot="${snapshot//$placeholder/$token}"
10+
local regex
11+
regex=$(printf '%s' "$sanitized_snapshot" | sed -e 's/[.[\\^$*+?{}()|]/\\&/g')
12+
regex="${regex//$token/(.|\\n)*}"
13+
regex="^${regex}$"
1314

1415
if command -v perl >/dev/null 2>&1; then
15-
REGEX="$regex" perl -0 -e '
16-
my $r = $ENV{REGEX};
17-
my $input = do { local $/; <STDIN> };
18-
exit($input =~ /$r/s ? 0 : 1);
19-
' <<< "$actual"
20-
return
21-
fi
22-
23-
if grep -P '' </dev/null >/dev/null 2>&1; then
24-
printf '%s\0' "$actual" | grep -Pzo "$regex" >/dev/null
25-
return
26-
fi
27-
28-
# fallback: only supports single-line ignores
29-
local pattern="${snapshot//$placeholder/.+}"
30-
pattern=$(printf '%s' "$pattern" | sed -e 's/[][\.^$*+?{}|()]/\\&/g')
31-
printf '%s' "$actual" | grep -zEq "^${pattern}$"
32-
}
33-
34-
function assert_snapshot::_normalize_snapshot_file() {
35-
local snapshot_file="$1"
36-
local funcname="${FUNCNAME[2]}"
37-
local source_file="${BASH_SOURCE[2]}"
38-
local directory test_file snapshot_name
39-
40-
if [[ -n "$snapshot_file" ]]; then
41-
echo "$snapshot_file"
42-
return
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
4334
fi
44-
45-
directory="$(dirname "$source_file")/snapshots"
46-
test_file="$(helper::normalize_variable_name "$(basename "$source_file")")"
47-
snapshot_name="$(helper::normalize_variable_name "$funcname").snapshot"
48-
49-
echo "${directory}/${test_file}.${snapshot_name}"
5035
}
5136

37+
# shellcheck disable=SC2155
5238
function assert_match_snapshot() {
53-
local actual snapshot_file snapshot
39+
local actual
5440
actual=$(echo -n "$1" | tr -d '\r')
55-
snapshot_file=$(assert_snapshot::_normalize_snapshot_file "${2-}")
41+
local snapshot_file="${2-}"
42+
43+
if [[ -z "$snapshot_file" ]]; then
44+
local directory="./$(dirname "${BASH_SOURCE[1]}")/snapshots"
45+
local test_file="$(helper::normalize_variable_name "$(basename "${BASH_SOURCE[1]}")")"
46+
local snapshot_name="$(helper::normalize_variable_name "${FUNCNAME[1]}").snapshot"
47+
snapshot_file="${directory}/${test_file}.${snapshot_name}"
48+
fi
5649

5750
if [[ ! -f "$snapshot_file" ]]; then
5851
mkdir -p "$(dirname "$snapshot_file")"
5952
echo "$actual" > "$snapshot_file"
53+
6054
state::add_assertions_snapshot
6155
return
6256
fi
6357

58+
local snapshot
6459
snapshot=$(tr -d '\r' < "$snapshot_file")
60+
6561
if ! snapshot::match_with_placeholder "$actual" "$snapshot"; then
6662
local label
6763
label=$(helper::normalize_test_function_name "${FUNCNAME[1]}")
64+
6865
state::add_assertions_failed
6966
console_results::print_failed_snapshot_test "$label" "$snapshot_file"
67+
7068
return
7169
fi
7270

7371
state::add_assertions_passed
7472
}
7573

74+
# shellcheck disable=SC2155
7675
function assert_match_snapshot_ignore_colors() {
77-
local actual snapshot_file snapshot
76+
local actual
7877
actual=$(echo -n "$1" | sed -r 's/\x1B\[[0-9;]*[mK]//g' | tr -d '\r')
79-
snapshot_file=$(assert_snapshot::_normalize_snapshot_file "${2-}")
78+
79+
local snapshot_file="${2-}"
80+
if [[ -z "$snapshot_file" ]]; then
81+
local directory="./$(dirname "${BASH_SOURCE[1]}")/snapshots"
82+
local test_file="$(helper::normalize_variable_name "$(basename "${BASH_SOURCE[1]}")")"
83+
local snapshot_name="$(helper::normalize_variable_name "${FUNCNAME[1]}").snapshot"
84+
snapshot_file="${directory}/${test_file}.${snapshot_name}"
85+
fi
8086

8187
if [[ ! -f "$snapshot_file" ]]; then
8288
mkdir -p "$(dirname "$snapshot_file")"
8389
echo "$actual" > "$snapshot_file"
90+
8491
state::add_assertions_snapshot
8592
return
8693
fi
8794

95+
local snapshot
8896
snapshot=$(tr -d '\r' < "$snapshot_file")
97+
8998
if ! snapshot::match_with_placeholder "$actual" "$snapshot"; then
9099
local label
91100
label=$(helper::normalize_test_function_name "${FUNCNAME[1]}")
101+
92102
state::add_assertions_failed
93103
console_results::print_failed_snapshot_test "$label" "$snapshot_file"
104+
94105
return
95106
fi
96107

0 commit comments

Comments
 (0)