#!/bin/sh # The release scripts never remove a worktree they didn't make (Astra's # Task 16 finding 1): in a disposable repository, a dirty worktree already # at the old fixed path survives a failed run and a good one; a run's own # worktree goes; a failed add leaves nothing behind. set -eu here=$(cd "$(dirname "$0")" && pwd) scratch=$(mktemp -d "${TMPDIR:-/tmp}/base-wt-test.XXXXXX") trap 'rm -rf "$scratch"' EXIT repo=$scratch/repo git init -q "$repo" git -C "$repo" -c user.name=t -c user.email=t@x commit -q --allow-empty -m one commit=$(git -C "$repo" rev-parse HEAD) # Someone's worktree at the path the old script used, with unsaved work. theirs=$scratch/base-tf-1 git -C "$repo" worktree add -q --detach "$theirs" "$commit" echo unsaved > "$theirs/unsaved.txt" fail() { echo "worktree-lib_test: $*" >&2; exit 1; } # A failed run: the add fails (no such commit); cleanup removes nothing of theirs. ( export TMPDIR=$scratch . "$here/worktree-lib.sh" if own_worktree_add "$repo" deadbeefdeadbeef base-tf-1 2>/dev/null; then exit 3; fi own_worktree_cleanup "$repo" [ ! -e "$OWN_WT" ] || exit 4 ) || fail "a failed add left something behind (status $?)" [ -f "$theirs/unsaved.txt" ] || fail "a failed run deleted an existing worktree" git -C "$repo" worktree list | grep -q "$(cd "$theirs" && pwd -P)" || fail "a failed run unregistered an existing worktree" # A good run: its own worktree, somewhere new, removed at the end; theirs stays. mine=$( export TMPDIR=$scratch . "$here/worktree-lib.sh" own_worktree_add "$repo" "$commit" base-tf-1 [ "$OWN_WT" != "$theirs" ] || exit 5 [ -f "$OWN_WT/.git" ] || exit 6 echo "$OWN_WT" own_worktree_cleanup "$repo" ) || fail "a good run failed" [ ! -e "$mine" ] || fail "a run's own worktree was left behind" [ -f "$theirs/unsaved.txt" ] || fail "a good run deleted an existing worktree" echo "worktree-lib_test: ok"