Skip to content

Commit 1268b32

Browse files
gavenrenByron
authored andcommitted
fix: accept current-directory files in parent assurance
`assure_directory_exists("notes.txt", is_file=True)` attempted to create the empty dirname and raised `FileNotFoundError`, despite the current directory already being present. Interpret an empty parent as `os.curdir` for file inputs. Preserve the helper's return convention, nested parent creation and failures when a parent is an existing regular file. Use a native scripted filesystem fixture for string and pathlib inputs. Two current-directory cases fail before the fix, while six controls pass. All eight cases pass afterward; utility and typing modules pass on both backends (129 passed, 39 skipped each). Applicable pre-commit checks, including shellcheck, pass. Assisted-by: OpenAI Codex (model: unknown, autonomous)
1 parent ec12459 commit 1268b32

3 files changed

Lines changed: 47 additions & 1 deletion

File tree

‎git/util.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ def assure_directory_exists(path: PathLike, is_file: bool = False) -> bool:
402402
``True`` if the directory was created, ``False`` if it already existed.
403403
"""
404404
if is_file:
405-
path = osp.dirname(path)
405+
path = osp.dirname(path) or osp.curdir
406406
# END handle file
407407
if not osp.isdir(path):
408408
os.makedirs(path, exist_ok=True)

‎test/test_assure_directory.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
5+
from git.util import assure_directory_exists
6+
from test.lib.fixtures import writable_fixture
7+
8+
9+
@pytest.fixture
10+
def directory_fixture(monkeypatch):
11+
with writable_fixture("assure-directory.sh") as temporary:
12+
directory = temporary.path().resolve()
13+
with monkeypatch.context() as context:
14+
context.chdir(directory)
15+
yield directory
16+
17+
18+
@pytest.mark.parametrize("path_type", [str, Path])
19+
def test_file_in_current_directory_already_has_parent(directory_fixture, path_type):
20+
assert assure_directory_exists(path_type("notes.txt"), is_file=True) is False
21+
assert not (directory_fixture / "notes.txt").exists()
22+
23+
24+
@pytest.mark.parametrize("path_type", [str, Path])
25+
def test_nested_file_creates_only_missing_parent_directories(directory_fixture, path_type):
26+
filename = path_type("new/nested/notes.txt")
27+
assert assure_directory_exists(filename, is_file=True) is True
28+
assert (directory_fixture / "new/nested").is_dir()
29+
assert not (directory_fixture / "new/nested/notes.txt").exists()
30+
assert assure_directory_exists(filename, is_file=True) is False
31+
32+
33+
@pytest.mark.parametrize("path_type", [str, Path])
34+
def test_existing_directory_is_unchanged(directory_fixture, path_type):
35+
assert assure_directory_exists(path_type("existing")) is False
36+
37+
38+
@pytest.mark.parametrize("path_type", [str, Path])
39+
def test_file_cannot_be_used_as_parent_directory(directory_fixture, path_type):
40+
with pytest.raises(FileExistsError):
41+
assure_directory_exists(path_type("blocked/notes.txt"), is_file=True)
42+
assert (directory_fixture / "blocked").read_text() == "occupied\n"

‎tests/fixtures/assure-directory.sh‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
set -eu
3+
mkdir existing
4+
printf 'occupied\n' > blocked

0 commit comments

Comments
 (0)