Skip to content

Commit c4b1eb7

Browse files
committed
feat: add assert_unsuccessful_code
1 parent d47f8bc commit c4b1eb7

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
- Add `assert_unsuccessful_code` assertion to check for non-zero exit codes
56
- Fix bench tests missing test_file var
67
- Fix compatibility with older python versions for clock::now
78

docs/assertions.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ Reports an error if the exit code of `callable` is not equal to `expected`.
333333

334334
If `callable` is not provided, it takes the last executed command or function instead.
335335

336-
[assert_successful_code](#assert-successful-code), [assert_general_error](#assert-general-error) and [assert_command_not_found](#assert-command-not-found)
336+
[assert_successful_code](#assert-successful-code), [assert_unsuccessful_code](#assert-unsuccessful-code), [assert_general_error](#assert-general-error) and [assert_command_not_found](#assert-command-not-found)
337337
are more semantic versions of this assertion, for which you don't need to specify an exit code.
338338
339339
::: code-group
@@ -453,6 +453,45 @@ function test_failure() {
453453
```
454454
:::
455455
456+
## assert_unsuccessful_code
457+
> `assert_unsuccessful_code ["callable"]`
458+
459+
Reports an error if the exit code of `callable` is not unsuccessful (non-zero).
460+
461+
If `callable` is not provided, it takes the last executed command or function instead.
462+
463+
[assert_exit_code](#assert-exit-code) is the full version of this assertion where you can specify the expected exit code.
464+
465+
::: code-group
466+
```bash [Example]
467+
function test_success_with_callable() {
468+
function foo() {
469+
return 1
470+
}
471+
472+
assert_unsuccessful_code "$(foo)"
473+
}
474+
475+
function test_success_without_callable() {
476+
function foo() {
477+
return 2
478+
}
479+
480+
foo # function took instead `callable`
481+
482+
assert_unsuccessful_code
483+
}
484+
485+
function test_failure() {
486+
function foo() {
487+
return 0
488+
}
489+
490+
assert_unsuccessful_code "$(foo)"
491+
}
492+
```
493+
:::
494+
456495
## assert_general_error
457496
> `assert_general_error ["callable"]`
458497

src/assert.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,20 @@ function assert_successful_code() {
378378
state::add_assertions_passed
379379
}
380380

381+
function assert_unsuccessful_code() {
382+
local actual_exit_code=${3-"$?"}
383+
384+
if [[ "$actual_exit_code" -eq 0 ]]; then
385+
local label
386+
label="$(helper::normalize_test_function_name "${FUNCNAME[1]}")"
387+
state::add_assertions_failed
388+
console_results::print_failed_test "${label}" "${actual_exit_code}" "to be non-zero" "but was 0"
389+
return
390+
fi
391+
392+
state::add_assertions_passed
393+
}
394+
381395
function assert_general_error() {
382396
local actual_exit_code=${3-"$?"}
383397
local expected_exit_code=1

tests/unit/assert_test.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,24 @@ function test_unsuccessful_assert_successful_code() {
247247
"$(assert_successful_code "$(fake_function)")"
248248
}
249249

250+
function test_successful_assert_unsuccessful_code() {
251+
function fake_function() {
252+
return 2
253+
}
254+
255+
assert_empty "$(assert_unsuccessful_code "$(fake_function)")"
256+
}
257+
258+
function test_unsuccessful_assert_unsuccessful_code() {
259+
function fake_function() {
260+
return 0
261+
}
262+
263+
assert_same\
264+
"$(console_results::print_failed_test "Unsuccessful assert unsuccessful code" "0" "to be non-zero" "but was 0")"\
265+
"$(assert_unsuccessful_code "$(fake_function)")"
266+
}
267+
250268
function test_successful_assert_general_error() {
251269
function fake_function() {
252270
return 1

0 commit comments

Comments
 (0)