diff --git a/CHANGELOG.md b/CHANGELOG.md index 38662be1..2b70f093 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - Enable parallel tests on Windows - Add `assert_not_called` - Improve `find_total_tests` performance +- Added `assert_match_snapshot_ignore_colors` ## [0.19.1](https://github.com/TypedDevs/bashunit/compare/0.19.0...0.19.1) - 2025-05-23 diff --git a/docs/snapshots.md b/docs/snapshots.md index 13c5d904..60594c71 100644 --- a/docs/snapshots.md +++ b/docs/snapshots.md @@ -52,3 +52,20 @@ Some tests failed You need to run the tests for this example twice to see them work. The first time you run them, the snapshots will be generated and the second time they will be asserted. ::: + +## assert_match_snapshot_ignore_colors +> `assert_match_snapshot_ignore_colors "actual"` + +Like `assert_match_snapshot` but ANSI escape codes in `actual` are ignored. This allows +verifying the output text while disregarding its style. + +::: code-group +```bash [Example] +function test_success() { + assert_match_snapshot_ignore_colors "$(printf '\e[31mHello\e[0m World!')" +} +function test_failure() { + assert_match_snapshot_ignore_colors "World" +} +``` +::: diff --git a/src/assert_snapshot.sh b/src/assert_snapshot.sh index 88afc92c..42039df6 100644 --- a/src/assert_snapshot.sh +++ b/src/assert_snapshot.sh @@ -35,3 +35,40 @@ function assert_match_snapshot() { state::add_assertions_passed } + +function assert_match_snapshot_ignore_colors() { + local actual + actual=$(echo -n "$1" | sed -r 's/\x1B\[[0-9;]*[mK]//g' | tr -d '\r') + + local directory + directory="./$(dirname "${BASH_SOURCE[1]}")/snapshots" + local test_file + test_file="$(helper::normalize_variable_name "$(basename "${BASH_SOURCE[1]}")")" + local snapshot_name + snapshot_name="$(helper::normalize_variable_name "${FUNCNAME[1]}").snapshot" + local snapshot_file + snapshot_file="${directory}/${test_file}.${snapshot_name}" + + if [[ ! -f "$snapshot_file" ]]; then + mkdir -p "$directory" + echo "$actual" > "$snapshot_file" + + state::add_assertions_snapshot + return + fi + + local snapshot + snapshot=$(tr -d '\r' < "$snapshot_file") + + if [[ "$actual" != "$snapshot" ]]; then + local label + label=$(helper::normalize_test_function_name "${FUNCNAME[1]}") + + state::add_assertions_failed + console_results::print_failed_snapshot_test "$label" "$snapshot_file" + + return + fi + + state::add_assertions_passed +} diff --git a/tests/unit/assert_snapshot_test.sh b/tests/unit/assert_snapshot_test.sh index 650391de..8ef22e97 100644 --- a/tests/unit/assert_snapshot_test.sh +++ b/tests/unit/assert_snapshot_test.sh @@ -40,3 +40,47 @@ function test_unsuccessful_assert_match_snapshot() { assert_equals "$expected" "$actual" } + +function test_successful_assert_match_snapshot_ignore_colors() { + local colored + colored=$(printf '\e[31mHello\e[0m World!') + assert_empty "$(assert_match_snapshot_ignore_colors "$colored")" +} + +function test_creates_a_snapshot_ignore_colors() { + local snapshot_file_path=tests/unit/snapshots/assert_snapshot_test_sh.test_creates_a_snapshot_ignore_colors.snapshot + local expected=$((_ASSERTIONS_SNAPSHOT + 1)) + + assert_file_not_exists $snapshot_file_path + + local colored + colored=$(printf '\e[32mExpected\e[0m snapshot') + + assert_match_snapshot_ignore_colors "$colored" + + assert_same "$expected" "$_ASSERTIONS_SNAPSHOT" + assert_file_exists $snapshot_file_path + assert_same "Expected snapshot" "$(cat $snapshot_file_path)" + + rm $snapshot_file_path +} + +function test_unsuccessful_assert_match_snapshot_ignore_colors() { + local expected + + if dependencies::has_git; then + expected="$(printf "✗ Failed: Unsuccessful assert match snapshot ignore colors + Expected to match the snapshot + [-Actual-]{+Expected+} snapshot[-text-]")" + else + expected="$(printf "✗ Failed: Unsuccessful assert match snapshot ignore colors + Expected to match the snapshot")" + fi + + local actual + local colored + colored=$(printf '\e[31mExpected snapshot\e[0m') + actual="$(assert_match_snapshot_ignore_colors "$colored")" + + assert_equals "$expected" "$actual" +} diff --git a/tests/unit/snapshots/assert_snapshot_test_sh.test_successful_assert_match_snapshot_ignore_colors.snapshot b/tests/unit/snapshots/assert_snapshot_test_sh.test_successful_assert_match_snapshot_ignore_colors.snapshot new file mode 100644 index 00000000..980a0d5f --- /dev/null +++ b/tests/unit/snapshots/assert_snapshot_test_sh.test_successful_assert_match_snapshot_ignore_colors.snapshot @@ -0,0 +1 @@ +Hello World! diff --git a/tests/unit/snapshots/assert_snapshot_test_sh.test_unsuccessful_assert_match_snapshot_ignore_colors.snapshot b/tests/unit/snapshots/assert_snapshot_test_sh.test_unsuccessful_assert_match_snapshot_ignore_colors.snapshot new file mode 100644 index 00000000..c4399009 --- /dev/null +++ b/tests/unit/snapshots/assert_snapshot_test_sh.test_unsuccessful_assert_match_snapshot_ignore_colors.snapshot @@ -0,0 +1 @@ +Actual snapshot text