Files
coredesktop/tests/utils.bats
T
davidandClaude Opus 5.5 d456e31192 Build a failed initramfs again, and don't offer a reboot until it builds
When mkinitcpio fails during an update or install, run it once more. If
that fails too, say why (a full /boot, a hook or module that isn't
installed) and don't offer a reboot. The install now logs its upgrade and
checks the build before finish-install offers one, and nvidia.sh's rebuild
no longer stops the install. A failed build is carried into the next
update's log, so it's rebuilt and checked until it builds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 07:34:51 -07:00

502 lines
15 KiB
Bash

#!/usr/bin/env bats
load test_helper
# --- run_migrations ---------------------------------------------------------
# migration NAME [EXIT]: add migrations/NAME.sh that logs its name and exits EXIT
migration() {
mkdir -p "$COREDESKTOP/migrations"
printf '#!/bin/bash\necho %s >> "%s"\nexit %s\n' "$1" "$RAN" "${2:-0}" \
> "$COREDESKTOP/migrations/$1.sh"
}
setup_migrations() {
export COREDESKTOP="$BATS_TEST_TMPDIR/coredesktop"
RAN="$BATS_TEST_TMPDIR/ran"
STATE="$HOME/.local/state/coredesktop/migrations"
source "$REPO/utils.sh"
}
@test "run_migrations runs each migration once, oldest first" {
setup_migrations
migration 1792592710
migration 1792592700
migration 1792592720
run run_migrations
[ "$status" -eq 0 ]
[ "$(cat "$RAN")" = "$(printf '1792592700\n1792592710\n1792592720')" ]
[ -f "$STATE/1792592700.sh" ]
[ -f "$STATE/1792592720.sh" ]
run run_migrations
[ "$status" -eq 0 ]
[ "$(wc -l < "$RAN")" -eq 3 ]
}
@test "run_migrations stops at a failure and retries it next time" {
setup_migrations
migration 1792592700
migration 1792592710 1
migration 1792592720
run run_migrations
[ "$status" -eq 1 ]
[ "$(cat "$RAN")" = "$(printf '1792592700\n1792592710')" ]
[ -f "$STATE/1792592700.sh" ]
[ ! -f "$STATE/1792592710.sh" ]
[ ! -f "$STATE/1792592720.sh" ]
migration 1792592710
run run_migrations
[ "$status" -eq 0 ]
[ "$(tail -n 2 "$RAN")" = "$(printf '1792592710\n1792592720')" ]
}
@test "run_migrations does nothing without a migrations dir" {
setup_migrations
run run_migrations
[ "$status" -eq 0 ]
[ ! -e "$RAN" ]
}
@test "migrations are named by timestamp so they sort in the order written" {
for file in "$REPO"/migrations/*; do
[[ "$(basename "$file")" =~ ^[0-9]{10}\.sh$ ]] || {
echo "bad migration name: $file"
return 1
}
done
}
# --- install_packages / remove_packages -------------------------------------
# A fake pacman that knows about the packages in $INSTALLED, of which those in
# $AS_DEPS were installed as dependencies
stub_pacman() {
stub pacman '
case "$1" in
-Qi) [[ " $INSTALLED " == *" $2 "* ]] ;;
-Qg) exit 1 ;;
-Qdq) shift; for p; do [[ " $AS_DEPS " == *" $p "* ]] && echo "$p"; done; exit 0 ;;
esac'
stub sudo
stub yay
}
@test "install_packages installs only the missing packages" {
export INSTALLED="git vim" AS_DEPS=""
stub_pacman
source "$REPO/utils.sh"
run install_packages git tmux vim neovim
[ "$status" -eq 0 ]
called yay -S --noconfirm --answerdiff=None --answeredit=None --answerclean=None tmux neovim
not_called sudo
}
@test "install_packages does nothing when everything is installed" {
export INSTALLED="git vim" AS_DEPS=""
stub_pacman
source "$REPO/utils.sh"
run install_packages git vim
[ "$status" -eq 0 ]
not_called yay
}
@test "install_packages marks packages installed as dependencies explicit" {
export INSTALLED="git python" AS_DEPS="python"
stub_pacman
source "$REPO/utils.sh"
run install_packages git python
[ "$status" -eq 0 ]
called sudo pacman -D --asexplicit python
not_called yay
}
@test "install_packages leaves out packages in skip-packages" {
export INSTALLED="" AS_DEPS=""
stub_pacman
mkdir -p "$HOME/.config/coredesktop"
printf '# Not on this machine\nlibreoffice-fresh\n\n gimp \n' > "$HOME/.config/coredesktop/skip-packages"
source "$REPO/utils.sh"
run install_packages git libreoffice-fresh gimp tmux
[ "$status" -eq 0 ]
called yay -S --noconfirm --answerdiff=None --answeredit=None --answerclean=None git tmux
}
@test "install_packages does nothing when every package is skipped" {
export INSTALLED="" AS_DEPS=""
stub_pacman
mkdir -p "$HOME/.config/coredesktop"
printf 'gimp\n' > "$HOME/.config/coredesktop/skip-packages"
source "$REPO/utils.sh"
run install_packages gimp
[ "$status" -eq 0 ]
# pacman -Qdq with no names would mark every dependency explicit
not_called pacman
not_called yay
}
@test "install-flatpaks.sh leaves out Flatpaks in skip-packages" {
mkdir -p "$HOME/.local/share" "$HOME/.config/coredesktop"
ln -s "$REPO" "$HOME/.local/share/coredesktop"
printf 'us.zoom.Zoom\n' > "$HOME/.config/coredesktop/skip-packages"
stub flatpak 'exit 1'
stub sudo
run bash "$REPO/install-flatpaks.sh"
[ "$status" -eq 0 ]
called sudo flatpak install --system --noninteractive org.mozilla.firefox
! grep -q 'us.zoom.Zoom' <(grep '^sudo' "$STUB_LOG")
}
@test "remove_packages removes only exact matches, not providers" {
# herdr-bin provides herdr, so pacman -Qq herdr prints herdr-bin
stub pacman '
[ "$1" = -Qq ] || exit 1
case "$2" in
herdr) echo herdr-bin ;;
vim) echo vim ;;
*) exit 1 ;;
esac'
stub sudo
source "$REPO/utils.sh"
run remove_packages herdr vim missing
[ "$status" -eq 0 ]
called sudo pacman -Rns --noconfirm vim
}
@test "remove_packages removes a package's -debug package too, even on its own" {
# voxtype-debug outlives voxtype, and conflicts with voxtype-bin-debug
stub pacman '
[ "$1" = -Qq ] || exit 1
case "$2" in
voxtype|voxtype-debug|herdr-debug) echo "$2" ;;
*) exit 1 ;;
esac'
stub sudo
source "$REPO/utils.sh"
run remove_packages voxtype herdr vim
[ "$status" -eq 0 ]
called sudo pacman -Rns --noconfirm voxtype voxtype-debug herdr-debug
}
@test "remove_packages does nothing when nothing is installed" {
stub pacman 'exit 1'
stub sudo
source "$REPO/utils.sh"
run remove_packages vim
[ "$status" -eq 0 ]
not_called sudo
}
# --- enable_services --------------------------------------------------------
@test "enable_services enables disabled services and skips the rest" {
stub systemctl '[ "$1" = is-enabled ] && [ "$2" = sshd ]'
stub sudo
mkdir -p "$HOME/.config/coredesktop"
echo "system cups" > "$HOME/.config/coredesktop/services-off"
source "$REPO/utils.sh"
run enable_services --now sshd cups bluetooth
[ "$status" -eq 0 ]
called sudo systemctl daemon-reload
called sudo systemctl enable --now bluetooth
uncalled sudo systemctl enable --now sshd
uncalled sudo systemctl enable --now cups
[[ "$output" == *"cups is turned off"* ]]
}
@test "enable_services without --now only enables" {
stub systemctl 'exit 1'
stub sudo
source "$REPO/utils.sh"
run enable_services bluetooth
[ "$status" -eq 0 ]
called sudo systemctl enable bluetooth
uncalled sudo systemctl daemon-reload
}
# --- initramfs_build_failed -------------------------------------------------
pacman_log() {
mkdir -p "$HOME/.local/state/coredesktop"
printf '%s\n' "$@" > "$HOME/.local/state/coredesktop/pacman-update.log"
source "$REPO/utils.sh"
}
@test "initramfs_build_failed is false without a log" {
source "$REPO/utils.sh"
run initramfs_build_failed
[ "$status" -ne 0 ]
}
@test "initramfs_build_failed is false when no initramfs was built" {
pacman_log "upgrading vim..."
run initramfs_build_failed
[ "$status" -ne 0 ]
}
@test "initramfs_build_failed is false when every image built" {
pacman_log "(3/4) Updating linux initcpios..." \
"==> Building image from preset: /etc/mkinitcpio.d/linux.preset: 'default'" \
"==> Image generation successful" \
"==> Building image from preset: /etc/mkinitcpio.d/linux.preset: 'fallback'" \
"==> Initcpio image generation successful"
run initramfs_build_failed
[ "$status" -ne 0 ]
}
@test "initramfs_build_failed is true when mkinitcpio reports an error" {
pacman_log "(3/4) Updating linux initcpios..." \
"==> ERROR: module not found" \
"==> Image generation successful"
run initramfs_build_failed
[ "$status" -eq 0 ]
}
@test "initramfs_build_failed is true when an image didn't build" {
pacman_log "(3/4) Updating linux initcpios..." \
"==> Building image from preset: /etc/mkinitcpio.d/linux.preset: 'default'" \
"==> Image generation successful" \
"==> Building image from preset: /etc/mkinitcpio.d/linux.preset: 'fallback'"
run initramfs_build_failed
[ "$status" -eq 0 ]
}
@test "initramfs_build_failed reads only the last build: a rebuild that worked clears it" {
pacman_log "(3/4) Updating linux initcpios..." \
"==> ERROR: Hook 'sd-plymouth' cannot be found" \
"==> CoreDesktop: rebuilding the initramfs (mkinitcpio -P)" \
"==> Building image from preset: /etc/mkinitcpio.d/linux.preset: 'default'" \
"==> Image generation successful"
run initramfs_build_failed
[ "$status" -ne 0 ]
}
@test "initramfs_build_failed is true when the rebuild failed too" {
pacman_log "(3/4) Updating linux initcpios..." \
"==> Image generation successful" \
"==> CoreDesktop: rebuilding the initramfs (mkinitcpio -P)" \
"==> ERROR: module not found: 'nvidia'"
run initramfs_build_failed
[ "$status" -eq 0 ]
}
@test "new_pacman_log carries a failed build over, and only a failed one" {
pacman_log "(3/4) Updating linux initcpios..." "==> ERROR: module not found: 'nvidia'"
new_pacman_log
initramfs_build_failed
[ "$(wc -l < "$PACMAN_UPDATE_LOG")" -eq 1 ]
# Carried over again by the next update that builds nothing
new_pacman_log
initramfs_build_failed
pacman_log "(3/4) Updating linux initcpios..." \
"==> Building image from preset: /etc/mkinitcpio.d/linux.preset: 'default'" \
"==> Image generation successful"
new_pacman_log
[ ! -s "$PACMAN_UPDATE_LOG" ]
}
# mkinitcpio -P through a fake sudo: it builds unless $MKINITCPIO_FAILS is set
fake_mkinitcpio() {
stub sudo 'echo "==> Building image from preset: /etc/mkinitcpio.d/linux.preset: '"'"'default'"'"'"
if [ -n "$MKINITCPIO_FAILS" ]; then echo "$MKINITCPIO_FAILS"; exit 1; fi
echo "==> Image generation successful"'
stub df 'printf "Avail\n%sM\n" "${BOOT_FREE:-500}"'
}
@test "check_initramfs does nothing when the build worked" {
pacman_log "(3/4) Updating linux initcpios..." \
"==> Building image from preset: /etc/mkinitcpio.d/linux.preset: 'default'" \
"==> Image generation successful"
fake_mkinitcpio
run check_initramfs
[ "$status" -eq 0 ]
not_called sudo
}
@test "check_initramfs builds it again, and is fine when that works" {
pacman_log "(3/4) Updating linux initcpios..." "==> ERROR: module not found: 'nvidia'"
fake_mkinitcpio
run check_initramfs
[ "$status" -eq 0 ]
[[ "$output" == *"Trying again"*"built this time"* ]]
called sudo mkinitcpio -P
refute initramfs_build_failed
}
@test "check_initramfs names a missing hook when building again fails too" {
pacman_log "(3/4) Updating linux initcpios..." "==> ERROR: Hook 'sd-plymouth' cannot be found"
fake_mkinitcpio
export MKINITCPIO_FAILS="==> ERROR: Hook 'sd-plymouth' cannot be found"
run check_initramfs
[ "$status" -eq 1 ]
[[ "$output" == *"Don't reboot until it's fixed"* ]]
[[ "$output" == *"HOOKS lists 'sd-plymouth'"* ]]
[[ "$output" != *"/boot is nearly full"* ]]
initramfs_build_failed
}
@test "explain_initramfs_failure says when /boot is full, and names a missing module" {
pacman_log "(3/4) Updating linux initcpios..." \
"==> ERROR: module not found: 'nvidia'" \
"cp: error writing '/boot/initramfs-linux.img': No space left on device"
fake_mkinitcpio
BOOT_FREE=3 run explain_initramfs_failure
[[ "$output" == *"/boot is nearly full (3 MB free)"* ]]
[[ "$output" == *"MODULES lists 'nvidia'"* ]]
}
# --- mirrors ----------------------------------------------------------------
setup_mirrors() {
export MIRRORLIST="$BATS_TEST_TMPDIR/mirrorlist"
cat > "$MIRRORLIST" << 'LIST'
## Worldwide
#Server = https://commented.example/archlinux/$repo/os/$arch
Server = https://first.example/archlinux/$repo/os/$arch
Server = https://second.example/$repo/os/$arch
LIST
# lastsync is $SYNCED, a Unix time
stub curl 'echo "$SYNCED"; [ -n "$SYNCED" ]'
source "$REPO/utils.sh"
}
@test "first_mirror is the first Server line that isn't commented out" {
setup_mirrors
[ "$(first_mirror)" = "https://first.example/archlinux" ]
}
@test "mirror_age_hours reads the first mirror's lastsync" {
setup_mirrors
export SYNCED="$(( $(date +%s) - 30 * 3600 ))"
run mirror_age_hours
[ "$status" -eq 0 ]
[ "$output" = 30 ]
called curl -sf --max-time 10 https://first.example/archlinux/lastsync
}
@test "mirror_age_hours fails when the mirror can't be reached or has no lastsync" {
setup_mirrors
export SYNCED=""
run mirror_age_hours
[ "$status" -ne 0 ]
export SYNCED="<html>Not Found</html>"
run mirror_age_hours
[ "$status" -ne 0 ]
}
@test "mirror_age_hours fails without a mirrorlist" {
setup_mirrors
rm "$MIRRORLIST"
run mirror_age_hours
[ "$status" -ne 0 ]
not_called curl
}
# --- unowned_conflicts, move_aside, put_back ---------------------------------
# A pacman -Su log that stopped on conflicting files: one line per path given
conflict_log() {
LOG="$BATS_TEST_TMPDIR/pacman.log"
{
echo ":: Proceed with installation? [Y/n] "
echo "(2/2) checking for file conflicts [######] 100%"
echo "error: failed to commit transaction (conflicting files)"
for line in "$@"; do printf '%s\r\n' "$line"; done
echo "Errors occurred, no packages were upgraded."
} > "$LOG"
}
@test "unowned_conflicts lists the files no package owns" {
source "$REPO/utils.sh"
stub pacman 'exit 1'
conflict_log "python-foo: /usr/lib/python3.13/site-packages/foo/__init__.py exists in filesystem" \
"python-foo: /usr/lib/python3.13/site-packages/foo exists in filesystem" \
"bar: /usr/bin/bar exists in filesystem"
run unowned_conflicts "$LOG"
[ "$status" -eq 0 ]
[ "$output" = "$(printf '%s\n' /usr/bin/bar /usr/lib/python3.13/site-packages/foo \
/usr/lib/python3.13/site-packages/foo/__init__.py)" ]
}
@test "unowned_conflicts gives up when a package owns a conflicting file" {
source "$REPO/utils.sh"
stub pacman 'exit 1'
conflict_log "foo: /usr/bin/foo exists in filesystem" \
"foo: /usr/bin/bar exists in filesystem (owned by bar)"
run unowned_conflicts "$LOG"
[ "$status" -eq 1 ]
[ -z "$output" ]
}
@test "unowned_conflicts gives up when pacman finds an owner now" {
source "$REPO/utils.sh"
stub pacman '[ "$2" = /usr/bin/foo ]'
conflict_log "foo: /usr/bin/foo exists in filesystem"
run unowned_conflicts "$LOG"
[ "$status" -eq 1 ]
}
@test "unowned_conflicts finds nothing in other failures" {
source "$REPO/utils.sh"
stub pacman 'exit 1'
LOG="$BATS_TEST_TMPDIR/pacman.log"
printf 'error: failed to prepare transaction (could not satisfy dependencies)\n' > "$LOG"
run unowned_conflicts "$LOG"
[ "$status" -eq 1 ]
run unowned_conflicts "$BATS_TEST_TMPDIR/missing.log"
[ "$status" -eq 1 ]
}
@test "move_aside moves files and folders under REPLACED_DIR, and put_back returns them" {
source "$REPO/utils.sh"
stub sudo '"$@"'
REPLACED_DIR="$BATS_TEST_TMPDIR/replaced"
local file="$BATS_TEST_TMPDIR/usr/bin/foo" dir="$BATS_TEST_TMPDIR/usr/lib/foo"
mkdir -p "$dir" "$(dirname "$file")"
echo old > "$file"
echo x > "$dir/x"
run move_aside "$file" "$dir"
[ "$status" -eq 0 ]
[ ! -e "$file" ] && [ ! -e "$dir" ]
[ "$(cat "$REPLACED_DIR$file")" = old ]
[ -f "$REPLACED_DIR$dir/x" ]
# The upgrade installed its own foo, but not the folder
echo new > "$file"
run put_back "$file" "$dir"
[ "$status" -eq 0 ]
[ "$(cat "$file")" = new ]
[ -f "$dir/x" ]
[ "$(cat "$REPLACED_DIR$file")" = old ]
}
@test "move_aside keeps what an earlier move left in REPLACED_DIR" {
source "$REPO/utils.sh"
stub sudo '"$@"'
REPLACED_DIR="$BATS_TEST_TMPDIR/replaced"
local file="$BATS_TEST_TMPDIR/usr/bin/foo"
mkdir -p "$(dirname "$file")"
echo first > "$file"
move_aside "$file"
echo second > "$file"
move_aside "$file"
[ "$(cat "$REPLACED_DIR$file")" = second ]
[ "$(cat "$REPLACED_DIR$file.~1~")" = first ]
}