11""" Tests for the query_parser module. """
22
33from pathlib import Path
4+ from unittest .mock import AsyncMock , patch
45
56import pytest
6- from unittest .mock import patch , AsyncMock
7- from gitingest .repository_clone import _check_repo_exists , fetch_remote_branch_list
87
98from gitingest .ignore_patterns import DEFAULT_IGNORE_PATTERNS
109from gitingest .query_parser import _parse_patterns , _parse_repo_source , parse_query
@@ -98,15 +97,18 @@ async def test_parse_query_invalid_pattern() -> None:
9897 with pytest .raises (ValueError , match = "Pattern.*contains invalid characters" ):
9998 await parse_query (url , max_file_size = 50 , from_web = True , include_patterns = "*.py;rm -rf" )
10099
100+
101101async def test_parse_url_with_subpaths () -> None :
102102 """
103103 Test `_parse_repo_source` with a URL containing a branch and subpath.
104104 Verifies that user name, repository name, branch, and subpath are correctly extracted.
105105 """
106106 url = "https://github.com/user/repo/tree/main/subdir/file"
107- with patch (' gitingest.repository_clone._run_git_command' , new_callable = AsyncMock ) as mock_run_git_command :
107+ with patch (" gitingest.repository_clone._run_git_command" , new_callable = AsyncMock ) as mock_run_git_command :
108108 mock_run_git_command .return_value = (b"refs/heads/main\n refs/heads/dev\n refs/heads/feature-branch\n " , b"" )
109- with patch ('gitingest.repository_clone.fetch_remote_branch_list' , new_callable = AsyncMock ) as mock_fetch_branches :
109+ with patch (
110+ "gitingest.repository_clone.fetch_remote_branch_list" , new_callable = AsyncMock
111+ ) as mock_fetch_branches :
110112 mock_fetch_branches .return_value = ["main" , "dev" , "feature-branch" ]
111113 result = await _parse_repo_source (url )
112114 assert result ["user_name" ] == "user"
@@ -227,9 +229,11 @@ async def test_parse_url_branch_and_commit_distinction() -> None:
227229 url_branch = "https://github.com/user/repo/tree/main"
228230 url_commit = "https://github.com/user/repo/tree/abcd1234abcd1234abcd1234abcd1234abcd1234"
229231
230- with patch (' gitingest.repository_clone._run_git_command' , new_callable = AsyncMock ) as mock_run_git_command :
232+ with patch (" gitingest.repository_clone._run_git_command" , new_callable = AsyncMock ) as mock_run_git_command :
231233 mock_run_git_command .return_value = (b"refs/heads/main\n refs/heads/dev\n refs/heads/feature-branch\n " , b"" )
232- with patch ('gitingest.repository_clone.fetch_remote_branch_list' , new_callable = AsyncMock ) as mock_fetch_branches :
234+ with patch (
235+ "gitingest.repository_clone.fetch_remote_branch_list" , new_callable = AsyncMock
236+ ) as mock_fetch_branches :
233237 mock_fetch_branches .return_value = ["main" , "dev" , "feature-branch" ]
234238
235239 result_branch = await _parse_repo_source (url_branch )
@@ -240,6 +244,7 @@ async def test_parse_url_branch_and_commit_distinction() -> None:
240244 assert result_commit ["branch" ] is None
241245 assert result_commit ["commit" ] == "abcd1234abcd1234abcd1234abcd1234abcd1234"
242246
247+
243248async def test_parse_query_uuid_uniqueness () -> None :
244249 """
245250 Test `parse_query` to ensure that each call generates a unique UUID for the query result.
@@ -283,38 +288,52 @@ async def test_parse_query_with_branch() -> None:
283288 assert result ["commit" ] is None
284289 assert result ["type" ] == "blob"
285290
291+
286292@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- ])
293+ @pytest .mark .parametrize (
294+ "url, expected_branch, expected_subpath" ,
295+ [
296+ ("https://github.com/user/repo/tree/main/src" , "main" , "/src" ),
297+ ("https://github.com/user/repo/tree/fix1" , "fix1" , "/" ),
298+ ("https://github.com/user/repo/tree/nonexistent-branch/src" , "nonexistent-branch" , "/src" ),
299+ ],
300+ )
292301async def test_parse_repo_source_with_failed_git_command (url , expected_branch , expected_subpath ):
293302 """
294303 Test `_parse_repo_source` when git command fails.
295304 Verifies that the function returns the first path component as the branch.
296305 """
297- with patch (' gitingest.repository_clone.fetch_remote_branch_list' , new_callable = AsyncMock ) as mock_fetch_branches :
306+ with patch (" gitingest.repository_clone.fetch_remote_branch_list" , new_callable = AsyncMock ) as mock_fetch_branches :
298307 mock_fetch_branches .side_effect = Exception ("Failed to fetch branch list" )
299-
308+
300309 result = await _parse_repo_source (url )
301-
310+
302311 assert result ["branch" ] == expected_branch
303312 assert result ["subpath" ] == expected_subpath
304313
314+
305315@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- ])
316+ @pytest .mark .parametrize (
317+ "url, expected_branch, expected_subpath" ,
318+ [
319+ ("https://github.com/user/repo/tree/feature/fix1/src" , "feature/fix1" , "/src" ),
320+ ("https://github.com/user/repo/tree/main/src" , "main" , "/src" ),
321+ ("https://github.com/user/repo" , None , "/" ), # No
322+ ("https://github.com/user/repo/tree/nonexistent-branch/src" , None , "/" ), # Non-existent branch
323+ ("https://github.com/user/repo/tree/fix" , "fix" , "/" ),
324+ ("https://github.com/user/repo/blob/fix/page.html" , "fix" , "/page.html" ),
325+ ],
326+ )
313327async 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\n refs/heads/main\n refs/heads/feature-branch\n refs/heads/fix\n " , b"" )
328+ with (
329+ patch ("gitingest.repository_clone._run_git_command" , new_callable = AsyncMock ) as mock_run_git_command ,
330+ patch ("gitingest.repository_clone.fetch_remote_branch_list" , new_callable = AsyncMock ) as mock_fetch_branches ,
331+ ):
332+
333+ mock_run_git_command .return_value = (
334+ b"refs/heads/feature/fix1\n refs/heads/main\n refs/heads/feature-branch\n refs/heads/fix\n " ,
335+ b"" ,
336+ )
318337 mock_fetch_branches .return_value = ["feature/fix1" , "main" , "feature-branch" ]
319338
320339 result = await _parse_repo_source (url )
0 commit comments