suse-professional-package-i.../tests/test_list_parsing.sh

73 lines
2.9 KiB
Bash
Executable file

#!/usr/bin/env bash
# Created by John A. Hoeven with the ethical assistance of Claude AI
# ---------------------------------------------------------------------------
# test_list_parsing.sh
# /home/john/projects/suse-professional-package-installer/tests/test_list_parsing.sh
# Version: v0.1.0 | Status: DEVELOPMENT
# ---------------------------------------------------------------------------
# Purpose: Exercises read_package_list() (comments/blank-line handling,
# empty-file rejection) without touching zypper or requiring
# privilege escalation. Kept separate from installation scripts
# per CE OS standard — never merged into main.sh or a subscript.
# Target: common (pure bash, no package-manager calls).
# Entry: ./test_list_parsing.sh
# Depends: lib/ce-common.sh
# ---------------------------------------------------------------------------
# shellcheck disable=SC2016
# ^ run_test()'s cmd args are intentionally single-quoted — deferred-eval
# expressions, not meant to expand at the call site.
_SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
# shellcheck source=../lib/ce-common.sh
source "${_SCRIPT_DIR}/../lib/ce-common.sh"
_TMP="$(mktemp -d)"
register_temp_dir "${_TMP}"
log_info "=== test_list_parsing ==="
# ── Comments, blank lines, whitespace ────────────────────────────────────
cat > "${_TMP}/mixed.list" <<'EOF'
# a full-line comment
foo-pkg
bar-pkg # trailing comment
baz-pkg
EOF
# shellcheck disable=SC2034 # populated via nameref inside read_package_list, read via eval in run_test
declare -a parsed
read_package_list "${_TMP}/mixed.list" parsed
run_test "parses exactly 3 packages" '[[ ${#parsed[@]} -eq 3 ]]' fail
run_test "first entry is foo-pkg" '[[ "${parsed[0]}" == "foo-pkg" ]]' fail
run_test "second entry is bar-pkg (trailing comment stripped)" \
'[[ "${parsed[1]}" == "bar-pkg" ]]' fail
run_test "third entry is baz-pkg (leading whitespace stripped)" \
'[[ "${parsed[2]}" == "baz-pkg" ]]' fail
# ── Empty / comment-only file is rejected ────────────────────────────────
cat > "${_TMP}/empty.list" <<'EOF'
# nothing but comments
EOF
# Functions are inherited into subshells, so a bare subshell (no re-source
# needed) is enough to isolate read_package_list()'s own `exit 1` from this
# test script's own process.
run_test "comment-only list exits non-zero" \
"! (declare -a x; read_package_list '${_TMP}/empty.list' x) 2>/dev/null" \
fail
# ── Missing file is rejected ─────────────────────────────────────────────
run_test "missing list file exits non-zero" \
"! (declare -a x; read_package_list '${_TMP}/does-not-exist.list' x) 2>/dev/null" \
fail
log_info "=== test_list_parsing: all checks passed ==="