Files
miaur/tests/run.sh
T
Cursor AgentandFrank Vitrano a4f4767c47 Add self-update command to refresh miaur from Git
Introduce `miaur self-update` / `--self-update` that clones RepoUrl@Branch,
detects user vs system install prefix, and reinstalls in place. Configurable
via conf/env; supports --noconfirm, --repo-url, and --branch.

Co-authored-by: Frank Vitrano <WHC2006@users.noreply.github.com>
2026-07-23 14:44:24 +00:00

188 lines
5.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Lightweight self-tests for miaur (no Arch/pacman required)
set -euo pipefail
ROOT=$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/.." && pwd)
# shellcheck source=lib/common.sh
source "$ROOT/lib/common.sh"
# shellcheck source=lib/cli.sh
source "$ROOT/lib/cli.sh"
fail() { echo "FAIL: $*" >&2; exit 1; }
pass() { echo "ok - $*"; }
# --- strip_miaur_opts ---
{
mapfile -t out < <(strip_miaur_opts -Rns --nosudoloop --ignore linux foo)
[[ ${#out[@]} -eq 4 ]] || fail "strip count=${#out[@]} contents=${out[*]}"
[[ ${out[0]} == -Rns && ${out[1]} == --ignore && ${out[2]} == linux && ${out[3]} == foo ]] \
|| fail "strip values: ${out[*]}"
pass "strip_miaur_opts keeps pacman argv"
}
{
mapfile -t out < <(strip_miaur_opts -S --noconfirm --aur pkg)
[[ ${#out[@]} -eq 2 && ${out[0]} == -S && ${out[1]} == pkg ]] \
|| fail "strip noconfirm/aur: ${out[*]}"
pass "strip_miaur_opts drops miaur-only flags"
}
# --- parse_args: --noremovemake ---
{
OP=
MIAUR_REMOVEMAKE=1
MIAUR_NOCONFIRM=0
DO_PASSTHRU=0
OP_REFRESH=0
OP_UPGRADE=0
TARGETS=()
PACMAN_EXTRA=()
parse_args --noremovemake -S
[[ $MIAUR_REMOVEMAKE -eq 0 ]] || fail "noremovemake not applied"
[[ $OP == S ]] || fail "OP after noremovermake+S"
pass "parse_args --noremovemake"
}
{
OP=
MIAUR_REMOVEMAKE=1
TARGETS=()
PACMAN_EXTRA=()
parse_args --noremovermake
[[ $MIAUR_REMOVEMAKE -eq 0 ]] || fail "typo alias noremovermake not applied"
pass "parse_args typo alias --noremovermake"
}
{
OP=
MIAUR_NOCONFIRM=0
OP_REFRESH=0
OP_UPGRADE=0
TARGETS=()
PACMAN_EXTRA=()
parse_args --noconfirm -Syu
[[ $MIAUR_NOCONFIRM -eq 1 ]] || fail "noconfirm not set"
[[ $OP == S && $OP_REFRESH -eq 1 && $OP_UPGRADE -eq 1 ]] || fail "Syu parse"
pass "parse_args --noconfirm -Syu"
}
{
OP=
DO_PASSTHRU=0
TARGETS=()
PACMAN_EXTRA=()
parse_args -R --ignore linux foo
[[ $OP == R && $DO_PASSTHRU -eq 1 ]] || fail "R passthrough flags"
found=0
for x in "${PACMAN_EXTRA[@]}"; do
[[ $x == --ignore ]] && found=1
done
[[ $found -eq 1 ]] || fail "PACMAN_EXTRA missing --ignore: ${PACMAN_EXTRA[*]}"
[[ ${TARGETS[0]} == linux && ${TARGETS[1]} == foo ]] || fail "targets: ${TARGETS[*]}"
pass "parse_args -R --ignore passthrough tokens"
}
# --- pac_noconfirm_args / confirm_ask ---
{
MIAUR_NOCONFIRM=0
mapfile -t c < <(pac_noconfirm_args)
[[ ${#c[@]} -eq 0 ]] || fail "expected no noconfirm by default"
MIAUR_NOCONFIRM=1
mapfile -t c < <(pac_noconfirm_args)
[[ ${#c[@]} -eq 1 && ${c[0]} == --noconfirm ]] || fail "noconfirm args: ${c[*]}"
pass "pac_noconfirm_args"
}
{
MIAUR_NOCONFIRM=1
confirm_ask "should skip" || fail "confirm_ask should succeed with noconfirm"
pass "confirm_ask respects noconfirm"
}
# --- install.sh resolve_src: process substitution must not cd-fail ---
{
rs_out=$(
bash <(
# shellcheck disable=SC2016
sed -n '/^resolve_src()/,/^}/p' "$ROOT/install.sh"
cat <<'INNER'
REPO_URL=https://example.invalid/miaur.git
BRANCH=main
die() { echo "die:$*" >&2; exit 1; }
git() { echo "git-called"; exit 0; }
resolve_src
echo "SRC=${SRC:-}"
INNER
) 2>&1
) || true
[[ $rs_out != *'No such file or directory'* ]] || fail "process substitution still cd-fails: $rs_out"
[[ $rs_out == *'Cloning'* || $rs_out == *'git-called'* || $rs_out == *'die:'* ]] \
|| fail "unexpected resolve_src output: $rs_out"
pass "resolve_src survives process substitution"
}
# --- self-update CLI ---
# shellcheck source=lib/self_update.sh
source "$ROOT/lib/self_update.sh"
{
OP=
MIAUR_NOCONFIRM=0
MIAUR_REPO=https://example.invalid/miaur.git
MIAUR_BRANCH=main
TARGETS=()
PACMAN_EXTRA=()
parse_args self-update --noconfirm --repo-url https://example.com/fork.git --branch dev
[[ $OP == selfupdate ]] || fail "OP selfupdate"
[[ $MIAUR_NOCONFIRM -eq 1 ]] || fail "noconfirm for self-update"
[[ $MIAUR_REPO == https://example.com/fork.git ]] || fail "repo url: $MIAUR_REPO"
[[ $MIAUR_BRANCH == dev ]] || fail "branch: $MIAUR_BRANCH"
pass "parse_args self-update options"
}
{
OP=
parse_args --self-update
[[ $OP == selfupdate ]] || fail "OP from --self-update"
pass "parse_args --self-update"
}
{
HOME=/tmp/miaur-home-test
MIAUR_HOME=/tmp/miaur-home-test/.local/share/miaur
info=$(miaur_detect_prefix)
[[ $info == "user|/tmp/miaur-home-test/.local" ]] || fail "user prefix detect: $info"
MIAUR_HOME=/usr/local/share/miaur
info=$(miaur_detect_prefix)
[[ $info == "system|/usr/local" ]] || fail "system prefix detect: $info"
pass "miaur_detect_prefix"
}
{
ver=$(_miaur_read_version_file "$ROOT/lib/common.sh")
[[ $ver == "$MIAUR_VERSION" ]] || fail "version read: $ver vs $MIAUR_VERSION"
pass "_miaur_read_version_file"
}
# Mocked self-update install into a temp HOME
{
fake_home=$(mktemp -d)
fake_src=$(mktemp -d)
mkdir -p "$fake_src/lib" "$fake_src/completions" "$fake_src/config"
printf '%s\n' '#!/usr/bin/env bash' 'echo miaur-fake' >"$fake_src/miaur"
chmod +x "$fake_src/miaur"
printf '%s\n' 'MIAUR_VERSION="9.9.9"' >"$fake_src/lib/common.sh"
printf '%s\n' '# fake' >"$fake_src/completions/miaur.bash"
printf '%s\n' '# fake' >"$fake_src/config/miaur.conf.example"
HOME=$fake_home
bin=$(_miaur_install_tree user "$fake_home/.local" "$fake_src")
[[ -x $bin ]] || fail "bin missing: $bin"
[[ -f $fake_home/.local/share/miaur/lib/common.sh ]] || fail "lib not installed"
grep -q '9.9.9' "$fake_home/.local/share/miaur/lib/common.sh" || fail "version file not copied"
pass "_miaur_install_tree user mode"
rm -rf "$fake_home" "$fake_src"
}
echo "All tests passed."