DistroHopper/tests/test_dh.sh
Claude 29ce40f05d
Add quickget/qget switch and test suite
qget:
- Add QGET_TEST_MODE guard so the script can be sourced for unit
  testing without executing the main logic

dh:
- Auto-detect getter tool (qget preferred, quickget fallback) in
  set_variables(); export as GETTER
- Add getter_list_os() wrapper using GETTER --list for portable
  OS enumeration
- Replace hard-coded quickget calls with $GETTER in
  renew_supported_vms(), renew_ready_vms(), and execmd strings
- Fix releases/editions/description parsing (sed instead of cut -f2
  which fails on colon-space separators)
- Add DH_TEST_MODE guard to prevent execution when sourcing for tests

action:
- Auto-detect getter tool at startup; export as GETTER
- Add --qget / -q and --quickget / -Q flags to override detection

tests/:
- tests/lib.sh: shared assert helpers (assert_eq, assert_ne,
  assert_match, assert_cmd, assert_fn_exists) with pass/fail counting
- tests/test_dh.sh: 31 unit tests covering set_variables, GETTER,
  getter_list_os, root_check, wayland_check, and all required
  function definitions
- tests/test_qget.sh: 23 unit tests covering os_support,
  test_result formatting, is_valid_language, check_hash, and all
  utility functions
- tests/test_action_files.sh: 784 structural tests verifying every
  action file has OSNAME, PRETTY, HOMEPAGE (valid URL), DESCRIPTION,
  releases_() and get_() functions
- tests/run_tests.sh: top-level runner executing all three suites

https://claude.ai/code/session_01M2UXTtQwzcGCNRnFiP2efQ
2026-02-28 19:42:33 +00:00

76 lines
2.7 KiB
Bash
Executable file

#!/usr/bin/env bash
# Unit tests for dh functions
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
source "$REPO_ROOT/tests/lib.sh"
echo "=== Testing dh ==="
cd "$REPO_ROOT"
# 1. Bash syntax check
assert_cmd "dh: bash syntax valid" bash -n ./dh
# 2. Mock external dependencies before sourcing
yad() { return 0; }
loginctl() { echo "Type=x11"; }
export -f yad loginctl
# 3. Source in test mode — only functions should be defined, nothing executed
DH_TEST_MODE=1 source ./dh 2>/dev/null
assert_eq "dh: sources in test mode (exit 0)" "0" "$?"
# 4. set_variables sets all required variables
set_variables 2>/dev/null
assert_ne "dh: DH_CONFIG_DIR is set" "" "$DH_CONFIG_DIR"
assert_ne "dh: DH_CONFIG is set" "" "$DH_CONFIG"
assert_ne "dh: DH_ICON_DIR is set" "" "$DH_ICON_DIR"
assert_ne "dh: VMS_DIR is set" "" "$VMS_DIR"
assert_ne "dh: TMP_DIR is set" "" "$TMP_DIR"
assert_ne "dh: terminal is set" "" "$terminal"
assert_ne "dh: version is set" "" "$version"
assert_ne "dh: replace is set" "" "$replace"
# 5. GETTER is set to a valid value after set_variables
assert_ne "dh: GETTER is set" "" "$GETTER"
if [ "$GETTER" = "qget" ] || [ "$GETTER" = "quickget" ]; then
pass "dh: GETTER is 'qget' or 'quickget'"
else
fail "dh: GETTER is 'qget' or 'quickget' (got '$GETTER')"
fi
# 6. getter_list_os function exists
assert_fn_exists "dh: getter_list_os function exists" "getter_list_os"
# 7. root_check sets root to a valid privilege escalator (or empty if root)
root_check 2>/dev/null
if [ "$root" = "sudo" ] || [ "$root" = "doas" ] || [ "$root" = "" ]; then
pass "dh: root_check sets root to valid value ('$root')"
else
fail "dh: root_check sets root to valid value (got '$root')"
fi
# 8. wayland_check handles mocked x11 session
output=$(XDG_SESSION_ID="1" wayland_check 2>/dev/null)
assert_match "dh: wayland_check handles x11 session" "Running on X" "$output"
# 9. wayland_check handles empty session type (TTY fallback)
loginctl() { echo "Type="; }
export -f loginctl
output=$(XDG_SESSION_ID="" wayland_check 2>/dev/null)
assert_match "dh: wayland_check handles empty session type" "(Running|Not sure)" "$output"
# 10. All required functions are defined
for fn in distrohopper_about help_show first_run create_desktop_entry \
desktop_entry_dh renew_ready renew_ready_vms renew_supported_vms \
installation_process create_dirs root_check wayland_check set_variables; do
assert_fn_exists "dh: function $fn exists" "$fn"
done
# 11. comment field has no stray #TODO marker
assert_cmd "dh: comment field has no #TODO" bash -c '! grep -q "comment=.*#TODO" dh'
# 12. DH_CONFIG_DIR is inside HOME
assert_match "dh: DH_CONFIG_DIR is under HOME" "^$HOME" "$DH_CONFIG_DIR"
summary