From 44aa534a4912c5b83f778ac5a5b3579074bbe498 Mon Sep 17 00:00:00 2001 From: joydeep049 Date: Mon, 30 Dec 2024 22:52:49 +0530 Subject: [PATCH 1/4] test: added unit tests for clone.py --- src/gitingest/tests/test_clone.py | 95 +++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/src/gitingest/tests/test_clone.py b/src/gitingest/tests/test_clone.py index e3b8128..4135eb7 100644 --- a/src/gitingest/tests/test_clone.py +++ b/src/gitingest/tests/test_clone.py @@ -74,3 +74,98 @@ async def test_check_repo_exists() -> None: # Test failed request mock_process.returncode = 1 assert await _check_repo_exists(url) is False + + +@pytest.mark.asyncio +async def test_clone_repo_invalid_url() -> None: + clone_config = CloneConfig( + url="", + local_path="/tmp/repo", + ) + with pytest.raises(ValueError, match="The 'url' parameter is required."): + await clone_repo(clone_config) + + +@pytest.mark.asyncio +async def test_clone_repo_invalid_local_path() -> None: + clone_config = CloneConfig( + url="https://github.com/user/repo", + local_path="", + ) + with pytest.raises(ValueError, match="The 'local_path' parameter is required."): + await clone_repo(clone_config) + + +@pytest.mark.asyncio +async def test_clone_repo_with_custom_branch() -> None: + clone_config = CloneConfig( + url="https://github.com/user/repo", + local_path="/tmp/repo", + branch="feature-branch", + ) + with patch("gitingest.clone._check_repo_exists", return_value=True): + with patch("gitingest.clone._run_git_command", new_callable=AsyncMock) as mock_exec: + await clone_repo(clone_config) + mock_exec.assert_called_once_with( + "git", + "clone", + "--depth=1", + "--single-branch", + "--branch", + "feature-branch", + clone_config.url, + clone_config.local_path, + ) + + +@pytest.mark.asyncio +async def test_git_command_failure() -> None: + clone_config = CloneConfig( + url="https://github.com/user/repo", + local_path="/tmp/repo", + ) + with patch("gitingest.clone._check_repo_exists", return_value=True): + with patch("gitingest.clone._run_git_command", side_effect=RuntimeError("Git command failed")): + with pytest.raises(RuntimeError, match="Git command failed"): + await clone_repo(clone_config) + + +@pytest.mark.asyncio +async def test_clone_repo_default_shallow_clone() -> None: + clone_config = CloneConfig( + url="https://github.com/user/repo", + local_path="/tmp/repo", + ) + with patch("gitingest.clone._check_repo_exists", return_value=True): + with patch("gitingest.clone._run_git_command", new_callable=AsyncMock) as mock_exec: + await clone_repo(clone_config) + mock_exec.assert_called_once_with( + "git", "clone", "--depth=1", "--single-branch", clone_config.url, clone_config.local_path + ) + + +@pytest.mark.asyncio +async def test_clone_repo_commit_without_branch() -> None: + clone_config = CloneConfig( + url="https://github.com/user/repo", + local_path="/tmp/repo", + commit="a" * 40, # Simulating a valid commit hash + ) + with patch("gitingest.clone._check_repo_exists", return_value=True): + with patch("gitingest.clone._run_git_command", new_callable=AsyncMock) as mock_exec: + await clone_repo(clone_config) + assert mock_exec.call_count == 2 # Clone and checkout calls + mock_exec.assert_any_call("git", "clone", "--single-branch", clone_config.url, clone_config.local_path) + mock_exec.assert_any_call("git", "-C", clone_config.local_path, "checkout", clone_config.commit) + + +@pytest.mark.asyncio +async def test_check_repo_exists_with_redirect() -> None: + url = "https://github.com/user/repo" + with patch("asyncio.create_subprocess_exec", new_callable=AsyncMock) as mock_exec: + mock_process = AsyncMock() + mock_process.communicate.return_value = (b"HTTP/1.1 302 Found\n", b"") + mock_process.returncode = 0 # Simulate successful request + mock_exec.return_value = mock_process + + assert await _check_repo_exists(url) is True From 2cf546829eca4c643b755b4a72aa4b2d5b666df2 Mon Sep 17 00:00:00 2001 From: Romain Courtois Date: Tue, 31 Dec 2024 07:33:18 +0100 Subject: [PATCH 2/4] Update src/gitingest/tests/test_clone.py Co-authored-by: Filip Christiansen <22807962+filipchristiansen@users.noreply.github.com> --- src/gitingest/tests/test_clone.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gitingest/tests/test_clone.py b/src/gitingest/tests/test_clone.py index 4135eb7..ab2c70e 100644 --- a/src/gitingest/tests/test_clone.py +++ b/src/gitingest/tests/test_clone.py @@ -73,7 +73,7 @@ async def test_check_repo_exists() -> None: # Test failed request mock_process.returncode = 1 - assert await _check_repo_exists(url) is False + assert await not _check_repo_exists(url) @pytest.mark.asyncio From 6102c2360f1d091124149e5873eee8230b9c3539 Mon Sep 17 00:00:00 2001 From: Romain Courtois Date: Tue, 31 Dec 2024 07:33:23 +0100 Subject: [PATCH 3/4] Update src/gitingest/tests/test_clone.py Co-authored-by: Filip Christiansen <22807962+filipchristiansen@users.noreply.github.com> --- src/gitingest/tests/test_clone.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gitingest/tests/test_clone.py b/src/gitingest/tests/test_clone.py index ab2c70e..1353b75 100644 --- a/src/gitingest/tests/test_clone.py +++ b/src/gitingest/tests/test_clone.py @@ -168,4 +168,4 @@ async def test_check_repo_exists_with_redirect() -> None: mock_process.returncode = 0 # Simulate successful request mock_exec.return_value = mock_process - assert await _check_repo_exists(url) is True + assert await _check_repo_exists(url) From 09d9baa1d0d3468398a6f73331b163df14c3d89e Mon Sep 17 00:00:00 2001 From: Romain Courtois Date: Tue, 31 Dec 2024 07:38:04 +0100 Subject: [PATCH 4/4] Update src/gitingest/tests/test_clone.py --- src/gitingest/tests/test_clone.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gitingest/tests/test_clone.py b/src/gitingest/tests/test_clone.py index 1353b75..c124730 100644 --- a/src/gitingest/tests/test_clone.py +++ b/src/gitingest/tests/test_clone.py @@ -73,7 +73,7 @@ async def test_check_repo_exists() -> None: # Test failed request mock_process.returncode = 1 - assert await not _check_repo_exists(url) + assert await _check_repo_exists(url) is False @pytest.mark.asyncio