Skip to content

Commit dceff33

Browse files
committed
Fix tmp file creation and add test
1 parent c4f4c4a commit dceff33

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

src/gitingest/config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
""" Configuration file for the project. """
22

3+
import tempfile
34
from pathlib import Path
45

56
MAX_FILE_SIZE = 10 * 1024 * 1024 # 10 MB
@@ -8,4 +9,5 @@
89
MAX_TOTAL_SIZE_BYTES = 500 * 1024 * 1024 # 500 MB
910

1011
OUTPUT_FILE_PATH = "digest.txt"
11-
TMP_BASE_PATH = Path("/tmp/gitingest")
12+
13+
TMP_BASE_PATH = Path(tempfile.gettempdir()) / "gitingest"

src/gitingest/repository_clone.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
""" This module contains functions for cloning a Git repository to a local path. """
22

33
import asyncio
4+
import os
45
from dataclasses import dataclass
6+
from pathlib import Path
57

68
from gitingest.utils import async_timeout
79

@@ -61,6 +63,8 @@ async def clone_repo(config: CloneConfig) -> tuple[bytes, bytes]:
6163
------
6264
ValueError
6365
If the 'url' or 'local_path' parameters are missing, or if the repository is not found.
66+
OSError
67+
If there is an error creating the parent directory structure.
6468
"""
6569
# Extract and validate query parameters
6670
url: str = config.url
@@ -74,6 +78,13 @@ async def clone_repo(config: CloneConfig) -> tuple[bytes, bytes]:
7478
if not local_path:
7579
raise ValueError("The 'local_path' parameter is required.")
7680

81+
# Create parent directory if it doesn't exist
82+
parent_dir = Path(local_path).parent
83+
try:
84+
os.makedirs(parent_dir, exist_ok=True)
85+
except OSError as e:
86+
raise OSError(f"Failed to create parent directory {parent_dir}: {e}") from e
87+
7788
# Check if the repository exists
7889
if not await _check_repo_exists(url):
7990
raise ValueError("Repository not found, make sure it is public")

tests/test_repository_clone.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import asyncio
99
import os
10+
from pathlib import Path
1011
from unittest.mock import AsyncMock, patch
1112

1213
import pytest
@@ -362,3 +363,36 @@ async def test_clone_branch_with_slashes(tmp_path):
362363
clone_config.url,
363364
clone_config.local_path,
364365
)
366+
367+
368+
@pytest.mark.asyncio
369+
async def test_clone_repo_creates_parent_directory(tmp_path: Path) -> None:
370+
"""
371+
Test that clone_repo creates parent directories if they don't exist.
372+
373+
Given a local path with non-existent parent directories:
374+
When `clone_repo` is called,
375+
Then it should create the parent directories before attempting to clone.
376+
"""
377+
nested_path = tmp_path / "deep" / "nested" / "path" / "repo"
378+
clone_config = CloneConfig(
379+
url="https://github.com/user/repo",
380+
local_path=str(nested_path),
381+
)
382+
383+
with patch("gitingest.repository_clone._check_repo_exists", return_value=True):
384+
with patch("gitingest.repository_clone._run_git_command", new_callable=AsyncMock) as mock_exec:
385+
await clone_repo(clone_config)
386+
387+
# Verify parent directory was created
388+
assert nested_path.parent.exists()
389+
390+
# Verify git clone was called with correct parameters
391+
mock_exec.assert_called_once_with(
392+
"git",
393+
"clone",
394+
"--depth=1",
395+
"--single-branch",
396+
clone_config.url,
397+
str(nested_path),
398+
)

0 commit comments

Comments
 (0)