78 lines
4.1 KiB
Python
78 lines
4.1 KiB
Python
import json
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
SOURCE = Path(__file__).resolve().parents[1]
|
|
INSTALLER = SOURCE / "scripts/install-testtask.py"
|
|
|
|
|
|
def run(*args, cwd=None):
|
|
return subprocess.run(args, cwd=cwd, capture_output=True, text=True)
|
|
|
|
|
|
class PortableTesttaskTests(unittest.TestCase):
|
|
def test_existing_repository(self):
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
repo = Path(temp) / "pitchinvader-fixture"
|
|
repo.mkdir()
|
|
self.assertEqual(run("git", "init", "-q", str(repo)).returncode, 0)
|
|
(repo / "docs").mkdir()
|
|
(repo / "docs/TASKS.md").write_text("## PI-001 — Fixture\nBranch/worktree: `feature-pi-001`\n")
|
|
(repo / "scripts").mkdir()
|
|
(repo / "scripts/run-tests.py").write_text("from pathlib import Path\nimport sys\nPath('test-ran').write_text(Path.cwd().name)\nsys.exit(int(Path('test-exit').read_text()) if Path('test-exit').exists() else 0)\n")
|
|
run("git", "add", ".", cwd=repo)
|
|
run("git", "-c", "user.name=Test", "-c", "user.email=test@example.org", "commit", "-qm", "base", cwd=repo)
|
|
self.assertEqual(run("git", "branch", "codex", cwd=repo).returncode, 0)
|
|
tree = Path(temp) / "pitchinvader-codex"
|
|
self.assertEqual(run("git", "worktree", "add", "-q", str(tree), "codex", cwd=repo).returncode, 0)
|
|
config = tree / ".bortstack/testtask.json"
|
|
config.parent.mkdir()
|
|
config.write_text(json.dumps({"command": ["python3", "scripts/run-tests.py"]}))
|
|
install = run("python3", str(INSTALLER), str(repo))
|
|
self.assertEqual(install.returncode, 0, install.stderr)
|
|
script = str(repo / "scripts/testtask.sh")
|
|
result = run(script, "codex")
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
self.assertIn("branch: codex", result.stdout)
|
|
self.assertIn(str(tree), result.stdout)
|
|
self.assertEqual((tree / "test-ran").read_text(), tree.name)
|
|
(tree / "test-exit").write_text("7")
|
|
self.assertEqual(run("sh", script, "codex").returncode, 7)
|
|
self.assertEqual(run("sh", script, "unknown").returncode, 2)
|
|
self.assertEqual(run("sh", script, "PI-001").returncode, 2)
|
|
config.write_text("not json")
|
|
self.assertIn("invalid test configuration", run("sh", script, "codex").stderr)
|
|
config.unlink()
|
|
self.assertEqual(run("sh", script, "codex").returncode, 3)
|
|
self.assertEqual(run("python3", str(INSTALLER), str(repo)).returncode, 2)
|
|
self.assertEqual(run("python3", str(INSTALLER), str(repo), "--update").returncode, 0)
|
|
(repo / "scripts/testtask.sh").write_text("custom\n")
|
|
self.assertEqual(run("python3", str(INSTALLER), str(repo), "--update").returncode, 2)
|
|
|
|
def test_task_id_and_missing_worktree(self):
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
repo = Path(temp) / "repo"
|
|
repo.mkdir()
|
|
run("git", "init", "-q", str(repo))
|
|
(repo / "docs").mkdir()
|
|
(repo / "docs/TASKS.md").write_text("## PI-001 — Fixture\nBranch/worktree: `feature-pi-001`\n")
|
|
run("git", "add", ".", cwd=repo)
|
|
run("git", "-c", "user.name=Test", "-c", "user.email=test@example.org", "commit", "-qm", "base", cwd=repo)
|
|
run("git", "branch", "feature-pi-001", cwd=repo)
|
|
run("python3", str(INSTALLER), str(repo))
|
|
script = str(repo / "scripts/testtask.sh")
|
|
self.assertIn("no existing worktree", run("sh", script, "PI-001").stderr)
|
|
tree = Path(temp) / "task-tree"
|
|
run("git", "worktree", "add", "-q", str(tree), "feature-pi-001", cwd=repo)
|
|
(tree / ".bortstack").mkdir()
|
|
(tree / ".bortstack/testtask.json").write_text(json.dumps({"command": ["true"]}))
|
|
result = run("sh", script, "PI-001")
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
self.assertIn("task: PI-001", result.stdout)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|