testsAndMisc/linux_configuration/tests/test_hosts_guard_pacman_integration.sh
Krzysztof kuhy Rudnicki e48f253b23 Fix test_hosts_guard_pacman_integration.sh: broken by the hosts-guard archive
Regression from this session's earlier commit (327d48b): the test still
referenced pacman-hooks/*.sh and install_pacman_hooks.sh, which that commit
archived to testsAndMisc-archive. shell-tests.yml runs this test directly
(not scoped by pre-commit's --files), so it went red on CI without being
caught locally before the push.

Dropped the assertions tied to the archived files; kept and extended the
ones checking pacman_wrapper.sh's guard-lib fallback logic, which was
already fully guard-lib-native and unaffected by the archive.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011w2Pj4Ek3ZNMQVYcFHgXDY
2026-08-18 22:58:35 +02:00

80 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
# Regression tests for pacman wrapper / guard-lib hosts-guard fallback integration.
#
# The pre-guard-lib hooks this test used to check (pacman-pre-unlock-hosts.sh,
# pacman-post-relock-hosts.sh, hosts-guard-common.sh, install_pacman_hooks.sh)
# were archived to testsAndMisc-archive once guard-lib's generic pacman hooks
# (10-guard-lib-unlock-all.hook / 90-guard-lib-relock-all.hook) fully replaced
# them -- see docs/superpowers/evidence/archive-setup-hosts-guard-2026-08-18.json.
# Only pacman_wrapper.sh's guard-lib-aware fallback logic remains to check.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_DIR="$(dirname "$SCRIPT_DIR")"
WRAPPER_FILE="$REPO_DIR/scripts/periodic_background/digital_wellbeing/pacman/pacman_wrapper.sh"
assert_contains() {
local file_path="$1"
local pattern="$2"
local message="$3"
if grep -Fq "$pattern" "$file_path"; then
echo "PASS: $message"
else
echo "FAIL: $message"
exit 1
fi
}
first_line_number() {
local file_path="$1"
local pattern="$2"
grep -n -F -m 1 "$pattern" "$file_path" | cut -d: -f1
}
assert_order() {
local file_path="$1"
local first_pattern="$2"
local second_pattern="$3"
local message="$4"
local first_line
local second_line
first_line="$(first_line_number "$file_path" "$first_pattern")"
second_line="$(first_line_number "$file_path" "$second_pattern")"
if [[ -z "$first_line" || -z "$second_line" ]]; then
echo "FAIL: $message"
exit 1
fi
if ((first_line < second_line)); then
echo "PASS: $message"
else
echo "FAIL: $message"
exit 1
fi
}
echo "=== Hosts guard pacman integration regression tests ==="
bash -n "$WRAPPER_FILE"
echo "PASS: shell syntax is valid"
assert_contains "$WRAPPER_FILE" 'pacman_hooks_manage_guard_lib()' \
"wrapper detects when guard-lib's pacman hooks are installed"
assert_contains "$WRAPPER_FILE" 'should_use_wrapper_guard_lib_fallback()' \
"wrapper exposes a dedicated fallback path for hosts guard"
assert_order "$WRAPPER_FILE" 'if ! check_and_handle_db_lock "$@"; then' 'if should_use_wrapper_guard_lib_fallback "$@"; then' \
"wrapper checks pacman db lock before any manual hosts unlock fallback"
assert_contains "$WRAPPER_FILE" 'manual_guard_lib_fallback=1' \
"wrapper tracks whether manual hosts guard fallback was used"
assert_contains "$WRAPPER_FILE" '/etc/pacman.d/hooks/10-guard-lib-unlock-all.hook' \
"wrapper's guard-lib detection checks the generic unlock-all hook"
assert_contains "$WRAPPER_FILE" '/etc/pacman.d/hooks/90-guard-lib-relock-all.hook' \
"wrapper's guard-lib detection checks the generic relock-all hook"
echo "All hosts guard pacman integration regression tests passed."