|
9 | 9 | from pathlib import Path
|
10 | 10 | from unittest.mock import AsyncMock
|
11 | 11 |
|
| 12 | +import httpx |
12 | 13 | import pytest
|
13 | 14 | from pytest_mock import MockerFixture
|
| 15 | +from starlette.status import HTTP_200_OK, HTTP_401_UNAUTHORIZED, HTTP_403_FORBIDDEN, HTTP_404_NOT_FOUND |
14 | 16 |
|
15 | 17 | from gitingest.clone import clone_repo
|
16 | 18 | from gitingest.schemas import CloneConfig
|
@@ -87,35 +89,24 @@ async def test_clone_nonexistent_repository(repo_exists_true: AsyncMock) -> None
|
87 | 89 |
|
88 | 90 | @pytest.mark.asyncio
|
89 | 91 | @pytest.mark.parametrize(
|
90 | | - ("mock_stdout", "return_code", "expected"), |
| 92 | + ("status_code", "expected"), |
91 | 93 | [
|
92 | | - (b"200\n", 0, True), # Existing repo |
93 | | - (b"404\n", 0, False), # Non-existing repo |
94 | | - (b"200\n", 1, False), # Failed request |
| 94 | + (HTTP_200_OK, True), |
| 95 | + (HTTP_401_UNAUTHORIZED, False), |
| 96 | + (HTTP_403_FORBIDDEN, False), |
| 97 | + (HTTP_404_NOT_FOUND, False), |
95 | 98 | ],
|
96 | 99 | )
|
97 | | -async def test_check_repo_exists( |
98 | | - mock_stdout: bytes, |
99 | | - *, |
100 | | - return_code: int, |
101 | | - expected: bool, |
102 | | - mocker: MockerFixture, |
103 | | -) -> None: |
104 | | - """Test the ``check_repo_exists`` function with different Git HTTP responses. |
105 | | - |
106 | | - Given various stdout lines and return codes: |
107 | | - When ``check_repo_exists`` is called, |
108 | | - Then it should correctly indicate whether the repository exists. |
109 | | - """ |
110 | | - mock_exec = mocker.patch("asyncio.create_subprocess_exec", new_callable=AsyncMock) |
111 | | - mock_process = AsyncMock() |
112 | | - mock_process.communicate.return_value = (mock_stdout, b"") |
113 | | - mock_process.returncode = return_code |
114 | | - mock_exec.return_value = mock_process |
| 100 | +async def test_check_repo_exists(status_code: int, *, expected: bool, mocker: MockerFixture) -> None: |
| 101 | + """Verify that ``check_repo_exists`` interprets httpx results correctly.""" |
| 102 | + mock_client = AsyncMock() |
| 103 | + mock_client.__aenter__.return_value = mock_client # context-manager protocol |
| 104 | + mock_client.head.return_value = httpx.Response(status_code=status_code) |
| 105 | + mocker.patch("httpx.AsyncClient", return_value=mock_client) |
115 | 106 |
|
116 | | - repo_exists = await check_repo_exists(DEMO_URL) |
| 107 | + result = await check_repo_exists(DEMO_URL) |
117 | 108 |
|
118 | | - assert repo_exists is expected |
| 109 | + assert result is expected |
119 | 110 |
|
120 | 111 |
|
121 | 112 | @pytest.mark.asyncio
|
@@ -218,25 +209,6 @@ async def test_check_repo_exists_with_redirect(mocker: MockerFixture) -> None:
|
218 | 209 | assert repo_exists is False
|
219 | 210 |
|
220 | 211 |
|
221 | | -@pytest.mark.asyncio |
222 | | -async def test_check_repo_exists_with_permanent_redirect(mocker: MockerFixture) -> None: |
223 | | - """Test ``check_repo_exists`` when a permanent redirect (301) is returned. |
224 | | - |
225 | | - Given a URL that responds with "301 Found": |
226 | | - When ``check_repo_exists`` is called, |
227 | | - Then it should return ``True``, indicating the repo may exist at the new location. |
228 | | - """ |
229 | | - mock_exec = mocker.patch("asyncio.create_subprocess_exec", new_callable=AsyncMock) |
230 | | - mock_process = AsyncMock() |
231 | | - mock_process.communicate.return_value = (b"301\n", b"") |
232 | | - mock_process.returncode = 0 # Simulate successful request |
233 | | - mock_exec.return_value = mock_process |
234 | | - |
235 | | - repo_exists = await check_repo_exists(DEMO_URL) |
236 | | - |
237 | | - assert repo_exists |
238 | | - |
239 | | - |
240 | 212 | @pytest.mark.asyncio
|
241 | 213 | async def test_clone_with_timeout(run_command_mock: AsyncMock) -> None:
|
242 | 214 | """Test cloning a repository when a timeout occurs.
|
|
0 commit comments