"""Real Git migration regression; no network, user settings or player data.""" import importlib.util import os from pathlib import Path import subprocess import tempfile import unittest from unittest.mock import patch SPEC = importlib.util.spec_from_file_location("airdef_build", Path(__file__).with_name("airdef.py")) airdef = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(airdef) def git(at, *args): return subprocess.check_output(["git", "-C", str(at), *args], text=True, stderr=subprocess.STDOUT).strip() def init(at): at.mkdir() git(at, "init", "-q") git(at, "config", "user.name", "Test") git(at, "config", "user.email", "test@example.invalid") def commit(at, text): (at / "engine.txt").write_text(text) git(at, "add", "engine.txt") git(at, "commit", "-qm", text) return git(at, "rev-parse", "HEAD") class ForkMigrationTests(unittest.TestCase): def test_stale_local_url_is_synced_before_fetching_new_pin(self): with tempfile.TemporaryDirectory(prefix="airdef-fork-test-") as tmp, patch.dict(os.environ, {"GIT_ALLOW_PROTOCOL": "file"}): root = Path(tmp) source, parent = root / "source", root / "airdef" init(source) old = commit(source, "old") canonical = root / "canonical.git" git(root, "clone", "--bare", str(source), str(canonical)) init(parent) git(parent, "submodule", "add", str(canonical), "horizon") child = parent / "horizon" new = commit(source, "new-fork-only-pin") git(source, "push", str(canonical), "HEAD") git(parent, "update-index", "--cacheinfo", f"160000,{new},horizon") stale = str(root / "missing-upstream.git") git(parent, "config", "submodule.horizon.url", stale) git(child, "remote", "set-url", "origin", stale) self.assertEqual(git(child, "rev-parse", "HEAD"), old) with patch.object(airdef, "ROOT", parent), patch.object(airdef, "FORK", child): airdef.fork() self.assertEqual(git(child, "rev-parse", "HEAD"), new) self.assertEqual(git(child, "remote", "get-url", "origin"), str(canonical)) # The fast path must also repair stale configuration for the next update. git(child, "remote", "set-url", "origin", stale) airdef.fork() self.assertEqual(git(child, "remote", "get-url", "origin"), str(canonical)) if __name__ == "__main__": unittest.main()