DistroHopper/tests/test-sync-upstream.bats
oSoWoSo-bot 1f27437bb6 test(sync-upstream): assert upstream changes land in root vendored files
Regression test proving scripts/sync-upstream.sh applies upstream changes to
the root quickemu/quickget/chunkcheck/quickreport files (the ones DistroHopper
actually runs) rather than only recording snapshots under upstream/. Fully
offline: a local fixture upstream is served via git insteadOf.

Co-authored-by: openhands <openhands@all-hands.dev>
2026-12-03 16:56:44 +01:00

106 lines
No EOL
4.9 KiB
Bash

#!/usr/bin/env bats
# scripts/sync-upstream.sh must apply upstream changes to the ROOT vendored
# files (quickemu, quickget, chunkcheck, quickreport) that DistroHopper
# actually runs - not merely record snapshots under upstream/.
#
# The test runs a COPY of the script inside a fixture repo so bash's
# BASH_SOURCE keeps the sync operating on the fixture, never on the real repo.
# Upstream traffic is diverted to a local fixture remote via git's insteadOf,
# so the test is fully offline.
setup() {
REPO_ROOT="$(cd "${BATS_TEST_DIRNAME}/.." && pwd)"
TEST_DIR="$(mktemp -d)"
STATE_FILE="$TEST_DIR/upstream-sync-state"
cd "$TEST_DIR"
# Fake upstream remote (what scripts/sync-upstream.sh fetches).
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' > quickget
printf 'C1\n' > chunkcheck
printf 'R1\n' > quickreport
printf 'Q1\n' > quickemu
git add -A
git commit --quiet -m "upstream baseline"
git remote add origin "$TEST_DIR/upstream.git"
git push --quiet origin master
# Snapshot the baseline content the DistroHopper checkout derives from.
mkdir -p "$TEST_DIR/base-snap"
cp quickget chunkcheck quickreport quickemu "$TEST_DIR/base-snap/"
# Upstream moves: quickget gains a new header and chunkcheck a new line.
printf 'MARKER\nA1\nA2\n' > quickget
printf 'C1\nC2\n' > chunkcheck
git add quickget chunkcheck
git commit --quiet -m "upstream change"
git push --quiet origin master
# DistroHopper checkout under test: root files carry the baseline plus
# a local patch on quickget (so the merge exercises the root files).
git init --quiet --initial-branch=all "$TEST_DIR/app"
cd "$TEST_DIR/app"
git config user.name Test
git config user.email test@example.com
mkdir -p upstream/baseline scripts
cp "$TEST_DIR/base-snap/quickget" upstream/baseline/quickget
cp "$TEST_DIR/base-snap/chunkcheck" upstream/baseline/chunkcheck
cp "$TEST_DIR/base-snap/quickreport" upstream/baseline/quickreport
cp "$TEST_DIR/base-snap/quickemu" upstream/baseline/quickemu
printf 'A1\nA2\nLOCAL-PATCH\n' > quickget # local patch on root file
cp "$TEST_DIR/base-snap/chunkcheck" chunkcheck
cp "$TEST_DIR/base-snap/quickreport" quickreport
cp "$TEST_DIR/base-snap/quickemu" quickemu
printf '{"repo":"quickemu-project/quickemu","branch":"master","sha":"old"}\n' > upstream/stamp.json
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 "distrohopper state"
# Divert the script's https fetch to the local fixture upstream.
git config url."file://$TEST_DIR/upstream.git".insteadOf https://github.com/quickemu-project/quickemu.git
}
teardown() {
rm -rf "$TEST_DIR"
}
@test "sync applies upstream changes to the root vendored files" {
run env UPSTREAM_STATE_FILE="$STATE_FILE" bash "$TEST_DIR/app/scripts/sync-upstream.sh"
[ "$status" -eq 0 ]
# Root quickget: upstream header merged cleanly on top of the local patch.
printf 'MARKER\nA1\nA2\nLOCAL-PATCH\n' > expected
[ "$(cat "$TEST_DIR/app/quickget")" = "$(cat expected)" ]
# Root chunkcheck: clean fast-forward applied to the root file.
[ "$(cat "$TEST_DIR/app/chunkcheck")" = "C1
C2" ]
# Root quickemu and quickreport: no upstream change, left untouched.
[ "$(cat "$TEST_DIR/app/quickemu")" = "Q1" ]
[ "$(cat "$TEST_DIR/app/quickreport")" = "R1" ]
# The root files are the ones modified in the working tree (staged by
# the workflow's stage_changes), not only the upstream snapshots.
run git -C "$TEST_DIR/app" status --porcelain
[[ "$output" == *" M quickget"* ]]
[[ "$output" == *" M chunkcheck"* ]]
}
@test "sync state file reports the root-applied change and pins the baseline" {
UPSTREAM_SHA="$(git --git-dir="$TEST_DIR/upstream.git" rev-parse master)"
run env UPSTREAM_STATE_FILE="$STATE_FILE" bash "$TEST_DIR/app/scripts/sync-upstream.sh"
[ "$status" -eq 0 ]
run cat "$STATE_FILE"
[ "$status" -eq 0 ]
[[ "$output" == *"UPSTREAM_SHA=${UPSTREAM_SHA}"* ]]
[[ "$output" == *"CHANGED=2"* ]] # quickget (3-way merge) + chunkcheck (fast-forward)
[[ "$output" == *"CONFLICTS=0"* ]]
# Baseline is re-pinned to the fetched upstream HEAD so the next run has
# a correct base, while the root file carries the applied change.
[ "$(cat "$TEST_DIR/app/upstream/baseline/quickget")" = "MARKER
A1
A2" ]
}