|
3 | 3 | from pathlib import Path |
4 | 4 |
|
5 | 5 | import pytest |
| 6 | +from unittest.mock import patch, AsyncMock |
| 7 | +from gitingest.repository_clone import _check_repo_exists, fetch_remote_branch_list |
6 | 8 |
|
7 | 9 | from gitingest.ignore_patterns import DEFAULT_IGNORE_PATTERNS |
8 | 10 | from gitingest.query_parser import _parse_patterns, _parse_repo_source, parse_query |
@@ -96,18 +98,21 @@ async def test_parse_query_invalid_pattern() -> None: |
96 | 98 | with pytest.raises(ValueError, match="Pattern.*contains invalid characters"): |
97 | 99 | await parse_query(url, max_file_size=50, from_web=True, include_patterns="*.py;rm -rf") |
98 | 100 |
|
99 | | - |
100 | 101 | async def test_parse_url_with_subpaths() -> None: |
101 | 102 | """ |
102 | 103 | Test `_parse_repo_source` with a URL containing a branch and subpath. |
103 | 104 | Verifies that user name, repository name, branch, and subpath are correctly extracted. |
104 | 105 | """ |
105 | 106 | url = "https://github.com/user/repo/tree/main/subdir/file" |
106 | | - result = await _parse_repo_source(url) |
107 | | - assert result["user_name"] == "user" |
108 | | - assert result["repo_name"] == "repo" |
109 | | - assert result["branch"] == "main" |
110 | | - assert result["subpath"] == "/subdir/file" |
| 107 | + with patch('gitingest.repository_clone._run_git_command', new_callable=AsyncMock) as mock_run_git_command: |
| 108 | + mock_run_git_command.return_value = (b"refs/heads/main\nrefs/heads/dev\nrefs/heads/feature-branch\n", b"") |
| 109 | + with patch('gitingest.repository_clone.fetch_remote_branch_list', new_callable=AsyncMock) as mock_fetch_branches: |
| 110 | + mock_fetch_branches.return_value = ["main", "dev", "feature-branch"] |
| 111 | + result = await _parse_repo_source(url) |
| 112 | + assert result["user_name"] == "user" |
| 113 | + assert result["repo_name"] == "repo" |
| 114 | + assert result["branch"] == "main" |
| 115 | + assert result["subpath"] == "/subdir/file" |
111 | 116 |
|
112 | 117 |
|
113 | 118 | async def test_parse_url_invalid_repo_structure() -> None: |
@@ -222,15 +227,18 @@ async def test_parse_url_branch_and_commit_distinction() -> None: |
222 | 227 | url_branch = "https://github.com/user/repo/tree/main" |
223 | 228 | url_commit = "https://github.com/user/repo/tree/abcd1234abcd1234abcd1234abcd1234abcd1234" |
224 | 229 |
|
225 | | - result_branch = await _parse_repo_source(url_branch) |
226 | | - result_commit = await _parse_repo_source(url_commit) |
227 | | - |
228 | | - assert result_branch["branch"] == "main" |
229 | | - assert result_branch["commit"] is None |
| 230 | + with patch('gitingest.repository_clone._run_git_command', new_callable=AsyncMock) as mock_run_git_command: |
| 231 | + mock_run_git_command.return_value = (b"refs/heads/main\nrefs/heads/dev\nrefs/heads/feature-branch\n", b"") |
| 232 | + with patch('gitingest.repository_clone.fetch_remote_branch_list', new_callable=AsyncMock) as mock_fetch_branches: |
| 233 | + mock_fetch_branches.return_value = ["main", "dev", "feature-branch"] |
230 | 234 |
|
231 | | - assert result_commit["branch"] is None |
232 | | - assert result_commit["commit"] == "abcd1234abcd1234abcd1234abcd1234abcd1234" |
| 235 | + result_branch = await _parse_repo_source(url_branch) |
| 236 | + result_commit = await _parse_repo_source(url_commit) |
| 237 | + assert result_branch["branch"] == "main" |
| 238 | + assert result_branch["commit"] is None |
233 | 239 |
|
| 240 | + assert result_commit["branch"] is None |
| 241 | + assert result_commit["commit"] == "abcd1234abcd1234abcd1234abcd1234abcd1234" |
234 | 242 |
|
235 | 243 | async def test_parse_query_uuid_uniqueness() -> None: |
236 | 244 | """ |
@@ -274,3 +282,41 @@ async def test_parse_query_with_branch() -> None: |
274 | 282 | assert result["branch"] == "2.2.x" |
275 | 283 | assert result["commit"] is None |
276 | 284 | assert result["type"] == "blob" |
| 285 | + |
| 286 | +@pytest.mark.asyncio |
| 287 | +@pytest.mark.parametrize("url, expected_branch, expected_subpath", [ |
| 288 | + ("https://github.com/user/repo/tree/main/src", "main", "/src"), |
| 289 | + ("https://github.com/user/repo/tree/fix1", "fix1", "/"), |
| 290 | + ("https://github.com/user/repo/tree/nonexistent-branch/src", "nonexistent-branch", "/src"), |
| 291 | +]) |
| 292 | +async def test_parse_repo_source_with_failed_git_command(url, expected_branch, expected_subpath): |
| 293 | + """ |
| 294 | + Test `_parse_repo_source` when git command fails. |
| 295 | + Verifies that the function returns the first path component as the branch. |
| 296 | + """ |
| 297 | + with patch('gitingest.repository_clone.fetch_remote_branch_list', new_callable=AsyncMock) as mock_fetch_branches: |
| 298 | + mock_fetch_branches.side_effect = Exception("Failed to fetch branch list") |
| 299 | + |
| 300 | + result = await _parse_repo_source(url) |
| 301 | + |
| 302 | + assert result["branch"] == expected_branch |
| 303 | + assert result["subpath"] == expected_subpath |
| 304 | + |
| 305 | +@pytest.mark.asyncio |
| 306 | +@pytest.mark.parametrize("url, expected_branch, expected_subpath", [ |
| 307 | + ("https://github.com/user/repo/tree/feature/fix1/src", "feature/fix1", "/src"), |
| 308 | + ("https://github.com/user/repo/tree/main/src", "main", "/src"), |
| 309 | + ("https://github.com/user/repo", None, "/"), # No |
| 310 | + ("https://github.com/user/repo/tree/nonexistent-branch/src", None, "/"), # Non-existent branch |
| 311 | + ("https://github.com/user/repo/tree/fix", "fix", "/"), |
| 312 | +]) |
| 313 | +async def test_parse_repo_source_with_various_url_patterns(url, expected_branch, expected_subpath): |
| 314 | + with patch('gitingest.repository_clone._run_git_command', new_callable=AsyncMock) as mock_run_git_command, \ |
| 315 | + patch('gitingest.repository_clone.fetch_remote_branch_list', new_callable=AsyncMock) as mock_fetch_branches: |
| 316 | + |
| 317 | + mock_run_git_command.return_value = (b"refs/heads/feature/fix1\nrefs/heads/main\nrefs/heads/feature-branch\nrefs/heads/fix\n", b"") |
| 318 | + mock_fetch_branches.return_value = ["feature/fix1", "main", "feature-branch"] |
| 319 | + |
| 320 | + result = await _parse_repo_source(url) |
| 321 | + assert result["branch"] == expected_branch |
| 322 | + assert result["subpath"] == expected_subpath |
0 commit comments