#!/usr/bin/env bats load test_helper # sibling runs against real git repositories: each sibling is a clone of a # repo in $ORIGINS, which a test can add commits and tags to. setup() { common_setup export GIT_AUTHOR_NAME=Test GIT_AUTHOR_EMAIL=test@example.com export GIT_COMMITTER_NAME=Test GIT_COMMITTER_EMAIL=test@example.com ORIGINS="$BATS_TEST_TMPDIR/origins" SIBLINGS="$HOME/.local/share/siblings" mkdir -p "$ORIGINS" "$SIBLINGS" # pacman's log, with an upgrade an hour ago, and an update that logs its runs export SIBLING_PACMAN_LOG="$BATS_TEST_TMPDIR/pacman.log" upgraded_hours_ago 1 mkdir -p "$HOME/.local/bin" printf '%s\n' '#!/bin/bash' 'echo "update ran" >> "$HOME/update.log"' > "$HOME/.local/bin/update" chmod +x "$HOME/.local/bin/update" } # upgraded_hours_ago N: pacman's log says the last full upgrade was N hours ago upgraded_hours_ago() { { echo "[$(date -d '30 days ago' +%Y-%m-%dT%H:%M:%S%z)] [PACMAN] starting full system upgrade" echo "[$(date -d "$1 hours ago" +%Y-%m-%dT%H:%M:%S%z)] [PACMAN] starting full system upgrade" echo "[$(date +%Y-%m-%dT%H:%M:%S%z)] [PACMAN] Running 'pacman -S foo'" } > "$SIBLING_PACMAN_LOG" } # A sibling's origin whose install.sh records that it ran installable() { git init -q -b main "$ORIGINS/$1" echo 'touch "$HOME/installed-$(basename "$PWD")"' > "$ORIGINS/$1/install.sh" git -C "$ORIGINS/$1" add -A git -C "$ORIGINS/$1" commit -qm first } # origin NAME: make a repo with one commit, and clone it as a sibling origin() { git init -q -b main "$ORIGINS/$1" commit "$1" first git clone -q "$ORIGINS/$1" "$SIBLINGS/$1" } # commit NAME MESSAGE: add a commit to NAME's origin commit() { echo "$2" >> "$ORIGINS/$1/log" git -C "$ORIGINS/$1" add -A git -C "$ORIGINS/$1" commit -qm "$2" } @test "sibling list shows a tagged sibling's tag, and an untagged one's date and hash" { origin tagged git -C "$ORIGINS/tagged" tag v1.0.0 git -C "$SIBLINGS/tagged" pull -q origin plain run bash "$REPO/sibling" list [ "$status" -eq 0 ] [[ "$output" == *"tagged v1.0.0 "*"$ORIGINS/tagged"* ]] [[ "$output" =~ plain\ +[0-9]{4}\.[0-9]{2}\.[0-9]{2}\ \([0-9a-f]+\) ]] } @test "sibling list counts the commits since the latest tag" { origin tools git -C "$ORIGINS/tools" tag v1.0.0 commit tools second commit tools third git -C "$SIBLINGS/tools" pull -q run bash "$REPO/sibling" list [[ "$output" == *"tools v1.0.0+2 "* ]] } @test "sibling check shows what's newer, what's up to date, and what it couldn't reach" { origin newer git -C "$ORIGINS/newer" tag v1.0.0 git -C "$SIBLINGS/newer" pull -q commit newer second git -C "$ORIGINS/newer" tag v1.1.0 origin current origin gone rm -rf "$ORIGINS/gone" run bash "$REPO/sibling" check [ "$status" -eq 0 ] [[ "$output" == *"newer v1.0.0"*"v1.1.0 is available"* ]] [[ "$output" == *"current "*"up to date"* ]] [[ "$output" == *"gone "*"couldn't check"* ]] run bash "$REPO/sibling" check --porcelain [[ "$output" == *$'newer\tv1.0.0\tv1.1.0'* ]] [[ "$output" == *$'gone\t'*$'\t?'* ]] } @test "sibling check exits 1 when nothing is newer" { origin current run bash "$REPO/sibling" check [ "$status" -eq 1 ] [[ "$output" == *"up to date"* ]] } @test "sibling update updates only the siblings named" { origin one origin two echo 'echo ran >> "$HOME/one-updated"' > "$ORIGINS/one/update.sh" commit one second git -C "$ORIGINS/one" tag v2.0.0 commit two second run bash "$REPO/sibling" update one [ "$status" -eq 0 ] [[ "$output" == *"one is at v2.0.0."* ]] [ "$(cat "$HOME/one-updated")" = ran ] [ "$(git -C "$SIBLINGS/two" rev-list --count HEAD)" -eq 1 ] } @test "sibling update with no names updates them all, and reports a failing update.sh" { origin one origin two echo 'exit 1' > "$ORIGINS/one/update.sh" commit one second commit two second run bash "$REPO/sibling" update [ "$status" -eq 1 ] [[ "$output" == *"one's update.sh failed"* ]] [ "$(git -C "$SIBLINGS/two" rev-list --count HEAD)" -eq 2 ] } @test "sibling update refuses a sibling that isn't installed" { run bash "$REPO/sibling" update nope [ "$status" -eq 1 ] [[ "$output" == *"No sibling named 'nope' is installed."* ]] } @test "sibling add clones the sibling and runs its install.sh" { installable tools run bash "$REPO/sibling" add "$ORIGINS/tools" [ "$status" -eq 0 ] [ -f "$HOME/installed-tools" ] [[ "$output" == *"Installed tools "* ]] [ ! -e "$HOME/update.log" ] } @test "sibling add runs update first when the last upgrade is over a day old" { installable tools upgraded_hours_ago 50 run bash "$REPO/sibling" add "$ORIGINS/tools" [ "$status" -eq 0 ] [[ "$output" == *"last upgraded 2 day(s) ago. Running update first"* ]] [ "$(cat "$HOME/update.log")" = "update ran" ] [ -f "$HOME/installed-tools" ] } @test "sibling add runs update first when it can't tell when the system was upgraded" { installable tools : > "$SIBLING_PACMAN_LOG" run bash "$REPO/sibling" add "$ORIGINS/tools" [[ "$output" == *"Couldn't tell when this system was last upgraded"* ]] [ "$(cat "$HOME/update.log")" = "update ran" ] } @test "sibling add goes on if update fails, and doesn't run update inside update" { installable tools upgraded_hours_ago 50 printf '%s\n' '#!/bin/bash' 'echo "update ran" >> "$HOME/update.log"; exit 1' > "$HOME/.local/bin/update" run bash "$REPO/sibling" add "$ORIGINS/tools" [ "$status" -eq 0 ] [[ "$output" == *"update reported errors (above); adding the sibling anyway."* ]] [ -f "$HOME/installed-tools" ] rm "$HOME/update.log" installable more COREDESKTOP_ALLOW_PACMAN=1 run bash "$REPO/sibling" add "$ORIGINS/more" [ "$status" -eq 0 ] [ ! -e "$HOME/update.log" ] }