From 147273e64e9505517582e041af72dda20b77c843 Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Thu, 28 Dec 2023 05:38:17 +0200 Subject: [PATCH] tests: implement unlink/rmdir --- dvc_azure/tests/cloud.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/dvc_azure/tests/cloud.py b/dvc_azure/tests/cloud.py index b0ed9ff..81dffbf 100644 --- a/dvc_azure/tests/cloud.py +++ b/dvc_azure/tests/cloud.py @@ -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) @@ -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()