From fbc715256d3a451d79d2ca909e7306651d121eee Mon Sep 17 00:00:00 2001 From: Jialei Date: Thu, 8 Dec 2022 10:20:36 +0800 Subject: [PATCH 1/2] fix(ci): make test of dataset stable --- client/starwhale/utils/fs.py | 8 ++++++-- client/tests/sdk/test_base.py | 3 ++- client/tests/sdk/test_dataset_sdk.py | 2 ++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/client/starwhale/utils/fs.py b/client/starwhale/utils/fs.py index c98ebabdc0..b35894a543 100644 --- a/client/starwhale/utils/fs.py +++ b/client/starwhale/utils/fs.py @@ -45,7 +45,11 @@ def ensure_file( os.chmod(path, mode) -def empty_dir(p: t.Union[str, Path]) -> None: +def empty_dir( + p: t.Union[str, Path], + ignore_errors: bool = False, + onerror: t.Optional[t.Callable] = None, +) -> None: if not p: return @@ -55,7 +59,7 @@ def empty_dir(p: t.Union[str, Path]) -> None: def _self_empty() -> None: if path.is_dir(): - shutil.rmtree(str(path.resolve())) + shutil.rmtree(str(path.resolve()), ignore_errors, onerror) else: path.unlink() diff --git a/client/tests/sdk/test_base.py b/client/tests/sdk/test_base.py index 325e4b29e0..1b5bd79527 100644 --- a/client/tests/sdk/test_base.py +++ b/client/tests/sdk/test_base.py @@ -24,7 +24,8 @@ def setUp(self) -> None: self.mock_atexit.start() def tearDown(self) -> None: - empty_dir(self.local_storage) + # use ignore_errors = True to prevent errors like "Directory not empty" caused by missing close action in test + empty_dir(self.local_storage, ignore_errors=True) os.environ.pop(ENV_SW_CLI_CONFIG, "") os.environ.pop(ENV_SW_LOCAL_STORAGE, "") diff --git a/client/tests/sdk/test_dataset_sdk.py b/client/tests/sdk/test_dataset_sdk.py index 2a688bb69e..2ab8d0f2ee 100644 --- a/client/tests/sdk/test_dataset_sdk.py +++ b/client/tests/sdk/test_dataset_sdk.py @@ -42,6 +42,7 @@ def _init_simple_dataset(self) -> URI: ) ) ds.commit() + ds.close() return ds.uri def _init_simple_dataset_with_str_id(self) -> URI: @@ -55,6 +56,7 @@ def _init_simple_dataset_with_str_id(self) -> URI: ) ) ds.commit() + ds.close() return ds.uri def test_create_from_empty(self) -> None: From 7aedec452c208fc45e151db3da9039fba32320b8 Mon Sep 17 00:00:00 2001 From: Jialei Date: Thu, 8 Dec 2022 19:10:23 +0800 Subject: [PATCH 2/2] remove close --- client/tests/sdk/test_dataset_sdk.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/client/tests/sdk/test_dataset_sdk.py b/client/tests/sdk/test_dataset_sdk.py index 2ab8d0f2ee..2a688bb69e 100644 --- a/client/tests/sdk/test_dataset_sdk.py +++ b/client/tests/sdk/test_dataset_sdk.py @@ -42,7 +42,6 @@ def _init_simple_dataset(self) -> URI: ) ) ds.commit() - ds.close() return ds.uri def _init_simple_dataset_with_str_id(self) -> URI: @@ -56,7 +55,6 @@ def _init_simple_dataset_with_str_id(self) -> URI: ) ) ds.commit() - ds.close() return ds.uri def test_create_from_empty(self) -> None: