@@ -28,6 +28,10 @@ function bashunit::runner::load_test_files() {
2828 # Auto-discover coverage paths if not explicitly set
2929 if [[ -z " $BASHUNIT_COVERAGE_PATHS " ]]; then
3030 BASHUNIT_COVERAGE_PATHS=$( bashunit::coverage::auto_discover_paths " ${files[@]} " )
31+ # Fallback: if auto-discovery yields no paths, track the src/ folder
32+ if [[ -z " $BASHUNIT_COVERAGE_PATHS " ]]; then
33+ BASHUNIT_COVERAGE_PATHS=" src/"
34+ fi
3135 fi
3236 bashunit::coverage::init
3337 fi
@@ -564,6 +568,11 @@ function bashunit::runner::run_test() {
564568 [[ -f ~ /.profile ]] && source ~ /.profile 2> /dev/null || true
565569 fi
566570
571+ # Enable coverage tracking early to include set_up/tear_down hooks
572+ if bashunit::env::is_coverage_enabled; then
573+ bashunit::coverage::enable_trap
574+ fi
575+
567576 # Run set_up and capture exit code without || to preserve errexit behavior
568577 local setup_exit_code=0
569578 bashunit::runner::run_set_up " $test_file "
@@ -579,11 +588,6 @@ function bashunit::runner::run_test() {
579588 set +euo pipefail
580589 fi
581590
582- # Enable coverage tracking if enabled
583- if bashunit::env::is_coverage_enabled; then
584- bashunit::coverage::enable_trap
585- fi
586-
587591 # 2>&1: Redirects the std-error (FD 2) to the std-output (FD 1).
588592 # points to the original std-output.
589593 " $fn_name " " $@ " 2>&1
@@ -1037,10 +1041,20 @@ function bashunit::runner::run_set_up_before_script() {
10371041 local start_time
10381042 start_time=$( bashunit::clock::now)
10391043
1044+ # Enable coverage trap to attribute lines executed during set_up_before_script
1045+ if bashunit::env::is_coverage_enabled; then
1046+ bashunit::coverage::enable_trap
1047+ fi
1048+
10401049 # Execute the hook (render_header=false since header is already rendered)
10411050 bashunit::runner::execute_file_hook ' set_up_before_script' " $test_file " false
10421051 local status=$?
10431052
1053+ # Disable coverage trap after hook execution
1054+ if bashunit::env::is_coverage_enabled; then
1055+ bashunit::coverage::disable_trap
1056+ fi
1057+
10441058 local end_time
10451059 end_time=$( bashunit::clock::now)
10461060 local duration_ns=$(( end_time - start_time))
@@ -1162,10 +1176,20 @@ function bashunit::runner::run_tear_down_after_script() {
11621176 local start_time
11631177 start_time=$( bashunit::clock::now)
11641178
1179+ # Enable coverage trap to attribute lines executed during tear_down_after_script
1180+ if bashunit::env::is_coverage_enabled; then
1181+ bashunit::coverage::enable_trap
1182+ fi
1183+
11651184 # Execute the hook
11661185 bashunit::runner::execute_file_hook ' tear_down_after_script' " $test_file "
11671186 local status=$?
11681187
1188+ # Disable coverage trap after hook execution
1189+ if bashunit::env::is_coverage_enabled; then
1190+ bashunit::coverage::disable_trap
1191+ fi
1192+
11691193 local end_time
11701194 end_time=$( bashunit::clock::now)
11711195 local duration_ns=$(( end_time - start_time))
0 commit comments