-
Notifications
You must be signed in to change notification settings - Fork 192
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
add new command modules test #1526
Merged
Merged
Changes from 9 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
54d218f
add new command modules test
5097f04
run tests with pytest
3227499
remove test workflow check
2e2ea58
add code tests
e763b96
Merge branch 'dev' into modules_test
mirpedrol c0b29f5
fix linting
99b5170
apply comments from PR review
57c7541
Merge branch 'modules_test' of https://github.com/asthara10/tools int…
b80fd62
fix linting
6c006fd
apply comments from PR
0e4a835
fix test
a808853
always set profile
4165a9f
do not abbreviate env var
b431f98
add new test
58df639
update CHANGELOG
530a383
specify test files as test_*.py in pytest.ini
b885e5f
apply comments from PR review
f49af39
check if profile is available
f33f1fb
modify linting
b732386
Add nf-core/modules folder warning
mirpedrol c182ef3
Add valid profile check
mirpedrol 69247e7
fix linting
1d8e567
avoid using shell=True when checking profile
8e33499
Apply suggestions from code review
mirpedrol aca376d
remove unnecessary libraryes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#!/usr/bin/env python | ||
""" | ||
The ModulesTest class runs the tests locally | ||
""" | ||
|
||
import logging | ||
import questionary | ||
import os | ||
import pytest | ||
import sys | ||
import rich | ||
|
||
import nf_core.utils | ||
from .modules_repo import ModulesRepo | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class ModulesTest(object): | ||
""" | ||
Class to run module pytests. | ||
|
||
... | ||
|
||
Attributes | ||
---------- | ||
module_name : str | ||
name of the tool to run tests for | ||
no_prompts : bool | ||
flat indicating if prompts are used | ||
pytest_args : tuple | ||
additional arguments passed to pytest command | ||
|
||
Methods | ||
------- | ||
run(): | ||
Run test steps | ||
__check_inputs(): | ||
mirpedrol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Check inputs. Ask for module_name if not provided and check that the directory exists | ||
__set_profile(): | ||
Set software profile | ||
__run_pytests(self): | ||
Run pytest | ||
""" | ||
|
||
def __init__( | ||
self, | ||
module_name=None, | ||
fabianegli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
no_prompts=False, | ||
pytest_args="", | ||
): | ||
mirpedrol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.module_name = module_name | ||
self.no_prompts = no_prompts | ||
self.pytest_args = pytest_args | ||
self.module_dir = None | ||
|
||
ggabernet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def run(self): | ||
"""Run test steps""" | ||
if not self.no_prompts: | ||
log.info( | ||
"[yellow]Press enter to use default values [cyan bold](shown in brackets) [yellow]or type your own responses" | ||
) | ||
self.__check_inputs() | ||
self.__set_profile() | ||
self.__run_pytests() | ||
|
||
def __check_inputs(self): | ||
"""Do more complex checks about supplied flags.""" | ||
|
||
# Get the tool name if not specified | ||
if self.module_name is None: | ||
if self.no_prompts: | ||
raise UserWarning( | ||
f"Tool name not provided and prompts deactivated. Please provide the tool name as TOOL/SUBTOOL or TOOL." | ||
) | ||
modules_repo = ModulesRepo() | ||
modules_repo.get_modules_file_tree() | ||
self.module_name = questionary.autocomplete( | ||
mirpedrol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"Tool name:", | ||
choices=modules_repo.modules_avail_module_names, | ||
style=nf_core.utils.nfcore_question_style, | ||
).ask() | ||
self.module_dir = os.path.join("modules", *self.module_name.split("/")) | ||
mirpedrol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# First, sanity check that the module directory exists | ||
if not os.path.isdir(self.module_dir): | ||
raise UserWarning(f"Cannot find directory '{self.module_dir}'. Should be TOOL/SUBTOOL or TOOL") | ||
|
||
def __set_profile(self): | ||
"""Set $PROFILE env variable. | ||
The config expects $PROFILE and Nextflow fails if it's not set. | ||
""" | ||
if os.environ.get("PROFILE") is None: | ||
os.environ["PROFILE"] = "" | ||
if self.no_prompts: | ||
log.info( | ||
"Setting env var '$PROFILE' to an empty string as not set.\n" | ||
mirpedrol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"Tests will run with Docker by default. " | ||
"To use Singularity set 'export PROFILE=singularity' in your shell before running this command." | ||
) | ||
else: | ||
question = { | ||
"type": "list", | ||
"name": "profile", | ||
"message": "Choose software profile", | ||
"choices": ["Docker", "Singularity", "Conda"], | ||
mirpedrol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
answer = questionary.unsafe_prompt([question], style=nf_core.utils.nfcore_question_style) | ||
profile = answer["profile"].lower() | ||
if profile in ["singularity", "conda"]: | ||
mirpedrol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
os.environ["PROFILE"] = profile | ||
log.info(f"Setting env var '$PROFILE' to '{profile}'") | ||
|
||
def __run_pytests(self): | ||
"""Given a module name, run tests.""" | ||
# Print nice divider line | ||
console = rich.console.Console() | ||
console.print("[black]" + "─" * console.width) | ||
mirpedrol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Set pytest arguments | ||
command_args = ["--tag", f"{self.module_name}", "--symlink", "--keep-workflow-wd"] | ||
mirpedrol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
command_args += self.pytest_args | ||
|
||
# Run pytest | ||
log.info(f"Running pytest for module '{self.module_name}'") | ||
sys.exit(pytest.main(command_args)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"""Test the 'modules test' command which runs module pytests.""" | ||
import os | ||
import pytest | ||
|
||
import nf_core.modules | ||
|
||
|
||
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) | ||
assert "Cannot find directory" in str(excinfo.value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm being picky, these are for the
__init__()
def really, so you could probably move them there. Unless this is some kind of Pythonic way of documenting stuff that I'm not aware of. Low prio though.