101 lines
5.2 KiB
Python
101 lines
5.2 KiB
Python
"""Exercise the contributor branch handoff with disposable Git repositories."""
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
|
|
def git(repo, *args):
|
|
return subprocess.check_output(["git", "-C", str(repo), *args], text=True).strip()
|
|
|
|
|
|
class ContributorWorkflowTests(unittest.TestCase):
|
|
def test_two_consecutive_tasks_preserve_unmerged_first_task(self):
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
base = Path(temp) / "project"
|
|
base.mkdir()
|
|
git(base, "init", "-q", "-b", "codex")
|
|
git(base, "config", "user.name", "Fixture")
|
|
git(base, "config", "user.email", "fixture@example.org")
|
|
(base / "AGENTS.md").write_text("Dedicated task branches; Captain approves merges.\n")
|
|
git(base, "add", "AGENTS.md")
|
|
git(base, "commit", "-qm", "base")
|
|
root_commit = git(base, "rev-parse", "HEAD")
|
|
|
|
first = Path(temp) / "task-001"
|
|
git(base, "worktree", "add", "-qb", "task-001", str(first), "codex")
|
|
(first / "feature-a.txt").write_text("task A\n")
|
|
(first / "notes.txt").write_text("safe untracked note\n")
|
|
self.assertIn("?? notes.txt", git(first, "status", "--short"))
|
|
git(first, "add", "feature-a.txt")
|
|
git(first, "commit", "-qm", "task A ready for review")
|
|
first_commit = git(first, "rev-parse", "HEAD")
|
|
self.assertIn("?? notes.txt", git(first, "status", "--short"))
|
|
# A local marker exercises the ordering gate; it is not an actual PR.
|
|
review_reference = f"local-review:{first_commit[:8]}"
|
|
self.assertTrue(review_reference.startswith("local-review:"))
|
|
|
|
second = Path(temp) / "task-002"
|
|
git(base, "worktree", "add", "-qb", "task-002", str(second), "codex")
|
|
(second / "feature-b.txt").write_text("task B\n")
|
|
git(second, "add", "feature-b.txt")
|
|
git(second, "commit", "-qm", "task B candidate")
|
|
self.assertEqual(git(base, "rev-parse", "HEAD"), root_commit)
|
|
self.assertNotEqual(git(second, "rev-parse", "HEAD"), first_commit)
|
|
self.assertFalse((second / "feature-a.txt").exists())
|
|
self.assertTrue((first / "notes.txt").exists())
|
|
self.assertIn(str(first), git(base, "worktree", "list"))
|
|
self.assertIn(str(second), git(base, "worktree", "list"))
|
|
|
|
def test_failed_push_preserves_committed_task_and_worktree(self):
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
base = Path(temp) / "project with spaces"
|
|
base.mkdir()
|
|
git(base, "init", "-q", "-b", "codex")
|
|
git(base, "config", "user.name", "Fixture")
|
|
git(base, "config", "user.email", "fixture@example.org")
|
|
(base / "base.txt").write_text("base\n")
|
|
git(base, "add", "base.txt")
|
|
git(base, "commit", "-qm", "base")
|
|
task = Path(temp) / "task with spaces"
|
|
git(base, "worktree", "add", "-qb", "task-a", str(task), "codex")
|
|
(task / "feature.txt").write_text("ready\n")
|
|
git(task, "add", "feature.txt")
|
|
git(task, "commit", "-qm", "task implementation")
|
|
commit = git(task, "rev-parse", "HEAD")
|
|
git(task, "remote", "add", "origin", str(Path(temp) / "missing-remote"))
|
|
push = subprocess.run(["git", "-C", str(task), "push", "origin", "task-a"], capture_output=True, text=True)
|
|
self.assertNotEqual(push.returncode, 0)
|
|
self.assertEqual(git(task, "rev-parse", "HEAD"), commit)
|
|
self.assertTrue((task / "feature.txt").exists())
|
|
self.assertIn(str(task), git(base, "worktree", "list"))
|
|
|
|
def test_failed_combined_check_does_not_close_task(self):
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
base = Path(temp) / "project"
|
|
base.mkdir()
|
|
git(base, "init", "-q", "-b", "codex")
|
|
git(base, "config", "user.name", "Fixture")
|
|
git(base, "config", "user.email", "fixture@example.org")
|
|
(base / "shared.txt").write_text("base\n")
|
|
(base / "TASKS.md").write_text("Task A: Ready for Review / Integration\n")
|
|
git(base, "add", "shared.txt", "TASKS.md")
|
|
git(base, "commit", "-qm", "base")
|
|
task = Path(temp) / "task-a"
|
|
git(base, "worktree", "add", "-qb", "task-a", str(task), "codex")
|
|
(task / "feature.txt").write_text("passes isolated check\n")
|
|
git(task, "add", "feature.txt")
|
|
git(task, "commit", "-qm", "task implementation")
|
|
self.assertTrue((task / "feature.txt").exists())
|
|
git(base, "merge", "--no-ff", "-qm", "approved candidate", "task-a")
|
|
combined = subprocess.run(["python3", "-c", "import sys; sys.exit(1)"], cwd=base)
|
|
self.assertNotEqual(combined.returncode, 0)
|
|
self.assertIn("Ready for Review / Integration", (base / "TASKS.md").read_text())
|
|
self.assertNotIn("Integrated\n", (base / "TASKS.md").read_text())
|
|
self.assertIn(str(task), git(base, "worktree", "list"))
|
|
self.assertEqual(git(task, "rev-parse", "HEAD"), git(base, "rev-parse", "task-a"))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|