#!/usr/bin/env bats # tests/unit/adopt.bats — Non-destructive adoption merge tests setup() { export JOSH_SYNC_ROOT="$(cd "$BATS_TEST_DIRNAME/../.." && pwd)" source "$JOSH_SYNC_ROOT/lib/core.sh" source "$JOSH_SYNC_ROOT/lib/adopt.sh" TEST_ROOT="$(mktemp -d)" export BOT_NAME="test-bot" export BOT_EMAIL="test-bot@test.local" export BOT_TRAILER="Josh-Sync-Origin" export JOSH_SYNC_TARGET_NAME="example" export SYNC_BRANCH_MONO="main" export SYNC_BRANCH_SUBREPO="main" } teardown() { rm -rf "$TEST_ROOT" } make_bare_repo() { local name="$1" local content="$2" local message="$3" local work="${TEST_ROOT}/${name}-work" local bare="${TEST_ROOT}/${name}.git" git init "$work" >/dev/null git -C "$work" checkout -b main >/dev/null git -C "$work" config user.name "Test User" git -C "$work" config user.email "test@test.local" printf '%s\n' "$content" > "${work}/README.md" git -C "$work" add README.md git -C "$work" commit -m "$message" >/dev/null git init --bare "$bare" >/dev/null git -C "$work" remote add origin "$bare" git -C "$work" push origin main >/dev/null printf '%s\n' "$bare" } subrepo_auth_url() { printf '%s\n' "$SUBREPO_BARE" } josh_auth_url() { printf '%s\n' "$JOSH_BARE" } @test "adopt_branch creates merge with josh head first parent and subrepo head second parent" { SUBREPO_BARE="$(make_bare_repo subrepo "same tree" "subrepo history")" JOSH_BARE="$(make_bare_repo josh "same tree" "josh filtered history")" local old_subrepo_head josh_head josh_tree result adopt_head old_subrepo_head=$(git --git-dir="$SUBREPO_BARE" rev-parse main) josh_head=$(git --git-dir="$JOSH_BARE" rev-parse main) josh_tree=$(git --git-dir="$JOSH_BARE" rev-parse 'main^{tree}') result=$(adopt_branch) [ "$result" = "adopted" ] adopt_head=$(git --git-dir="$SUBREPO_BARE" rev-parse main) [ "$adopt_head" != "$old_subrepo_head" ] [ "$(git --git-dir="$SUBREPO_BARE" rev-parse 'main^1')" = "$josh_head" ] [ "$(git --git-dir="$SUBREPO_BARE" rev-parse 'main^2')" = "$old_subrepo_head" ] [ "$(git --git-dir="$SUBREPO_BARE" rev-parse 'main^{tree}')" = "$josh_tree" ] } @test "adopt_branch aborts when trees differ and leaves subrepo head unchanged" { SUBREPO_BARE="$(make_bare_repo subrepo "subrepo tree" "subrepo history")" JOSH_BARE="$(make_bare_repo josh "josh tree" "josh filtered history")" local old_subrepo_head result old_subrepo_head=$(git --git-dir="$SUBREPO_BARE" rev-parse main) result=$(adopt_branch) [ "$result" = "tree-mismatch" ] [ "$(git --git-dir="$SUBREPO_BARE" rev-parse main)" = "$old_subrepo_head" ] } @test "adopt_branch reports missing subrepo branch before cloning" { SUBREPO_BARE="$(make_bare_repo subrepo "same tree" "subrepo history")" JOSH_BARE="$(make_bare_repo josh "same tree" "josh filtered history")" export SYNC_BRANCH_SUBREPO="missing" local result result=$(adopt_branch) [ "$result" = "missing-branch" ] }