Skip to content

Commit 3ef5e6f

Browse files
authored
Respect exist_ok in mkdir when creating parent directories (#83)
* Respect exist_ok in mkdir when creating parent directories * Add makedirs and fix tests * Tests for makedirs
1 parent e0f1e32 commit 3ef5e6f

File tree

6 files changed

+66
-6
lines changed

6 files changed

+66
-6
lines changed

upath/core.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ def mkdir(self, path, create_parents=True, **kwargs):
8080
self._format_path(path), create_parents=create_parents, **kwargs
8181
)
8282

83+
def makedirs(self, path, exist_ok=False, **kwargs):
84+
return self._fs.makedirs(
85+
self._format_path(path), exist_ok=exist_ok, **kwargs
86+
)
87+
8388
def touch(self, path, **kwargs):
8489
return self._fs.touch(self._format_path(path), **kwargs)
8590

@@ -444,12 +449,9 @@ def mkdir(
444449
Create a new directory at this given path.
445450
"""
446451
if parents:
447-
self._accessor.mkdir(
448-
self,
449-
create_parents=True,
450-
exist_ok=exist_ok,
451-
mode=mode,
452-
)
452+
if not exist_ok and self.exists():
453+
raise FileExistsError
454+
self._accessor.makedirs(self, exist_ok=exist_ok)
453455
else:
454456
try:
455457
self._accessor.mkdir(

upath/tests/cases.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,36 @@ def test_mkdir_exists_ok_false(self):
156156
with pytest.raises(FileExistsError):
157157
new_dir.mkdir(exist_ok=False)
158158

159+
def test_mkdir_parents_true_exists_ok_true(self):
160+
new_dir = self.path.joinpath("parent", "new_dir_may_not_exist")
161+
new_dir.mkdir(parents=True)
162+
if not self.SUPPORTS_EMPTY_DIRS:
163+
new_dir.joinpath(".file").touch()
164+
new_dir.mkdir(parents=True, exist_ok=True)
165+
166+
def test_mkdir_parents_true_exists_ok_false(self):
167+
new_dir = self.path.joinpath("parent", "new_dir_may_exist")
168+
new_dir.mkdir(parents=True)
169+
if not self.SUPPORTS_EMPTY_DIRS:
170+
new_dir.joinpath(".file").touch()
171+
with pytest.raises(FileExistsError):
172+
new_dir.mkdir(parents=True, exist_ok=False)
173+
174+
def test_makedirs_exist_ok_true(self):
175+
new_dir = self.path.joinpath("parent", "child", "dir_may_not_exist")
176+
new_dir._accessor.makedirs(new_dir, exist_ok=True)
177+
if not self.SUPPORTS_EMPTY_DIRS:
178+
new_dir.joinpath(".file").touch()
179+
new_dir._accessor.makedirs(new_dir, exist_ok=True)
180+
181+
def test_makedirs_exist_ok_false(self):
182+
new_dir = self.path.joinpath("parent", "child", "dir_may_exist")
183+
new_dir._accessor.makedirs(new_dir, exist_ok=False)
184+
if not self.SUPPORTS_EMPTY_DIRS:
185+
new_dir.joinpath(".file").touch()
186+
with pytest.raises(FileExistsError):
187+
new_dir._accessor.makedirs(new_dir, exist_ok=False)
188+
159189
def test_open(self):
160190
pass
161191

upath/tests/implementations/test_azure.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ def test_rmdir(self):
3737

3838
with pytest.raises(NotADirectoryError):
3939
(self.path / "a" / "file.txt").rmdir()
40+
41+
@pytest.mark.skip
42+
def test_makedirs_exist_ok_false(self):
43+
pass

upath/tests/implementations/test_gcs.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ def test_rmdir(self):
2929
assert not mock_dir.exists()
3030
with pytest.raises(NotADirectoryError):
3131
self.path.joinpath("file1.txt").rmdir()
32+
33+
@pytest.mark.skip
34+
def test_makedirs_exist_ok_false(self):
35+
pass

upath/tests/implementations/test_http.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ def test_mkdir_exists_ok_false(self):
4646
def test_mkdir_exists_ok_true(self):
4747
pass
4848

49+
@pytest.mark.skip
50+
def test_mkdir_parents_true_exists_ok_true(self):
51+
pass
52+
53+
@pytest.mark.skip
54+
def test_mkdir_parents_true_exists_ok_false(self):
55+
pass
56+
57+
@pytest.mark.skip
58+
def test_makedirs_exist_ok_true(self):
59+
pass
60+
61+
@pytest.mark.skip
62+
def test_makedirs_exist_ok_false(self):
63+
pass
64+
4965
@pytest.mark.skip
5066
def test_touch_unlink(self):
5167
pass

upath/tests/implementations/test_s3.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ def test_iterdir_with_plus_in_name(self, s3_with_plus_chr_name):
9494
(file,) = files
9595
assert file == p.joinpath("file.txt")
9696

97+
@pytest.mark.skip
98+
def test_makedirs_exist_ok_false(self):
99+
pass
100+
97101

98102
@pytest.fixture
99103
def s3_with_plus_chr_name(s3_server):

0 commit comments

Comments
 (0)