|
1 | 1 | """ Tests for the query_parser module. """ |
2 | 2 |
|
3 | 3 | from pathlib import Path |
| 4 | +from unittest.mock import AsyncMock, patch |
4 | 5 |
|
5 | 6 | import pytest |
6 | 7 |
|
@@ -109,11 +110,17 @@ async def test_parse_url_with_subpaths() -> None: |
109 | 110 | Verifies that user name, repository name, branch, and subpath are correctly extracted. |
110 | 111 | """ |
111 | 112 | url = "https://github.com/user/repo/tree/main/subdir/file" |
112 | | - result = await _parse_repo_source(url) |
113 | | - assert result["user_name"] == "user" |
114 | | - assert result["repo_name"] == "repo" |
115 | | - assert result["branch"] == "main" |
116 | | - assert result["subpath"] == "/subdir/file" |
| 113 | + with patch("gitingest.repository_clone._run_git_command", new_callable=AsyncMock) as mock_run_git_command: |
| 114 | + mock_run_git_command.return_value = (b"refs/heads/main\nrefs/heads/dev\nrefs/heads/feature-branch\n", b"") |
| 115 | + with patch( |
| 116 | + "gitingest.repository_clone.fetch_remote_branch_list", new_callable=AsyncMock |
| 117 | + ) as mock_fetch_branches: |
| 118 | + mock_fetch_branches.return_value = ["main", "dev", "feature-branch"] |
| 119 | + result = await _parse_repo_source(url) |
| 120 | + assert result["user_name"] == "user" |
| 121 | + assert result["repo_name"] == "repo" |
| 122 | + assert result["branch"] == "main" |
| 123 | + assert result["subpath"] == "/subdir/file" |
117 | 124 |
|
118 | 125 |
|
119 | 126 | async def test_parse_url_invalid_repo_structure() -> None: |
@@ -228,14 +235,20 @@ async def test_parse_url_branch_and_commit_distinction() -> None: |
228 | 235 | url_branch = "https://github.com/user/repo/tree/main" |
229 | 236 | url_commit = "https://github.com/user/repo/tree/abcd1234abcd1234abcd1234abcd1234abcd1234" |
230 | 237 |
|
231 | | - result_branch = await _parse_repo_source(url_branch) |
232 | | - result_commit = await _parse_repo_source(url_commit) |
| 238 | + with patch("gitingest.repository_clone._run_git_command", new_callable=AsyncMock) as mock_run_git_command: |
| 239 | + mock_run_git_command.return_value = (b"refs/heads/main\nrefs/heads/dev\nrefs/heads/feature-branch\n", b"") |
| 240 | + with patch( |
| 241 | + "gitingest.repository_clone.fetch_remote_branch_list", new_callable=AsyncMock |
| 242 | + ) as mock_fetch_branches: |
| 243 | + mock_fetch_branches.return_value = ["main", "dev", "feature-branch"] |
233 | 244 |
|
234 | | - assert result_branch["branch"] == "main" |
235 | | - assert result_branch["commit"] is None |
| 245 | + result_branch = await _parse_repo_source(url_branch) |
| 246 | + result_commit = await _parse_repo_source(url_commit) |
| 247 | + assert result_branch["branch"] == "main" |
| 248 | + assert result_branch["commit"] is None |
236 | 249 |
|
237 | | - assert result_commit["branch"] is None |
238 | | - assert result_commit["commit"] == "abcd1234abcd1234abcd1234abcd1234abcd1234" |
| 250 | + assert result_commit["branch"] is None |
| 251 | + assert result_commit["commit"] == "abcd1234abcd1234abcd1234abcd1234abcd1234" |
239 | 252 |
|
240 | 253 |
|
241 | 254 | async def test_parse_query_uuid_uniqueness() -> None: |
@@ -280,3 +293,55 @@ async def test_parse_query_with_branch() -> None: |
280 | 293 | assert result["branch"] == "2.2.x" |
281 | 294 | assert result["commit"] is None |
282 | 295 | assert result["type"] == "blob" |
| 296 | + |
| 297 | + |
| 298 | +@pytest.mark.asyncio |
| 299 | +@pytest.mark.parametrize( |
| 300 | + "url, expected_branch, expected_subpath", |
| 301 | + [ |
| 302 | + ("https://github.com/user/repo/tree/main/src", "main", "/src"), |
| 303 | + ("https://github.com/user/repo/tree/fix1", "fix1", "/"), |
| 304 | + ("https://github.com/user/repo/tree/nonexistent-branch/src", "nonexistent-branch", "/src"), |
| 305 | + ], |
| 306 | +) |
| 307 | +async def test_parse_repo_source_with_failed_git_command(url, expected_branch, expected_subpath): |
| 308 | + """ |
| 309 | + Test `_parse_repo_source` when git command fails. |
| 310 | + Verifies that the function returns the first path component as the branch. |
| 311 | + """ |
| 312 | + with patch("gitingest.repository_clone.fetch_remote_branch_list", new_callable=AsyncMock) as mock_fetch_branches: |
| 313 | + mock_fetch_branches.side_effect = Exception("Failed to fetch branch list") |
| 314 | + |
| 315 | + result = await _parse_repo_source(url) |
| 316 | + |
| 317 | + assert result["branch"] == expected_branch |
| 318 | + assert result["subpath"] == expected_subpath |
| 319 | + |
| 320 | + |
| 321 | +@pytest.mark.asyncio |
| 322 | +@pytest.mark.parametrize( |
| 323 | + "url, expected_branch, expected_subpath", |
| 324 | + [ |
| 325 | + ("https://github.com/user/repo/tree/feature/fix1/src", "feature/fix1", "/src"), |
| 326 | + ("https://github.com/user/repo/tree/main/src", "main", "/src"), |
| 327 | + ("https://github.com/user/repo", None, "/"), # No |
| 328 | + ("https://github.com/user/repo/tree/nonexistent-branch/src", None, "/"), # Non-existent branch |
| 329 | + ("https://github.com/user/repo/tree/fix", "fix", "/"), |
| 330 | + ("https://github.com/user/repo/blob/fix/page.html", "fix", "/page.html"), |
| 331 | + ], |
| 332 | +) |
| 333 | +async def test_parse_repo_source_with_various_url_patterns(url, expected_branch, expected_subpath): |
| 334 | + with ( |
| 335 | + patch("gitingest.repository_clone._run_git_command", new_callable=AsyncMock) as mock_run_git_command, |
| 336 | + patch("gitingest.repository_clone.fetch_remote_branch_list", new_callable=AsyncMock) as mock_fetch_branches, |
| 337 | + ): |
| 338 | + |
| 339 | + mock_run_git_command.return_value = ( |
| 340 | + b"refs/heads/feature/fix1\nrefs/heads/main\nrefs/heads/feature-branch\nrefs/heads/fix\n", |
| 341 | + b"", |
| 342 | + ) |
| 343 | + mock_fetch_branches.return_value = ["feature/fix1", "main", "feature-branch"] |
| 344 | + |
| 345 | + result = await _parse_repo_source(url) |
| 346 | + assert result["branch"] == expected_branch |
| 347 | + assert result["subpath"] == expected_subpath |
0 commit comments