Skip to content

Commit c70c981

Browse files
authored
Merge pull request #404 from TypedDevs/codex/add-assert_match_snapshot_ignore_colors
Add assert_match_snapshot_ignore_colors
2 parents 1f4d6e3 + 832ca96 commit c70c981

6 files changed

+101
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Enable parallel tests on Windows
1010
- Add `assert_not_called`
1111
- Improve `find_total_tests` performance
12+
- Added `assert_match_snapshot_ignore_colors`
1213

1314
## [0.19.1](https://github.com/TypedDevs/bashunit/compare/0.19.0...0.19.1) - 2025-05-23
1415

docs/snapshots.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,20 @@ Some tests failed
5252
You need to run the tests for this example twice to see them work.
5353
The first time you run them, the snapshots will be generated and the second time they will be asserted.
5454
:::
55+
56+
## assert_match_snapshot_ignore_colors
57+
> `assert_match_snapshot_ignore_colors "actual"`
58+
59+
Like `assert_match_snapshot` but ANSI escape codes in `actual` are ignored. This allows
60+
verifying the output text while disregarding its style.
61+
62+
::: code-group
63+
```bash [Example]
64+
function test_success() {
65+
assert_match_snapshot_ignore_colors "$(printf '\e[31mHello\e[0m World!')"
66+
}
67+
function test_failure() {
68+
assert_match_snapshot_ignore_colors "World"
69+
}
70+
```
71+
:::

src/assert_snapshot.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,40 @@ function assert_match_snapshot() {
3535

3636
state::add_assertions_passed
3737
}
38+
39+
function assert_match_snapshot_ignore_colors() {
40+
local actual
41+
actual=$(echo -n "$1" | sed -r 's/\x1B\[[0-9;]*[mK]//g' | tr -d '\r')
42+
43+
local directory
44+
directory="./$(dirname "${BASH_SOURCE[1]}")/snapshots"
45+
local test_file
46+
test_file="$(helper::normalize_variable_name "$(basename "${BASH_SOURCE[1]}")")"
47+
local snapshot_name
48+
snapshot_name="$(helper::normalize_variable_name "${FUNCNAME[1]}").snapshot"
49+
local snapshot_file
50+
snapshot_file="${directory}/${test_file}.${snapshot_name}"
51+
52+
if [[ ! -f "$snapshot_file" ]]; then
53+
mkdir -p "$directory"
54+
echo "$actual" > "$snapshot_file"
55+
56+
state::add_assertions_snapshot
57+
return
58+
fi
59+
60+
local snapshot
61+
snapshot=$(tr -d '\r' < "$snapshot_file")
62+
63+
if [[ "$actual" != "$snapshot" ]]; then
64+
local label
65+
label=$(helper::normalize_test_function_name "${FUNCNAME[1]}")
66+
67+
state::add_assertions_failed
68+
console_results::print_failed_snapshot_test "$label" "$snapshot_file"
69+
70+
return
71+
fi
72+
73+
state::add_assertions_passed
74+
}

tests/unit/assert_snapshot_test.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,47 @@ function test_unsuccessful_assert_match_snapshot() {
4040

4141
assert_equals "$expected" "$actual"
4242
}
43+
44+
function test_successful_assert_match_snapshot_ignore_colors() {
45+
local colored
46+
colored=$(printf '\e[31mHello\e[0m World!')
47+
assert_empty "$(assert_match_snapshot_ignore_colors "$colored")"
48+
}
49+
50+
function test_creates_a_snapshot_ignore_colors() {
51+
local snapshot_file_path=tests/unit/snapshots/assert_snapshot_test_sh.test_creates_a_snapshot_ignore_colors.snapshot
52+
local expected=$((_ASSERTIONS_SNAPSHOT + 1))
53+
54+
assert_file_not_exists $snapshot_file_path
55+
56+
local colored
57+
colored=$(printf '\e[32mExpected\e[0m snapshot')
58+
59+
assert_match_snapshot_ignore_colors "$colored"
60+
61+
assert_same "$expected" "$_ASSERTIONS_SNAPSHOT"
62+
assert_file_exists $snapshot_file_path
63+
assert_same "Expected snapshot" "$(cat $snapshot_file_path)"
64+
65+
rm $snapshot_file_path
66+
}
67+
68+
function test_unsuccessful_assert_match_snapshot_ignore_colors() {
69+
local expected
70+
71+
if dependencies::has_git; then
72+
expected="$(printf "✗ Failed: Unsuccessful assert match snapshot ignore colors
73+
Expected to match the snapshot
74+
[-Actual-]{+Expected+} snapshot[-text-]")"
75+
else
76+
expected="$(printf "✗ Failed: Unsuccessful assert match snapshot ignore colors
77+
Expected to match the snapshot")"
78+
fi
79+
80+
local actual
81+
local colored
82+
colored=$(printf '\e[31mExpected snapshot\e[0m')
83+
actual="$(assert_match_snapshot_ignore_colors "$colored")"
84+
85+
assert_equals "$expected" "$actual"
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello World!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Actual snapshot text

0 commit comments

Comments
 (0)