|
1 | 1 | #!/usr/bin/env bash |
| 2 | +# shellcheck disable=SC2155 |
2 | 3 |
|
3 | | -function snapshot::match_with_placeholder() { |
4 | | - local actual="$1" |
5 | | - local snapshot="$2" |
6 | | - local placeholder="${BASHUNIT_SNAPSHOT_PLACEHOLDER:-::ignore::}" |
7 | | - local token="__BASHUNIT_IGNORE__" |
| 4 | +function assert_match_snapshot() { |
| 5 | + local actual=$(echo -n "$1" | tr -d '\r') |
| 6 | + local snapshot_file=$(snapshot::resolve_file "${2:-}" "${FUNCNAME[1]}") |
8 | 7 |
|
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 |
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 |
| 8 | + if [[ ! -f "$snapshot_file" ]]; then |
| 9 | + snapshot::initialize "$snapshot_file" "$actual" |
| 10 | + return |
34 | 11 | fi |
| 12 | + |
| 13 | + snapshot::compare "$actual" "$snapshot_file" "${FUNCNAME[1]}" |
35 | 14 | } |
36 | 15 |
|
37 | | -# shellcheck disable=SC2155 |
38 | | -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 |
| 16 | +function assert_match_snapshot_ignore_colors() { |
| 17 | + local actual=$(echo -n "$1" | sed 's/\x1B\[[0-9;]*[mK]//g' | tr -d '\r') |
| 18 | + local snapshot_file=$(snapshot::resolve_file "${2:-}" "${FUNCNAME[1]}") |
49 | 19 |
|
50 | 20 | if [[ ! -f "$snapshot_file" ]]; then |
51 | | - mkdir -p "$(dirname "$snapshot_file")" |
52 | | - echo "$actual" > "$snapshot_file" |
53 | | - |
54 | | - state::add_assertions_snapshot |
| 21 | + snapshot::initialize "$snapshot_file" "$actual" |
55 | 22 | return |
56 | 23 | fi |
57 | 24 |
|
58 | | - local snapshot |
59 | | - snapshot=$(tr -d '\r' < "$snapshot_file") |
| 25 | + snapshot::compare "$actual" "$snapshot_file" "${FUNCNAME[1]}" |
| 26 | +} |
60 | 27 |
|
61 | | - if ! snapshot::match_with_placeholder "$actual" "$snapshot"; then |
62 | | - local label |
63 | | - label=$(helper::normalize_test_function_name "${FUNCNAME[1]}") |
| 28 | +function snapshot::match_with_placeholder() { |
| 29 | + local actual="$1" |
| 30 | + local snapshot="$2" |
| 31 | + local placeholder="${BASHUNIT_SNAPSHOT_PLACEHOLDER:-::ignore::}" |
| 32 | + local token="__BASHUNIT_IGNORE__" |
64 | 33 |
|
65 | | - state::add_assertions_failed |
66 | | - console_results::print_failed_snapshot_test "$label" "$snapshot_file" |
| 34 | + local sanitized="${snapshot//$placeholder/$token}" |
| 35 | + local escaped=$(printf '%s' "$sanitized" | sed -e 's/[.[\\^$*+?{}()|]/\\&/g') |
| 36 | + local regex="^${escaped//$token/(.|\\n)*}$" |
67 | 37 |
|
68 | | - return |
| 38 | + if which perl >/dev/null 2>&1; then |
| 39 | + echo "$actual" | REGEX="$regex" perl -0 -e ' |
| 40 | + my $r = $ENV{REGEX}; |
| 41 | + my $input = join("", <STDIN>); |
| 42 | + exit($input =~ /$r/s ? 0 : 1); |
| 43 | + ' && return 0 || return 1 |
| 44 | + else |
| 45 | + local fallback=$(printf '%s' "$snapshot" | sed -e "s|$placeholder|.*|g" -e 's/[][\.^$*+?{}|()]/\\&/g') |
| 46 | + fallback="^${fallback}$" |
| 47 | + echo "$actual" | grep -Eq "$fallback" && return 0 || return 1 |
69 | 48 | fi |
70 | | - |
71 | | - state::add_assertions_passed |
72 | 49 | } |
73 | 50 |
|
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 snapshot::resolve_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}" |
85 | 62 | fi |
| 63 | +} |
86 | 64 |
|
87 | | - if [[ ! -f "$snapshot_file" ]]; then |
88 | | - mkdir -p "$(dirname "$snapshot_file")" |
89 | | - echo "$actual" > "$snapshot_file" |
| 65 | +function snapshot::initialize() { |
| 66 | + local path="$1" |
| 67 | + local content="$2" |
| 68 | + mkdir -p "$(dirname "$path")" |
| 69 | + echo "$content" > "$path" |
| 70 | + state::add_assertions_snapshot |
| 71 | +} |
90 | 72 |
|
91 | | - state::add_assertions_snapshot |
92 | | - return |
93 | | - fi |
| 73 | +function snapshot::compare() { |
| 74 | + local actual="$1" |
| 75 | + local snapshot_path="$2" |
| 76 | + local func_name="$3" |
94 | 77 |
|
95 | 78 | local snapshot |
96 | | - snapshot=$(tr -d '\r' < "$snapshot_file") |
| 79 | + snapshot=$(tr -d '\r' < "$snapshot_path") |
97 | 80 |
|
98 | 81 | 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") |
102 | 83 | 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 |
106 | 86 | fi |
107 | 87 |
|
108 | 88 | state::add_assertions_passed |
|
0 commit comments