Skip to content

Commit

Permalink
tests: cloud: introduce unlink/rmdir (#60)
Browse files Browse the repository at this point in the history
Will be useful in the near future.
  • Loading branch information
efiop authored Dec 21, 2023
1 parent 7f9f013 commit 43f7022
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions dvc_s3/tests/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ def mkdir(self, mode=0o777, parents=False, exist_ok=False):
def write_bytes(self, contents):
self._s3.put_object(Bucket=self.bucket, Key=self.path, Body=contents)

def unlink(self, missing_ok: bool = False) -> None:
if not self.exists():
if not missing_ok:
raise FileNotFoundError(str(self))
return
self._s3.delete_object(Bucket=self.bucket, Key=self.path)

def rmdir(self, recursive: bool = True) -> None:
if not self.is_dir():
raise NotADirectoryError(str(self))

path = (self / "").path
resp = self._s3.list_objects(Bucket=self.bucket, Prefix=path)
entries = resp.get("Contents")
if not entries:
return

if not recursive:
raise OSError(f"Not recursive and directory not empty: {self}")

for entry in entries:
self._s3.delete_object(Bucket=self.bucket, Key=entry["Key"])

def read_bytes(self):
data = self._s3.get_object(Bucket=self.bucket, Key=self.path)
return data["Body"].read()
Expand Down

0 comments on commit 43f7022

Please sign in to comment.