diff --git a/nf_core/components/components_test.py b/nf_core/components/components_test.py index 689a6819eb..06cc11f3ba 100644 --- a/nf_core/components/components_test.py +++ b/nf_core/components/components_test.py @@ -19,7 +19,6 @@ import nf_core.utils from nf_core.components.components_command import ComponentCommand -from tests.utils import set_wd log = logging.getLogger(__name__) @@ -92,7 +91,7 @@ def run(self) -> None: os.environ[ "NFT_DIFF_ARGS" ] = "--line-numbers --expand-tabs=2" # taken from https://code.askimed.com/nf-test/docs/assertions/snapshots/#snapshot-differences - with set_wd(Path(self.dir)): + with nf_core.utils.set_wd(Path(self.dir)): self.check_snapshot_stability() if len(self.errors) > 0: errors = "\n - ".join(self.errors) diff --git a/nf_core/utils.py b/nf_core/utils.py index c57990a9d3..eda0ed8f55 100644 --- a/nf_core/utils.py +++ b/nf_core/utils.py @@ -16,8 +16,9 @@ import subprocess import sys import time +from contextlib import contextmanager from pathlib import Path -from typing import Tuple, Union +from typing import Generator, Tuple, Union import git import prompt_toolkit @@ -1147,3 +1148,21 @@ def validate_file_md5(file_name, expected_md5hex): raise IOError(f"{file_name} md5 does not match remote: {expected_md5hex} - {file_md5hex}") return True + + +@contextmanager +def set_wd(path: Path) -> Generator[None, None, None]: + """Sets the working directory for this context. + + Arguments + --------- + + path : Path + Path to the working directory to be used inside this context. + """ + start_wd = Path().absolute() + os.chdir(Path(path).resolve()) + try: + yield + finally: + os.chdir(start_wd) diff --git a/tests/components/generate_snapshot.py b/tests/components/generate_snapshot.py index c7eb696722..46fd63fe3f 100644 --- a/tests/components/generate_snapshot.py +++ b/tests/components/generate_snapshot.py @@ -6,8 +6,9 @@ import pytest from nf_core.components.components_test import ComponentsTest +from nf_core.utils import set_wd -from ..utils import GITLAB_NFTEST_BRANCH, GITLAB_URL, set_wd +from ..utils import GITLAB_NFTEST_BRANCH, GITLAB_URL def test_generate_snapshot_module(self): diff --git a/tests/components/snapshot_test.py b/tests/components/snapshot_test.py index 371f0d6fbe..d774618476 100644 --- a/tests/components/snapshot_test.py +++ b/tests/components/snapshot_test.py @@ -5,8 +5,7 @@ import pytest from nf_core.components.components_test import ComponentsTest - -from ..utils import set_wd +from nf_core.utils import set_wd def test_components_test_check_inputs(self): diff --git a/tests/modules/lint.py b/tests/modules/lint.py index a3a7a80b71..f35b7eee1e 100644 --- a/tests/modules/lint.py +++ b/tests/modules/lint.py @@ -5,8 +5,9 @@ import nf_core.modules from nf_core.modules.lint import main_nf +from nf_core.utils import set_wd -from ..utils import GITLAB_URL, set_wd +from ..utils import GITLAB_URL from .patch import BISMARK_ALIGN, CORRECT_SHA, PATCH_BRANCH, REPO_NAME, modify_main_nf diff --git a/tests/test_test_utils.py b/tests/test_test_utils.py index c4e3d49ae0..154a31fca6 100644 --- a/tests/test_test_utils.py +++ b/tests/test_test_utils.py @@ -3,7 +3,7 @@ import pytest -from .utils import set_wd, with_temporary_file, with_temporary_folder +from .utils import with_temporary_file, with_temporary_folder def test_with_temporary_file(): @@ -30,20 +30,3 @@ def tmp_folder_exists(tmp_folder): 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/test_utils.py b/tests/test_utils.py index 2ab5b64bfc..56e83ef190 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -207,3 +207,20 @@ def test_validate_file_md5(): nf_core.utils.validate_file_md5(test_file, different_md5) with pytest.raises(ValueError): nf_core.utils.validate_file_md5(test_file, non_hex_string) + + +def test_set_wd(): + with tempfile.TemporaryDirectory() as tmpdirname: + with nf_core.utils.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 nf_core.utils.set_wd(tmpdirname): + raise Exception + assert wd_before_context == Path().resolve() diff --git a/tests/utils.py b/tests/utils.py index 307129b5b2..198ac3d583 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -59,24 +59,6 @@ def wrapper(*args: Any, **kwargs: Any) -> Any: return wrapper -@contextmanager -def set_wd(path: Path) -> Generator[None, None, None]: - """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(path).resolve()) - try: - yield - finally: - os.chdir(start_wd) - - def mock_anaconda_api_calls(rsps: responses.RequestsMock, module: str, version: str) -> None: """Mock anaconda api calls for module""" anaconda_api_url = f"https://api.anaconda.org/package/bioconda/{module}"