Skip to content

Commit acd1cc5

Browse files
author
Omar Gallo
committed
test: improve coverage
1 parent 8140d00 commit acd1cc5

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

src/coverage.sh

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ function bashunit::coverage::init() {
8282
export _BASHUNIT_COVERAGE_TRACKED_FILES
8383
export _BASHUNIT_COVERAGE_TRACKED_CACHE_FILE
8484
export _BASHUNIT_COVERAGE_TEST_HITS_FILE
85+
86+
# Ensure we have inclusion paths; if none provided or auto-discovered,
87+
# default to tracking the src/ folder to avoid empty reports
88+
if [[ -z "${BASHUNIT_COVERAGE_PATHS:-}" ]]; then
89+
BASHUNIT_COVERAGE_PATHS="src/"
90+
fi
8591
}
8692

8793
function bashunit::coverage::enable_trap() {
@@ -95,7 +101,18 @@ function bashunit::coverage::enable_trap() {
95101
# Set DEBUG trap to record line execution
96102
# Use ${VAR:-} to handle unset variables when set -u is active (in subshells)
97103
# shellcheck disable=SC2154
98-
trap 'bashunit::coverage::record_line "${BASH_SOURCE:-}" "${LINENO:-}"' DEBUG
104+
trap '
105+
# Prefer immediate callee frame; fall back to caller when current file is excluded
106+
local __cov_file="${BASH_SOURCE[0]:-}"
107+
local __cov_line="${LINENO:-}"
108+
if [[ -n "$__cov_file" ]] && ! bashunit::coverage::should_track "$__cov_file"; then
109+
# Try next stack frame if available (e.g., called function from a tracked src file)
110+
if [[ -n "${BASH_SOURCE[1]:-}" ]]; then
111+
__cov_file="${BASH_SOURCE[1]}"
112+
fi
113+
fi
114+
bashunit::coverage::record_line "$__cov_file" "$__cov_line"
115+
' DEBUG
99116
}
100117

101118
function bashunit::coverage::disable_trap() {

src/runner.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ function bashunit::runner::load_test_files() {
2323
# Auto-discover coverage paths if not explicitly set
2424
if [[ -z "$BASHUNIT_COVERAGE_PATHS" ]]; then
2525
BASHUNIT_COVERAGE_PATHS=$(bashunit::coverage::auto_discover_paths "${files[@]}")
26+
# Fallback: if auto-discovery yields no paths, track the src/ folder
27+
if [[ -z "$BASHUNIT_COVERAGE_PATHS" ]]; then
28+
BASHUNIT_COVERAGE_PATHS="src/"
29+
fi
2630
fi
2731
bashunit::coverage::init
2832
fi
@@ -850,8 +854,16 @@ function bashunit::runner::execute_file_hook() {
850854
trap '_BASHUNIT_HOOK_ERR_STATUS=$?; set +eE; trap - ERR; return $_BASHUNIT_HOOK_ERR_STATUS' ERR
851855

852856
{
857+
# Enable coverage trap inside hooks to attribute lines executed during setup/teardown
858+
if bashunit::env::is_coverage_enabled; then
859+
bashunit::coverage::enable_trap
860+
fi
853861
"$hook_name"
854862
} >"$hook_output_file" 2>&1
863+
# Disable coverage trap after hook execution
864+
if bashunit::env::is_coverage_enabled; then
865+
bashunit::coverage::disable_trap
866+
fi
855867

856868
# Capture exit status from global variable and clean up
857869
status=$_BASHUNIT_HOOK_ERR_STATUS
@@ -943,8 +955,16 @@ function bashunit::runner::execute_test_hook() {
943955
trap '_BASHUNIT_HOOK_ERR_STATUS=$?; set +eE; trap - ERR; return $_BASHUNIT_HOOK_ERR_STATUS' ERR
944956

945957
{
958+
# Enable coverage trap inside test-level hooks
959+
if bashunit::env::is_coverage_enabled; then
960+
bashunit::coverage::enable_trap
961+
fi
946962
"$hook_name"
947963
} >"$hook_output_file" 2>&1
964+
# Disable coverage trap after hook execution
965+
if bashunit::env::is_coverage_enabled; then
966+
bashunit::coverage::disable_trap
967+
fi
948968

949969
# Capture exit status from global variable and clean up
950970
status=$_BASHUNIT_HOOK_ERR_STATUS
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
3+
function set_up_before_script() {
4+
TEST_ENV_FILE="tests/acceptance/fixtures/.env.default"
5+
LCOV_FILE="$(bashunit::temp_file "lcov-output")"
6+
}
7+
8+
function test_coverage_includes_src_hits_from_setup_hook() {
9+
# Enable coverage in-process and exercise code in a hook-like context
10+
BASHUNIT_COVERAGE=true
11+
BASHUNIT_COVERAGE_PATHS="" # force auto-discovery / fallback
12+
bashunit::coverage::init
13+
14+
# Simulate hook execution with coverage trap enabled
15+
bashunit::coverage::enable_trap
16+
# Call a src function to generate attributable hits
17+
local f
18+
f="$(bashunit::temp_file "cov-hooks")"
19+
[[ -n "${f:-}" ]] && echo "tmp created" > /dev/null
20+
bashunit::coverage::disable_trap
21+
22+
# Generate LCOV and assert presence of src entries
23+
bashunit::coverage::report_lcov "$LCOV_FILE"
24+
local lcov
25+
lcov="$(cat "$LCOV_FILE" 2>/dev/null)"
26+
assert_contains "SF:$(pwd)/src/" "$lcov"
27+
assert_contains "DA:" "$lcov"
28+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
3+
# This fixture exercises coverage attribution inside lifecycle hooks.
4+
5+
function set_up() {
6+
# Invoke src functions to generate attributable coverage hits
7+
local f
8+
f="$(bashunit::temp_file \"cov-hooks\")"
9+
[[ -n "${f:-}" ]] && echo "tmp created" > /dev/null
10+
}
11+
12+
function test_noop() {
13+
# No-op test; coverage should still attribute lines from set_up
14+
assert_true true
15+
}

0 commit comments

Comments
 (0)