base.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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,
  26. capture_output=True,
  27. )
  28. return
  29. # Build repo from plain fixture files with 3 commits
  30. workdir.mkdir(parents=True, exist_ok=True)
  31. _git(["git", "init", "-b", "main"], cwd=workdir)
  32. _git(["git", "config", "user.email", "drill@test.local"], cwd=workdir)
  33. _git(["git", "config", "user.name", "Drill Test"], cwd=workdir)
  34. # Commit 1: package.json + README.md
  35. for name in ("package.json", "README.md"):
  36. src = template_dir / name
  37. if src.exists():
  38. shutil.copy2(src, workdir / name)
  39. _git(["git", "add", "package.json", "README.md"], cwd=workdir)
  40. _git(["git", "commit", "-m", "initial commit"], cwd=workdir)
  41. # Commit 2: src/utils.js
  42. src_dir = workdir / "src"
  43. src_dir.mkdir(exist_ok=True)
  44. utils_src = template_dir / "src" / "utils.js"
  45. if utils_src.exists():
  46. shutil.copy2(utils_src, src_dir / "utils.js")
  47. _git(["git", "add", "src/utils.js"], cwd=workdir)
  48. _git(["git", "commit", "-m", "add utils module"], cwd=workdir)
  49. # Commit 3: src/index.js
  50. index_src = template_dir / "src" / "index.js"
  51. if index_src.exists():
  52. shutil.copy2(index_src, src_dir / "index.js")
  53. _git(["git", "add", "src/index.js"], cwd=workdir)
  54. _git(["git", "commit", "-m", "add entry point"], cwd=workdir)