Files
coredesktop/tests/version.bats
T
davidandClaude Opus 5.5 1c468948c6
shellcheck / shellcheck (push) Failing after 18s
version: show a spinner while it checks for updates
Checking for newer versions goes to the network and can take several
seconds with nothing on screen. Show "Checking for updates..." with a
spinner on the terminal until every check is done, then print the
results all at once. The spinner goes to stderr and only to a terminal.

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

151 lines
5.4 KiB
Bash

#!/usr/bin/env bats
load test_helper
# version runs from a fake checkout with fake git, uname, pacman and
# checkupdates. The running kernel's modules never exist here, so the kernel
# line always also says a newer one is installed. Sibling projects are real git
# repositories: only git commands on the fake checkout are faked.
setup() {
common_setup
CD="$HOME/.local/share/coredesktop"
mkdir -p "$CD"
cp "$REPO/version" "$REPO/utils.sh" "$REPO/sibling" "$CD/"
export CD REAL_GIT
REAL_GIT="$(command -v git)"
stub git '[ "$2" = "$CD" ] || exec "$REAL_GIT" "$@"
case "$3" in
rev-parse) exit 0 ;;
log) echo "2026.10.31 (6bc4fdc)" ;;
fetch) exit 1 ;;
esac'
stub uname 'echo "${KERNEL:-9.9.1-arch1-1}"'
# pacman -Q <package>: the versions in $INSTALLED, "name version" per line
stub pacman 'grep "^$2 " <<< "$INSTALLED" || exit 1'
stub checkupdates 'printf "%s" "$PENDING"; [ -n "$PENDING" ] || exit 2'
export INSTALLED="mesa 1:26.2.6-1" PENDING=""
# The mirror synced $SYNCED_HOURS ago (an hour by default)
export MIRRORLIST="$BATS_TEST_TMPDIR/mirrorlist" SYNCED_HOURS=1
echo 'Server = https://mirror.example/archlinux/$repo/os/$arch' > "$MIRRORLIST"
stub curl 'echo "$(( $(date +%s) - SYNCED_HOURS * 3600 ))"'
}
@test "version shows the kernel and Mesa, with nothing newer to install" {
run bash "$CD/version"
[ "$status" -eq 0 ]
[[ "$output" == *"CoreDesktop 2026.10.31 (6bc4fdc)"* ]]
[[ "$output" == *"Kernel 9.9.1-arch1-1 (newer kernel installed, reboot to use it)"$'\n'* ]]
[[ "$output" == *"Video driver Mesa 26.2.6"$'\n'* ]]
[[ "$output" != *"is available"* ]]
[[ "$output" != *"mirror"* ]]
}
@test "version suggests update for a newer kernel and video driver" {
export PENDING=$'linux 9.9.1.arch1-1 -> 9.9.2.arch1-1\nmesa 1:26.2.6-1 -> 1:26.3.0-1\n'
run bash "$CD/version"
[ "$status" -eq 0 ]
[[ "$output" == *"Kernel 9.9.1-arch1-1"*", 9.9.2 is available (run update)"* ]]
[[ "$output" == *"Video driver Mesa 26.2.6, 26.3.0 is available (run update)"* ]]
}
@test "version shows NVIDIA's driver over Mesa, and ignores a rebuild of the same version" {
export INSTALLED=$'mesa 1:26.2.6-1\nnvidia-utils 615.78.08-1'
export PENDING=$'nvidia-utils 615.78.08-1 -> 615.78.08-2\n'
run bash "$CD/version"
[ "$status" -eq 0 ]
[[ "$output" == *"Video driver NVIDIA 615.78.08"$'\n'* ]]
[[ "$output" != *"is available"* ]]
}
@test "version checks the kernel package that's running, e.g. linux-lts" {
export KERNEL=6.12.50-1-lts
export PENDING=$'linux 9.9.1.arch1-1 -> 9.9.2.arch1-1\nlinux-lts 6.12.50-1 -> 6.12.51-1\n'
run bash "$CD/version"
[ "$status" -eq 0 ]
[[ "$output" == *"6.12.51 is available"* ]]
[[ "$output" != *"9.9.2"* ]]
}
@test "version leaves out what's available when it can't check" {
rm "$STUB_BIN/checkupdates"
stub checkupdates 'exit 1'
export PENDING=$'linux 9.9.1.arch1-1 -> 9.9.2.arch1-1\n'
run bash "$CD/version"
[ "$status" -eq 0 ]
[[ "$output" == *"Kernel 9.9.1-arch1-1"* ]]
[[ "$output" != *"is available"* ]]
}
@test "version warns when the package mirror is behind Arch" {
export SYNCED_HOURS=73
run bash "$CD/version"
[ "$status" -eq 0 ]
[[ "$output" == *"Your package mirror (https://mirror.example/archlinux) last synced with Arch 3 days ago"* ]]
[[ "$output" == *"Run update"* ]]
}
@test "version doesn't warn about a mirror it can't reach" {
rm "$STUB_BIN/curl"
stub curl 'exit 7'
run bash "$CD/version"
[ "$status" -eq 0 ]
[[ "$output" != *"mirror"* ]]
}
# sibling_repo NAME: a sibling at v1.0.0 whose origin has a newer v1.1.0
sibling_repo() {
export GIT_AUTHOR_NAME=Test GIT_AUTHOR_EMAIL=test@example.com
export GIT_COMMITTER_NAME=Test GIT_COMMITTER_EMAIL=test@example.com
local origin="$BATS_TEST_TMPDIR/origins/$1"
git init -q -b main "$origin"
echo 'echo "$PWD" >> "$HOME/updated"' > "$origin/update.sh"
git -C "$origin" add -A
git -C "$origin" commit -qm first
git -C "$origin" tag v1.0.0
git clone -q "$origin" "$HOME/.local/share/siblings/$1"
git -C "$origin" commit -q --allow-empty -m second
git -C "$origin" tag v1.1.0
}
@test "version leaves out sibling projects when none are installed" {
run bash "$CD/version"
[ "$status" -eq 0 ]
[[ "$output" != *"Sibling"* ]]
}
@test "version lists sibling projects, and doesn't ask to update without a terminal" {
sibling_repo tools
run bash "$CD/version" < /dev/null
[ "$status" -eq 0 ]
[[ "$output" == *"Sibling projects"$'\n'" tools v1.0.0, v1.1.0 is available"* ]]
[[ "$output" != *"Update tools"* ]]
[ ! -f "$HOME/updated" ]
}
@test "version updates the sibling projects the user says yes to" {
sibling_repo keep
sibling_repo tools
run script -qec "bash '$CD/version'" /dev/null <<< $'n\ny'
[ "$status" -eq 0 ]
[[ "$output" == *"Update keep to v1.1.0? [y/N]"* ]]
[[ "$output" == *"tools is at v1.1.0."* ]]
[ "$(cat "$HOME/updated")" = "$HOME/.local/share/siblings/tools" ]
}
@test "version shows a spinner on a terminal while it checks, and clears it" {
run script -qec "bash '$CD/version'" /dev/null < /dev/null
[ "$status" -eq 0 ]
[[ "$output" == *"Checking for updates..."* ]]
# Cleared, and the cursor shown again, before the results are printed
[[ "$output" == *$'\r\e[K\e[?25hCoreDesktop 2026.10.31'* ]]
}
@test "version shows no spinner when its output isn't a terminal" {
run bash "$CD/version"
[ "$status" -eq 0 ]
[[ "$output" != *"Checking"* ]]
[[ "$output" != *$'\e[?25l'* ]]
}