Files
HamPack/tests/helpers.bash
T
David YoungandClaude Opus 5.5 5f15ff3821 Let you key a radio through a CM108 sound card's PTT line
Dire Wolf, Graywolf and others key a radio through a CM108-family sound
card (a Digirig, an AIOC) by setting a GPIO pin through its hidraw
device, which only root can open otherwise. Dire Wolf's own rule gives
it to the audio group, which most Arch users aren't in, and shouldn't
need to be. udev/70-hampack-cm108.rules tags those devices uaccess
instead, so whoever is at the desktop can use them, as they can the
sound card. It matches the devices Dire Wolf 1.8 treats as
CM108-compatible, and no other C-Media devices.

update.sh installs it in /etc/udev/rules.d when it changes, and reloads
and re-applies udev's rules; hampackremove removes it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 13:54:42 -07:00

124 lines
4.6 KiB
Bash

# Shared setup for HamPack's bats tests. Each test gets a fake $HOME, and
# commands that would touch the system (sudo, pacman, flatpak, systemctl...) are
# replaced by stubs that log their arguments to $STUB_LOG.
REPO="$(cd "$(dirname "$BATS_TEST_FILENAME")/.." && pwd)"
setup_fake_home() {
export HOME="$BATS_TEST_TMPDIR/home"
export USER="${USER:-tester}"
export TMPDIR="$BATS_TEST_TMPDIR/tmp"
export HAMPACK_DIR="$REPO"
export HAMPACK_STATE="$HOME/.local/state/HamPack"
export STUB_DIR="$BATS_TEST_TMPDIR/stubs"
export STUB_LOG="$BATS_TEST_TMPDIR/stub.log"
# Fake pacman database: one file per installed package; a file containing
# "dep" means it was installed as a dependency
export FAKE_PACMAN_DB="$BATS_TEST_TMPDIR/pacman-db"
export FAKE_FLATPAKS="$BATS_TEST_TMPDIR/flatpaks"
# Where update.sh installs udev rules
export HAMPACK_UDEV_RULES_DIR="$BATS_TEST_TMPDIR/udev-rules"
unset DISPLAY WAYLAND_DISPLAY WINEPREFIX HAMPACK_SKIP_WINDOWS_APPS
mkdir -p "$HOME/.local/bin" "$TMPDIR" "$STUB_DIR" "$FAKE_PACMAN_DB" "$FAKE_FLATPAKS"
: > "$STUB_LOG"
PATH="$STUB_DIR:$PATH"
# Commands that just log and succeed
local cmd
for cmd in systemctl udevadm groupadd yay update-desktop-database update-mime-database \
xdg-icon-resource xdg-desktop-menu wine wineboot winetricks xdotool; do
stub "$cmd" 'exit 0'
done
# sudo runs stubbed commands and only logs the rest, so tests never change the system
stub sudo '
case "$1" in -v|-n) exit 0 ;; esac
if [ -x "$STUB_DIR/$1" ]; then "$@"; else exit 0; fi'
# Fake group database: the groups you're in, one per line, in $FAKE_GROUPS.
# `id -nG` (this login's groups) reads $FAKE_SESSION_GROUPS if set, as a
# login from before usermod would
export FAKE_GROUPS="$BATS_TEST_TMPDIR/groups"
unset FAKE_SESSION_GROUPS
: > "$FAKE_GROUPS"
stub id '
[ "$1" = -nG ] || exit 1
if [ -n "$2" ]; then groups="$FAKE_GROUPS"; else groups="${FAKE_SESSION_GROUPS:-$FAKE_GROUPS}"; fi
echo "$USER" $(cat "$groups")'
stub usermod '[ "$1" = -aG ] && echo "$2" >> "$FAKE_GROUPS"'
stub gpasswd '[ "$1" = -d ] && grep -vx "$3" "$FAKE_GROUPS" > "$FAKE_GROUPS.new"; mv "$FAKE_GROUPS.new" "$FAKE_GROUPS"'
stub pacman '
case "$1" in
-Qi|-Q|-Qq)
shift; status=0
for p in "$@"; do
if [ -f "$FAKE_PACMAN_DB/$p" ]; then [ "$1" = -Qi ] || echo "$p"; else status=1; fi
done
exit $status ;;
-Qg) exit 1 ;;
-Qdq)
shift; for p in "$@"; do grep -qx dep "$FAKE_PACMAN_DB/$p" 2>/dev/null && echo "$p"; done; exit 0 ;;
-Qdtq)
for f in "$FAKE_PACMAN_DB"/*; do
[ -f "$f" ] || continue
p="$(basename "$f")"
grep -qx dep "$f" && ! grep -qx "$p" "$FAKE_PACMAN_DB"/.required 2>/dev/null && echo "$p"
done; exit 0 ;;
-D)
mode="$2"; shift 2
for p in "$@"; do
if [ "$mode" = --asdeps ]; then echo dep > "$FAKE_PACMAN_DB/$p"; else : > "$FAKE_PACMAN_DB/$p"; fi
done ;;
-Rns)
shift; for p in "$@"; do [ "$p" = --noconfirm ] || rm -f "$FAKE_PACMAN_DB/$p"; done ;;
esac
exit 0'
stub flatpak '
case "$1" in
info) [ -f "$FAKE_FLATPAKS/$2" ] ;;
install) for p in "$@"; do :; done; touch "$FAKE_FLATPAKS/$p" ;;
uninstall) for p in "$@"; do rm -f "$FAKE_FLATPAKS/$p"; done ;;
esac'
}
# Create a stub command: stub NAME BODY. Every call is logged as "NAME ARGS".
stub() {
printf '#!/bin/bash\necho "%s $*" >> "$STUB_LOG"\n%s\n' "$1" "$2" > "$STUB_DIR/$1"
chmod +x "$STUB_DIR/$1"
}
# A minimal CoreDesktop: the utils.sh functions HamPack uses, backed by the fake pacman
fake_coredesktop() {
local cd="$HOME/.local/share/coredesktop"
mkdir -p "$cd"
printf 'SYSTEM_UTILS=(\n coredesktop-pkg\n)\n' > "$cd/packages.conf"
cat > "$cd/utils.sh" << 'EOF'
is_installed() { [ -f "$FAKE_PACMAN_DB/$1" ]; }
without_skipped() { printf '%s\n' "$@"; }
install_packages() {
local p
for p in "$@"; do
[ -f "$FAKE_PACMAN_DB/$p" ] || { echo "yay -S $p" >> "$STUB_LOG"; : > "$FAKE_PACMAN_DB/$p"; }
done
}
enable_services() { echo "enable_services $*" >> "$STUB_LOG"; }
install_script() { cp "$1" "$2" && chmod 755 "$2"; }
remove_flatpaks() { local p; for p in "$@"; do echo "remove_flatpaks $p" >> "$STUB_LOG"; rm -f "$FAKE_FLATPAKS/$p"; done; }
EOF
}
mark_installed() {
local p
for p in "$@"; do : > "$FAKE_PACMAN_DB/$p"; done
}
# All packages and Flatpaks in packages.conf
all_hampack_packages() {
(source "$REPO/packages.conf"; printf '%s\n' "${UTILITIES[@]}" "${APPLICATIONS[@]}")
}
all_hampack_flatpaks() {
(source "$REPO/packages.conf"; printf '%s\n' "${FLATPAKS[@]}")
}