From 3c123ba745d98f8f53003276990eb34d8864f002 Mon Sep 17 00:00:00 2001 From: fabianegli Date: Wed, 7 Sep 2022 17:33:11 +0200 Subject: [PATCH 1/4] change to the working directory in a context --- tests/modules/lint.py | 10 ++++----- tests/modules/module_test.py | 40 ++++++++++++++++-------------------- tests/utils.py | 19 +++++++++++++++++ 3 files changed, 41 insertions(+), 28 deletions(-) diff --git a/tests/modules/lint.py b/tests/modules/lint.py index d5793dfd05..df3a4cbe24 100644 --- a/tests/modules/lint.py +++ b/tests/modules/lint.py @@ -4,7 +4,7 @@ import nf_core.modules -from ..utils import GITLAB_URL +from ..utils import GITLAB_URL, set_wd from .patch import BISMARK_ALIGN, PATCH_BRANCH, setup_patch @@ -65,11 +65,9 @@ def test_modules_lint_patched_modules(self): # change temporarily working directory to the pipeline directory # to avoid error from try_apply_patch() during linting - wd_old = os.getcwd() - os.chdir(self.pipeline_dir) - module_lint = nf_core.modules.ModuleLint(dir=self.pipeline_dir, remote_url=GITLAB_URL) - module_lint.lint(print_results=False, all_modules=True) - os.chdir(wd_old) + with set_wd(self.pipeline_dir): + module_lint = nf_core.modules.ModuleLint(dir=self.pipeline_dir, remote_url=GITLAB_URL) + module_lint.lint(print_results=False, all_modules=True) assert len(module_lint.failed) == 0 assert len(module_lint.passed) > 0 diff --git a/tests/modules/module_test.py b/tests/modules/module_test.py index ef955d0061..ce57312bf1 100644 --- a/tests/modules/module_test.py +++ b/tests/modules/module_test.py @@ -7,39 +7,35 @@ import nf_core.modules +from ..utils import set_wd + def test_modules_test_check_inputs(self): """Test the check_inputs() function - raise UserWarning because module doesn't exist""" - cwd = os.getcwd() - os.chdir(self.nfcore_modules) - meta_builder = nf_core.modules.ModulesTest("none", True, "") - with pytest.raises(UserWarning) as excinfo: - meta_builder._check_inputs() - os.chdir(cwd) + with set_wd(self.nfcore_modules): + meta_builder = nf_core.modules.ModulesTest("none", True, "") + with pytest.raises(UserWarning) as excinfo: + meta_builder._check_inputs() assert "Cannot find directory" in str(excinfo.value) def test_modules_test_no_name_no_prompts(self): """Test the check_inputs() function - raise UserWarning prompts are deactivated and module name is not provided.""" - cwd = os.getcwd() - os.chdir(self.nfcore_modules) - meta_builder = nf_core.modules.ModulesTest(None, True, "") - with pytest.raises(UserWarning) as excinfo: - meta_builder._check_inputs() - os.chdir(cwd) + with set_wd(self.nfcore_modules): + meta_builder = nf_core.modules.ModulesTest(None, True, "") + with pytest.raises(UserWarning) as excinfo: + meta_builder._check_inputs() assert "Tool name not provided and prompts deactivated." in str(excinfo.value) def test_modules_test_no_installed_modules(self): """Test the check_inputs() function - raise UserWarning because installed modules were not found""" - cwd = os.getcwd() - os.chdir(self.nfcore_modules) - module_dir = Path(self.nfcore_modules, "modules") - shutil.rmtree(module_dir) - module_dir.mkdir() - meta_builder = nf_core.modules.ModulesTest(None, False, "") - meta_builder.repo_type = "modules" - with pytest.raises(UserWarning) as excinfo: - meta_builder._check_inputs() - os.chdir(cwd) + with set_wd(self.nfcore_modules): + module_dir = Path(self.nfcore_modules, "modules") + shutil.rmtree(module_dir) + module_dir.mkdir() + meta_builder = nf_core.modules.ModulesTest(None, False, "") + meta_builder.repo_type = "modules" + with pytest.raises(UserWarning) as excinfo: + meta_builder._check_inputs() assert "No installed modules were found" in str(excinfo.value) diff --git a/tests/utils.py b/tests/utils.py index 03bfe272a0..a3e319b617 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -5,7 +5,10 @@ """ import functools +import os import tempfile +from contextlib import contextmanager +from pathlib import Path OLD_TRIMGALORE_SHA = "20d8250d9f39ddb05dfb437603aaf99b5c0b2b41" GITLAB_URL = "https://gitlab.com/nf-core/modules-test.git" @@ -44,3 +47,19 @@ def wrapper(*args, **kwargs): return func(*args, tmpfile, **kwargs) return wrapper + + +@contextmanager +def set_wd(path: Path): + """Sets the working directory for this context. + + Arguments + --------- + + path : Path + Path to the working directory to be used iside this context. + """ + start_wd = Path().absolute() + os.chdir(path) + yield + os.chdir(start_wd) From 6f84c980aeae062157f9b063f7e9ba374822519d Mon Sep 17 00:00:00 2001 From: fabianegli Date: Wed, 7 Sep 2022 17:39:01 +0200 Subject: [PATCH 2/4] use contextlib to temporarily set a working directry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 27b86f23ce..6c0af1a1b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ ### General - Add function to enable chat notifications on MS Teams, accompanied by `hook_url` param to enable it. +- Use contextlib to temporarily change working directories ([#1819](https://github.com/nf-core/tools/pull/1819)) ### Modules From cf738225088c4931c887c96dc281915cd966d7f7 Mon Sep 17 00:00:00 2001 From: Fabian Egli Date: Thu, 8 Sep 2022 08:31:34 +0200 Subject: [PATCH 3/4] make the context switch robust --- tests/utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/utils.py b/tests/utils.py index a3e319b617..164df96c83 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -61,5 +61,7 @@ def set_wd(path: Path): """ start_wd = Path().absolute() os.chdir(path) - yield - os.chdir(start_wd) + try: + yield + finally: + os.chdir(start_wd) From 9aa0d91fa5c2ccc9f0b4a59e1bc39dea1fb03f0c Mon Sep 17 00:00:00 2001 From: fabianegli Date: Mon, 12 Sep 2022 11:34:34 +0200 Subject: [PATCH 4/4] test test utils --- tests/test_test_utils.py | 50 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 tests/test_test_utils.py diff --git a/tests/test_test_utils.py b/tests/test_test_utils.py new file mode 100644 index 0000000000..ddf88ef74a --- /dev/null +++ b/tests/test_test_utils.py @@ -0,0 +1,50 @@ +import tempfile +from pathlib import Path + +import pytest + +from .utils import set_wd, with_temporary_file, with_temporary_folder + + +def test_with_temporary_file(): + @with_temporary_file + def tmp_file_exists(tmp_file): + assert Path(tmp_file.name).exists() + + tmp_file_exists() + + +def test_does_not_exist_after(): + tmp_file = with_temporary_file(lambda x: x.name)() + assert not Path(tmp_file).exists() + + +def test_with_temporary_folder(): + @with_temporary_folder + def tmp_folder_exists(tmp_folder): + assert Path(tmp_folder).exists() + + tmp_folder_exists() + + +def test_tmp_folder_does_not_exist_after(): + tmp_folder = with_temporary_folder(lambda x: x)() + assert not Path(tmp_folder).exists() + + +def test_set_wd(): + + with tempfile.TemporaryDirectory() as tmpdirname: + with set_wd(tmpdirname): + context_wd = Path().resolve() + assert context_wd == Path(tmpdirname).resolve() + assert context_wd != Path().resolve() + + +def test_set_wd_revert_on_raise(): + wd_before_context = Path().resolve() + with tempfile.TemporaryDirectory() as tmpdirname: + with pytest.raises(Exception): + with set_wd(tmpdirname): + raise Exception + assert wd_before_context == Path().resolve()