Skip to content

Commit dcb2d83

Browse files
committed
refactor(coverage): simplify trap and improve hook coverage tests
- Simplify DEBUG trap to avoid redundant should_track calls - Remove duplicate default path logic from coverage::init - Fix escaped quotes in fixture file - Add end-to-end test for coverage hooks via external bashunit run
1 parent acd1cc5 commit dcb2d83

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

src/coverage.sh

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,6 @@ 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
9185
}
9286

9387
function bashunit::coverage::enable_trap() {
@@ -101,18 +95,7 @@ function bashunit::coverage::enable_trap() {
10195
# Set DEBUG trap to record line execution
10296
# Use ${VAR:-} to handle unset variables when set -u is active (in subshells)
10397
# shellcheck disable=SC2154
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
98+
trap 'bashunit::coverage::record_line "${BASH_SOURCE[0]:-}" "${LINENO:-}"' DEBUG
11699
}
117100

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

tests/acceptance/coverage_hooks_test.sh

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
#!/usr/bin/env bash
2+
# shellcheck disable=SC2034
23

34
function set_up_before_script() {
45
TEST_ENV_FILE="tests/acceptance/fixtures/.env.default"
56
LCOV_FILE="$(bashunit::temp_file "lcov-output")"
7+
LCOV_FILE_EXTERNAL="$(bashunit::temp_file "lcov-external")"
68
}
79

810
function test_coverage_includes_src_hits_from_setup_hook() {
911
# Enable coverage in-process and exercise code in a hook-like context
1012
BASHUNIT_COVERAGE=true
11-
BASHUNIT_COVERAGE_PATHS="" # force auto-discovery / fallback
13+
BASHUNIT_COVERAGE_PATHS="src/"
1214
bashunit::coverage::init
1315

1416
# Simulate hook execution with coverage trap enabled
@@ -26,3 +28,21 @@ function test_coverage_includes_src_hits_from_setup_hook() {
2628
assert_contains "SF:$(pwd)/src/" "$lcov"
2729
assert_contains "DA:" "$lcov"
2830
}
31+
32+
function test_coverage_hooks_via_external_bashunit() {
33+
# Run bashunit on fixture with coverage enabled and verify hook code is tracked
34+
local output
35+
output=$(BASHUNIT_COVERAGE=true \
36+
BASHUNIT_COVERAGE_PATHS="src/" \
37+
BASHUNIT_COVERAGE_REPORT="$LCOV_FILE_EXTERNAL" \
38+
./bashunit tests/acceptance/fixtures/test_coverage_hooks.sh --simple 2>&1)
39+
40+
assert_successful_code
41+
42+
# Verify LCOV file was created with coverage data
43+
assert_file_exists "$LCOV_FILE_EXTERNAL"
44+
local lcov
45+
lcov="$(cat "$LCOV_FILE_EXTERNAL" 2>/dev/null)"
46+
assert_contains "SF:" "$lcov"
47+
assert_contains "DA:" "$lcov"
48+
}

tests/acceptance/fixtures/test_coverage_hooks.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
function set_up() {
66
# Invoke src functions to generate attributable coverage hits
77
local f
8-
f="$(bashunit::temp_file \"cov-hooks\")"
8+
f="$(bashunit::temp_file "cov-hooks")"
99
[[ -n "${f:-}" ]] && echo "tmp created" > /dev/null
1010
}
1111

0 commit comments

Comments
 (0)