mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-18 20:14:26 +01:00
Two levels of pure taxonomy carrying no information, and a 2-directory
cap leaves no room for them. Categories now sit directly under
linux_configuration/: features, fixes, utils, misc, fresh-install,
installers, diagnostics, gaming, boot_recovery, periodic_background, lib,
meta, tests. A script's own lib/ and tests/ are exempt from the cap, so
those stay put.
Depth violations 248 -> 139; roughly 40 of the remainder are inside
digital_wellbeing and hosts, deferred until the guard-lib repoint.
The moves are the easy half. What breaks is every path that counted
levels, and three classes of it had to be found by resolving paths rather
than reading diffs:
- 52 relative `source` lines for lib/common.sh and lib/android.sh. The
correct prefix differs per file: 40 were ../../ and 12 were ../../../,
so a blanket replace would have fixed one set and broken the other.
- REPO_ROOT in 7 fixes/lib/tests harnesses and setup_syncyomi_guard.sh,
each walking up four levels from a directory now two levels shallower.
This caused a real regression -- the fixes suite went red with
"log_warn: command not found" -- caught by running it and comparing
against the pre-move baseline worktree.
- conftest.py's sys.path entries, which took the pytest gate down with
14 collection errors, and the `# shellcheck source=` directives, which
took shellcheck down with SC1091.
16 symlinks in $HOME broke, including the entire i3blocks bar. All
repointed, and cpu_monitor/memory/disk/time/volume were run to confirm
they emit real output rather than merely resolving.
Verified: 1798 tests pass at 100% coverage, 8 of 9 lib/tests suites pass
(the ninth needs the coverage jail, identically at baseline), pre-commit
is clean, and the two untracked secret configs are still ignored after
their gitignore paths changed.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PJrqKERAo5QUeEHWS5Asgp
166 lines
4.3 KiB
Bash
Executable File
166 lines
4.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# lib/tests/hosts_harness.sh — shared fixture for the hosts/install.sh split.
|
|
#
|
|
# Sourced, not executed. Scope is deliberately narrow: only the two anti-tamper
|
|
# guards and their helpers are exercised here. The install phases themselves
|
|
# write /etc/hosts, unmount the guard's bind mount and restart
|
|
# systemd-resolved, so running them under test would take down the very
|
|
# protection they exist to install — they are covered by the textual
|
|
# wrap-verification in the evidence instead.
|
|
#
|
|
# The state-file paths were already variables in the pre-split script, so a
|
|
# test can point them at a tmpdir. chattr is shimmed because the real one
|
|
# cannot mark a file immutable on a tmpfs and would abort the guard.
|
|
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() {
|
|
local want="$1"
|
|
local got="$2"
|
|
local what="$3"
|
|
if [[ "$got" == "$want" ]]; then
|
|
_t_pass "$what"
|
|
else
|
|
_t_fail "$what (want '${want}', got '${got}')"
|
|
fi
|
|
}
|
|
|
|
_t_has() {
|
|
local haystack="$1"
|
|
local needle="$2"
|
|
local what="$3"
|
|
if [[ "$haystack" == *"$needle"* ]]; then
|
|
_t_pass "$what"
|
|
else
|
|
_t_fail "$what (want a substring '${needle}')"
|
|
fi
|
|
}
|
|
|
|
# The guards return 0 to allow the install and 1 to block it. Which way they
|
|
# fail is the entire point, so assert on the exit status explicitly rather than
|
|
# letting `set -e` decide.
|
|
_t_allows() {
|
|
local what="$2"
|
|
if ("$1"); then
|
|
_t_pass "$what"
|
|
else
|
|
_t_fail "$what (the guard blocked when it should have allowed)"
|
|
fi
|
|
}
|
|
|
|
_t_blocks() {
|
|
local what="$2"
|
|
if ("$1"); then
|
|
_t_fail "$what (the guard allowed when it should have blocked)"
|
|
else
|
|
_t_pass "$what"
|
|
fi
|
|
}
|
|
|
|
_t_summary() {
|
|
printf '\n%s: %d passed, %d failed\n' "$(basename "$0")" "$PASS" "$FAIL"
|
|
[[ $FAIL -eq 0 ]]
|
|
}
|
|
|
|
TEST_TMPDIR="$(mktemp -d)"
|
|
trap 'rm -rf "${TEST_TMPDIR}"' EXIT
|
|
|
|
readonly FAKE_BIN="${TEST_TMPDIR}/fake_bin"
|
|
mkdir -p "${FAKE_BIN}"
|
|
|
|
# chattr cannot set the immutable bit on a tmpfs and exits non-zero; the guards
|
|
# tolerate that with `|| true`, but shimming it keeps the test output clean and
|
|
# makes "was the state file locked" observable.
|
|
cat >"${FAKE_BIN}/chattr" <<'CHATTRSHIM'
|
|
#!/usr/bin/env bash
|
|
printf '%s\n' "chattr $*" >>"${HOSTS_TEST_CALLS}"
|
|
exit 0
|
|
CHATTRSHIM
|
|
chmod +x "${FAKE_BIN}/chattr"
|
|
|
|
export HOSTS_TEST_CALLS="${TEST_TMPDIR}/calls"
|
|
export PATH="${FAKE_BIN}:${PATH}"
|
|
|
|
# --- globals the guards read ------------------------------------------------
|
|
|
|
export CUSTOM_ENTRIES_STATE_FILE="${TEST_TMPDIR}/custom.state"
|
|
export UNBLOCK_STATE_FILE="${TEST_TMPDIR}/unblock.state"
|
|
|
|
# A fake install.sh plus the custom_entries.hosts beside it. The guards resolve
|
|
# the data file from the script path, so this reproduces the real layout.
|
|
readonly FAKE_ROOT="${TEST_TMPDIR}/root"
|
|
readonly FAKE_SCRIPT="${FAKE_ROOT}/install.sh"
|
|
|
|
# Stage the "new" entry lists the guards will read: the custom blocklist as a
|
|
# data file, and the unblock list as marked-up comments inside the script.
|
|
stage_script() { # <custom-domain>... -- <unblock-domain>...
|
|
mkdir -p "${FAKE_ROOT}"
|
|
local mode="custom"
|
|
local custom=()
|
|
local unblock=()
|
|
local arg
|
|
for arg in "$@"; do
|
|
if [[ "$arg" == "--" ]]; then
|
|
mode="unblock"
|
|
continue
|
|
fi
|
|
if [[ "$mode" == "custom" ]]; then
|
|
custom+=("$arg")
|
|
else
|
|
unblock+=("$arg")
|
|
fi
|
|
done
|
|
|
|
{
|
|
printf '# Custom blocking entries\n'
|
|
local d
|
|
for d in "${custom[@]}"; do
|
|
printf '0.0.0.0 %s\n' "$d"
|
|
done
|
|
} >"${FAKE_ROOT}/custom_entries.hosts"
|
|
|
|
{
|
|
printf '#!/bin/bash\n'
|
|
printf '# PROTECTED_UNBLOCK_LIST_START\n'
|
|
local d
|
|
for d in "${unblock[@]}"; do
|
|
printf '# %s\n' "$d"
|
|
done
|
|
printf '# PROTECTED_UNBLOCK_LIST_END\n'
|
|
} >"${FAKE_SCRIPT}"
|
|
}
|
|
|
|
# Seed the saved state the guards compare against.
|
|
stage_custom_state() {
|
|
printf '%s\n' "$@" | sort -u >"${CUSTOM_ENTRIES_STATE_FILE}"
|
|
}
|
|
|
|
stage_unblock_state() {
|
|
printf '%s\n' "$@" | sort -u >"${UNBLOCK_STATE_FILE}"
|
|
}
|
|
|
|
reset_state() {
|
|
rm -rf "${FAKE_ROOT}"
|
|
rm -f "${CUSTOM_ENTRIES_STATE_FILE}" "${UNBLOCK_STATE_FILE}"
|
|
# Markers a test sets to steer a shim (e.g. curl_fails) are part of the
|
|
# machine that test describes, so they reset too -- otherwise a failure
|
|
# marker leaks into the next group and it passes for the wrong reason.
|
|
rm -f "$(dirname "${HOSTS_TEST_CALLS}")"/curl_fails \
|
|
"$(dirname "${HOSTS_TEST_CALLS}")"/remote_body
|
|
: >"${HOSTS_TEST_CALLS}"
|
|
mkdir -p "${FAKE_ROOT}"
|
|
}
|
|
reset_state
|