Skip to content

Commit 9cc65bf

Browse files
committed
refactor: unit/assert_snapshot_test more
1 parent 134b4a3 commit 9cc65bf

File tree

1 file changed

+52
-72
lines changed

1 file changed

+52
-72
lines changed

src/assert_snapshot.sh

Lines changed: 52 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,88 @@
11
#!/usr/bin/env bash
2+
# shellcheck disable=SC2155
23

34
function snapshot::match_with_placeholder() {
45
local actual="$1"
56
local snapshot="$2"
67
local placeholder="${BASHUNIT_SNAPSHOT_PLACEHOLDER:-::ignore::}"
78
local token="__BASHUNIT_IGNORE__"
89

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}$"
14-
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
10+
local sanitized="${snapshot//$placeholder/$token}"
11+
local escaped=$(printf '%s' "$sanitized" | sed -e 's/[.[\\^$*+?{}()|]/\\&/g')
12+
local regex="^${escaped//$token/(.|\\n)*}$"
13+
14+
if which perl >/dev/null 2>&1; then
15+
echo "$actual" | REGEX="$regex" perl -0 -e '
16+
my $r = $ENV{REGEX};
17+
my $input = join("", <STDIN>);
18+
exit($input =~ /$r/s ? 0 : 1);
19+
' && return 0 || return 1
2120
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
21+
local fallback=$(printf '%s' "$snapshot" | sed -e "s|$placeholder|.*|g" -e 's/[][\.^$*+?{}|()]/\\&/g')
22+
fallback="^${fallback}$"
23+
echo "$actual" | grep -Eq "$fallback" && return 0 || return 1
3424
fi
3525
}
3626

37-
# shellcheck disable=SC2155
3827
function assert_match_snapshot() {
39-
local actual
40-
actual=$(echo -n "$1" | tr -d '\r')
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
28+
local actual=$(echo -n "$1" | tr -d '\r')
29+
local snapshot_file=$(_resolve_snapshot_file "${2:-}" "${FUNCNAME[1]}")
4930

5031
if [[ ! -f "$snapshot_file" ]]; then
51-
mkdir -p "$(dirname "$snapshot_file")"
52-
echo "$actual" > "$snapshot_file"
53-
54-
state::add_assertions_snapshot
32+
_initialize_snapshot "$snapshot_file" "$actual"
5533
return
5634
fi
5735

58-
local snapshot
59-
snapshot=$(tr -d '\r' < "$snapshot_file")
60-
61-
if ! snapshot::match_with_placeholder "$actual" "$snapshot"; then
62-
local label
63-
label=$(helper::normalize_test_function_name "${FUNCNAME[1]}")
36+
_compare_snapshot "$actual" "$snapshot_file" "${FUNCNAME[1]}"
37+
}
6438

65-
state::add_assertions_failed
66-
console_results::print_failed_snapshot_test "$label" "$snapshot_file"
39+
function assert_match_snapshot_ignore_colors() {
40+
local actual=$(echo -n "$1" | sed 's/\x1B\[[0-9;]*[mK]//g' | tr -d '\r')
41+
local snapshot_file=$(_resolve_snapshot_file "${2:-}" "${FUNCNAME[1]}")
6742

43+
if [[ ! -f "$snapshot_file" ]]; then
44+
_initialize_snapshot "$snapshot_file" "$actual"
6845
return
6946
fi
7047

71-
state::add_assertions_passed
48+
_compare_snapshot "$actual" "$snapshot_file" "${FUNCNAME[1]}"
7249
}
7350

74-
# shellcheck disable=SC2155
75-
function assert_match_snapshot_ignore_colors() {
76-
local actual
77-
actual=$(echo -n "$1" | sed -r 's/\x1B\[[0-9;]*[mK]//g' | tr -d '\r')
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}"
51+
function _resolve_snapshot_file() {
52+
local file_hint="$1"
53+
local func_name="$2"
54+
55+
if [[ -n "$file_hint" ]]; then
56+
echo "$file_hint"
57+
else
58+
local dir="./$(dirname "${BASH_SOURCE[2]}")/snapshots"
59+
local test_file="$(helper::normalize_variable_name "$(basename "${BASH_SOURCE[2]}")")"
60+
local name="$(helper::normalize_variable_name "$func_name").snapshot"
61+
echo "${dir}/${test_file}.${name}"
8562
fi
63+
}
8664

87-
if [[ ! -f "$snapshot_file" ]]; then
88-
mkdir -p "$(dirname "$snapshot_file")"
89-
echo "$actual" > "$snapshot_file"
65+
function _initialize_snapshot() {
66+
local path="$1"
67+
local content="$2"
68+
mkdir -p "$(dirname "$path")"
69+
echo "$content" > "$path"
70+
state::add_assertions_snapshot
71+
}
9072

91-
state::add_assertions_snapshot
92-
return
93-
fi
73+
function _compare_snapshot() {
74+
local actual="$1"
75+
local snapshot_path="$2"
76+
local func_name="$3"
9477

9578
local snapshot
96-
snapshot=$(tr -d '\r' < "$snapshot_file")
79+
snapshot=$(tr -d '\r' < "$snapshot_path")
9780

9881
if ! snapshot::match_with_placeholder "$actual" "$snapshot"; then
99-
local label
100-
label=$(helper::normalize_test_function_name "${FUNCNAME[1]}")
101-
82+
local label=$(helper::normalize_test_function_name "$func_name")
10283
state::add_assertions_failed
103-
console_results::print_failed_snapshot_test "$label" "$snapshot_file"
104-
105-
return
84+
console_results::print_failed_snapshot_test "$label" "$snapshot_path"
85+
return 1
10686
fi
10787

10888
state::add_assertions_passed

0 commit comments

Comments
 (0)