Skip to content

Commit 1d7a4ce

Browse files
committed
feat(runner): show source context in failure summary
When a test fails, the failure summary now shows the assertion lines from the test function, making it easier to identify which assertion caused the failure without opening the source file.
1 parent 9a8f990 commit 1d7a4ce

13 files changed

Lines changed: 68 additions & 1 deletion

src/runner.sh

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,43 @@ function bashunit::runner::write_failure_result_output() {
936936
output_section="\n Output:\n$raw_output"
937937
fi
938938

939-
echo -e "$test_nr) $test_file:$line_number\n$error_msg$output_section" >>"$FAILURES_OUTPUT_PATH"
939+
local source_context=""
940+
if [[ -n "$line_number" && -f "$test_file" ]]; then
941+
source_context=$(bashunit::runner::get_failure_source_context \
942+
"$test_file" "$line_number")
943+
fi
944+
945+
echo -e "$test_nr) $test_file:$line_number\n$error_msg$output_section$source_context" \
946+
>>"$FAILURES_OUTPUT_PATH"
947+
}
948+
949+
function bashunit::runner::get_failure_source_context() {
950+
local file=$1
951+
local fn_line=$2
952+
953+
local end_line start_line
954+
end_line=$(wc -l <"$file")
955+
start_line=$((fn_line + 1))
956+
957+
local line_text line_num assert_lines=""
958+
line_num=$start_line
959+
while [[ $line_num -le $end_line ]]; do
960+
line_text=$(sed -n "${line_num}p" "$file")
961+
# Stop at the closing brace of the function
962+
if [[ "$line_text" =~ ^[[:space:]]*\}[[:space:]]*$ ]]; then
963+
break
964+
fi
965+
# Collect lines containing assert calls
966+
if [[ "$line_text" == *assert_* ]] || [[ "$line_text" == *assert\ * ]]; then
967+
local trimmed="${line_text#"${line_text%%[![:space:]]*}"}"
968+
assert_lines="${assert_lines}\n ${_BASHUNIT_COLOR_FAINT}${line_num}:${_BASHUNIT_COLOR_DEFAULT} ${trimmed}"
969+
fi
970+
line_num=$((line_num + 1))
971+
done
972+
973+
if [[ -n "$assert_lines" ]]; then
974+
echo -e "\n ${_BASHUNIT_COLOR_FAINT}Source:${_BASHUNIT_COLOR_DEFAULT}${assert_lines}"
975+
fi
940976
}
941977

942978
function bashunit::runner::write_skipped_result_output() {

tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_simple_output_env.snapshot

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
|✗ Failed: Assert failing
77
| Expected '1'
88
| but got  '0'
9+
| Source:
10+
| 13: assert_same 1 0
911

1012

1113

tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_simple_output_option.snapshot

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
|✗ Failed: Assert failing
77
| Expected '1'
88
| but got  '0'
9+
| Source:
10+
| 13: assert_same 1 0
911

1012

1113

tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_verbose_output_env.snapshot

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
|✗ Failed: Assert failing
1414
| Expected '1'
1515
| but got  '0'
16+
| Source:
17+
| 13: assert_same 1 0
1618

1719
Tests:  4 passed, 1 failed, 5 total
1820
Assertions: 6 passed, 1 failed, 7 total

tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_verbose_output_option.snapshot

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
|✗ Failed: Assert failing
1414
| Expected '1'
1515
| but got  '0'
16+
| Source:
17+
| 13: assert_same 1 0
1618

1719
Tests:  4 passed, 1 failed, 5 total
1820
Assertions: 6 passed, 1 failed, 7 total

tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_with_multiple_failing_tests.snapshot

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
|✗ Failed: Assert failing
1515
| Expected '1'
1616
| but got  '2'
17+
| Source:
18+
| 8: assert_same 1 2
19+
| 9: assert_same 3 4
1720

1821
Tests:  1 passed, 0 skipped, 2 incomplete, 1 failed, 4 total
1922
Assertions: 1 passed, 2 skipped, 2 incomplete, 1 failed, 6 total

tests/acceptance/snapshots/bashunit_log_junit_test_sh.test_bashunit_when_log_junit_env.snapshot

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
|✗ Failed: Failure
1111
| Expected '2'
1212
| but got  '3'
13+
| Source:
14+
| 8: assert_same 2 3
1315

1416
Tests:  1 passed, 1 failed, 2 total
1517
Assertions: 1 passed, 1 failed, 2 total

tests/acceptance/snapshots/bashunit_log_junit_test_sh.test_bashunit_when_log_junit_option.snapshot

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
|✗ Failed: Failure
1111
| Expected '2'
1212
| but got  '3'
13+
| Source:
14+
| 8: assert_same 2 3
1315

1416
Tests:  1 passed, 1 failed, 2 total
1517
Assertions: 1 passed, 1 failed, 2 total

tests/acceptance/snapshots/bashunit_report_html_test_sh.test_bashunit_when_report_html_env.snapshot

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
|✗ Failed: Fail
1313
| Expected 'to be empty'
1414
| but got  'non empty'
15+
| Source:
16+
| 8: assert_empty "non empty"
1517

1618
Tests:  1 passed, 1 skipped, 1 incomplete, 1 failed, 4 total
1719
Assertions: 1 passed, 1 skipped, 1 incomplete, 1 failed, 4 total

tests/acceptance/snapshots/bashunit_report_html_test_sh.test_bashunit_when_report_html_option.snapshot

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
|✗ Failed: Fail
1313
| Expected 'to be empty'
1414
| but got  'non empty'
15+
| Source:
16+
| 8: assert_empty "non empty"
1517

1618
Tests:  1 passed, 1 skipped, 1 incomplete, 1 failed, 4 total
1719
Assertions: 1 passed, 1 skipped, 1 incomplete, 1 failed, 4 total

0 commit comments

Comments
 (0)