From 112c604738a552e186ee49258e24444512598eb5 Mon Sep 17 00:00:00 2001 From: harupy Date: Mon, 29 Nov 2021 19:15:54 +0900 Subject: [PATCH 1/6] Remove mlruns directory in module teardown Signed-off-by: harupy --- tests/conftest.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index 94df3fd2a807c..7ff90e987ab7b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,6 @@ import os import inspect +import shutil from unittest import mock import pytest @@ -96,3 +97,12 @@ def new_exception(msg, *_, **__): yield else: yield + + +@pytest.fixture(autouse=True, scope="module") +def clean_up_mlruns_direcotry(request): + """ + Clean up an `mlruns` directory in module teardown. + """ + yield + shutil.rmtree(os.path.join(request.config.rootpath, "mlruns")) From d3951ac1b9b4898b5e878d0f0798aa51f3bbac21 Mon Sep 17 00:00:00 2001 From: harupy Date: Mon, 29 Nov 2021 19:23:41 +0900 Subject: [PATCH 2/6] fix docstring Signed-off-by: harupy --- tests/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index 7ff90e987ab7b..9b8608b16e5e4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -102,7 +102,7 @@ def new_exception(msg, *_, **__): @pytest.fixture(autouse=True, scope="module") def clean_up_mlruns_direcotry(request): """ - Clean up an `mlruns` directory in module teardown. + Clean up an `mlruns` directory on each test module teardown. """ yield shutil.rmtree(os.path.join(request.config.rootpath, "mlruns")) From 979cf7e1081d8aa41cc2d91667f86e1a7eaeefa9 Mon Sep 17 00:00:00 2001 From: harupy Date: Mon, 29 Nov 2021 19:28:56 +0900 Subject: [PATCH 3/6] remove if exists Signed-off-by: harupy --- tests/conftest.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index 9b8608b16e5e4..b3b213c85a4eb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -105,4 +105,6 @@ def clean_up_mlruns_direcotry(request): Clean up an `mlruns` directory on each test module teardown. """ yield - shutil.rmtree(os.path.join(request.config.rootpath, "mlruns")) + mlruns_dir = os.path.join(request.config.rootpath, "mlruns") + if os.path.exists(mlruns_dir): + shutil.rmtree(mlruns_dir) From d4777cffc7f5159314242fec15a8b9635a27de8b Mon Sep 17 00:00:00 2001 From: harupy Date: Mon, 29 Nov 2021 21:16:55 +0900 Subject: [PATCH 4/6] try onerror Signed-off-by: harupy --- tests/conftest.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index b3b213c85a4eb..1f736633180b2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,5 @@ import os +import stat import inspect import shutil from unittest import mock @@ -99,6 +100,11 @@ def new_exception(msg, *_, **__): yield +def on_rm_error(func, path, exc_info): # pylint: disable=unused-argument + os.chmod(path, stat.S_IWRITE) + os.unlink(path) + + @pytest.fixture(autouse=True, scope="module") def clean_up_mlruns_direcotry(request): """ @@ -107,4 +113,4 @@ def clean_up_mlruns_direcotry(request): yield mlruns_dir = os.path.join(request.config.rootpath, "mlruns") if os.path.exists(mlruns_dir): - shutil.rmtree(mlruns_dir) + shutil.rmtree(mlruns_dir, onerror=on_rm_error) From 60572a6566883e709d8743c94ea16a010abc5d40 Mon Sep 17 00:00:00 2001 From: harupy Date: Tue, 30 Nov 2021 02:02:57 +0900 Subject: [PATCH 5/6] try sudo Signed-off-by: harupy --- tests/conftest.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 1f736633180b2..a8830b70d8bea 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,7 @@ import os -import stat import inspect import shutil +import subprocess from unittest import mock import pytest @@ -100,11 +100,6 @@ def new_exception(msg, *_, **__): yield -def on_rm_error(func, path, exc_info): # pylint: disable=unused-argument - os.chmod(path, stat.S_IWRITE) - os.unlink(path) - - @pytest.fixture(autouse=True, scope="module") def clean_up_mlruns_direcotry(request): """ @@ -113,4 +108,9 @@ def clean_up_mlruns_direcotry(request): yield mlruns_dir = os.path.join(request.config.rootpath, "mlruns") if os.path.exists(mlruns_dir): - shutil.rmtree(mlruns_dir, onerror=on_rm_error) + try: + shutil.rmtree(mlruns_dir) + except IOError: + if os.name == "nt": + raise + subprocess.run(["sudo", "rm", "-rf", mlruns_dir], check=True) From b44c5faac1cd1e9d810480432c3868bb0ae7a175 Mon Sep 17 00:00:00 2001 From: harupy Date: Tue, 30 Nov 2021 09:50:53 +0900 Subject: [PATCH 6/6] skip clean up on CI Signed-off-by: harupy --- tests/conftest.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index a8830b70d8bea..3f440c1397c30 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -103,9 +103,14 @@ def new_exception(msg, *_, **__): @pytest.fixture(autouse=True, scope="module") def clean_up_mlruns_direcotry(request): """ - Clean up an `mlruns` directory on each test module teardown. + Clean up an `mlruns` directory on each test module teardown on CI to save the disk space. """ yield + + # Only run this fixture on CI. + if "GITHUB_ACTIONS" not in os.environ: + return + mlruns_dir = os.path.join(request.config.rootpath, "mlruns") if os.path.exists(mlruns_dir): try: @@ -113,4 +118,5 @@ def clean_up_mlruns_direcotry(request): except IOError: if os.name == "nt": raise + # `shutil.rmtree` can't remove files owned by root in a docker container. subprocess.run(["sudo", "rm", "-rf", mlruns_dir], check=True)