Skip to content

Commit 9f1ebd1

Browse files
authored
Merge pull request #455 from TypedDevs/feat/add-line-number-to-failing-tests
Add line number to failing tests
2 parents dfd9b48 + 37a1ebc commit 9f1ebd1

17 files changed

+38
-18
lines changed

CHANGELOG.md

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

55
- Fix broken core snapshot tests
66
- Improve NixOS support
7+
- Add line number to failing tests
78

89
## [0.22.1](https://github.com/TypedDevs/bashunit/compare/0.22.0...0.22.1) - 2025-07-23
910

src/helpers.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,18 @@ function helper::load_bench_files() {
279279

280280
printf "%s\n" "${bench_files[@]}"
281281
}
282+
283+
#
284+
# @param $1 string function name
285+
# @return number line number of the function in the source file
286+
#
287+
function helper::get_function_line_number() {
288+
local fn_name=$1
289+
290+
shopt -s extdebug
291+
local line_number
292+
line_number=$(declare -F "$fn_name" | awk '{print $2}')
293+
shopt -u extdebug
294+
295+
echo "$line_number"
296+
}

src/runner.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,15 +295,15 @@ function runner::run_test() {
295295
state::add_tests_failed
296296
console_results::print_error_test "$fn_name" "$runtime_error"
297297
reports::add_test_failed "$test_file" "$fn_name" "$duration" "$total_assertions"
298-
runner::write_failure_result_output "$test_file" "$runtime_error"
298+
runner::write_failure_result_output "$test_file" "$fn_name" "$runtime_error"
299299
internal_log "Test error" "$fn_name" "$runtime_error"
300300
return
301301
fi
302302

303303
if [[ "$current_assertions_failed" != "$(state::get_assertions_failed)" ]]; then
304304
state::add_tests_failed
305305
reports::add_test_failed "$test_file" "$fn_name" "$duration" "$total_assertions"
306-
runner::write_failure_result_output "$test_file" "$subshell_output"
306+
runner::write_failure_result_output "$test_file" "$fn_name" "$subshell_output"
307307

308308
internal_log "Test failed" "$fn_name"
309309

@@ -466,14 +466,18 @@ function runner::parse_result_sync() {
466466

467467
function runner::write_failure_result_output() {
468468
local test_file=$1
469-
local error_msg=$2
469+
local fn_name=$2
470+
local error_msg=$3
471+
472+
local line_number
473+
line_number=$(helper::get_function_line_number "$fn_name")
470474

471475
local test_nr="*"
472476
if ! parallel::is_enabled; then
473477
test_nr=$(state::get_tests_failed)
474478
fi
475479

476-
echo -e "$test_nr) $test_file\n$error_msg" >> "$FAILURES_OUTPUT_PATH"
480+
echo -e "$test_nr) $test_file:$line_number\n$error_msg" >> "$FAILURES_OUTPUT_PATH"
477481
}
478482

479483
function runner::run_set_up() {

tests/acceptance/snapshots/bashunit_exit_code_test_sh.test_bashunit_when_a_test_returns_non_zero.snapshot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
There was 1 failure:
66

7-
|1) tests/acceptance/fixtures/test_bashunit_when_a_test_returns_non_zero.sh
7+
|1) tests/acceptance/fixtures/test_bashunit_when_a_test_returns_non_zero.sh:3
88

99
Tests:  1 failed, 1 total
1010
Assertions: 0 failed, 0 total

tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_simple_output_env.snapshot

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

33
There was 1 failure:
44

5-
|1) ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh
5+
|1) ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh:12
66
|✗ Failed: Assert failing
77
| Expected '1'
88
| but got  '0'

tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_simple_output_option.snapshot

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

33
There was 1 failure:
44

5-
|1) ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh
5+
|1) ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh:12
66
|✗ Failed: Assert failing
77
| Expected '1'
88
| but got  '0'

tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_verbose_output_env.snapshot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
There was 1 failure:
1111

12-
|1) ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh
12+
|1) ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh:12
1313
|✗ Failed: Assert failing
1414
| Expected '1'
1515
| but got  '0'

tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_when_a_test_fail_verbose_output_option.snapshot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
There was 1 failure:
1111

12-
|1) ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh
12+
|1) ./tests/acceptance/fixtures/test_bashunit_when_a_test_fail.sh:12
1313
|✗ Failed: Assert failing
1414
| Expected '1'
1515
| but got  '0'

tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_with_a_test_fail_and_exit_immediately.snapshot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
There was 1 failure:
66

7-
|1) ./tests/acceptance/fixtures/test_bashunit_when_exit_immediately_after_execution_error.sh
7+
|1) ./tests/acceptance/fixtures/test_bashunit_when_exit_immediately_after_execution_error.sh:3
88

99
Tests:  1 failed, 1 total
1010
Assertions: 0 failed, 0 total

tests/acceptance/snapshots/bashunit_fail_test_sh.test_bashunit_with_multiple_failing_tests.snapshot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
There was 1 failure:
1515

16-
|1) ./tests/acceptance/fixtures/test_bashunit_with_multiple_failing_tests.sh
16+
|1) ./tests/acceptance/fixtures/test_bashunit_with_multiple_failing_tests.sh:7
1717
|✗ Failed: Assert failing
1818
| Expected '1'
1919
| but got  '2'

0 commit comments

Comments
 (0)