diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ee1a97beb..1e801c728e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,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 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/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() diff --git a/tests/utils.py b/tests/utils.py index 03bfe272a0..164df96c83 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,21 @@ 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) + try: + yield + finally: + os.chdir(start_wd)