Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a context to temporarily change the working directory #1819

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 4 additions & 6 deletions tests/modules/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down
40 changes: 18 additions & 22 deletions tests/modules/module_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
50 changes: 50 additions & 0 deletions tests/test_test_utils.py
Original file line number Diff line number Diff line change
@@ -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()
21 changes: 21 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)