base.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from __future__ import annotations
  2. import shutil
  3. import subprocess
  4. from pathlib import Path
  5. def _git(args: list[str], cwd: Path, **kwargs) -> subprocess.CompletedProcess:
  6. env = {
  7. "GIT_AUTHOR_NAME": "Drill Test",
  8. "GIT_AUTHOR_EMAIL": "drill@test.local",
  9. "GIT_COMMITTER_NAME": "Drill Test",
  10. "GIT_COMMITTER_EMAIL": "drill@test.local",
  11. **__import__("os").environ,
  12. }
  13. return subprocess.run(args, cwd=cwd, check=True, capture_output=True, env=env, **kwargs)
  14. def create_base_repo(workdir: Path, template_dir: Path) -> None:
  15. """Clone template_dir into workdir with full 3-commit history.
  16. If template_dir has a .git, clone it directly. Otherwise (plain
  17. fixture files), init a fresh repo and replay the canonical 3-commit
  18. history so tests always get a predictable git graph.
  19. """
  20. workdir = Path(workdir)
  21. template_dir = Path(template_dir)
  22. if (template_dir / ".git").exists():
  23. subprocess.run(
  24. ["git", "clone", str(template_dir), str(workdir)],
  25. check=True, capture_output=True,
  26. )
  27. return
  28. # Build repo from plain fixture files with 3 commits
  29. workdir.mkdir(parents=True, exist_ok=True)
  30. _git(["git", "init", "-b", "main"], cwd=workdir)
  31. _git(["git", "config", "user.email", "drill@test.local"], cwd=workdir)
  32. _git(["git", "config", "user.name", "Drill Test"], cwd=workdir)
  33. # Commit 1: package.json + README.md
  34. for name in ("package.json", "README.md"):
  35. src = template_dir / name
  36. if src.exists():
  37. shutil.copy2(src, workdir / name)
  38. _git(["git", "add", "package.json", "README.md"], cwd=workdir)
  39. _git(["git", "commit", "-m", "initial commit"], cwd=workdir)
  40. # Commit 2: src/utils.js
  41. src_dir = workdir / "src"
  42. src_dir.mkdir(exist_ok=True)
  43. utils_src = template_dir / "src" / "utils.js"
  44. if utils_src.exists():
  45. shutil.copy2(utils_src, src_dir / "utils.js")
  46. _git(["git", "add", "src/utils.js"], cwd=workdir)
  47. _git(["git", "commit", "-m", "add utils module"], cwd=workdir)
  48. # Commit 3: src/index.js
  49. index_src = template_dir / "src" / "index.js"
  50. if index_src.exists():
  51. shutil.copy2(index_src, src_dir / "index.js")
  52. _git(["git", "add", "src/index.js"], cwd=workdir)
  53. _git(["git", "commit", "-m", "add entry point"], cwd=workdir)