Skip to content

Commit

Permalink
Merge pull request nf-core#2529 from mashehu/mv-set_wd
Browse files Browse the repository at this point in the history
move set_wd from tests/utils.py to utils.py
  • Loading branch information
mashehu authored Nov 23, 2023
2 parents 417533d + e6d6f2c commit b51eba2
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 43 deletions.
3 changes: 1 addition & 2 deletions nf_core/components/components_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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)
Expand Down
21 changes: 20 additions & 1 deletion nf_core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
3 changes: 2 additions & 1 deletion tests/components/generate_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions tests/components/snapshot_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion tests/modules/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
19 changes: 1 addition & 18 deletions tests/test_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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()
17 changes: 17 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
18 changes: 0 additions & 18 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down

0 comments on commit b51eba2

Please sign in to comment.