Test aggregation and cleanup
Fail-fast stops after the first failure; aggregation collects results from independent tests before returning a final status. Dependent operations should still be skipped after prerequisites fail. Choose a documented policy for final statuses: this lab returns 1 when any required test fails, while preserving each individual code in the report. A skipped test is not a pass. CI must see the final nonzero exit status, even when diagnostics were successfully written.
Preserve status and clean up
Call each test in an explicit conditional and save its status immediately. Record the test name from a fixed list and numeric code. Do not parse free-form output to infer success. Capture outputs separately for diagnostics; avoid emitting raw secrets or untrusted log control sequences. A cleanup handler should capture $? first, clean only resources it owns, and preserve an existing failure. If cleanup fails after successful work, define whether that is a new failure; in this chapter it is. Keep diagnostic artifacts separately from transient resources. EXIT traps do not guarantee cleanup after SIGKILL or loss of the runner.
A runner may cancel other matrix jobs on failure or continue them depending on CI policy; the local loop only models sequential independent tests. Use bounded deadlines, isolate tests and avoid leaving background processes behind. No actual CI jobs are scheduled. The cleanup drill registers its trap immediately after creating the temporary directory, checks the marker write, and verifies both status 7 and absence of the owned directory including a dangling symlink. A reporting failure must not be presented as a completed test report.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code. Git, GNU tar, gzip and coreutils are external dependencies for the relevant examples. Do not run these in an existing repository or CI workspace.
Collect all independent results
run_tests() { local code failed=0 for code in 0 7 0; do if ( exit "$code" ); then printf 'pass\n' || return 2 else printf 'fail=%s\n' "$?" || return 2 failed=1 fi done return "$failed"}if run_tests; then exit 1; else rc=$? [[ $rc == 1 ]] || exit 1 printf 'aggregate=%s\n' "$rc"fiExpected stdout:
passfail=7passaggregate=1The wrapper demonstrates the nonzero aggregate status without failing the lab itself.
Stop at the first failure
for code in 0 4 0; do if ( exit "$code" ); then printf 'pass\n' else printf 'stop=%s\n' "$?" break fidoneExpected stdout:
passstop=4The final case is not executed. Real CI must propagate the failure.
Preserve failure through cleanup
( work=$(mktemp -d ./work.XXXXXX) || exit 1 cleanup() { local rc=$? rm -f -- "$work/item" && rmdir -- "$work" || { (( rc != 0 )) || rc=1; } exit "$rc" } trap cleanup EXIT printf '%s\n' "$work" > owned.path || exit 1 printf 'temp\n' > "$work/item" || exit 1 exit 7)rc=$?[[ $rc == 7 ]] || exit 1IFS= read -r owned < owned.path || exit 1[[ -n $owned && ! -e $owned && ! -L $owned ]] || exit 1printf 'status=%s cleaned\n' "$rc"Expected stdout:
status=7 cleanedThe EXIT trap removes the owned fixture and preserves status 7.
Common mistakes
Do not let a successful final printf mask earlier failures; do not count skipped tests as passed.
Practice
Change two test statuses to nonzero and compare the fail-fast report with aggregation. Verify the final status and ensure the owned temporary directory is gone.
Next: Cloud and container automation