git-annex-mirror-action/tests/test-git-annex-ref-mirror

386 lines
8.5 KiB
Shell
Executable file

#!/usr/bin/env bash
set -Eeuo pipefail
tool=${GIT_ANNEX_REF_MIRROR:-git-annex-ref-mirror}
fixture_script=$(cd "$(dirname "$0")" && pwd)/setup-git-annex-fixture.sh
fail() {
echo "FAIL: $*" >&2
exit 1
}
pass() {
echo "PASS: $*"
}
section() {
echo
echo "> $*"
}
gitc() {
local repo=$1
shift
git -C "$repo" "$@"
}
ref_oid() {
gitc "$1" rev-parse "$2"
}
object_count() {
local repo=$1
local objects_dir
objects_dir=$(gitc "$repo" rev-parse --git-path annex/objects)
if [[ -d "$repo/$objects_dir" ]]; then
find "$repo/$objects_dir" -type f -print | wc -l
else
echo 0
fi
}
assert_equal() {
local actual=$1
local expected=$2
local description=$3
[[ "$actual" == "$expected" ]] ||
fail "$description"
}
assert_not_equal() {
local actual=$1
local expected=$2
local description=$3
[[ "$actual" != "$expected" ]] ||
fail "$description"
}
assert_ref_exists() {
local repo=$1
local ref=$2
gitc "$repo" show-ref --verify --quiet "$ref" ||
fail "ref does not exist: $repo $ref"
}
assert_ref_absent() {
local repo=$1
local ref=$2
if gitc "$repo" show-ref --verify --quiet "$ref"; then
fail "ref unexpectedly exists: $repo $ref"
fi
}
assert_refs_equal() {
local repo_a=$1
local ref_a=$2
local repo_b=$3
local ref_b=$4
assert_equal \
"$(ref_oid "$repo_a" "$ref_a")" \
"$(ref_oid "$repo_b" "$ref_b")" \
"refs differ: $repo_a $ref_a != $repo_b $ref_b"
}
assert_file_contains() {
local file=$1
local pattern=$2
grep -F -- "$pattern" "$file" >/dev/null ||
fail "file '$file' does not contain: $pattern"
}
expect_failure() {
local output=$1
local description=$2
shift 2
if "$@" >"$output" 2>&1; then
fail "$description unexpectedly succeeded"
fi
}
sync_mirror() {
local options=()
while (($# > 0)); do
case "$1" in
--prune|--force-tags)
options+=("$1")
;;
*)
fail "unknown synchronization option: $1"
;;
esac
shift
done
"$tool" --quiet "${options[@]}" "$mirror_repo" upstream
}
cleanup() {
chmod -R u+rwx "$tmpdir"
rm -rf -- "$tmpdir"
}
for command in "$tool" git git-annex; do
command -v "$command" >/dev/null 2>&1 ||
fail "cannot find '$command' in PATH"
done
source "$fixture_script"
trap cleanup EXIT
echo "Using temporary directory: $tmpdir"
# ----------------------------------------------------------------------
section "Testing command-line behavior"
expect_failure \
"$tmpdir/no-arguments.out" \
"invocation without arguments" \
"$tool"
assert_file_contains \
"$tmpdir/no-arguments.out" \
"Usage:"
pass "missing arguments produce usage"
"$tool" --help >"$tmpdir/help.out" 2>&1
assert_file_contains \
"$tmpdir/help.out" \
"Synchronize Git branch"
assert_file_contains \
"$tmpdir/help.out" \
"--force-tags"
pass "--help documents the current options"
# ----------------------------------------------------------------------
section "Validating source repository"
[[ -n "$source_uuid" ]] ||
fail "source repository did not receive an annex UUID"
for ref in \
refs/heads/main \
refs/heads/git-annex \
refs/heads/old-branch \
refs/tags/v1
do
assert_ref_exists "$source_repo" "$ref"
done
[[ "$(object_count "$source_repo")" -gt 0 ]] ||
fail "source repository did not contain an annex object"
# ----------------------------------------------------------------------
section "Validating destination repository"
[[ -n "$mirror_uuid" ]] ||
fail "destination did not receive an annex UUID"
assert_not_equal \
"$mirror_uuid" \
"$source_uuid" \
"source and destination unexpectedly have the same annex UUID"
# ----------------------------------------------------------------------
section "Testing initial synchronization"
sync_mirror
for ref in \
refs/heads/main \
refs/heads/old-branch
do
assert_refs_equal "$source_repo" "$ref" "$mirror_repo" "$ref"
done
assert_ref_exists \
"$mirror_repo" \
refs/remotes/upstream/git-annex
assert_ref_exists \
"$mirror_repo" \
refs/heads/git-annex
assert_ref_absent \
"$mirror_repo" \
refs/tags/v1
assert_not_equal \
"$(ref_oid "$source_repo" refs/heads/git-annex)" \
"$(ref_oid "$mirror_repo" refs/heads/git-annex)" \
"destination git-annex branch was replaced by the remote branch"
assert_equal \
"$(object_count "$mirror_repo")" \
0 \
"destination acquired annex content"
pass "branches were synchronized"
pass "remote git-annex was fetched but not copied over the local branch"
pass "tags were ignored by default"
pass "annex content was not copied"
# ----------------------------------------------------------------------
section "Testing tag synchronization and forced updates"
sync_mirror --force-tags
assert_refs_equal \
"$source_repo" refs/tags/v1 \
"$mirror_repo" refs/tags/v1
old_tag_oid=$(ref_oid "$mirror_repo" refs/tags/v1)
printf 'second annex content\n' >"$source_repo/second.txt"
gitc "$source_repo" annex add second.txt >/dev/null
gitc "$source_repo" commit -q -m "Add second annexed file"
source_main_after_update=$(ref_oid "$source_repo" refs/heads/main)
# Move the existing tag to the new commit.
gitc "$source_repo" tag -f v1 >/dev/null
new_tag_oid=$(ref_oid "$source_repo" refs/tags/v1)
assert_not_equal \
"$new_tag_oid" \
"$old_tag_oid" \
"source tag did not change"
# Branches update normally, but changed tags do not update without
# --force-tags.
sync_mirror
assert_equal \
"$(ref_oid "$mirror_repo" refs/heads/main)" \
"$source_main_after_update" \
"updated source branch was not mirrored"
assert_equal \
"$(ref_oid "$mirror_repo" refs/tags/v1)" \
"$old_tag_oid" \
"tag changed without --force-tags"
sync_mirror --force-tags
assert_equal \
"$(ref_oid "$mirror_repo" refs/tags/v1)" \
"$new_tag_oid" \
"changed remote tag was not force-updated"
assert_equal \
"$(object_count "$mirror_repo")" \
0 \
"destination acquired annex content after source update"
pass "branch updates were synchronized"
pass "changed tags were ignored by default"
pass "changed tags were force-updated with --force-tags"
pass "source updates did not copy annex content"
# ----------------------------------------------------------------------
section "Testing idempotence"
main_before=$(ref_oid "$mirror_repo" refs/heads/main)
annex_before=$(ref_oid "$mirror_repo" refs/heads/git-annex)
tag_before=$(ref_oid "$mirror_repo" refs/tags/v1)
sync_mirror --force-tags
assert_equal \
"$(ref_oid "$mirror_repo" refs/heads/main)" \
"$main_before" \
"main changed during an idempotent run"
assert_equal \
"$(ref_oid "$mirror_repo" refs/heads/git-annex)" \
"$annex_before" \
"git-annex changed during an idempotent run"
assert_equal \
"$(ref_oid "$mirror_repo" refs/tags/v1)" \
"$tag_before" \
"tag changed during an idempotent run"
pass "repeated synchronization is idempotent"
# ----------------------------------------------------------------------
section "Testing pruning behavior"
gitc "$source_repo" branch -D old-branch >/dev/null
gitc "$source_repo" tag -d v1 >/dev/null
# Without --prune, both deleted refs remain locally.
sync_mirror
assert_ref_exists \
"$mirror_repo" \
refs/heads/old-branch
assert_ref_exists \
"$mirror_repo" \
refs/tags/v1
pass "deleted branches are retained without --prune"
pass "deleted tags are retained without --prune"
# --prune removes the local branch, but --force-tags deliberately does
# not prune local-only tags.
sync_mirror --prune --force-tags
assert_ref_absent \
"$mirror_repo" \
refs/heads/old-branch
assert_ref_exists \
"$mirror_repo" \
refs/tags/v1
assert_ref_exists \
"$mirror_repo" \
refs/heads/git-annex
assert_ref_exists \
"$mirror_repo" \
refs/remotes/upstream/git-annex
pass "deleted branches are removed with --prune"
pass "local-only tags are retained with --prune"
pass "git-annex branches are not pruned"
# ----------------------------------------------------------------------
section "Final checks"
assert_not_equal \
"$mirror_uuid" \
"$source_uuid" \
"destination annex UUID changed unexpectedly"
assert_equal \
"$(object_count "$mirror_repo")" \
0 \
"destination contains annex content"
pass "destination retained its own annex UUID"
pass "destination contains no annex content"
echo
echo "All tests passed."