testsAndMisc/meta/scripts/fixtures/set_e_function_tail.sh
Krzysztof kuhy Rudnicki f6356d4725
Some checks are pending
Pre-commit checks / pre-commit (push) Waiting to run
Shell tests / shell-tests (push) Waiting to run
Shell tests / arch-tests (push) Waiting to run
Trace scripts that place files, via a prefix redirect
Shell redirections are builtins, so no PATH stub can intercept
`cat > /etc/modprobe.d/x.conf`. That left installer scripts unverifiable:
stub cp/chmod/ln and the trace is nearly empty, don't and the run rewrites
the live system.

--prefix redirects HOME and every XDG_* base into a temp tree. XDG_DATA_HOME
is exported explicitly, not left to default off HOME: install_leechblock.sh
reads it directly and then runs `rsync -a --delete` at it.

--bind-abs binds hardcoded absolutes via bwrap --unshare-user --uid 0, leaf
directories only. uid 0 is load-bearing beyond permissions: common.sh's
require_root does `exec sudo "$0"`, and under the sudo stub that truncated
the whole run to one recorded call -- a trace that diffs clean against any
split. Binding /etc wholesale was measured to shadow /etc/passwd, collapse
$SUDO_USER to /home//, and mask a nested bind, so the scanner never emits it.

The trace gains a manifest of every file written, with content hashes. That
section is the point: with it, a dropped write in the fixture pair is a
one-line diff; with it stripped, the two traces are identical, i.e. the old
harness passed that broken split. Hashes normalize the prefix to @PREFIX@
because install_usage_monitoring.sh writes an unquoted heredoc that bakes
$HOME into the artifact.

Verified: ~/.config/systemd/user, ~/.local/bin, ~/.local/share and
/etc/modprobe.d byte-identical before and after; usage-report units still 2.
The three prior regressions are now committed fixtures rather than ad-hoc --
one was silently neutered mid-change when a trailing echo added for SC2034
became the function's last statement and turned exit 1 into exit 0.

Stub generation moved to lib/ because --prefix pushed the harness to 252
lines, over the cap it exists to serve.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011w2Pj4Ek3ZNMQVYcFHgXDY
2026-08-16 23:31:49 +02:00

33 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Regression fixture: the `set -e` function-tail abort.
#
# Reproduces the analyze_repo.sh bug. Eight `((MAP[x] > 0)) && HAS_Y=true` lines
# were top-level statements, where a false `&&` merely leaves $? non-zero.
# Wrapped into a function they become its RETURN VALUE, and the last one --
# false for most inputs -- kills the script under set -e.
#
# Traced, this shows up as exit 1 with every later call missing from the trace.
set -euo pipefail
declare -A LANG_FILES=([python]=3 [java]=0)
detect_languages() {
local HAS_PYTHON=false HAS_JAVA=false
((LANG_FILES[python] > 0)) && HAS_PYTHON=true
# Read both flags BEFORE the failing tail, not after. An earlier revision
# put `echo "java=$HAS_JAVA"` at the end to satisfy SC2034 -- which made
# that echo the function's last statement, gave it exit 0, and silently
# deleted the very bug this fixture exists to reproduce. The trace went
# from exit 1 to exit 0 and the check passed while testing nothing. Keep
# the bare conditional last.
echo "python=$HAS_PYTHON java=$HAS_JAVA"
# The tail. False, so the function returns 1 and set -e aborts the script.
((LANG_FILES[java] > 0)) && HAS_JAVA=true
}
detect_languages
echo "reached the line after detect_languages"
systemctl status something