diff --git a/.claude/rules/testing.md b/.claude/rules/testing.md index 03ba6e4c..786131c5 100644 --- a/.claude/rules/testing.md +++ b/.claude/rules/testing.md @@ -91,8 +91,8 @@ function test_should_add() { ```bash assert_match_snapshot "$output" -# Re-record: delete the snapshot file and re-run; the assertion writes it when missing -# (so a deleted snapshot never fails — read the diff before deleting) +assert_match_named_snapshot "stderr" "$stderr" # multiple snapshots in one test +# Re-record deliberately: ./bashunit --snapshot-update --filter "test name" tests/ ``` ## Test Isolation diff --git a/README.md b/README.md index a7c255e3..e46dc2df 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ ## Why bashunit A lightweight, fast testing framework for **Bash 3.0+**, focused on developer experience. -It ships 71 assertions plus spies, mocks, data providers, snapshots and more. +It ships 73 assertions plus spies, mocks, data providers, snapshots and more. ## Quick start diff --git a/completions/_bashunit b/completions/_bashunit index 87342484..a162e56d 100644 --- a/completions/_bashunit +++ b/completions/_bashunit @@ -32,6 +32,7 @@ _bashunit() { assert_is_directory_writable assert_is_file assert_is_file_empty assert_json_contains assert_json_equals assert_json_key_exists assert_less_or_equal_than assert_less_than assert_line_count + assert_match_named_snapshot assert_match_named_snapshot_ignore_colors assert_match_snapshot assert_match_snapshot_ignore_colors assert_matches assert_not_contains assert_not_empty assert_not_equals assert_not_matches assert_not_same assert_same assert_string_ends_with diff --git a/completions/bashunit.bash b/completions/bashunit.bash index 9e8b14cf..d894761e 100644 --- a/completions/bashunit.bash +++ b/completions/bashunit.bash @@ -45,6 +45,7 @@ assert_is_directory_not_writable assert_is_directory_readable \ assert_is_directory_writable assert_is_file assert_is_file_empty \ assert_json_contains assert_json_equals assert_json_key_exists \ assert_less_or_equal_than assert_less_than assert_line_count \ +assert_match_named_snapshot assert_match_named_snapshot_ignore_colors \ assert_match_snapshot assert_match_snapshot_ignore_colors assert_matches \ assert_not_contains assert_not_empty assert_not_equals assert_not_matches \ assert_not_same assert_same assert_string_ends_with \ diff --git a/docs/assertions.md b/docs/assertions.md index d8279230..6bdfba93 100644 --- a/docs/assertions.md +++ b/docs/assertions.md @@ -28,7 +28,7 @@ to narrow it (`bashunit doc json`). | **Arrays** | [assert_arrays_equal](#assert-arrays-equal) · [assert_array_contains](#assert-array-contains) · [assert_array_not_contains](#assert-array-not-contains) · [assert_array_length](#assert-array-length) | | **JSON** | [assert_json_equals](#assert-json-equals) · [assert_json_contains](#assert-json-contains) · [assert_json_key_exists](#assert-json-key-exists) | | **Duration** | [assert_duration](#assert-duration) · [assert_duration_less_than](#assert-duration-less-than) · [assert_duration_greater_than](#assert-duration-greater-than) | -| **Snapshots** | [assert_match_snapshot](#assert-match-snapshot) · [assert_match_snapshot_ignore_colors](#assert-match-snapshot-ignore-colors) | +| **Snapshots** | [assert_match_snapshot](#assert-match-snapshot) · [assert_match_named_snapshot](#assert-match-named-snapshot) · [assert_match_snapshot_ignore_colors](#assert-match-snapshot-ignore-colors) · [assert_match_named_snapshot_ignore_colors](#assert-match-named-snapshot-ignore-colors) | | **Spies** | [assert_have_been_called](#assert-have-been-called) · [assert_not_called](#assert-not-called) · [assert_have_been_called_with](#assert-have-been-called-with) · [assert_have_been_called_with_any](#assert-have-been-called-with-any) · [assert_have_been_called_with_args](#assert-have-been-called-with-args) · [assert_have_been_called_nth_with](#assert-have-been-called-nth-with) · [assert_have_been_called_times](#assert-have-been-called-times) | | **Assertions** | [assert_assertion_passes](#assert-assertion-passes) · [assert_assertion_fails](#assert-assertion-fails) · [assert_assertion_fails_with](#assert-assertion-fails-with) | | **Manual failure** | [bashunit::fail](#bashunit-fail) | @@ -1633,6 +1633,34 @@ function test_failure() { ``` ::: +## assert_match_named_snapshot +> `assert_match_named_snapshot "name" "actual"` + +Matches `actual` against a snapshot whose filename includes `name`. Use it for multiple independent snapshots in one test without constructing file paths yourself. Names are normalized so they cannot escape the test's `snapshots/` directory. + +::: code-group +```bash [Example] +function test_render_modes() { + assert_match_named_snapshot "compact" "$(./bin/render --compact)" + assert_match_named_snapshot "verbose" "$(./bin/render --verbose)" +} +``` +::: + +## assert_match_named_snapshot_ignore_colors +> `assert_match_named_snapshot_ignore_colors "name" "actual"` + +Named version of [assert_match_snapshot_ignore_colors](#assert-match-snapshot-ignore-colors). ANSI escape sequences are stripped from `actual` before it is stored or compared. + +::: code-group +```bash [Example] +function test_colored_render_modes() { + assert_match_named_snapshot_ignore_colors "compact" "$(./bin/render --compact)" + assert_match_named_snapshot_ignore_colors "verbose" "$(./bin/render --verbose)" +} +``` +::: + ## assert_have_been_called > `assert_have_been_called "command"` diff --git a/docs/command-line.md b/docs/command-line.md index 49766ad5..4756d422 100644 --- a/docs/command-line.md +++ b/docs/command-line.md @@ -575,10 +575,10 @@ steps: > `bashunit test --snapshot-update` -Re-record snapshots: every `assert_match_snapshot` / -`assert_match_snapshot_ignore_colors` whose snapshot already exists is +Re-record snapshots: every snapshot assertion whose file already exists is overwritten with the value this run produced, and reported as a recorded -snapshot instead of a pass. A missing snapshot is written as usual. +snapshot instead of a pass. A missing snapshot is written as usual. This +includes default, named and ignore-colors snapshots. Use it when an output change is deliberate. It replaces deleting snapshot files by hand — the path is derived from the test file and function name, so a wrong diff --git a/docs/public/bashunit-skill.md b/docs/public/bashunit-skill.md index e4cac90b..82dbcc8b 100644 --- a/docs/public/bashunit-skill.md +++ b/docs/public/bashunit-skill.md @@ -118,7 +118,7 @@ cleaned up automatically and are safe under `--parallel`. ## Assertions -`bashunit doc` prints the full catalogue (71 assertions) locally; `bashunit doc contains` +`bashunit doc` prints the full catalogue (73 assertions) locally; `bashunit doc contains` filters it. The same list is at https://bashunit.com/assertions. **Do not invent names** — a wrong name is a runtime error, not a failed assertion. @@ -132,7 +132,8 @@ filters it. The same list is at https://bashunit.com/assertions. **Do not invent `assert_directory_exists`, `assert_file_permissions` - Arrays: `assert_array_contains`, `assert_array_length`, `assert_arrays_equal` - JSON: `assert_json_equals`, `assert_json_contains`, `assert_json_key_exists` -- Snapshots: `assert_match_snapshot`, `assert_match_snapshot_ignore_colors` +- Snapshots: `assert_match_snapshot`, `assert_match_snapshot_ignore_colors`, + `assert_match_named_snapshot`, `assert_match_named_snapshot_ignore_colors` - Spies: `assert_have_been_called`, `assert_not_called`, `assert_have_been_called_with`, `assert_have_been_called_with_any`, `assert_have_been_called_with_args`, `assert_have_been_called_times`, `assert_have_been_called_nth_with` diff --git a/docs/snapshots.md b/docs/snapshots.md index 7a75d15e..73e1a253 100644 --- a/docs/snapshots.md +++ b/docs/snapshots.md @@ -20,7 +20,8 @@ Pass `snapshot_file` to point at a specific snapshot — useful to share one sna By default each test gets its own, named after the test function. ::: tip -You can update the snapshot by deleting it and running its test again. +Update snapshots deliberately with `--snapshot-update`; combine it with `--filter` to +re-record a single test. ::: ::: code-group @@ -77,6 +78,27 @@ function test_failure() { ``` ::: +## Named snapshots + +Use named snapshots when one test needs to capture several independent values: + +> `assert_match_named_snapshot "name" "actual"` + +```bash +function test_render_modes() { + assert_match_named_snapshot "compact" "$(./bin/render --compact)" + assert_match_named_snapshot "verbose" "$(./bin/render --verbose)" +} +``` + +The name becomes a normalized filename suffix, so these resolve beside the default +snapshot as `test_render_modes.compact.snapshot` and +`test_render_modes.verbose.snapshot`. Spaces and punctuation are safe and cannot escape +the test's `snapshots/` directory. + +Use `assert_match_named_snapshot_ignore_colors "name" "actual"` for the same behavior +with ANSI escape sequences removed. + ## Placeholders Snapshot files can contain placeholder tokens to ignore variable parts of the output. diff --git a/src/assert/snapshot.sh b/src/assert/snapshot.sh index d0f6afef..811cad2f 100644 --- a/src/assert/snapshot.sh +++ b/src/assert/snapshot.sh @@ -24,6 +24,19 @@ function assert_match_snapshot() { bashunit::snapshot::assert "$actual" "$snapshot_file" "$test_fn" } +function assert_match_named_snapshot() { + local snapshot_name=$1 + local _snapshot_normalized + bashunit::snapshot::normalize_actual "$2" + local actual=$_snapshot_normalized + bashunit::helper::find_test_function_name_to_slot + local test_fn=$_BASHUNIT_HELPER_TESTFN_OUT + bashunit::snapshot::resolve_file "" "$test_fn" "$snapshot_name" + local snapshot_file=$_BASHUNIT_SNAPSHOT_FILE_OUT + + bashunit::snapshot::assert "$actual" "$snapshot_file" "$test_fn" +} + function assert_match_snapshot_ignore_colors() { # Only fork sed when the input actually carries an escape sequence; plain, # colorless output takes a pure-bash fast path. The sed pattern is kept @@ -44,6 +57,25 @@ function assert_match_snapshot_ignore_colors() { bashunit::snapshot::assert "$actual" "$snapshot_file" "$test_fn" } +function assert_match_named_snapshot_ignore_colors() { + local snapshot_name=$1 + # Keep this byte-compatible with assert_match_snapshot_ignore_colors: only + # ANSI sequences ending in m/K are stripped, and only when ESC is present. + local stripped=$2 + case "$stripped" in + *$'\e'*) stripped=$(printf '%s' "$stripped" | sed 's/\x1B\[[0-9;]*[mK]//g') ;; + esac + local _snapshot_normalized + bashunit::snapshot::normalize_actual "$stripped" + local actual=$_snapshot_normalized + bashunit::helper::find_test_function_name_to_slot + local test_fn=$_BASHUNIT_HELPER_TESTFN_OUT + bashunit::snapshot::resolve_file "" "$test_fn" "$snapshot_name" + local snapshot_file=$_BASHUNIT_SNAPSHOT_FILE_OUT + + bashunit::snapshot::assert "$actual" "$snapshot_file" "$test_fn" +} + # Strips the `./` resolve_file prepends and squeezes doubled slashes, so a path # recorded by an assertion and the same path as `find` prints it compare equal. function bashunit::snapshot::normalize_path() { @@ -214,6 +246,7 @@ _BASHUNIT_SNAPSHOT_FILE_OUT="" function bashunit::snapshot::resolve_file() { local file_hint="$1" local func_name="$2" + local snapshot_name="${3:-}" if [ -n "$file_hint" ]; then _BASHUNIT_SNAPSHOT_FILE_OUT=$file_hint @@ -233,9 +266,13 @@ function bashunit::snapshot::resolve_file() { bashunit::helper::normalize_variable_name_to_slot "$base_part" local test_file=$_BASHUNIT_HELPER_VARNAME_OUT bashunit::helper::normalize_variable_name_to_slot "$func_name" - local name="$_BASHUNIT_HELPER_VARNAME_OUT.snapshot" + local name=$_BASHUNIT_HELPER_VARNAME_OUT + if [ -n "$snapshot_name" ]; then + bashunit::helper::normalize_variable_name_to_slot "$snapshot_name" + name="$name.$_BASHUNIT_HELPER_VARNAME_OUT" + fi - _BASHUNIT_SNAPSHOT_FILE_OUT="./${dir_part}/snapshots/${test_file}.${name}" + _BASHUNIT_SNAPSHOT_FILE_OUT="./${dir_part}/snapshots/${test_file}.${name}.snapshot" } function bashunit::snapshot::initialize() { diff --git a/src/console/test_line.sh b/src/console/test_line.sh index 4ccd65b3..fddb6f99 100644 --- a/src/console/test_line.sh +++ b/src/console/test_line.sh @@ -162,16 +162,21 @@ function bashunit::console_results::print_failed_snapshot_test() { local line line="$(printf "${_BASHUNIT_COLOR_FAILED}✗ Failed${_BASHUNIT_COLOR_DEFAULT}: %s - ${_BASHUNIT_COLOR_FAINT}Expected to match the snapshot${_BASHUNIT_COLOR_DEFAULT}\n" "$function_name")" + ${_BASHUNIT_COLOR_FAINT}Expected to match the snapshot${_BASHUNIT_COLOR_DEFAULT} + ${_BASHUNIT_COLOR_FAINT}Snapshot: %s${_BASHUNIT_COLOR_DEFAULT} + ${_BASHUNIT_COLOR_FAINT}Re-record with '--snapshot-update'${_BASHUNIT_COLOR_DEFAULT}\n" \ + "$function_name" "$snapshot_file")" if bashunit::dependencies::has_git; then local actual_file="${snapshot_file}.tmp" echo "$actual_content" >"$actual_file" - line="$line$(bashunit::console_results::render_diff "$snapshot_file" "$actual_file")" + line="$line +$(bashunit::console_results::render_diff "$snapshot_file" "$actual_file")" rm "$actual_file" else - line="$line$(bashunit::console_results::snapshot_line_diff \ + line="$line +$(bashunit::console_results::snapshot_line_diff \ "$(cat "$snapshot_file")" "$actual_content")" fi @@ -282,4 +287,3 @@ function bashunit::console_results::print_worker_stderr() { "$_BASHUNIT_COLOR_SKIPPED" "$test_file" "$_BASHUNIT_COLOR_DEFAULT" sed 's/^/|/' "$stderr_file" } - diff --git a/tests/acceptance/bashunit_fail_test.sh b/tests/acceptance/bashunit_fail_test.sh index c49e6467..91fd99fe 100644 --- a/tests/acceptance/bashunit_fail_test.sh +++ b/tests/acceptance/bashunit_fail_test.sh @@ -21,8 +21,12 @@ function test_bashunit_when_a_test_fail_verbose_output_option() { } function test_different_verbose_snapshots_matches() { - bashunit::todo \ - "The different snapshots for these tests should also be identical, option to choose snapshot name?" + local test_file=./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh + + assert_match_named_snapshot "env" \ + "$(./bashunit --no-parallel --env "$TEST_ENV_FILE" "$test_file")" + assert_match_named_snapshot "option" \ + "$(./bashunit --no-parallel --env "$TEST_ENV_FILE_SIMPLE" "$test_file" --detailed)" } function test_bashunit_when_a_test_fail_simple_output_env() { @@ -54,6 +58,10 @@ function test_bashunit_with_a_test_fail_and_exit_immediately() { } function test_different_simple_snapshots_matches() { - bashunit::todo \ - "The different snapshots for these tests should also be identical, option to choose snapshot name?" + local test_file=./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh + + assert_match_named_snapshot "env" \ + "$(./bashunit --no-parallel --env "$TEST_ENV_FILE_SIMPLE" "$test_file")" + assert_match_named_snapshot "option" \ + "$(./bashunit --no-parallel --env "$TEST_ENV_FILE" "$test_file" --simple)" } diff --git a/tests/acceptance/bashunit_snapshot_named_test.sh b/tests/acceptance/bashunit_snapshot_named_test.sh new file mode 100644 index 00000000..963457b8 --- /dev/null +++ b/tests/acceptance/bashunit_snapshot_named_test.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash +set -euo pipefail + +function set_up_before_script() { + BASHUNIT_BIN="$(pwd)/bashunit" + FIXTURE="$(pwd)/tests/acceptance/fixtures/snapshot_named/named.sh" +} + +function set_up() { + WORKDIR="$(mktemp -d)" + cp "$FIXTURE" "$WORKDIR/named.sh" + SNAPSHOTS="$WORKDIR/snapshots" + DEFAULT="$SNAPSHOTS/named_sh.test_named_snapshots.snapshot" + FIRST="$SNAPSHOTS/named_sh.test_named_snapshots.first_value.snapshot" + SAFE="$SNAPSHOTS/named_sh.test_named_snapshots._______second_.snapshot" + COLORED="$SNAPSHOTS/named_sh.test_named_snapshots.colored.snapshot" +} + +function tear_down() { + rm -rf "$WORKDIR" +} + +function test_named_snapshots_create_distinct_safe_files() { + (cd "$WORKDIR" && "$BASHUNIT_BIN" --no-parallel --skip-env-file named.sh) \ + >/dev/null 2>&1 + + assert_same "default value" "$(<"$DEFAULT")" + assert_same "named value" "$(<"$FIRST")" + assert_same "safe value" "$(<"$SAFE")" + assert_same "colored value" "$(<"$COLORED")" +} + +function test_named_snapshot_mismatch_names_the_path_and_update_flag() { + mkdir -p "$SNAPSHOTS" + printf 'stale value\n' >"$FIRST" + + local output status=0 + output=$(cd "$WORKDIR" && "$BASHUNIT_BIN" --no-parallel --skip-env-file \ + named.sh 2>&1) || status=$? + + assert_same 1 "$status" + assert_contains "snapshots/named_sh.test_named_snapshots.first_value.snapshot" "$output" + assert_contains "--snapshot-update" "$output" +} + +function test_snapshot_update_rewrites_a_named_snapshot() { + mkdir -p "$SNAPSHOTS" + printf 'stale value\n' >"$FIRST" + + (cd "$WORKDIR" && "$BASHUNIT_BIN" --no-parallel --skip-env-file \ + --snapshot-update named.sh) >/dev/null 2>&1 + + assert_same "named value" "$(<"$FIRST")" +} diff --git a/tests/acceptance/bashunit_stop_on_failure_test.sh b/tests/acceptance/bashunit_stop_on_failure_test.sh index d76f58a5..23818583 100644 --- a/tests/acceptance/bashunit_stop_on_failure_test.sh +++ b/tests/acceptance/bashunit_stop_on_failure_test.sh @@ -21,8 +21,12 @@ function test_bashunit_when_stop_on_failure_env() { } function test_different_snapshots_matches() { - bashunit::todo \ - "The different snapshots for these tests should also be identical, option to choose snapshot name?" + local test_file=./tests/acceptance/fixtures/test_bashunit_when_stop_on_failure.sh + + assert_match_named_snapshot "option" \ + "$(./bashunit --no-parallel --env "$TEST_ENV_FILE" --stop-on-failure "$test_file")" + assert_match_named_snapshot "env" \ + "$(./bashunit --no-parallel --env "$TEST_ENV_FILE_STOP_ON_FAILURE" "$test_file")" } function test_bashunit_when_stop_on_failure_env_simple_output() { diff --git a/tests/acceptance/bashunit_summary_output_test.sh b/tests/acceptance/bashunit_summary_output_test.sh index 5cba9f0e..0f60850b 100644 --- a/tests/acceptance/bashunit_summary_output_test.sh +++ b/tests/acceptance/bashunit_summary_output_test.sh @@ -42,7 +42,8 @@ function test_incomplete_tests_displayed_with_show_incomplete_flag() { function test_both_flags_can_be_used_together() { local output output=$(./bashunit --env "$TEST_ENV_FILE" --no-parallel --simple --show-skipped --show-incomplete \ - "tests/acceptance/bashunit_fail_test.sh" "tests/acceptance/bashunit_init_test.sh" 2>&1) || true + "tests/acceptance/bashunit_execution_error_test.sh" \ + "tests/acceptance/bashunit_init_test.sh" 2>&1) || true assert_contains "incomplete test" "$output" assert_contains "skipped test" "$output" diff --git a/tests/acceptance/fixtures/snapshot_named/named.sh b/tests/acceptance/fixtures/snapshot_named/named.sh new file mode 100644 index 00000000..59da2eb8 --- /dev/null +++ b/tests/acceptance/fixtures/snapshot_named/named.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +function test_named_snapshots() { + assert_match_snapshot "default value" + assert_match_named_snapshot "first value" "named value" + assert_match_named_snapshot "../../ second!" "safe value" + assert_match_named_snapshot_ignore_colors "colored" $'\e[31mcolored value\e[0m' +} diff --git a/tests/acceptance/snapshots/bashunit_fail_test_sh.test_different_simple_snapshots_matches.env.snapshot b/tests/acceptance/snapshots/bashunit_fail_test_sh.test_different_simple_snapshots_matches.env.snapshot new file mode 100644 index 00000000..861ae40c --- /dev/null +++ b/tests/acceptance/snapshots/bashunit_fail_test_sh.test_different_simple_snapshots_matches.env.snapshot @@ -0,0 +1,18 @@ +..F.. + +There was 1 failure: + +|1) ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh:12 +|✗ Failed: Assert failing +| Expected '1' +| but got  '0' +| at ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh:12 +| Source: +| 13: assert_same 1 0 + + + +Tests:  4 passed, 1 failed, 5 total +Assertions: 6 passed, 1 failed, 7 total + + Some tests failed  diff --git a/tests/acceptance/snapshots/bashunit_fail_test_sh.test_different_simple_snapshots_matches.option.snapshot b/tests/acceptance/snapshots/bashunit_fail_test_sh.test_different_simple_snapshots_matches.option.snapshot new file mode 100644 index 00000000..861ae40c --- /dev/null +++ b/tests/acceptance/snapshots/bashunit_fail_test_sh.test_different_simple_snapshots_matches.option.snapshot @@ -0,0 +1,18 @@ +..F.. + +There was 1 failure: + +|1) ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh:12 +|✗ Failed: Assert failing +| Expected '1' +| but got  '0' +| at ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh:12 +| Source: +| 13: assert_same 1 0 + + + +Tests:  4 passed, 1 failed, 5 total +Assertions: 6 passed, 1 failed, 7 total + + Some tests failed  diff --git a/tests/acceptance/snapshots/bashunit_fail_test_sh.test_different_verbose_snapshots_matches.env.snapshot b/tests/acceptance/snapshots/bashunit_fail_test_sh.test_different_verbose_snapshots_matches.env.snapshot new file mode 100644 index 00000000..ff9c83d8 --- /dev/null +++ b/tests/acceptance/snapshots/bashunit_fail_test_sh.test_different_verbose_snapshots_matches.env.snapshot @@ -0,0 +1,24 @@ +Running ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh +✓ Passed: Assert same +✓ Passed: Assert contains +✗ Failed: Assert failing + Expected '1' + but got  '0' + at ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh:12 +✓ Passed: Assert greater and less than +✓ Passed: Assert empty + +There was 1 failure: + +|1) ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh:12 +|✗ Failed: Assert failing +| Expected '1' +| but got  '0' +| at ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh:12 +| Source: +| 13: assert_same 1 0 + +Tests:  4 passed, 1 failed, 5 total +Assertions: 6 passed, 1 failed, 7 total + + Some tests failed  diff --git a/tests/acceptance/snapshots/bashunit_fail_test_sh.test_different_verbose_snapshots_matches.option.snapshot b/tests/acceptance/snapshots/bashunit_fail_test_sh.test_different_verbose_snapshots_matches.option.snapshot new file mode 100644 index 00000000..ff9c83d8 --- /dev/null +++ b/tests/acceptance/snapshots/bashunit_fail_test_sh.test_different_verbose_snapshots_matches.option.snapshot @@ -0,0 +1,24 @@ +Running ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh +✓ Passed: Assert same +✓ Passed: Assert contains +✗ Failed: Assert failing + Expected '1' + but got  '0' + at ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh:12 +✓ Passed: Assert greater and less than +✓ Passed: Assert empty + +There was 1 failure: + +|1) ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh:12 +|✗ Failed: Assert failing +| Expected '1' +| but got  '0' +| at ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh:12 +| Source: +| 13: assert_same 1 0 + +Tests:  4 passed, 1 failed, 5 total +Assertions: 6 passed, 1 failed, 7 total + + Some tests failed  diff --git a/tests/acceptance/snapshots/bashunit_stop_on_failure_test_sh.test_different_snapshots_matches.env.snapshot b/tests/acceptance/snapshots/bashunit_stop_on_failure_test_sh.test_different_snapshots_matches.env.snapshot new file mode 100644 index 00000000..9ed7e82a --- /dev/null +++ b/tests/acceptance/snapshots/bashunit_stop_on_failure_test_sh.test_different_snapshots_matches.env.snapshot @@ -0,0 +1,24 @@ +Running ./tests/acceptance/fixtures/test_bashunit_when_stop_on_failure.sh +✓ Passed: A success +✗ Failed: B error + Expected '1' + but got  '2' + at ./tests/acceptance/fixtures/test_bashunit_when_stop_on_failure.sh:6 + +Stop on failure enabled... +There was 1 failure: + +|1) ./tests/acceptance/fixtures/test_bashunit_when_stop_on_failure.sh:6 +|✗ Failed: B error +| Expected '1' +| but got  '2' +| at ./tests/acceptance/fixtures/test_bashunit_when_stop_on_failure.sh:6 +| Source: +| 7: assert_same 1 1 +| 8: assert_same 1 2 # error +| 9: assert_same 2 2 + +Tests:  1 passed, 1 failed, 2 total +Assertions: 2 passed, 1 failed, 3 total + + Some tests failed  diff --git a/tests/acceptance/snapshots/bashunit_stop_on_failure_test_sh.test_different_snapshots_matches.option.snapshot b/tests/acceptance/snapshots/bashunit_stop_on_failure_test_sh.test_different_snapshots_matches.option.snapshot new file mode 100644 index 00000000..9ed7e82a --- /dev/null +++ b/tests/acceptance/snapshots/bashunit_stop_on_failure_test_sh.test_different_snapshots_matches.option.snapshot @@ -0,0 +1,24 @@ +Running ./tests/acceptance/fixtures/test_bashunit_when_stop_on_failure.sh +✓ Passed: A success +✗ Failed: B error + Expected '1' + but got  '2' + at ./tests/acceptance/fixtures/test_bashunit_when_stop_on_failure.sh:6 + +Stop on failure enabled... +There was 1 failure: + +|1) ./tests/acceptance/fixtures/test_bashunit_when_stop_on_failure.sh:6 +|✗ Failed: B error +| Expected '1' +| but got  '2' +| at ./tests/acceptance/fixtures/test_bashunit_when_stop_on_failure.sh:6 +| Source: +| 7: assert_same 1 1 +| 8: assert_same 1 2 # error +| 9: assert_same 2 2 + +Tests:  1 passed, 1 failed, 2 total +Assertions: 2 passed, 1 failed, 3 total + + Some tests failed  diff --git a/tests/acceptance/snapshots/bashunit_test_sh.test_bashunit_should_display_all_assert_docs.snapshot b/tests/acceptance/snapshots/bashunit_test_sh.test_bashunit_should_display_all_assert_docs.snapshot index 989bda4f..cdcc7766 100644 --- a/tests/acceptance/snapshots/bashunit_test_sh.test_bashunit_should_display_all_assert_docs.snapshot +++ b/tests/acceptance/snapshots/bashunit_test_sh.test_bashunit_should_display_all_assert_docs.snapshot @@ -635,6 +635,20 @@ See Snapshots(/snapshots) for the full workflow, including how to update a snaps Same as assert_match_snapshot, but strips ANSI escape sequences from `actual` before comparing. Use it for commands whose colouring depends on the terminal. +## assert_match_named_snapshot +-------------- +> `assert_match_named_snapshot "name" "actual"` + +Matches `actual` against a snapshot whose filename includes `name`. Use it for multiple independent snapshots in one test without constructing file paths yourself. Names are normalized so they cannot escape the test's `snapshots/` directory. + + +## assert_match_named_snapshot_ignore_colors +-------------- +> `assert_match_named_snapshot_ignore_colors "name" "actual"` + +Named version of assert_match_snapshot_ignore_colors. ANSI escape sequences are stripped from `actual` before it is stored or compared. + + ## assert_have_been_called -------------- > `assert_have_been_called "command"` diff --git a/tests/unit/assert/snapshot_test.sh b/tests/unit/assert/snapshot_test.sh index 365b72bc..4187e52e 100644 --- a/tests/unit/assert/snapshot_test.sh +++ b/tests/unit/assert/snapshot_test.sh @@ -28,6 +28,8 @@ function test_unsuccessful_assert_match_snapshot() { assert_matches "Unsuccessful assert match snapshot" "$actual" assert_matches "Expected to match the snapshot" "$actual" + assert_contains "snapshot_test_sh.test_unsuccessful_assert_match_snapshot.snapshot" "$actual" + assert_contains "--snapshot-update" "$actual" } function test_successful_assert_match_snapshot_ignore_colors() { @@ -58,6 +60,9 @@ function test_unsuccessful_assert_match_snapshot_ignore_colors() { assert_matches "Unsuccessful assert match snapshot ignore colors" "$actual" assert_matches "Expected to match the snapshot" "$actual" + assert_contains \ + "snapshot_test_sh.test_unsuccessful_assert_match_snapshot_ignore_colors.snapshot" "$actual" + assert_contains "--snapshot-update" "$actual" } function test_assert_match_snapshot_strips_carriage_returns_from_actual() {