From 2654bee6f246ea27836a0cc8a422e270302cf2c5 Mon Sep 17 00:00:00 2001 From: Ian Thomas Date: Thu, 21 Sep 2023 17:49:50 +0100 Subject: [PATCH] Correct rmdir when not just a bucket --- s3fs/core.py | 7 +++++++ s3fs/tests/test_s3fs.py | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/s3fs/core.py b/s3fs/core.py index 3d1a61af..11e154f4 100644 --- a/s3fs/core.py +++ b/s3fs/core.py @@ -919,6 +919,13 @@ async def _makedirs(self, path, exist_ok=False): makedirs = sync_wrapper(_makedirs) async def _rmdir(self, path): + bucket, key, _ = self.split_path(path) + if key: + if await self._exists(path): + # User may have meant rm(path, recursive=True) + raise FileExistsError + raise FileNotFoundError + try: await self._call_s3("delete_bucket", Bucket=path) except botocore.exceptions.ClientError as e: diff --git a/s3fs/tests/test_s3fs.py b/s3fs/tests/test_s3fs.py index 769439e1..4ae1b3be 100644 --- a/s3fs/tests/test_s3fs.py +++ b/s3fs/tests/test_s3fs.py @@ -621,6 +621,22 @@ def test_rmdir(s3): s3.rmdir(bucket) assert bucket not in s3.ls("/") + # Issue 689, s3fs rmdir command returns error when given a valid s3 path. + dir = test_bucket_name + "/dir" + + assert not s3.exists(dir) + with pytest.raises(FileNotFoundError): + s3.rmdir(dir) + + s3.touch(dir + "/file") + assert s3.exists(dir) + assert s3.exists(dir + "/file") + with pytest.raises(FileExistsError): + s3.rmdir(dir) + + with pytest.raises(OSError): + s3.rmdir(test_bucket_name) + def test_mkdir(s3): bucket = "test1_bucket"