Skip to content

Commit 9fd3794

Browse files
authored
Merge pull request #393 from elebeaup/fix/fix-false-negative
Fix false negative with `set -e`
2 parents 1e0b718 + d399082 commit 9fd3794

File tree

7 files changed

+55
-4
lines changed

7 files changed

+55
-4
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+
- Fixed false negative with `set -e`
56
- Fixed name rendered when having `test_test_*`
67
- Fixed duplicate function detection
78
- Fixed display test with multiple outputs in multiline

src/runner.sh

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,19 @@ function runner::run_test() {
146146
exec 3>&1
147147

148148
local test_execution_result=$(
149+
trap '
150+
state::set_test_exit_code $?
151+
runner::run_tear_down
152+
runner::clear_mocks
153+
state::export_subshell_context
154+
' EXIT
149155
state::initialize_assertions_count
150156
runner::run_set_up
151157
152158
# 2>&1: Redirects the std-error (FD 2) to the std-output (FD 1).
153159
# points to the original std-output.
154160
"$function_name" "$@" 2>&1
155161
156-
runner::run_tear_down
157-
runner::clear_mocks
158-
state::export_subshell_context
159162
)
160163

161164
# Closes FD 3, which was used temporarily to hold the original stdout.
@@ -216,8 +219,9 @@ function runner::run_test() {
216219
runner::parse_result "$function_name" "$test_execution_result" "$@"
217220

218221
local total_assertions="$(state::calculate_total_assertions "$test_execution_result")"
222+
local test_exit_code="$(state::get_test_exit_code)"
219223

220-
if [[ -n $runtime_error ]]; then
224+
if [[ -n $runtime_error || $test_exit_code -ne 0 ]]; then
221225
state::add_tests_failed
222226
console_results::print_error_test "$function_name" "$runtime_error"
223227
reports::add_test_failed "$test_file" "$function_name" "$duration" "$total_assertions"
@@ -360,13 +364,20 @@ function runner::parse_result_sync() {
360364
sed -E -e 's/.*##ASSERTIONS_SNAPSHOT=([0-9]*)##.*/\1/g'\
361365
)
362366

367+
local test_exit_code=$(\
368+
echo "$execution_result" |\
369+
tail -n 1 |\
370+
sed -E -e 's/.*##TEST_EXIT_CODE=([0-9]*)##.*/\1/g'\
371+
)
372+
363373
log "debug" "[SYNC]" "function_name:$function_name" "execution_result:$execution_result"
364374

365375
((_ASSERTIONS_PASSED += assertions_passed)) || true
366376
((_ASSERTIONS_FAILED += assertions_failed)) || true
367377
((_ASSERTIONS_SKIPPED += assertions_skipped)) || true
368378
((_ASSERTIONS_INCOMPLETE += assertions_incomplete)) || true
369379
((_ASSERTIONS_SNAPSHOT += assertions_snapshot)) || true
380+
((_TEST_EXIT_CODE += test_exit_code)) || true
370381
}
371382

372383
function runner::write_failure_result_output() {

src/state.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ _DUPLICATED_FUNCTION_NAMES=""
1414
_FILE_WITH_DUPLICATED_FUNCTION_NAMES=""
1515
_DUPLICATED_TEST_FUNCTIONS_FOUND=false
1616
_TEST_OUTPUT=""
17+
_TEST_EXIT_CODE=0
1718

1819
function state::get_tests_passed() {
1920
echo "$_TESTS_PASSED"
@@ -123,6 +124,14 @@ function state::add_test_output() {
123124
_TEST_OUTPUT+="$1"
124125
}
125126

127+
function state::get_test_exit_code() {
128+
echo "$_TEST_EXIT_CODE"
129+
}
130+
131+
function state::set_test_exit_code() {
132+
_TEST_EXIT_CODE="$1"
133+
}
134+
126135
function state::set_duplicated_functions_merged() {
127136
state::set_duplicated_test_functions_found
128137
state::set_file_with_duplicated_function_names "$1"
@@ -155,6 +164,7 @@ function state::export_subshell_context() {
155164
##ASSERTIONS_SKIPPED=$_ASSERTIONS_SKIPPED\
156165
##ASSERTIONS_INCOMPLETE=$_ASSERTIONS_INCOMPLETE\
157166
##ASSERTIONS_SNAPSHOT=$_ASSERTIONS_SNAPSHOT\
167+
##TEST_EXIT_CODE=$_TEST_EXIT_CODE\
158168
##TEST_OUTPUT=$encoded_test_output\
159169
##
160170
EOF

tests/acceptance/bashunit_fail_test.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ function test_bashunit_with_multiple_failing_tests() {
4545
assert_general_error "$(./bashunit --no-parallel --env "$TEST_ENV_FILE" "$test_file" --simple)"
4646
}
4747

48+
function test_bashunit_with_a_test_fail_and_exit_immediately() {
49+
local test_file=./tests/acceptance/fixtures/test_bashunit_when_exit_immediately_after_execution_error.sh
50+
51+
assert_match_snapshot "$(./bashunit --no-parallel --env "$TEST_ENV_FILE" "$test_file")"
52+
assert_general_error "$(./bashunit --no-parallel --env "$TEST_ENV_FILE" "$test_file" --simple)"
53+
}
54+
4855
function test_different_simple_snapshots_matches() {
4956
todo "The different snapshots for these tests should also be identical to each other, option to choose snapshot name?"
5057
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
function test_error() {
4+
set -e
5+
invalid_function_name arg1 arg2 &>/dev/null
6+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Running ./tests/acceptance/fixtures/test_bashunit_when_exit_immediately_after_execution_error.sh
2+
✗ Error: Error
3+

4+
5+
There was 1 failure:
6+
7+
|1) ./tests/acceptance/fixtures/test_bashunit_when_exit_immediately_after_execution_error.sh
8+
9+
Tests:  1 failed, 1 total
10+
Assertions: 0 failed, 0 total
11+
12+
 Some tests failed 

tests/unit/state_test.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ function test_initialize_assertions_count() {
254254
##ASSERTIONS_SKIPPED=0\
255255
##ASSERTIONS_INCOMPLETE=0\
256256
##ASSERTIONS_SNAPSHOT=0\
257+
##TEST_EXIT_CODE=0\
257258
##TEST_OUTPUT=\
258259
##"\
259260
"$export_assertions_count"
@@ -270,6 +271,7 @@ function test_export_assertions_count() {
270271
_ASSERTIONS_INCOMPLETE=12
271272
_ASSERTIONS_SNAPSHOT=33
272273
_ASSERTIONS_SNAPSHOT=33
274+
_TEST_EXIT_CODE=1
273275
_TEST_OUTPUT="something"
274276
275277
state::export_subshell_context
@@ -281,6 +283,7 @@ function test_export_assertions_count() {
281283
##ASSERTIONS_SKIPPED=42\
282284
##ASSERTIONS_INCOMPLETE=12\
283285
##ASSERTIONS_SNAPSHOT=33\
286+
##TEST_EXIT_CODE=1\
284287
##TEST_OUTPUT=$(echo -n "something" | base64)##"\
285288
"$export_assertions_count"
286289
}
@@ -291,6 +294,7 @@ function test_calculate_total_assertions() {
291294
##ASSERTIONS_SKIPPED=3\
292295
##ASSERTIONS_INCOMPLETE=4\
293296
##ASSERTIONS_SNAPSHOT=5\
297+
##TEST_EXIT_CODE=0\
294298
##TEST_OUTPUT=3zhbEncodedBase64##"
295299

296300
assert_same 15 "$(state::calculate_total_assertions "$input")"

0 commit comments

Comments
 (0)