#!/usr/bin/env bash set -euo pipefail function_path=$1 workdir=$(mktemp -d) trap 'rm -rf "$workdir"' EXIT export HOME="$workdir/home" mkdir -p "$HOME" git config --global user.email test@example.invalid git config --global user.name 'Deploy Test' origin="$workdir/origin.git" repo="$workdir/repo" ssh_log="$workdir/ssh.log" mock_bin="$workdir/bin" git init --bare "$origin" >/dev/null git clone "$origin" "$repo" >/dev/null 2>&1 cd "$repo" git switch -c impl/test >/dev/null echo test >README.md git add README.md git commit -m 'test fixture' >/dev/null git push -u origin impl/test >/dev/null 2>&1 mkdir -p "$mock_bin" bash_path=${BASH_PATH:-$(command -v bash)} cat >"$mock_bin/ssh" <"\$SSH_LOG" printf '%s\n' "\$2" >>"\$SSH_LOG" SSH chmod +x "$mock_bin/ssh" export SSH_LOG="$ssh_log" export PATH="$mock_bin:$PATH" fish_definition="function deploy-to; source '$function_path' \$argv; end" default_command="$fish_definition; deploy-to pikachu" explicit_command="$fish_definition; deploy-to pikachu impl/test" fish -c "$default_command" expected_command='cd ~/wrk/dotfiles && git fetch origin && git switch -- impl/test && git reset --hard origin/impl/test && sudo nixos-rebuild switch --flake .#pikachu --print-build-logs' mapfile -t ssh_call <"$ssh_log" test "${ssh_call[0]}" = pikachu test "${ssh_call[1]}" = "$expected_command" rm "$ssh_log" fish -c "$explicit_command" mapfile -t ssh_call <"$ssh_log" test "${ssh_call[0]}" = pikachu test "${ssh_call[1]}" = "$expected_command" rm "$ssh_log" echo dirty >>README.md if fish -c "$default_command" 2>"$workdir/dirty.err" then echo 'deploy-to unexpectedly allowed a dirty worktree' >&2 exit 1 fi grep -F 'dirty local worktree' "$workdir/dirty.err" >/dev/null git checkout -- README.md git commit --allow-empty -m 'unpushed fixture' >/dev/null if fish -c "$default_command" 2>"$workdir/unpushed.err" then echo 'deploy-to unexpectedly allowed a branch behind origin' >&2 exit 1 fi grep -F 'local HEAD differs from origin/impl/test' "$workdir/unpushed.err" >/dev/null