From 4cb0e1cc1e7efe53b05d1e63d3fab44e8707420a Mon Sep 17 00:00:00 2001 From: Filip Christiansen <22807962+filipchristiansen@users.noreply.github.com> Date: Tue, 14 Jan 2025 00:33:17 +0100 Subject: [PATCH] test: add coverage for run_ingest_query and clone_repo async timeout --- tests/test_query_ingestion.py | 24 +++++++++++++++++++++++- tests/test_repository_clone.py | 17 +++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/tests/test_query_ingestion.py b/tests/test_query_ingestion.py index ad5a8b56..9d2b826d 100644 --- a/tests/test_query_ingestion.py +++ b/tests/test_query_ingestion.py @@ -4,7 +4,7 @@ from typing import Any from unittest.mock import patch -from gitingest.query_ingestion import _extract_files_content, _read_file_content, _scan_directory +from gitingest.query_ingestion import _extract_files_content, _read_file_content, _scan_directory, run_ingest_query def test_scan_directory(temp_directory: Path, sample_query: dict[str, Any]) -> None: @@ -153,6 +153,28 @@ def test_include_src_wildcard_prefix(temp_directory: Path, sample_query: dict[st assert file_paths == expected_paths, "Missing or unexpected files in result" +def test_run_ingest_query(temp_directory: Path, sample_query: dict[str, Any]) -> None: + """ + Test the run_ingest_query function to ensure it processes the directory correctly. + """ + sample_query["local_path"] = temp_directory + sample_query["subpath"] = "/" + sample_query["type"] = None + + summary, _, content = run_ingest_query(sample_query) + + assert "Repository: test_user/test_repo" in summary + assert "Files analyzed: 8" in summary + assert "src/subfile1.txt" in content + assert "src/subfile2.py" in content + assert "src/subdir/file_subdir.txt" in content + assert "src/subdir/file_subdir.py" in content + assert "file1.txt" in content + assert "file2.py" in content + assert "dir1/file_dir1.txt" in content + assert "dir2/file_dir2.txt" in content + + # multiple patterns # TODO: test with multiple include patterns: ['*.txt', '*.py'] # TODO: test with multiple include patterns: ['/src/*', '*.txt'] diff --git a/tests/test_repository_clone.py b/tests/test_repository_clone.py index 3bfa3b2f..9ff2736f 100644 --- a/tests/test_repository_clone.py +++ b/tests/test_repository_clone.py @@ -1,9 +1,11 @@ """ Tests for the repository_clone module. """ +import asyncio from unittest.mock import AsyncMock, patch import pytest +from gitingest.exceptions import AsyncTimeoutError from gitingest.repository_clone import CloneConfig, _check_repo_exists, clone_repo @@ -233,3 +235,18 @@ async def test_check_repo_exists_with_permanent_redirect() -> None: mock_exec.return_value = mock_process assert await _check_repo_exists(url) + + +@pytest.mark.asyncio +async def test_clone_repo_with_timeout() -> None: + """ + Test the `clone_repo` function when the cloning process exceeds the timeout limit. + Verifies that an AsyncTimeoutError is raised. + """ + clone_config = CloneConfig(url="https://github.com/user/repo", local_path="/tmp/repo") + + with patch("gitingest.repository_clone._check_repo_exists", return_value=True): + with patch("gitingest.repository_clone._run_git_command", new_callable=AsyncMock) as mock_exec: + mock_exec.side_effect = asyncio.TimeoutError + with pytest.raises(AsyncTimeoutError, match="Operation timed out after"): + await clone_repo(clone_config)