From 5bda89ea041cbeafd63cdea6e29a69637c5e15ba Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Sat, 25 Apr 2026 17:41:48 +0200 Subject: [PATCH] fix(assert): reject empty bool assertions --- CHANGELOG.md | 1 + src/assert.sh | 8 ++++++++ tests/unit/assert_basic_test.sh | 14 ++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ce8f4b5..a04141a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - `clock::now` handles `EPOCHREALTIME` values that use a comma decimal separator - Invalid `.env.example` coverage threshold entry; CI now copies `.env.example` to `.env` so config parse errors are caught - Coverage no longer counts case patterns with trailing comments (e.g. `*thing) # note`) or loop terminators with redirections/pipes (e.g. `done < file`, `done <<<"$var"`, `done | sort`) as executable lines (#634) +- `assert_true` and `assert_false` now report empty strings as assertion failures instead of trying to execute them ## [0.34.1](https://github.com/TypedDevs/bashunit/compare/0.34.0...0.34.1) - 2026-03-20 diff --git a/src/assert.sh b/src/assert.sh index 3cad4745..35beb171 100755 --- a/src/assert.sh +++ b/src/assert.sh @@ -43,6 +43,10 @@ function assert_true() { # Check for expected literal values first case "$actual" in + "") + bashunit::handle_bool_assertion_failure "true or 0" "$actual" + return + ;; "true" | "0") bashunit::state::add_assertions_passed return @@ -71,6 +75,10 @@ function assert_false() { # Check for expected literal values first case "$actual" in + "") + bashunit::handle_bool_assertion_failure "false or 1" "$actual" + return + ;; "false" | "1") bashunit::state::add_assertions_passed return diff --git a/tests/unit/assert_basic_test.sh b/tests/unit/assert_basic_test.sh index 14924590..e95fb455 100644 --- a/tests/unit/assert_basic_test.sh +++ b/tests/unit/assert_basic_test.sh @@ -32,6 +32,13 @@ function test_unsuccessful_assert_true() { "$(assert_true false)" } +function test_unsuccessful_assert_true_with_empty_value() { + assert_same "$(bashunit::console_results::print_failed_test "Unsuccessful assert true with empty value" \ + "true or 0" \ + "but got " "")" \ + "$(assert_true "")" +} + function test_successful_assert_true_on_function() { assert_empty "$(assert_true ls)" } @@ -62,6 +69,13 @@ function test_unsuccessful_assert_false() { "$(assert_false true)" } +function test_unsuccessful_assert_false_with_empty_value() { + assert_same "$(bashunit::console_results::print_failed_test "Unsuccessful assert false with empty value" \ + "false or 1" \ + "but got " "")" \ + "$(assert_false "")" +} + function test_successful_assert_false_on_function() { assert_empty "$(assert_false "eval return 1")" }