#!/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 } # --- enable_gnome_extension ------------------------------------------------- # fake_gsettings VALUE: a gsettings whose enabled-extensions starts as VALUE # and is kept in $GSETTINGS fake_gsettings() { export GSETTINGS="$BATS_TEST_TMPDIR/enabled-extensions" printf '%s' "$1" > "$GSETTINGS" stub gsettings 'case "$1" in get) cat "$GSETTINGS" ;; set) printf "%s" "$4" > "$GSETTINGS" ;; esac' } @test "enable_gnome_extension adds the extension, keeping the others" { fake_gsettings "['a@b', 'c@d']" source "$REPO/utils.sh" run enable_gnome_extension coredesktop@federated.computer [ "$status" -eq 0 ] [ "$(cat "$GSETTINGS")" = "['a@b', 'c@d', 'coredesktop@federated.computer']" ] } @test "enable_gnome_extension works on an empty list" { fake_gsettings "@as []" source "$REPO/utils.sh" run enable_gnome_extension coredesktop@federated.computer [ "$status" -eq 0 ] [ "$(cat "$GSETTINGS")" = "['coredesktop@federated.computer']" ] } @test "enable_gnome_extension leaves the list alone if it's already there" { fake_gsettings "['coredesktop@federated.computer', 'a@b']" source "$REPO/utils.sh" run enable_gnome_extension coredesktop@federated.computer [ "$status" -eq 0 ] refute grep -q '^gsettings set' "$STUB_LOG" } @test "enable_gnome_extension fails if gsettings can't read the list" { stub gsettings 'exit 1' source "$REPO/utils.sh" run enable_gnome_extension coredesktop@federated.computer [ "$status" -ne 0 ] refute grep -q '^gsettings set' "$STUB_LOG" } # --- 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="Not Found" 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 ] }