Skip to content

Commit 162dd2f

Browse files
authored
Fix touch exists_ok if file exists (#262)
* tests: touch exists_ok behavior * upath: fix touch exists_ok * upath.implementations.cloud: remove AzurePath.touch custom method
1 parent c457f06 commit 162dd2f

File tree

5 files changed

+31
-24
lines changed

5 files changed

+31
-24
lines changed

upath/core.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,16 @@ def readlink(self) -> Self:
976976
raise NotImplementedError
977977

978978
def touch(self, mode=0o666, exist_ok=True) -> None:
979-
self.fs.touch(self.path, truncate=not exist_ok)
979+
exists = self.fs.exists(self.path)
980+
if exists and not exist_ok:
981+
raise FileExistsError(str(self))
982+
if not exists:
983+
self.fs.touch(self.path, truncate=True)
984+
else:
985+
try:
986+
self.fs.touch(self.path, truncate=False)
987+
except (NotImplementedError, ValueError):
988+
pass # unsupported by filesystem
980989

981990
def mkdir(self, mode=0o777, parents=False, exist_ok=False) -> None:
982991
if parents and not exist_ok and self.exists():

upath/implementations/cloud.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,3 @@ class S3Path(CloudPath):
8080

8181
class AzurePath(CloudPath):
8282
__slots__ = ()
83-
84-
def touch(self, mode=0o666, exist_ok=True):
85-
if exist_ok and self.exists():
86-
with self.fs.open(self.path, mode="a"):
87-
pass
88-
else:
89-
self.fs.touch(self.path, truncate=True)

upath/tests/cases.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,19 @@ def test_rglob(self, pathlib_base):
320320
def test_symlink_to(self):
321321
pass
322322

323+
def test_touch_exists_ok_false(self):
324+
f = self.path.joinpath("file1.txt")
325+
assert f.exists()
326+
with pytest.raises(FileExistsError):
327+
f.touch(exist_ok=False)
328+
329+
def test_touch_exists_ok_true(self):
330+
f = self.path.joinpath("file1.txt")
331+
assert f.exists()
332+
data = f.read_text()
333+
f.touch(exist_ok=True)
334+
assert f.read_text() == data
335+
323336
def test_touch_unlink(self):
324337
path = self.path.joinpath("test_touch.txt")
325338
path.touch()

upath/tests/implementations/test_data.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,15 @@ def test_rglob(self, pathlib_base):
133133
with pytest.raises(NotImplementedError):
134134
list(self.path.rglob("*"))
135135

136+
def test_touch_exists_ok_false(self):
137+
with pytest.raises(FileExistsError):
138+
self.path.touch(exist_ok=False)
139+
140+
def test_touch_exists_ok_true(self):
141+
self.path.touch()
142+
136143
def test_touch_unlink(self):
137-
with pytest.raises(NotImplementedError):
138-
self.path.touch()
144+
self.path.touch()
139145
with pytest.raises(NotImplementedError):
140146
self.path.unlink()
141147

upath/tests/implementations/test_s3.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,6 @@ def test_iterdir_root(self):
5353
assert x.name != ""
5454
assert x.exists()
5555

56-
def test_touch_unlink(self):
57-
path = self.path.joinpath("test_touch.txt")
58-
path.touch()
59-
assert path.exists()
60-
path.unlink()
61-
assert not path.exists()
62-
63-
# should raise FileNotFoundError since file is missing
64-
with pytest.raises(FileNotFoundError):
65-
path.unlink()
66-
67-
# file doesn't exists, but missing_ok is True
68-
path.unlink(missing_ok=True)
69-
7056
@pytest.mark.parametrize(
7157
"joiner", [["bucket", "path", "file"], ["bucket/path/file"]]
7258
)

0 commit comments

Comments
 (0)