Skip to content

Commit

Permalink
Fix broken mkdir (#177)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ap-- authored Feb 8, 2024
1 parent 8314a65 commit cfa0795
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
25 changes: 12 additions & 13 deletions upath/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,20 +748,19 @@ def touch(self, mode=0o666, exist_ok=True):
self.fs.touch(self.path, truncate=not exist_ok)

def mkdir(self, mode=0o777, parents=False, exist_ok=False):
if parents:
if not exist_ok and self.exists():
if parents and not exist_ok and self.exists():
raise FileExistsError(str(self))
try:
self.fs.mkdir(
self.path,
create_parents=parents,
mode=mode,
)
except FileExistsError:
if not exist_ok:
raise FileExistsError(str(self))
if not self.is_dir():
raise FileExistsError(str(self))
self.fs.makedirs(self.path, exist_ok=exist_ok)
else:
try:
self.fs.mkdir(
self.path,
create_parents=False,
mode=mode,
)
except FileExistsError:
if not exist_ok or not self.is_dir():
raise FileExistsError(str(self))

def chmod(self, mode, *, follow_symlinks=True):
raise NotImplementedError
Expand Down
9 changes: 9 additions & 0 deletions upath/implementations/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ def relative_to(self, other, /, *_deprecated, walk_up=False):
class GCSPath(CloudPath):
__slots__ = ()

def mkdir(
self, mode: int = 0o777, parents: bool = False, exist_ok: bool = False
) -> None:
try:
super().mkdir(mode=mode, parents=parents, exist_ok=exist_ok)
except TypeError as err:
if "unexpected keyword argument 'create_parents'" in str(err):
self.fs.mkdir(self.path)


class S3Path(CloudPath):
__slots__ = ()
Expand Down
12 changes: 12 additions & 0 deletions upath/tests/implementations/test_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,15 @@ def test_protocol(self):
# test all valid protocols for azure...
protocol = self.path.protocol
assert protocol in ["abfs", "abfss", "adl", "az"]

def test_broken_mkdir(self):
path = UPath(
"az://new-container/",
**self.storage_options,
)
if path.exists():
path.rmdir()
path.mkdir(parents=True, exist_ok=False)

(path / "file").write_text("foo")
assert path.exists()
13 changes: 0 additions & 13 deletions upath/tests/implementations/test_gcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from ..cases import BaseTests
from ..utils import skip_on_windows
from ..utils import xfail_if_version


@skip_on_windows
Expand Down Expand Up @@ -35,15 +34,3 @@ def test_rmdir(self):
@pytest.mark.skip
def test_makedirs_exist_ok_false(self):
pass

@xfail_if_version("gcsfs", lt="2022.7.1", reason="requires gcsfs>=2022.7.1")
def test_mkdir(self):
super().test_mkdir()

@xfail_if_version("gcsfs", lt="2022.7.1", reason="requires gcsfs>=2022.7.1")
def test_mkdir_exists_ok_false(self):
super().test_mkdir_exists_ok_false()

@xfail_if_version("gcsfs", lt="2022.7.1", reason="requires gcsfs>=2022.7.1")
def test_mkdir_exists_ok_true(self):
super().test_mkdir_exists_ok_true()

0 comments on commit cfa0795

Please sign in to comment.