#!/usr/bin/env bash # shellcheck disable=SC2317 # Subshell tracking edge cases. # bashunit relies on `set -T` plus the DEBUG trap so child shell contexts # inherit the recorder. These tests pin the documented behavior so future # regressions surface as failing tests instead of silent gaps. _ORIG_COVERAGE_DATA_FILE="" _ORIG_COVERAGE_TRACKED_FILES="" _ORIG_COVERAGE_TRACKED_CACHE_FILE="" _ORIG_COVERAGE_TEST_HITS_FILE="" _ORIG_COVERAGE="" _ORIG_COVERAGE_PATHS="" _ORIG_COVERAGE_EXCLUDE="" _ORIG_COVERAGE_ENGINE="" # Whole-suite skip: enabling the DEBUG trap inside a parallel test # worker process makes the worker fire the trap on every internal # coordination command, which combines with file-I/O contention on # /tmp to deadlock CI runners. The contracts tested here are # deterministic in single-process mode, so the parallel run is not # a useful execution context. function _skip_when_unsupported_context() { if [ "${BASHUNIT_PARALLEL_RUN:-false}" = "true" ]; then bashunit::skip "subshell tracking tests require single-process execution" return 0 fi case "$(uname -s 2>/dev/null)" in CYGWIN* | MINGW* | MSYS*) bashunit::skip "DEBUG trap + set -T behavior is unstable on Git Bash" return 0 ;; esac # Before Bash 4 the DEBUG trap does not reach a subshell even under `set -T`, # so the lines inside one are never recorded and there is nothing to assert. # This was written as a macOS skip, on the assumption that it was the system # Bash 3.2 being odd -- the Bash 3.0 job shows it is the version, not the OS. if [ "${BASH_VERSINFO[0]:-0}" -lt 4 ]; then bashunit::skip "the DEBUG trap does not reach subshells before Bash 4" return 0 fi return 1 } function set_up() { _ORIG_COVERAGE_DATA_FILE="$_BASHUNIT_COVERAGE_DATA_FILE" _ORIG_COVERAGE_TRACKED_FILES="$_BASHUNIT_COVERAGE_TRACKED_FILES" _ORIG_COVERAGE_TRACKED_CACHE_FILE="$_BASHUNIT_COVERAGE_TRACKED_CACHE_FILE" _ORIG_COVERAGE_TEST_HITS_FILE="$_BASHUNIT_COVERAGE_TEST_HITS_FILE" _ORIG_COVERAGE="${BASHUNIT_COVERAGE:-}" _ORIG_COVERAGE_PATHS="${BASHUNIT_COVERAGE_PATHS:-}" _ORIG_COVERAGE_EXCLUDE="${BASHUNIT_COVERAGE_EXCLUDE:-}" _ORIG_COVERAGE_ENGINE="${BASHUNIT_COVERAGE_ENGINE:-}" # These assert on data files right after disable_trap, which is the trap # engine's contract. The xtrace engine only writes records in # coverage::finalize at the end of a run; it is covered end-to-end by # tests/acceptance/coverage_engine_test.sh instead. export BASHUNIT_COVERAGE_ENGINE="trap" _BASHUNIT_COVERAGE_DATA_FILE="" _BASHUNIT_COVERAGE_TRACKED_FILES="" _BASHUNIT_COVERAGE_TRACKED_CACHE_FILE="" _BASHUNIT_COVERAGE_TEST_HITS_FILE="" export BASHUNIT_COVERAGE="true" export BASHUNIT_COVERAGE_PATHS="${TMPDIR:-/tmp}" export BASHUNIT_COVERAGE_EXCLUDE="*_test.sh" } function tear_down() { trap - DEBUG set +T if [ -n "$_BASHUNIT_COVERAGE_DATA_FILE" ] && [ "$_BASHUNIT_COVERAGE_DATA_FILE" != "$_ORIG_COVERAGE_DATA_FILE" ]; then local coverage_dir coverage_dir=$(dirname "$_BASHUNIT_COVERAGE_DATA_FILE") rm -rf "$coverage_dir" 2>/dev/null || true fi _BASHUNIT_COVERAGE_DATA_FILE="$_ORIG_COVERAGE_DATA_FILE" _BASHUNIT_COVERAGE_TRACKED_FILES="$_ORIG_COVERAGE_TRACKED_FILES" _BASHUNIT_COVERAGE_TRACKED_CACHE_FILE="$_ORIG_COVERAGE_TRACKED_CACHE_FILE" _BASHUNIT_COVERAGE_TEST_HITS_FILE="$_ORIG_COVERAGE_TEST_HITS_FILE" if [ -n "$_ORIG_COVERAGE" ]; then export BASHUNIT_COVERAGE="$_ORIG_COVERAGE" else unset BASHUNIT_COVERAGE fi if [ -n "$_ORIG_COVERAGE_PATHS" ]; then export BASHUNIT_COVERAGE_PATHS="$_ORIG_COVERAGE_PATHS" else unset BASHUNIT_COVERAGE_PATHS fi if [ -n "$_ORIG_COVERAGE_EXCLUDE" ]; then export BASHUNIT_COVERAGE_EXCLUDE="$_ORIG_COVERAGE_EXCLUDE" else unset BASHUNIT_COVERAGE_EXCLUDE fi if [ -n "$_ORIG_COVERAGE_ENGINE" ]; then export BASHUNIT_COVERAGE_ENGINE="$_ORIG_COVERAGE_ENGINE" else unset BASHUNIT_COVERAGE_ENGINE fi } # Helper: run a fixture under coverage tracking and return how many # distinct hit lines were recorded for it. function _run_fixture_under_coverage() { local fixture="$1" bashunit::coverage::init echo "$fixture" >>"$_BASHUNIT_COVERAGE_TRACKED_FILES" bashunit::coverage::enable_trap # shellcheck disable=SC1090 source "$fixture" >/dev/null 2>&1 bashunit::coverage::disable_trap # When the suite itself runs in parallel mode, hits are flushed to a # per-PID data file. Aggregate so the assertion below sees them. bashunit::coverage::aggregate_parallel bashunit::coverage::get_all_line_hits "$fixture" | wc -l | tr -d ' ' } function test_coverage_records_lines_inside_command_substitution() { _skip_when_unsupported_context && return 0 local fixture fixture=$(mktemp) cat >"$fixture" <<'EOF' result=$(echo "inside-subst") echo "after $result" EOF local hit_count hit_count=$(_run_fixture_under_coverage "$fixture") # Documented limitation: the outer line containing $(...) is recorded, # but the command inside the subshell does not propagate hits back to # the parent's coverage data file. Both outer lines should be hit. assert_equals "2" "$hit_count" rm -f "$fixture" } function test_coverage_records_explicit_subshell_block() { _skip_when_unsupported_context && return 0 local fixture fixture=$(mktemp) cat >"$fixture" <<'EOF' ( echo "in subshell" ) echo "after" EOF local hit_count hit_count=$(_run_fixture_under_coverage "$fixture") # Both lines: the one inside ( ... ) and the trailing echo. Records used to # go into an in-memory buffer that died with the subshell, so the inner line # was lost and this asserted 1 (#1101). assert_equals "2" "$hit_count" rm -f "$fixture" } function test_coverage_records_pipeline_lhs() { _skip_when_unsupported_context && return 0 local fixture fixture=$(mktemp) cat >"$fixture" <<'EOF' echo "one" | cat >/dev/null echo "two" EOF local hit_count hit_count=$(_run_fixture_under_coverage "$fixture") # Each pipeline source line is recorded once (the pipeline as a unit). assert_equals "2" "$hit_count" rm -f "$fixture" } function test_coverage_records_process_substitution_consumer() { _skip_when_unsupported_context && return 0 local fixture fixture=$(mktemp) cat >"$fixture" <<'EOF' while read -r _line; do : "$_line" done < <(echo "a") echo "after" EOF local hit_count hit_count=$(_run_fixture_under_coverage "$fixture") # The `while` line, the loop body, the trailing echo, and the `echo "a"` # inside <(...) -- that last one used to die with its subshell (#1101). assert_equals "4" "$hit_count" rm -f "$fixture" } function test_coverage_records_lines_inside_function_called_from_subshell() { _skip_when_unsupported_context && return 0 local fixture fixture=$(mktemp) cat >"$fixture" <<'EOF' function _sub_helper() { echo "in helper" } result=$(_sub_helper) echo "after $result" EOF local hit_count hit_count=$(_run_fixture_under_coverage "$fixture") # The declaration, the caller, the trailing echo, and the helper body that # runs inside the $(...) subshell -- the body used to be lost with the # subshell buffer, so this asserted 2 (#1101). assert_equals "4" "$hit_count" rm -f "$fixture" }