Skip to content

Commit

Permalink
tests: implement unlink/rmdir
Browse files Browse the repository at this point in the history
  • Loading branch information
efiop committed Dec 28, 2023
1 parent 6b27904 commit 147273e
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions dvc_azure/tests/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def service_client(self):

return service_client

@property
def container_client(self):
return self.service_client.get_container_client(container=self.bucket)

@property
def blob_client(self):
return self.service_client.get_blob_client(self.bucket, self.path)
Expand All @@ -43,6 +47,32 @@ def mkdir(self, mode=0o777, parents=False, exist_ok=False):
def write_bytes(self, contents):
self.blob_client.upload_blob(contents, overwrite=True)

def unlink(self, missing_ok: bool = False) -> None:
if not self.exists():
if not missing_ok:
raise FileNotFoundError(str(self))
return
self.blob_client.delete_blob()

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

blobs = [
blob.name
for blob in self.container_client.list_blobs(
name_starts_with=(self / "").path
)
]
if not blobs:
return

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

for blob in blobs:
self.container_client.delete_blob(blob)

def read_bytes(self):
stream = self.blob_client.download_blob()
return stream.readall()
Expand Down

0 comments on commit 147273e

Please sign in to comment.