@@ -6,102 +6,86 @@ function snapshot::match_with_placeholder() {
66 local placeholder=" ${BASHUNIT_SNAPSHOT_PLACEHOLDER:-:: ignore:: } "
77 local token=" __BASHUNIT_IGNORE__"
88
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} $"
9+ local normalized=" ${snapshot// $placeholder / $token } "
10+ local escaped
11+ escaped=$( printf ' %s' " $normalized " | sed -e ' s/[.[\\^$*+?{}()|]/\\&/g' )
12+ local regex=" ^${escaped// $token / (.|\\ n)* } $"
1413
1514 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' " $actual " | grep -zEq " $fallback_pattern " ; then
30- return 0
31- else
32- return 1
33- fi
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
3421 fi
22+
23+ # fallback: only supports single-line ignores
24+ local pattern=" ${snapshot// $placeholder / .+} "
25+ pattern=$( printf ' %s' " $pattern " | sed -e ' s/[][\.^$*+?{}|()]/\\&/g' )
26+ printf ' %s' " $actual " | grep -zEq " ^${pattern} $"
3527}
3628
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-} "
29+ function assert_snapshot::_normalize_snapshot_file() {
30+ local snapshot_file= " $1 "
31+ local funcname= " ${FUNCNAME[2]} "
32+ local source_file= " ${BASH_SOURCE[2]} "
33+ local directory test_file snapshot_name
4234
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} "
35+ if [[ -n " $snapshot_file " ]]; then
36+ echo " $snapshot_file "
37+ return
4838 fi
4939
40+ directory=" $( dirname " $source_file " ) /snapshots"
41+ test_file=" $( helper::normalize_variable_name " $( basename " $source_file " ) " ) "
42+ snapshot_name=" $( helper::normalize_variable_name " $funcname " ) .snapshot"
43+
44+ echo " ${directory} /${test_file} .${snapshot_name} "
45+ }
46+
47+ function assert_match_snapshot() {
48+ local actual snapshot_file snapshot
49+ actual=$( echo -n " $1 " | tr -d ' \r' )
50+ snapshot_file=$( assert_snapshot::_normalize_snapshot_file " ${2-} " )
51+
5052 if [[ ! -f " $snapshot_file " ]]; then
5153 mkdir -p " $( dirname " $snapshot_file " ) "
5254 echo " $actual " > " $snapshot_file "
53-
5455 state::add_assertions_snapshot
5556 return
5657 fi
5758
58- local snapshot
5959 snapshot=$( tr -d ' \r' < " $snapshot_file " )
60-
6160 if ! snapshot::match_with_placeholder " $actual " " $snapshot " ; then
6261 local label
6362 label=$( helper::normalize_test_function_name " ${FUNCNAME[1]} " )
64-
6563 state::add_assertions_failed
6664 console_results::print_failed_snapshot_test " $label " " $snapshot_file "
67-
6865 return
6966 fi
7067
7168 state::add_assertions_passed
7269}
7370
74- # shellcheck disable=SC2155
7571function assert_match_snapshot_ignore_colors() {
76- local actual
72+ local actual snapshot_file snapshot
7773 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} "
85- fi
74+ snapshot_file=$( assert_snapshot::_normalize_snapshot_file " ${2-} " )
8675
8776 if [[ ! -f " $snapshot_file " ]]; then
8877 mkdir -p " $( dirname " $snapshot_file " ) "
8978 echo " $actual " > " $snapshot_file "
90-
9179 state::add_assertions_snapshot
9280 return
9381 fi
9482
95- local snapshot
9683 snapshot=$( tr -d ' \r' < " $snapshot_file " )
97-
9884 if ! snapshot::match_with_placeholder " $actual " " $snapshot " ; then
9985 local label
10086 label=$( helper::normalize_test_function_name " ${FUNCNAME[1]} " )
101-
10287 state::add_assertions_failed
10388 console_results::print_failed_snapshot_test " $label " " $snapshot_file "
104-
10589 return
10690 fi
10791
0 commit comments