test_repo.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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(
  51. repo, "_get_repo_remote_url", lambda _target_path: "git@github.com:ChristianLempa/boilerplates-library.git"
  52. )
  53. monkeypatch.setattr(
  54. repo,
  55. "_pull_repo_updates",
  56. lambda _name, _target_path, _branch: (
  57. False,
  58. "Pull failed: fatal: Not possible to fast-forward, aborting.",
  59. ),
  60. )
  61. def fake_replace_repo_checkout(
  62. name: str,
  63. url: str,
  64. target_path: Path,
  65. branch: str | None,
  66. sparse_dir: str | None,
  67. *,
  68. reason: str,
  69. ) -> tuple[bool, str]:
  70. clone_calls.append(reason)
  71. return True, "recloned after divergence"
  72. monkeypatch.setattr(repo, "_replace_repo_checkout", fake_replace_repo_checkout)
  73. success, message = repo._clone_or_pull_repo(
  74. "default",
  75. "https://github.com/christianlempa/boilerplates-library.git",
  76. target_path,
  77. branch="main",
  78. sparse_dir=".",
  79. )
  80. assert success is True
  81. assert message == "recloned after divergence"
  82. assert clone_calls == ["managed checkout diverged from origin"]