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 more pytest fixtures in modules tests #2226

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
105 changes: 105 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import os
import re

import pytest

import nf_core.modules

from .utils import (
GITLAB_BRANCH_TEST_BRANCH,
GITLAB_BRANCH_TEST_OLD_SHA,
GITLAB_DEFAULT_BRANCH,
GITLAB_URL,
OLD_TRIMGALORE_BRANCH,
OLD_TRIMGALORE_SHA,
)


@pytest.fixture
def tmp_dir(tmpdir):
return tmpdir


@pytest.fixture
def pipeline_dir(tmp_dir):
pipeline_dir = os.path.join(tmp_dir, "mypipeline")
nf_core.create.PipelineCreate(
"mypipeline", "it is mine", "me", no_git=True, outdir=pipeline_dir, plain=True
).init_pipeline()
return pipeline_dir


@pytest.fixture
def modules_repo_dummy(tmp_dir):
"""Create a dummy copy of the nf-core/modules repo"""

root_dir = os.path.join(tmp_dir, "modules")
os.makedirs(os.path.join(root_dir, "modules", "nf-core"))
os.makedirs(os.path.join(root_dir, "tests", "modules", "nf-core"))
os.makedirs(os.path.join(root_dir, "tests", "config"))
with open(os.path.join(root_dir, "tests", "config", "pytest_modules.yml"), "w") as fh:
fh.writelines(["test:", "\n - modules/test/**", "\n - tests/modules/test/**"])
with open(os.path.join(root_dir, ".nf-core.yml"), "w") as fh:
fh.writelines(["repository_type: modules", "\n", "org_path: nf-core", "\n"])

# mock biocontainers and anaconda response
module_create = nf_core.modules.ModuleCreate(root_dir, "bpipe/test", "@author", "process_single", False, False)
module_create.create()

return root_dir


@pytest.fixture
def local_modules_repo(modules_repo_dummy, tmp_dir, pipeline_dir):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will probably need to split it into separate fixtures. For the tests you have implemented we only need the modules repo, but for example to test installing modules we will need the installation objects.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. But normally I try to go a step as small as possible. Splitting it up will be a later step when it is needed.

"""Create a new PipelineSchema and Launch objects"""
component_type = "modules"

# Set up the schema
root_repo_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
template_dir = os.path.join(root_repo_dir, "nf_core", "pipeline-template")
# Set up install objects
mods_install = nf_core.modules.ModuleInstall(pipeline_dir, prompt=False, force=True)
mods_install_alt = nf_core.modules.ModuleInstall(pipeline_dir, prompt=True, force=True)
fabianegli marked this conversation as resolved.
Show resolved Hide resolved
mods_install_old = nf_core.modules.ModuleInstall(
pipeline_dir,
prompt=False,
force=False,
sha=OLD_TRIMGALORE_SHA,
remote_url=GITLAB_URL,
branch=OLD_TRIMGALORE_BRANCH,
)
mods_install_trimgalore = nf_core.modules.ModuleInstall(
pipeline_dir,
prompt=False,
force=True,
remote_url=GITLAB_URL,
branch=OLD_TRIMGALORE_BRANCH,
)
mods_install_gitlab = nf_core.modules.ModuleInstall(
pipeline_dir,
prompt=False,
force=True,
remote_url=GITLAB_URL,
branch=GITLAB_DEFAULT_BRANCH,
)
mods_install_gitlab_old = nf_core.modules.ModuleInstall(
pipeline_dir,
prompt=False,
force=True,
remote_url=GITLAB_URL,
branch=GITLAB_BRANCH_TEST_BRANCH,
sha=GITLAB_BRANCH_TEST_OLD_SHA,
)

# Set up remove objects
mods_remove = nf_core.modules.ModuleRemove(pipeline_dir)
mods_remove_gitlab = nf_core.modules.ModuleRemove(
pipeline_dir,
remote_url=GITLAB_URL,
branch=GITLAB_DEFAULT_BRANCH,
)

# Set up the nf-core/modules repo dummy
nfcore_modules = modules_repo_dummy

return nfcore_modules
Empty file added tests/test_modules/__init__.py
Empty file.
51 changes: 51 additions & 0 deletions tests/test_modules/test_bump_versions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import os
import re

import pytest

import nf_core.modules
from nf_core.modules.modules_utils import ModuleException


def test_modules_bump_versions_single_module(local_modules_repo):
"""Test updating a single module"""
# Change the bpipe/test version to an older version
print(local_modules_repo.__dir__())
fabianegli marked this conversation as resolved.
Show resolved Hide resolved
main_nf_path = os.path.join(local_modules_repo, "modules", "nf-core", "bpipe", "test", "main.nf")
with open(main_nf_path, "r") as fh:
content = fh.read()
new_content = re.sub(r"bioconda::star=\d.\d.\d\D?", r"bioconda::star=2.6.1d", content)
with open(main_nf_path, "w") as fh:
fh.write(new_content)
version_bumper = nf_core.modules.ModuleVersionBumper(pipeline_dir=local_modules_repo)
version_bumper.bump_versions(module="bpipe/test")
assert len(version_bumper.failed) == 0


def test_modules_bump_versions_all_modules(local_modules_repo):
"""Test updating all modules"""
version_bumper = nf_core.modules.ModuleVersionBumper(pipeline_dir=local_modules_repo)
version_bumper.bump_versions(all_modules=True)
assert len(version_bumper.failed) == 0


def test_modules_bump_versions_fail(local_modules_repo):
"""Fail updating a module with wrong name"""
version_bumper = nf_core.modules.ModuleVersionBumper(pipeline_dir=local_modules_repo)
with pytest.raises(ModuleException) as excinfo:
version_bumper.bump_versions(module="no/module")
assert "Could not find the specified module:" in str(excinfo.value)


def test_modules_bump_versions_fail_unknown_version(local_modules_repo):
"""Fail because of an unknown version"""
# Change the bpipe/test version to an older version
main_nf_path = os.path.join(local_modules_repo, "modules", "nf-core", "bpipe", "test", "main.nf")
with open(main_nf_path, "r") as fh:
content = fh.read()
new_content = re.sub(r"bioconda::bpipe=\d.\d.\d\D?", r"bioconda::bpipe=xxx", content)
with open(main_nf_path, "w") as fh:
fh.write(new_content)
version_bumper = nf_core.modules.ModuleVersionBumper(pipeline_dir=local_modules_repo)
version_bumper.bump_versions(module="bpipe/test")
assert "Conda package had unknown version" in version_bumper.failed[0][0]
66 changes: 66 additions & 0 deletions tests/test_modules/test_create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import os

import pytest
import requests_cache
import responses

import nf_core.modules

from ..utils import mock_anaconda_api_calls, mock_biocontainers_api_calls


def test_modules_create_succeed(pipeline_dir):
"""Succeed at creating the TrimGalore! module"""
with responses.RequestsMock() as rsps:
mock_anaconda_api_calls(rsps, "trim-galore", "0.6.7")
mock_biocontainers_api_calls(rsps, "trim-galore", "0.6.7")
module_create = nf_core.modules.ModuleCreate(
pipeline_dir, "trimgalore", "@author", "process_single", True, True, conda_name="trim-galore"
)
with requests_cache.disabled():
module_create.create()
assert os.path.exists(os.path.join(pipeline_dir, "modules", "local", "trimgalore.nf"))


def test_modules_create_fail_exists(pipeline_dir):
"""Fail at creating the same module twice"""
with responses.RequestsMock() as rsps:
mock_anaconda_api_calls(rsps, "trim-galore", "0.6.7")
mock_biocontainers_api_calls(rsps, "trim-galore", "0.6.7")
module_create = nf_core.modules.ModuleCreate(
pipeline_dir, "trimgalore", "@author", "process_single", False, False, conda_name="trim-galore"
)
with requests_cache.disabled():
module_create.create()
with pytest.raises(UserWarning) as excinfo:
with requests_cache.disabled():
module_create.create()
assert "Module file exists already" in str(excinfo.value)


def test_modules_create_nfcore_modules(local_modules_repo):
"""Create a module in nf-core/modules clone"""
with responses.RequestsMock() as rsps:
mock_anaconda_api_calls(rsps, "fastqc", "0.11.9")
mock_biocontainers_api_calls(rsps, "fastqc", "0.11.9")
module_create = nf_core.modules.ModuleCreate(
local_modules_repo, "fastqc", "@author", "process_low", False, False
)
with requests_cache.disabled():
module_create.create()
assert os.path.exists(os.path.join(local_modules_repo, "modules", "nf-core", "fastqc", "main.nf"))
assert os.path.exists(os.path.join(local_modules_repo, "tests", "modules", "nf-core", "fastqc", "main.nf"))


def test_modules_create_nfcore_modules_subtool(local_modules_repo):
"""Create a tool/subtool module in a nf-core/modules clone"""
with responses.RequestsMock() as rsps:
mock_anaconda_api_calls(rsps, "star", "2.8.10a")
mock_biocontainers_api_calls(rsps, "star", "2.8.10a")
module_create = nf_core.modules.ModuleCreate(
local_modules_repo, "star/index", "@author", "process_medium", False, False
)
with requests_cache.disabled():
module_create.create()
assert os.path.exists(os.path.join(local_modules_repo, "modules", "nf-core", "star", "index", "main.nf"))
assert os.path.exists(os.path.join(local_modules_repo, "tests", "modules", "nf-core", "star", "index", "main.nf"))
File renamed without changes.