#!/usr/bin/env bats # Regression tests for scripts/sync-upstream.sh base handling: # - a baseline recorded from CURRENT upstream while the working copy actually # forks an OLDER upstream must be re-based onto the real ancestor, then # advanced to current upstream (previously the files stayed wedged forever); # - a first run with no baseline and a customized working copy must bootstrap # the baseline from the fork's real upstream base instead of assuming the # working copy derives from current upstream. # # Fully offline: a local fixture upstream is served via git's insteadOf, and a # COPY of the script is placed in the fixture app so BASH_SOURCE keeps the sync # operating on the fixture. setup() { REPO_ROOT="$(cd "${BATS_TEST_DIRNAME}/.." && pwd)" TEST_DIR="$(mktemp -d)" STATE_FILE="$TEST_DIR/upstream-sync-state" cd "$TEST_DIR" # Upstream history: c1 is the fork base, c3 is current upstream. git init --bare --quiet "$TEST_DIR/upstream.git" git init --quiet --initial-branch=master "$TEST_DIR/upstream-work" cd "$TEST_DIR/upstream-work" git config user.name Test git config user.email test@example.com printf 'A1\nA2\n' > quickemu printf 'G1\nG2\n' > quickget printf 'C1\n' > chunkcheck printf 'R1\nR2\n' > quickreport chmod a+x quickemu quickget quickreport # chunkcheck stays 0644 git add -A git commit --quiet -m c1 git remote add origin "$TEST_DIR/upstream.git" git push --quiet origin master BASE_C1="$(git rev-parse master)" printf 'A1\nA2\nA3\n' > quickemu git commit --quiet -am c2 printf 'A1\nA2\nA3\nA4\n' > quickemu printf 'G1\nG2\nG3\n' > quickget printf 'C1\nC2\n' > chunkcheck git add -A git commit --quiet -m c3 git push --quiet origin master UPSTREAM_SHA="$(git rev-parse master)" } teardown() { rm -rf "$TEST_DIR" } # Build the DistroHopper checkout under test. WRONG_BASE records baselines from # current upstream even though the root files fork c1; NO_BASE leaves baselines # absent (first run). make_app() { local mode="$1" mkdir -p "$TEST_DIR/app/upstream/baseline" "$TEST_DIR/app/scripts" cd "$TEST_DIR/app" git init --quiet --initial-branch=all . git config user.name Test git config user.email test@example.com # Root vendored files fork upstream c1 and carry a local patch. printf 'PATCH1\nA1\nA2\n' > quickemu printf 'PG\nG1\nG2\n' > quickget printf 'C1\n' > chunkcheck printf 'R1\nR2\n' > quickreport # equals current upstream: stay in sync chmod a+x quickemu quickget chunkcheck quickreport if [ "$mode" = wrong_base ]; then printf 'A1\nA2\nA3\nA4\n' > upstream/baseline/quickemu printf 'G1\nG2\nG3\n' > upstream/baseline/quickget printf 'C1\nC2\n' > upstream/baseline/chunkcheck printf 'R1\nR2\n' > upstream/baseline/quickreport printf '{"repo":"quickemu-project/quickemu","branch":"master","sha":"%s"}\n' "$UPSTREAM_SHA" > upstream/stamp.json fi cp "$REPO_ROOT/scripts/sync-upstream.sh" scripts/sync-upstream.sh chmod a+x scripts/sync-upstream.sh git add -A git commit --quiet -m state git config url."file://$TEST_DIR/upstream.git".insteadOf https://github.com/quickemu-project/quickemu.git } @test "sync re-bases a baseline recorded from the wrong upstream revision" { make_app wrong_base run env UPSTREAM_STATE_FILE="$STATE_FILE" bash "$TEST_DIR/app/scripts/sync-upstream.sh" [ "$status" -eq 0 ] # Merged file keeps its working-copy executable bit (not dropped by the temp spool). [ -x "$TEST_DIR/app/quickemu" ] [ -x "$TEST_DIR/app/quickget" ] [ "$(cat "$TEST_DIR/app/quickemu")" = "PATCH1 A1 A2 A3 A4" ] [ "$(cat "$TEST_DIR/app/quickget")" = "PG G1 G2 G3" ] [ "$(cat "$TEST_DIR/app/chunkcheck")" = "C1 C2" ] run cat "$STATE_FILE" [[ "$output" == *"CHANGED=3"* ]] [[ "$output" == *"CONFLICTS=0"* ]] # Baselines are re-pinned to the newly applied upstream HEAD. [ "$(cat "$TEST_DIR/app/upstream/baseline/quickemu")" = "A1 A2 A3 A4" ] } @test "sync vendors a brand-new upstream file into the working copy on first run" { mkdir -p "$TEST_DIR/app/upstream/baseline" "$TEST_DIR/app/scripts" cd "$TEST_DIR/app" git init --quiet --initial-branch=all . git config user.name Test git config user.email test@example.com # quickreport and chunkcheck are new upstream; the rest are already current. # chunkcheck (upstream 0644) must not abort the sync despite having no # mode to apply; quickreport (upstream 100755) must keep its exec bit. printf 'A1\nA2\nA3\nA4\n' > quickemu printf 'G1\nG2\nG3\n' > quickget cp "$REPO_ROOT/scripts/sync-upstream.sh" scripts/sync-upstream.sh chmod a+x scripts/sync-upstream.sh git add -A git commit --quiet -m state git config url."file://$TEST_DIR/upstream.git".insteadOf https://github.com/quickemu-project/quickemu.git run env UPSTREAM_STATE_FILE="$STATE_FILE" bash "$TEST_DIR/app/scripts/sync-upstream.sh" [ "$status" -eq 0 ] # The missing root files are vendored and reported as changed. [ -x "$TEST_DIR/app/quickreport" ] [ "$(cat "$TEST_DIR/app/quickreport")" = "R1 R2" ] [ ! -x "$TEST_DIR/app/chunkcheck" ] [ "$(cat "$TEST_DIR/app/chunkcheck")" = "C1 C2" ] [ "$(cat "$TEST_DIR/app/upstream/baseline/quickreport")" = "R1 R2" ] [ "$(cat "$TEST_DIR/app/upstream/baseline/chunkcheck")" = "C1 C2" ] run cat "$STATE_FILE" [[ "$output" == *"CHANGED=2"* ]] [[ "$output" == *"CONFLICTS=0"* ]] } @test "sync caches the fork base and skips the history scan on later runs" { local c1="$BASE_C1" make_app wrong_base run env UPSTREAM_STATE_FILE="$STATE_FILE" bash "$TEST_DIR/app/scripts/sync-upstream.sh" [ "$status" -eq 0 ] # First run detects and caches the real fork base (c1 is a zero-diff match). [ "$(cat "$TEST_DIR/app/upstream/baseline/quickemu.base")" = "$c1" ] [ "$(cat "$TEST_DIR/app/upstream/baseline/quickget.base")" = "$c1" ] # Second run with unchanged upstream reuses the cache, reports no change, # and must not regress (previously it rescanned the whole history). run env UPSTREAM_STATE_FILE="$STATE_FILE" bash "$TEST_DIR/app/scripts/sync-upstream.sh" [ "$status" -eq 0 ] [ "$(cat "$TEST_DIR/app/upstream/baseline/quickemu.base")" = "$c1" ] run cat "$STATE_FILE" [[ "$output" == *"CHANGED=0"* ]] [[ "$output" == *"CONFLICTS=0"* ]] # A cached commit that is present but not an ancestor of the fetched # upstream branch (e.g. upstream rewritten) must be discarded and # re-detected instead of trusted. local poisoned poisoned="$(git -C "$TEST_DIR/app" rev-parse HEAD)" printf '%s\n' "$poisoned" > "$TEST_DIR/app/upstream/baseline/quickemu.base" run env UPSTREAM_STATE_FILE="$STATE_FILE" bash "$TEST_DIR/app/scripts/sync-upstream.sh" [ "$status" -eq 0 ] # The stale, non-ancestor commit must not be trusted; the cache is # re-derived from the fetched branch. [ "$(cat "$TEST_DIR/app/upstream/baseline/quickemu.base")" != "$poisoned" ] } @test "sync finds the fork base from a merge commit's resolved blob" { mkdir -p "$TEST_DIR/merge-upstream.git" "$TEST_DIR/merge-work" "$TEST_DIR/app/scripts" git init --quiet --bare "$TEST_DIR/merge-upstream.git" git init --quiet --initial-branch=master "$TEST_DIR/merge-work" cd "$TEST_DIR/merge-work" git config user.name Test git config user.email test@example.com printf 'A1\nA2\n' > quickemu git add -A && git commit --quiet -m c1 git remote add origin "$TEST_DIR/merge-upstream.git" git push --quiet origin master git checkout --quiet -b feature printf 'A1\nF2\n' > quickemu && git commit --quiet -am f2 git checkout --quiet master printf 'A1\nM2\n' > quickemu && git commit --quiet -am m2 git merge feature >/dev/null 2>&1 || true # The fixture depends on this being a real, conflicted merge; a normal # commit with the same blob would make the test pass without ever # exercising the merge-commit code path. [ -f "$TEST_DIR/merge-work/.git/MERGE_HEAD" ] # Resolved blob A1/R2 exists only at the merge commit. printf 'A1\nR2\n' > quickemu git add -A git commit --quiet -m merge local merge_sha merge_sha="$(git rev-parse HEAD)" git push --quiet origin master cd "$TEST_DIR/app" git init --quiet --initial-branch=all . git config user.name Test git config user.email test@example.com # Working copy forks the merge commit's resolved blob (plus a patch). printf 'A1\nR2\nPATCH\n' > quickemu cp "$REPO_ROOT/scripts/sync-upstream.sh" scripts/sync-upstream.sh chmod a+x scripts/sync-upstream.sh git add -A && git commit --quiet -m state git config url."file://$TEST_DIR/merge-upstream.git".insteadOf https://github.com/quickemu-project/quickemu.git git remote add upstream https://github.com/quickemu-project/quickemu.git run env UPSTREAM_STATE_FILE="$STATE_FILE" bash "$TEST_DIR/app/scripts/sync-upstream.sh" [ "$status" -eq 0 ] # The baseline is the merge commit's blob, not a linear parent's. [ "$(cat "$TEST_DIR/app/upstream/baseline/quickemu")" = "A1 R2" ] [ "$(cat "$TEST_DIR/app/upstream/baseline/quickemu.base")" = "$merge_sha" ] } @test "sync bootstraps a fork baseline from its real upstream base on first run" { make_app no_base run env UPSTREAM_STATE_FILE="$STATE_FILE" bash "$TEST_DIR/app/scripts/sync-upstream.sh" [ "$status" -eq 0 ] [ "$(cat "$TEST_DIR/app/quickemu")" = "PATCH1 A1 A2 A3 A4" ] [ "$(cat "$TEST_DIR/app/quickget")" = "PG G1 G2 G3" ] [ "$(cat "$TEST_DIR/app/quickreport")" = "R1 R2" ] run cat "$STATE_FILE" [[ "$output" == *"FIRST_RUN=1"* ]] [[ "$output" == *"CHANGED=3"* ]] [[ "$output" == *"CONFLICTS=0"* ]] [[ "$output" == *"UPSTREAM_SHA=${UPSTREAM_SHA}"* ]] }