test_repo.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """Tests for managed library repository sync behavior."""
  2. from __future__ import annotations
  3. from pathlib import Path
  4. from cli.core import repo
  5. def test_clone_or_pull_repo_replaces_checkout_when_origin_changes(monkeypatch, tmp_path: Path) -> None:
  6. """Existing managed checkouts should be replaced when config points at a new remote."""
  7. target_path = tmp_path / "default"
  8. (target_path / ".git").mkdir(parents=True)
  9. clone_calls: list[tuple[str, str, Path, str | None, str | None, str]] = []
  10. def fake_get_repo_remote_url(_target_path: Path) -> str:
  11. return "https://github.com/christianlempa/boilerplates.git"
  12. def fake_replace_repo_checkout(
  13. name: str,
  14. url: str,
  15. target_path: Path,
  16. branch: str | None,
  17. sparse_dir: str | None,
  18. *,
  19. reason: str,
  20. ) -> tuple[bool, str]:
  21. clone_calls.append((name, url, target_path, branch, sparse_dir, reason))
  22. return True, "recloned"
  23. monkeypatch.setattr(repo, "_get_repo_remote_url", fake_get_repo_remote_url)
  24. monkeypatch.setattr(repo, "_replace_repo_checkout", fake_replace_repo_checkout)
  25. success, message = repo._clone_or_pull_repo(
  26. "default",
  27. "https://github.com/christianlempa/boilerplates-library.git",
  28. target_path,
  29. branch="main",
  30. sparse_dir=".",
  31. )
  32. assert success is True
  33. assert message == "recloned"
  34. assert clone_calls == [
  35. (
  36. "default",
  37. "https://github.com/christianlempa/boilerplates-library.git",
  38. target_path,
  39. "main",
  40. ".",
  41. "configured remote changed from https://github.com/christianlempa/boilerplates.git to "
  42. "https://github.com/christianlempa/boilerplates-library.git",
  43. )
  44. ]
  45. def test_clone_or_pull_repo_replaces_checkout_after_diverged_pull(monkeypatch, tmp_path: Path) -> None:
  46. """Fast-forward failures should fall back to a fresh managed clone."""
  47. target_path = tmp_path / "default"
  48. (target_path / ".git").mkdir(parents=True)
  49. clone_calls: list[str] = []
  50. monkeypatch.setattr(repo, "_get_repo_remote_url", lambda _target_path: "git@github.com:ChristianLempa/boilerplates-library.git")
  51. monkeypatch.setattr(
  52. repo,
  53. "_pull_repo_updates",
  54. lambda _name, _target_path, _branch: (
  55. False,
  56. "Pull failed: fatal: Not possible to fast-forward, aborting.",
  57. ),
  58. )
  59. def fake_replace_repo_checkout(
  60. name: str,
  61. url: str,
  62. target_path: Path,
  63. branch: str | None,
  64. sparse_dir: str | None,
  65. *,
  66. reason: str,
  67. ) -> tuple[bool, str]:
  68. clone_calls.append(reason)
  69. return True, "recloned after divergence"
  70. monkeypatch.setattr(repo, "_replace_repo_checkout", fake_replace_repo_checkout)
  71. success, message = repo._clone_or_pull_repo(
  72. "default",
  73. "https://github.com/christianlempa/boilerplates-library.git",
  74. target_path,
  75. branch="main",
  76. sparse_dir=".",
  77. )
  78. assert success is True
  79. assert message == "recloned after divergence"
  80. assert clone_calls == ["managed checkout diverged from origin"]