|
| 1 | +"""Tests for git_clone operation""" |
| 2 | + |
| 3 | +import shutil |
| 4 | +import tempfile |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from src.mcp_server_git.utils.git_import import Repo |
| 10 | + |
| 11 | + |
| 12 | +def _make_source_repo( |
| 13 | + path: Path, num_commits: int = 1, branch: str | None = None |
| 14 | +) -> Repo: |
| 15 | + """Helper: initialise a bare-ish source repo with commits for use as a local remote.""" |
| 16 | + repo = Repo.init(str(path)) |
| 17 | + |
| 18 | + # Configure a minimal identity so commits work without system config |
| 19 | + with repo.config_writer() as cw: |
| 20 | + cw.set_value("user", "name", "Test User") |
| 21 | + cw.set_value("user", "email", "test@example.com") |
| 22 | + |
| 23 | + # Write and commit files |
| 24 | + for i in range(num_commits): |
| 25 | + dummy = path / f"file_{i}.txt" |
| 26 | + dummy.write_text(f"content {i}") |
| 27 | + repo.index.add([str(dummy)]) |
| 28 | + repo.index.commit(f"commit {i}") |
| 29 | + |
| 30 | + if branch is not None: |
| 31 | + repo.create_head(branch) |
| 32 | + |
| 33 | + return repo |
| 34 | + |
| 35 | + |
| 36 | +class TestGitClone: |
| 37 | + """Test git_clone function""" |
| 38 | + |
| 39 | + def setup_method(self): |
| 40 | + self._tmpdirs: list[str] = [] |
| 41 | + |
| 42 | + def teardown_method(self): |
| 43 | + for d in self._tmpdirs: |
| 44 | + shutil.rmtree(d, ignore_errors=True) |
| 45 | + |
| 46 | + def _tmpdir(self) -> Path: |
| 47 | + d = tempfile.mkdtemp() |
| 48 | + self._tmpdirs.append(d) |
| 49 | + return Path(d) |
| 50 | + |
| 51 | + # ------------------------------------------------------------------ |
| 52 | + # Happy-path tests |
| 53 | + # ------------------------------------------------------------------ |
| 54 | + |
| 55 | + def test_clone_happy_path(self): |
| 56 | + """Clone a local source repo into a fresh empty target and verify success.""" |
| 57 | + from src.mcp_server_git.git.operations import git_clone |
| 58 | + |
| 59 | + src = self._tmpdir() |
| 60 | + src_repo = _make_source_repo(src) |
| 61 | + expected_sha = src_repo.head.commit.hexsha |
| 62 | + |
| 63 | + target = self._tmpdir() / "cloned" |
| 64 | + |
| 65 | + result = git_clone(str(src), str(target)) |
| 66 | + |
| 67 | + assert "✅" in result |
| 68 | + assert str(src) in result |
| 69 | + assert str(target) in result |
| 70 | + |
| 71 | + cloned_repo = Repo(str(target)) |
| 72 | + assert cloned_repo.head.commit.hexsha == expected_sha |
| 73 | + |
| 74 | + def test_clone_with_branch(self): |
| 75 | + """Clone with branch= and verify active branch in cloned repo.""" |
| 76 | + from src.mcp_server_git.git.operations import git_clone |
| 77 | + |
| 78 | + src = self._tmpdir() |
| 79 | + _make_source_repo(src, num_commits=1, branch="feature-x") |
| 80 | + |
| 81 | + target = self._tmpdir() / "cloned" |
| 82 | + |
| 83 | + result = git_clone(str(src), str(target), branch="feature-x") |
| 84 | + |
| 85 | + assert "✅" in result |
| 86 | + |
| 87 | + cloned_repo = Repo(str(target)) |
| 88 | + assert cloned_repo.active_branch.name == "feature-x" |
| 89 | + |
| 90 | + def test_clone_with_depth(self): |
| 91 | + """Clone with depth=1 from a 3-commit repo and verify shallow history.""" |
| 92 | + from src.mcp_server_git.git.operations import git_clone |
| 93 | + |
| 94 | + src = self._tmpdir() |
| 95 | + _make_source_repo(src, num_commits=3) |
| 96 | + |
| 97 | + target = self._tmpdir() / "cloned" |
| 98 | + |
| 99 | + result = git_clone(f"file://{src}", str(target), depth=1) |
| 100 | + |
| 101 | + assert "✅" in result |
| 102 | + |
| 103 | + cloned_repo = Repo(str(target)) |
| 104 | + commit_count = cloned_repo.git.rev_list("--count", "HEAD") |
| 105 | + assert commit_count.strip() == "1" |
| 106 | + |
| 107 | + def test_clone_single_branch_flag(self): |
| 108 | + """Clone with single_branch=True succeeds.""" |
| 109 | + from src.mcp_server_git.git.operations import git_clone |
| 110 | + |
| 111 | + src = self._tmpdir() |
| 112 | + _make_source_repo(src) |
| 113 | + |
| 114 | + target = self._tmpdir() / "cloned" |
| 115 | + |
| 116 | + result = git_clone(str(src), str(target), single_branch=True) |
| 117 | + |
| 118 | + assert "✅" in result |
| 119 | + |
| 120 | + # ------------------------------------------------------------------ |
| 121 | + # Validation / rejection tests |
| 122 | + # ------------------------------------------------------------------ |
| 123 | + |
| 124 | + def test_clone_rejects_existing_nonempty_target(self): |
| 125 | + """Raise ValueError when target directory already contains files.""" |
| 126 | + from src.mcp_server_git.git.operations import git_clone |
| 127 | + |
| 128 | + src = self._tmpdir() |
| 129 | + _make_source_repo(src) |
| 130 | + |
| 131 | + target = self._tmpdir() / "nonempty" |
| 132 | + target.mkdir() |
| 133 | + (target / "existing.txt").write_text("not empty") |
| 134 | + |
| 135 | + with pytest.raises(ValueError, match="not empty"): |
| 136 | + git_clone(str(src), str(target)) |
| 137 | + |
| 138 | + def test_clone_rejects_missing_parent(self): |
| 139 | + """Raise ValueError when the parent of target_path does not exist.""" |
| 140 | + from src.mcp_server_git.git.operations import git_clone |
| 141 | + |
| 142 | + src = self._tmpdir() |
| 143 | + _make_source_repo(src) |
| 144 | + |
| 145 | + nonexistent_parent = self._tmpdir() / "ghost" / "cloned" |
| 146 | + |
| 147 | + with pytest.raises(ValueError, match="Parent directory does not exist"): |
| 148 | + git_clone(str(src), str(nonexistent_parent)) |
| 149 | + |
| 150 | + # ------------------------------------------------------------------ |
| 151 | + # Error-path tests |
| 152 | + # ------------------------------------------------------------------ |
| 153 | + |
| 154 | + def test_clone_bad_url_returns_error_message(self): |
| 155 | + """Return a '❌' string when repo_url does not point to a valid repo.""" |
| 156 | + from src.mcp_server_git.git.operations import git_clone |
| 157 | + |
| 158 | + target = self._tmpdir() / "cloned" |
| 159 | + |
| 160 | + # A path that does not exist is not a valid git repo; GitPython raises |
| 161 | + # GitCommandError which the implementation catches and returns as "❌ Clone failed: ..." |
| 162 | + bad_url = "/tmp/this_path_does_not_exist_at_all_xyz123" |
| 163 | + |
| 164 | + result = git_clone(bad_url, str(target)) |
| 165 | + |
| 166 | + assert "❌" in result |
0 commit comments