Skip to content

Commit cfa0795

Browse files
authored
Fix broken mkdir (#177)
* tests: add azure mkdir test for new bucket source: dtrifiro@fac2bcf * upath: don't use makedirs in UPath.mkdir * upath.implementations.cloud: handle older versions of gcsfs's mkdir * tests: remove xfails from gcsfs mkdir tests
1 parent 8314a65 commit cfa0795

File tree

4 files changed

+33
-26
lines changed

4 files changed

+33
-26
lines changed

upath/core.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -748,20 +748,19 @@ def touch(self, mode=0o666, exist_ok=True):
748748
self.fs.touch(self.path, truncate=not exist_ok)
749749

750750
def mkdir(self, mode=0o777, parents=False, exist_ok=False):
751-
if parents:
752-
if not exist_ok and self.exists():
751+
if parents and not exist_ok and self.exists():
752+
raise FileExistsError(str(self))
753+
try:
754+
self.fs.mkdir(
755+
self.path,
756+
create_parents=parents,
757+
mode=mode,
758+
)
759+
except FileExistsError:
760+
if not exist_ok:
761+
raise FileExistsError(str(self))
762+
if not self.is_dir():
753763
raise FileExistsError(str(self))
754-
self.fs.makedirs(self.path, exist_ok=exist_ok)
755-
else:
756-
try:
757-
self.fs.mkdir(
758-
self.path,
759-
create_parents=False,
760-
mode=mode,
761-
)
762-
except FileExistsError:
763-
if not exist_ok or not self.is_dir():
764-
raise FileExistsError(str(self))
765764

766765
def chmod(self, mode, *, follow_symlinks=True):
767766
raise NotImplementedError

upath/implementations/cloud.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ def relative_to(self, other, /, *_deprecated, walk_up=False):
5959
class GCSPath(CloudPath):
6060
__slots__ = ()
6161

62+
def mkdir(
63+
self, mode: int = 0o777, parents: bool = False, exist_ok: bool = False
64+
) -> None:
65+
try:
66+
super().mkdir(mode=mode, parents=parents, exist_ok=exist_ok)
67+
except TypeError as err:
68+
if "unexpected keyword argument 'create_parents'" in str(err):
69+
self.fs.mkdir(self.path)
70+
6271

6372
class S3Path(CloudPath):
6473
__slots__ = ()

upath/tests/implementations/test_azure.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,15 @@ def test_protocol(self):
4949
# test all valid protocols for azure...
5050
protocol = self.path.protocol
5151
assert protocol in ["abfs", "abfss", "adl", "az"]
52+
53+
def test_broken_mkdir(self):
54+
path = UPath(
55+
"az://new-container/",
56+
**self.storage_options,
57+
)
58+
if path.exists():
59+
path.rmdir()
60+
path.mkdir(parents=True, exist_ok=False)
61+
62+
(path / "file").write_text("foo")
63+
assert path.exists()

upath/tests/implementations/test_gcs.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from ..cases import BaseTests
77
from ..utils import skip_on_windows
8-
from ..utils import xfail_if_version
98

109

1110
@skip_on_windows
@@ -35,15 +34,3 @@ def test_rmdir(self):
3534
@pytest.mark.skip
3635
def test_makedirs_exist_ok_false(self):
3736
pass
38-
39-
@xfail_if_version("gcsfs", lt="2022.7.1", reason="requires gcsfs>=2022.7.1")
40-
def test_mkdir(self):
41-
super().test_mkdir()
42-
43-
@xfail_if_version("gcsfs", lt="2022.7.1", reason="requires gcsfs>=2022.7.1")
44-
def test_mkdir_exists_ok_false(self):
45-
super().test_mkdir_exists_ok_false()
46-
47-
@xfail_if_version("gcsfs", lt="2022.7.1", reason="requires gcsfs>=2022.7.1")
48-
def test_mkdir_exists_ok_true(self):
49-
super().test_mkdir_exists_ok_true()

0 commit comments

Comments
 (0)