Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

nushell/tests/git-sync-module.sh

Raw
#!/usr/bin/env bash
set -euo pipefail

repo_root="$(git rev-parse --show-toplevel)"
module="$repo_root/nushell/modules/sy.nu"
real_git="$(command -v git)"
tmp="$(mktemp -d /tmp/dotfiles-sy-git-test-XXXXXX)"
trap 'rm -rf "$tmp"' EXIT

fail() { echo "FAIL: $*" >&2; exit 1; }

bin="$tmp/bin"
mkdir -p "$bin"
cat >"$bin/git" <<'SH'
#!/usr/bin/env bash
set -euo pipefail
for arg in "$@"; do printf '%q ' "$arg" >>"$DOTFILES_SY_GIT_LOG"; done
printf '\n' >>"$DOTFILES_SY_GIT_LOG"
exec "$DOTFILES_REAL_GIT" "$@"
SH
cat >"$bin/macchina" <<'SH'
#!/usr/bin/env bash
printf 'macchina-ok\n'
SH
cat >"$bin/topgrade" <<'SH'
#!/usr/bin/env bash
printf 'topgrade-ok\n'
SH
chmod +x "$bin/git" "$bin/macchina" "$bin/topgrade"
log="$tmp/git.log"

run_nu() {
  PATH="$bin:$PATH" DOTFILES="$tmp/work" DOTFILES_REAL_GIT="$real_git" DOTFILES_SY_GIT_LOG="$log" nu --commands "use '$module'; $*"
}

git init --bare "$tmp/origin.git" >/dev/null
git clone "$tmp/origin.git" "$tmp/seed" >/dev/null 2>&1
git -C "$tmp/seed" config user.email test@example.invalid
git -C "$tmp/seed" config user.name test
echo root >"$tmp/seed/root.txt"
git -C "$tmp/seed" add root.txt
git -C "$tmp/seed" commit -m init >/dev/null
git -C "$tmp/seed" branch -M trunk
git -C "$tmp/seed" push -u origin trunk >/dev/null 2>&1
git --git-dir="$tmp/origin.git" symbolic-ref HEAD refs/heads/trunk

git clone "$tmp/origin.git" "$tmp/work" >/dev/null 2>&1
git -C "$tmp/work" config user.email test@example.invalid
git -C "$tmp/work" config user.name test

run_nu "sy" | grep -q 'macchina-ok' || fail 'main did not run macchina'
run_nu "sy up" >/tmp/dotfiles-sy-up.log
run_nu "sy down" | grep -q 'topgrade-ok' || fail 'down did not run topgrade'

git -C "$tmp/work" status --short --branch >/tmp/dotfiles-sy-status.log
grep -q '## trunk' /tmp/dotfiles-sy-status.log || fail 'work repo not on trunk after sy commands'

require() {
  local label="$1"
  local pattern="$2"
  if ! grep -Eq -- "$pattern" "$log"; then
    echo "FAIL: missing sy git invocation: $label" >&2
    echo "pattern: $pattern" >&2
    echo "--- git log ---" >&2
    cat "$log" >&2
    exit 1
  fi
}

require 'status short branch' "^-C $tmp/work status --short --branch $"
require 'add all' "^-C $tmp/work add --all $"
require 'commit all' "^-C $tmp/work commit --all $"
require 'push' "^-C $tmp/work push $"
require 'pull' "^-C $tmp/work pull $"

printf 'ok git-sync-module\n'