#!/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" } # 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."* ]] }