From ac1cf5765623f389742af067aefc00521a5c8c9d Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Sat, 22 Aug 2026 13:14:02 +0200 Subject: [PATCH] Give testsAndMisc-bash/lib a harness; cover transcribe_deps and _pkgmgr The directory held five allowlisted libs and no tests. It now has a harness, a runner, and suites for two libs: transcribe_deps.sh (the fourth hardest-first target, 48/52 = 92.31%) and transcribe_pkgmgr.sh (49/50 = 98.00%). Both stay on the allowlist. 15 + 18 assertions, 0 failures. No jail is needed here and no jail_args marker is created: every branch turns on `command -v ` and every install goes through sudo, both intercepted by a PATH stub dir. Verified the runner exits 0 when invoked bare, which is how both gates call it. The harness REPLACES PATH rather than prepending. This host really has pacman, and a prepended stub dir cannot HIDE a binary, so every "manager not installed" case would have asserted nothing. `bash` and `env` are in the private coreutils dir for a non-obvious reason: every stub's `#!/usr/bin/env bash` shebang resolves through that restricted PATH, and without them each stub died with "env: 'bash': No such file or directory" while looking exactly like a coverage gap. Assertions are on recorded ARGV rather than on the call happening, because which packages a branch installs is what differs between distros -- pacman gets python-virtualenv where apt gets python3-venv. Three assertions aimed at the libsndfile branch were REMOVED rather than left green-but-vacuous: /usr/lib/libsndfile.so exists on this host, so need_libsndfile is legitimately 0 and lines 19/52/53/55 are unreachable without a jail that hides the library. The limitation is written into the suite instead of asserted around. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_016sxxsDHWgdRhmdmzNMA6XM --- .../transcribe-lib-harness-2026-08-22.json | 20 +++ .../transcribe-lib-tests-2026-08-22.json | 53 ++++++++ .../testsAndMisc-bash/lib/tests/run_all.sh | 28 ++++ .../lib/tests/test_transcribe_deps.sh | 121 ++++++++++++++++++ .../lib/tests/test_transcribe_pkgmgr.sh | 117 +++++++++++++++++ .../lib/tests/transcribe_harness.sh | 116 +++++++++++++++++ 6 files changed, 455 insertions(+) create mode 100644 docs/superpowers/contracts/transcribe-lib-harness-2026-08-22.json create mode 100644 docs/superpowers/evidence/transcribe-lib-tests-2026-08-22.json create mode 100755 linux_configuration/scripts/single_use/misc/testsAndMisc-bash/lib/tests/run_all.sh create mode 100755 linux_configuration/scripts/single_use/misc/testsAndMisc-bash/lib/tests/test_transcribe_deps.sh create mode 100755 linux_configuration/scripts/single_use/misc/testsAndMisc-bash/lib/tests/test_transcribe_pkgmgr.sh create mode 100755 linux_configuration/scripts/single_use/misc/testsAndMisc-bash/lib/tests/transcribe_harness.sh diff --git a/docs/superpowers/contracts/transcribe-lib-harness-2026-08-22.json b/docs/superpowers/contracts/transcribe-lib-harness-2026-08-22.json new file mode 100644 index 00000000..730ba050 --- /dev/null +++ b/docs/superpowers/contracts/transcribe-lib-harness-2026-08-22.json @@ -0,0 +1,20 @@ +{ + "title": "Give testsAndMisc-bash/lib a test harness and cover its two package-manager libs", + "objective": "Create the first tests/run_all.sh for linux_configuration/scripts/single_use/misc/testsAndMisc-bash/lib -- a directory holding five allowlisted libs and no tests -- and use it to cover transcribe_deps.sh (the fourth hardest-first target) and transcribe_pkgmgr.sh.", + "acceptance_criteria": [ + "tests/run_all.sh exists and exits 0 when invoked BARE, because no jail_args marker is created and both ci_mirror.sh and shell-tests.yml therefore run it directly.", + "Every assertion passes: test_transcribe_deps.sh and test_transcribe_pkgmgr.sh both report 0 failed.", + "Each package-manager branch is asserted on its RECORDED ARGV, not merely on the manager being called -- pacman must be shown asking for python-virtualenv where apt asks for python3-venv.", + "The 'not installed' cases genuinely hide the binary: PATH is replaced, not prepended, since this host really has pacman.", + "Coverage of both libs is measured through run_all.sh, the subject is_covered() builds, and reported as the measured figure.", + "Both libs remain on the allowlist unless they measure 100%.", + "No assertion is left in place that cannot fail on this host; an unreachable branch is documented rather than asserted vacuously.", + "shellcheck clean on every new file with no suppressions, and all files within the 250-line cap." + ], + "out_of_scope": [ + "The other three libs in the directory (clean_audio_filters.sh, transcribe_venv.sh, unity_handler.sh) -- they inherit the harness but are not covered here.", + "Any change to transcribe_deps.sh or transcribe_pkgmgr.sh; no latent bug surfaced in either.", + "Making the directory jail-only in order to hide /usr/lib/libsndfile.so and reach the four libsndfile lines." + ], + "verifier": "bash .../testsAndMisc-bash/lib/tests/run_all.sh (expects 15 + 18 assertions, 0 failed, exit 0), plus meta/scripts/shell_coverage_jail.sh --subject that runner --measure each lib --fail-on-case-error, plus shellcheck on the four new files and meta/scripts/check_file_length.sh --all." +} diff --git a/docs/superpowers/evidence/transcribe-lib-tests-2026-08-22.json b/docs/superpowers/evidence/transcribe-lib-tests-2026-08-22.json new file mode 100644 index 00000000..f49ad77c --- /dev/null +++ b/docs/superpowers/evidence/transcribe-lib-tests-2026-08-22.json @@ -0,0 +1,53 @@ +{ + "intent": "Give the testsAndMisc-bash/lib directory its first test harness and cover transcribe_deps.sh (score 50, the fourth hardest-first target) plus transcribe_pkgmgr.sh alongside it. Both stay on the allowlist: 92.31% and 98.00%, below the 100% bar.", + "scope": [ + "linux_configuration/scripts/single_use/misc/testsAndMisc-bash/lib/tests/transcribe_harness.sh: new shared fixture", + ".../tests/run_all.sh: new runner, deliberately WITHOUT a jail_args marker", + ".../tests/test_transcribe_deps.sh: new, 15 assertions", + ".../tests/test_transcribe_pkgmgr.sh: new, 18 assertions", + "Non-goal: the other three libs in the directory (clean_audio_filters, transcribe_venv, unity_handler) -- the harness they need now exists", + "Non-goal: no change to either subject; no latent bug surfaced" + ], + "changes": [ + "These suites need NO jail: every branch turns on `command -v ` and every install goes through sudo, both intercepted by a PATH stub dir. No jail_args file is created, so ci_mirror.sh and shell-tests.yml both run this runner bare -- verified, exit 0.", + "The harness REPLACES PATH rather than prepending to it. This host really has pacman, and a prepended stub dir cannot HIDE a binary, so every 'manager not installed' case would silently assert nothing. A private symlink dir supplies the coreutils the suites themselves need.", + "`bash` and `env` are in that dir for a non-obvious reason: every stub carries a `#!/usr/bin/env bash` shebang resolved through the restricted PATH. Without them each stub died with \"env: 'bash': No such file or directory\" and the branch it stood in for was silently never taken -- a failure that looked like a coverage gap.", + "Assertions are on RECORDED ARGV, not on the call happening: which packages a branch installs is what differs between distros (pacman gets python-virtualenv where apt gets python3-venv) and is exactly what a presence test cannot see.", + "ensure_cuda_runtime calls `exit 6` when cuBLAS is still missing. Called at top level that exits the SUITE, so each branch assertion runs in a subshell." + ], + "verification": [ + { + "command": ".../lib/tests/run_all.sh", + "result": "pass", + "evidence": "test_transcribe_deps.sh: 15 passed, 0 failed; test_transcribe_pkgmgr.sh: 18 passed, 0 failed; 'All transcribe lib test files passed.' Bare runner exit=0, which is how both gates invoke it." + }, + { + "command": "shell_coverage_jail.sh --subject .../tests/run_all.sh --measure transcribe_deps.sh --min 1 --fail-on-case-error", + "result": "pass", + "evidence": "transcribe_deps.sh: 48/52 = 92.31%, uncovered 19, 52, 53, 55. Measured through run_all.sh, the subject is_covered() builds." + }, + { + "command": "same, --measure transcribe_pkgmgr.sh", + "result": "pass", + "evidence": "transcribe_pkgmgr.sh: 49/50 = 98.00%, uncovered 42. A second lib covered by the same harness." + }, + { + "command": "ls -la /usr/lib/libsndfile.so", + "result": "pass", + "evidence": "The symlink exists on this host, so need_libsndfile is legitimately 0 and lines 19/52/53/55 are unreachable here. Three assertions that appeared to test this branch were REMOVED rather than left green-but-vacuous, and the limitation is written into the suite." + }, + { + "command": "shellcheck on all four new files", + "result": "pass", + "evidence": "clean; no suppressions added. A malformed `# shellcheck source=... -- comment` directive was rewritten rather than deleted." + } + ], + "risks": [ + "The libsndfile branch cannot be reached on a host that has the library. Covering it needs a jail that hides /usr/lib/libsndfile.so, which would mean adding a jail_args marker and making the whole directory jail-only for one branch. Deferred deliberately; the cost is 4 lines of the 52.", + "The restricted PATH must list every binary the suites use. A future assertion invoking an unlisted tool fails with 'command not found' rather than a clear assertion failure -- the bash/env case above cost real time to diagnose." + ], + "rollback": [ + "git revert the commit; the directory loses its harness and all five of its libs stay allowlisted with no tests.", + "After rollback, confirm no tests/run_all.sh remains under testsAndMisc-bash/lib, so the gate's view of the directory is unchanged." + ] +} diff --git a/linux_configuration/scripts/single_use/misc/testsAndMisc-bash/lib/tests/run_all.sh b/linux_configuration/scripts/single_use/misc/testsAndMisc-bash/lib/tests/run_all.sh new file mode 100755 index 00000000..2320dd7a --- /dev/null +++ b/linux_configuration/scripts/single_use/misc/testsAndMisc-bash/lib/tests/run_all.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +# lib/tests/run_all.sh — run every transcribe lib test in one process tree. +# +# Also the subject handed to shell_coverage_jail.sh when measuring coverage of +# a lib in this directory: coverage is only complete across the whole suite. +# +# No jail_args file sits beside this runner, and that is deliberate: these +# suites intercept every package manager and sudo call with a PATH stub dir, +# so they touch nothing outside their own mktemp -d. Both ci_mirror.sh and +# the shell-tests workflow therefore run this runner directly. +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +failed=0 +for test_script in "${SCRIPT_DIR}"/test_*.sh; do + printf '\n### %s\n' "$(basename "$test_script")" + if ! "$test_script"; then + failed=$((failed + 1)) + fi +done + +if [[ $failed -gt 0 ]]; then + printf '\n%d test file(s) FAILED\n' "$failed" + exit 1 +fi + +printf '\nAll transcribe lib test files passed.\n' diff --git a/linux_configuration/scripts/single_use/misc/testsAndMisc-bash/lib/tests/test_transcribe_deps.sh b/linux_configuration/scripts/single_use/misc/testsAndMisc-bash/lib/tests/test_transcribe_deps.sh new file mode 100755 index 00000000..1e514699 --- /dev/null +++ b/linux_configuration/scripts/single_use/misc/testsAndMisc-bash/lib/tests/test_transcribe_deps.sh @@ -0,0 +1,121 @@ +#!/usr/bin/env bash +# Tests for transcribe_deps.sh — the system-dependency install phase. +# +# No jail: every branch turns on `command -v` and every install goes through +# sudo, both intercepted by the harness's PATH stub dir. The assertions are on +# RECORDED ARGV, because WHICH packages each distro branch would install is +# the part that actually differs between them and the part a presence test +# cannot see -- pacman gets python-virtualenv where apt gets python3-venv. +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +# shellcheck source=./transcribe_harness.sh +. "$SCRIPT_DIR/transcribe_harness.sh" + +_t_setup_env +trap _t_teardown EXIT + +# Globals the entry script defines above its source line. +OFFLINE=0 +FW_DIARIZE=0 + +# detect_pkg_mgr lives next door and is what selects the branch below. +# shellcheck source=../transcribe_pkgmgr.sh +. "$TRANSCRIBE_LIB_DIR/transcribe_pkgmgr.sh" +# shellcheck source=../transcribe_deps.sh +. "$TRANSCRIBE_LIB_DIR/transcribe_deps.sh" + +# A pass-through sudo, so the package-manager stub records the real argv. +printf '#!/usr/bin/env bash\nexec "$@"\n' >"$TEST_TMPDIR/bin/sudo" +chmod +x "$TEST_TMPDIR/bin/sudo" + +# --- everything already present is a no-op ---------------------------------- +_t_stub ffmpeg +_t_stub espeak-ng +_t_reset_calls +present_out="$(install_system_deps 2>&1)" +_t_has "$present_out" 'System deps present' "install_system_deps reports a satisfied system" +_t_lacks "$(_t_calls)" 'pacman' "nothing is installed when ffmpeg and espeak-ng are both present" + +# --- offline with a missing dep exits 5 ------------------------------------- +rm -f "$TEST_TMPDIR/bin/ffmpeg" +offline_rc=0 +( + OFFLINE=1 + install_system_deps +) >/dev/null 2>&1 || offline_rc=$? +_t_eq "5" "$offline_rc" "a missing dep in offline mode exits 5" + +# --- no sudo means the install is skipped, not failed ----------------------- +rm -f "$TEST_TMPDIR/bin/sudo" +_t_stub pacman +no_sudo_out="$(install_system_deps 2>&1)" +_t_has "$no_sudo_out" 'sudo not found' "a missing sudo skips the install instead of failing" +printf '#!/usr/bin/env bash\nexec "$@"\n' >"$TEST_TMPDIR/bin/sudo" +chmod +x "$TEST_TMPDIR/bin/sudo" + +# --- the pacman branch uses Arch package names ------------------------------ +_t_reset_calls +install_system_deps >/dev/null 2>&1 +pac_calls="$(_t_calls)" +_t_has "$pac_calls" 'python-virtualenv' "the pacman branch asks for python-virtualenv, not python3-venv" +_t_has "$pac_calls" 'ffmpeg' "the pacman branch installs the missing ffmpeg" +_t_lacks "$pac_calls" 'espeak-ng' "the pacman branch omits espeak-ng, which is already present" + +# --- the apt branch uses Debian package names and refreshes first ----------- +rm -f "$TEST_TMPDIR/bin/pacman" +_t_stub apt-get +_t_reset_calls +install_system_deps >/dev/null 2>&1 +apt_calls="$(_t_calls)" +_t_has "$apt_calls" 'apt-get update -y' "the apt branch refreshes the package list first" +_t_has "$apt_calls" 'python3-venv' "the apt branch asks for python3-venv, not python-virtualenv" + +# --- the dnf branch --------------------------------------------------------- +rm -f "$TEST_TMPDIR/bin/apt-get" +_t_stub dnf +_t_reset_calls +install_system_deps >/dev/null 2>&1 +_t_has "$(_t_calls)" 'dnf install -y python3-venv' "the dnf branch installs python3-venv" + +# --- the yum branch --------------------------------------------------------- +rm -f "$TEST_TMPDIR/bin/dnf" +_t_stub yum +_t_reset_calls +install_system_deps >/dev/null 2>&1 +_t_has "$(_t_calls)" 'yum install -y python3-venv' "the yum branch installs python3-venv" + +# --- the zypper branch uses its own versioned python names ------------------ +rm -f "$TEST_TMPDIR/bin/yum" +_t_stub zypper +_t_reset_calls +install_system_deps >/dev/null 2>&1 +_t_has "$(_t_calls)" 'python311-virtualenv' "the zypper branch asks for python311-virtualenv" + +# --- an unknown package manager warns rather than guessing ------------------ +rm -f "$TEST_TMPDIR/bin/zypper" +unknown_out="$(install_system_deps 2>&1)" +_t_has "$unknown_out" 'Unknown package manager' "an unrecognised manager warns instead of installing" + +# --- a failing install is best-effort, never fatal -------------------------- +_t_stub_failing pacman +failing_out="$(install_system_deps 2>&1)" +_t_has "$failing_out" 'pacman install failed; continuing' "a failing install is reported and survived" + +# --- diarization: the libsndfile probe is host-dependent -------------------- +# FW_DIARIZE=1 adds a check for libsndfile at three absolute paths. This host +# HAS /usr/lib/libsndfile.so, so need_libsndfile is legitimately 0 here and +# the "add libsndfile to the package list" branch cannot be reached without a +# jail that hides the library. Asserting it would mean asserting nothing. +# +# What IS testable without a jail: with diarization on and the library +# present, the satisfied-system message names libsndfile. +_t_stub ffmpeg +diarize_out="$( + FW_DIARIZE=1 + install_system_deps 2>&1 +)" +_t_has "$diarize_out" 'libsndfile' \ + "FW_DIARIZE=1 names libsndfile in the satisfied-deps message when the library is present" + +_t_report "test_transcribe_deps.sh" diff --git a/linux_configuration/scripts/single_use/misc/testsAndMisc-bash/lib/tests/test_transcribe_pkgmgr.sh b/linux_configuration/scripts/single_use/misc/testsAndMisc-bash/lib/tests/test_transcribe_pkgmgr.sh new file mode 100755 index 00000000..ef9da1c9 --- /dev/null +++ b/linux_configuration/scripts/single_use/misc/testsAndMisc-bash/lib/tests/test_transcribe_pkgmgr.sh @@ -0,0 +1,117 @@ +#!/usr/bin/env bash +# Tests for transcribe_pkgmgr.sh — package-manager detection and the CUDA check. +# +# No jail: every branch turns on `command -v ` and every install goes +# through sudo, both of which the harness's PATH stub dir intercepts. The +# suite asserts on RECORDED ARGV, because which packages a branch would +# install is the part a presence test cannot see. +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +# shellcheck source=./transcribe_harness.sh +. "$SCRIPT_DIR/transcribe_harness.sh" + +_t_setup_env +trap _t_teardown EXIT + +# Globals the entry script defines above its source line. +OFFLINE=0 +VENV_DIR="$TEST_TMPDIR/venv" +PY_HELPERS="$TEST_TMPDIR/helpers.py" + +# shellcheck source=../transcribe_pkgmgr.sh +. "$TRANSCRIBE_LIB_DIR/transcribe_pkgmgr.sh" + +# --- detect_pkg_mgr: none of them present ----------------------------------- +_t_eq "none" "$(detect_pkg_mgr)" "detect_pkg_mgr reports 'none' with no manager on PATH" + +# --- detect_pkg_mgr: each manager, in the documented priority order --------- +# Later entries are added on top of earlier ones to prove the ORDER, not just +# that each name is recognised: apt must win over dnf, dnf over yum, and so on. +_t_stub zypper +_t_eq "zypper" "$(detect_pkg_mgr)" "detect_pkg_mgr finds zypper" +_t_stub pacman +_t_eq "pacman" "$(detect_pkg_mgr)" "pacman takes priority over zypper" +_t_stub yum +_t_eq "yum" "$(detect_pkg_mgr)" "yum takes priority over pacman" +_t_stub dnf +_t_eq "dnf" "$(detect_pkg_mgr)" "dnf takes priority over yum" +_t_stub apt-get +_t_eq "apt" "$(detect_pkg_mgr)" "apt-get takes highest priority and reports 'apt'" + +# --- has_libcublas12: absent everywhere ------------------------------------- +cublas_rc=0 +has_libcublas12 || cublas_rc=$? +_t_eq "1" "$cublas_rc" "has_libcublas12 fails when no CUDA library exists" + +# --- has_libcublas12: found in a venv's nvidia site-packages ---------------- +mkdir -p "$VENV_DIR/bin" +printf '#!/usr/bin/env bash\nprintf "3.11\\n"\n' >"$VENV_DIR/bin/python" +chmod +x "$VENV_DIR/bin/python" +mkdir -p "$VENV_DIR/lib/python3.11/site-packages/nvidia/cublas/lib" +: >"$VENV_DIR/lib/python3.11/site-packages/nvidia/cublas/lib/libcublas.so.12" +cublas_rc=0 +has_libcublas12 || cublas_rc=$? +_t_eq "0" "$cublas_rc" "has_libcublas12 finds the library inside the venv's nvidia packages" + +# --- ensure_cuda_runtime: already present is a no-op ------------------------ +_t_reset_calls +ensure_cuda_runtime +_t_lacks "$(_t_calls)" 'sudo' "ensure_cuda_runtime installs nothing when cuBLAS is already present" + +# --- ensure_cuda_runtime: offline and missing exits 6 ----------------------- +rm -f "$VENV_DIR/lib/python3.11/site-packages/nvidia/cublas/lib/libcublas.so.12" +offline_rc=0 +( + OFFLINE=1 + ensure_cuda_runtime +) >/dev/null 2>&1 || offline_rc=$? +_t_eq "6" "$offline_rc" "ensure_cuda_runtime exits 6 when offline and CUDA is missing" + +# --- ensure_cuda_runtime: no sudo means the install is skipped ------------- +no_sudo_out="$( (ensure_cuda_runtime) 2>&1)" || true +_t_has "$no_sudo_out" 'sudo not found' "ensure_cuda_runtime reports a missing sudo instead of failing on it" + +# --- ensure_cuda_runtime: the pacman branch --------------------------------- +# sudo is a pass-through so the manager stub below records the real argv. +printf '#!/usr/bin/env bash\nexec "$@"\n' >"$TEST_TMPDIR/bin/sudo" +chmod +x "$TEST_TMPDIR/bin/sudo" +rm -f "$TEST_TMPDIR/bin/apt-get" "$TEST_TMPDIR/bin/dnf" "$TEST_TMPDIR/bin/yum" +_t_reset_calls +pac_rc=0 +(ensure_cuda_runtime) >/dev/null 2>&1 || pac_rc=$? +_t_has "$(_t_calls)" 'pacman -Sy --noconfirm cuda cudnn' "the pacman branch installs cuda and cudnn" +_t_eq "6" "$pac_rc" "a still-missing cuBLAS after the install attempt exits 6" + +# --- ensure_cuda_runtime: the apt branch ------------------------------------ +_t_stub apt-get +_t_reset_calls +(ensure_cuda_runtime) >/dev/null 2>&1 || true +_t_has "$(_t_calls)" 'apt-get install -y nvidia-cuda-toolkit' "the apt branch installs the CUDA toolkit" + +# --- ensure_cuda_runtime: dnf and yum share one branch ---------------------- +rm -f "$TEST_TMPDIR/bin/apt-get" +_t_stub dnf +_t_reset_calls +(ensure_cuda_runtime) >/dev/null 2>&1 || true +_t_has "$(_t_calls)" 'dnf install -y cuda cudnn' "the dnf branch installs cuda and cudnn" + +rm -f "$TEST_TMPDIR/bin/dnf" +_t_stub yum +_t_reset_calls +(ensure_cuda_runtime) >/dev/null 2>&1 || true +_t_has "$(_t_calls)" 'yum install -y cuda cudnn' "the shared dnf|yum branch also drives yum" + +# --- ensure_cuda_runtime: the zypper branch --------------------------------- +rm -f "$TEST_TMPDIR/bin/yum" "$TEST_TMPDIR/bin/pacman" +_t_stub zypper +_t_reset_calls +(ensure_cuda_runtime) >/dev/null 2>&1 || true +_t_has "$(_t_calls)" 'zypper install -y cuda cudnn' "the zypper branch installs cuda and cudnn" + +# --- ensure_cuda_runtime: an unknown manager warns -------------------------- +rm -f "$TEST_TMPDIR/bin/zypper" +unknown_out="$( (ensure_cuda_runtime) 2>&1)" || true +_t_has "$unknown_out" 'cannot install CUDA automatically' "an unknown package manager warns rather than guessing" + +_t_report "test_transcribe_pkgmgr.sh" diff --git a/linux_configuration/scripts/single_use/misc/testsAndMisc-bash/lib/tests/transcribe_harness.sh b/linux_configuration/scripts/single_use/misc/testsAndMisc-bash/lib/tests/transcribe_harness.sh new file mode 100755 index 00000000..2c498f7a --- /dev/null +++ b/linux_configuration/scripts/single_use/misc/testsAndMisc-bash/lib/tests/transcribe_harness.sh @@ -0,0 +1,116 @@ +#!/usr/bin/env bash +# lib/tests/transcribe_harness.sh — shared fixture for the transcribe libs. +# +# Sourced, not executed. One harness per DIRECTORY, not per file: jscpd fails +# the commit above 2% duplication, and near-identical per-file harnesses are +# exactly how that trips. +# +# Unlike the features/lib harness, these subjects need NO jail. They branch on +# `command -v ` and shell out through sudo; both are intercepted +# by a PATH stub dir, so nothing here can reach a real package manager. The +# suites assert on the RECORDED ARGV -- which packages a branch would install +# is the thing worth checking, and it is invisible to a presence test. +set -euo pipefail + +PASS=0 +FAIL=0 + +_t_pass() { + PASS=$((PASS + 1)) + printf ' OK: %s\n' "$1" +} + +_t_fail() { + FAIL=$((FAIL + 1)) + printf ' FAIL: %s\n' "$1" +} + +_t_eq() { + if [[ $2 == "$1" ]]; then + _t_pass "$3" + else + _t_fail "$3 (want '${1}', got '${2}')" + fi +} + +_t_has() { + if [[ $1 == *"$2"* ]]; then + _t_pass "$3" + else + _t_fail "$3 (want a substring '${2}')" + fi +} + +_t_lacks() { + if [[ $1 != *"$2"* ]]; then + _t_pass "$3" + else + _t_fail "$3 (did not want a substring '${2}')" + fi +} + +TRANSCRIBE_LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +export TRANSCRIBE_LIB_DIR + +log() { printf '[log] %s\n' "$*"; } + +_t_setup_env() { + TEST_TMPDIR="$(mktemp -d)" + export TEST_TMPDIR + mkdir -p "$TEST_TMPDIR/bin" + # PATH is REPLACED rather than prepended, but coreutils is kept: these + # subjects branch on which package managers exist and this host really + # has pacman, so a prepended dir could not HIDE it and every "not + # installed" case would silently assert nothing. The stub dir plus a + # private coreutils dir is the whole PATH; package managers and sudo + # appear only when a test puts them there. + mkdir -p "$TEST_TMPDIR/coreutils" + local tool + # `bash` is in this list because every stub carries a `#!/usr/bin/env bash` + # shebang, and env resolves it through the RESTRICTED PATH. Without it each + # stub dies with "env: 'bash': No such file or directory" and the branch it + # was standing in for is silently never taken. + for tool in bash cat chmod cp env grep ln mkdir mktemp printf rm sed; do + if command -v "$tool" >/dev/null 2>&1; then + ln -sf "$(command -v "$tool")" "$TEST_TMPDIR/coreutils/$tool" + fi + done + PATH="$TEST_TMPDIR/bin:$TEST_TMPDIR/coreutils" + export PATH +} + +# Record-and-succeed stub. The argv is the assertion target, so it is written +# verbatim to one line of the call log. +_t_stub() { + printf '#!/usr/bin/env bash\nprintf "%%s %%s\\n" "%s" "$*" >>"%s/calls.log"\nexit 0\n' \ + "$1" "$TEST_TMPDIR" >"$TEST_TMPDIR/bin/$1" + chmod +x "$TEST_TMPDIR/bin/$1" +} + +# A stub that FAILS, for the "install failed; continuing" best-effort paths. +_t_stub_failing() { + printf '#!/usr/bin/env bash\nprintf "%%s %%s\\n" "%s" "$*" >>"%s/calls.log"\nexit 1\n' \ + "$1" "$TEST_TMPDIR" >"$TEST_TMPDIR/bin/$1" + chmod +x "$TEST_TMPDIR/bin/$1" +} + +_t_calls() { + if [[ -f "$TEST_TMPDIR/calls.log" ]]; then + cat "$TEST_TMPDIR/calls.log" + fi +} + +_t_reset_calls() { + : >"$TEST_TMPDIR/calls.log" +} + +_t_teardown() { + if [[ -n ${TEST_TMPDIR:-} && -d ${TEST_TMPDIR} ]]; then + rm -rf "$TEST_TMPDIR" + fi +} + +_t_report() { + printf '\n%s: %d passed, %d failed\n' "${1:-tests}" "$PASS" "$FAIL" + [[ $FAIL -eq 0 ]] +}