Skip to content

Commit 0bd4ff0

Browse files
committed
Overhaul testing.
- Refactor to cleanup test_planemo, introduce higher-level concepts in new CliTestCase. - Add integration test for tool initialization and linting. - Bug fixes for tool linting overcovered with these commands. - Add integration test for initializing demo project and testing (cat.xml). - Add DOI to tool init (so we can have a lintable passing tool from init to test). - Add test for --help at root command. - Add test for --version at root command. - Update dev-requirements to include nose and coverage. - Add coveralls to travis testing.
1 parent 51896f5 commit 0bd4ff0

File tree

9 files changed

+176
-24
lines changed

9 files changed

+176
-24
lines changed

.travis.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ python:
1111

1212
install:
1313
- if [[ $TRAVIS_PYTHON_VERSION == 2.6 ]]; then pip install unittest2; fi
14+
- pip install coveralls --use-mirrors # Required for coveralls reporting.
1415
- pip install -r requirements.txt
15-
- pip install pyflakes flake8 readme
16+
- pip install pyflakes flake8 readme coverage
17+
18+
after_success:
19+
- coveralls
1620

1721
# command to run tests, e.g. python setup.py test
1822
script: make lint && make test && if [[ $TRAVIS_PYTHON_VERSION == 2.6 ]]; then true; else make lint-readme; fi

dev-requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# For testing
2+
nose
3+
coverage
4+
15
# For dev
26
sphinx
37

planemo/commands/cmd_tool_init.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,15 @@
152152
prompt=False,
153153
help="Add a Docker image identifier for this tool."
154154
)
155+
@click.option(
156+
"--doi",
157+
type=click.STRING,
158+
default=None,
159+
multiple=True,
160+
prompt=False,
161+
help=("Supply a DOI (http://www.doi.org/) easing citation of the tool "
162+
"for Galxy users (e.g. 10.1101/014043).")
163+
)
155164
@click.option(
156165
"--test_case",
157166
is_flag=True,

planemo/tool_builder.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@
6262
TODO: Fill in help.
6363
{%- endif %}
6464
]]></help>
65+
{%- if doi %}
66+
<citations>
67+
{%- for single_doi in doi %}
68+
<citation type="doi">{{ single_doi }}</citation>
69+
{%- endfor %}
70+
</citations>
71+
{%- endif %}
6572
</tool>
6673
"""
6774

planemo_ext/galaxy/tools/lint.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ def lint_xml(tool_xml, level=LEVEL_ALL, fail_level=LEVEL_WARN):
1717
lint_context.lint(module, name, value, tool_xml)
1818
found_warns = lint_context.found_warns
1919
found_errors = lint_context.found_errors
20-
if level == LEVEL_WARN and (found_warns or found_errors):
21-
return False
20+
if fail_level == LEVEL_WARN:
21+
lint_fail = (found_warns or found_errors)
2222
else:
23-
return found_errors
23+
lint_fail = found_errors
24+
return not lint_fail
2425

2526

2627
class LintContext(object):

tests/test_build_and_lint.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
from .test_utils import CliTestCase
3+
4+
5+
INIT_COMMAND = [
6+
"tool_init", "--force",
7+
"--id", "seqtk_seq",
8+
"--name", "Convert to FASTA (seqtk)",
9+
"--requirement", "seqtk@1.0-r68",
10+
"--example_command", "seqtk seq -a 2.fastq > 2.fasta",
11+
"--example_input", "2.fastq",
12+
"--example_output", "2.fasta",
13+
"--test_case",
14+
"--help_text", "The help text.",
15+
"--doi", "10.1101/014043"
16+
]
17+
18+
19+
class BuildAndLintTestCase(CliTestCase):
20+
21+
def test_build_and_lint(self):
22+
with self._isolate():
23+
self._check_exit_code(_init_command())
24+
self._check_lint(exit_code=0)
25+
26+
def test_lint_fails_if_no_help(self):
27+
with self._isolate():
28+
self._check_exit_code(_init_command(help_text=False))
29+
self._check_lint(exit_code=1)
30+
31+
def test_lint_fails_if_no_test(self):
32+
with self._isolate():
33+
self._check_exit_code(_init_command(test_case=False))
34+
self._check_lint(exit_code=1)
35+
36+
def test_lint_fails_if_no_doi(self):
37+
with self._isolate():
38+
self._check_exit_code(_init_command(doi=False))
39+
self._check_lint(exit_code=1)
40+
41+
def _check_lint(self, exit_code=0):
42+
lint_cmd = ["lint", "--fail_level", "warn", "seqtk_seq.xml"]
43+
self._check_exit_code(lint_cmd, exit_code=exit_code)
44+
45+
46+
def _init_command(test_case=True, help_text=True, doi=True):
47+
command = [
48+
"tool_init", "--force",
49+
"--id", "seqtk_seq",
50+
"--name", "Convert to FASTA (seqtk)",
51+
"--requirement", "seqtk@1.0-r68",
52+
"--example_command", "seqtk seq -a 2.fastq > 2.fasta",
53+
"--example_input", "2.fastq",
54+
"--example_output", "2.fasta"
55+
]
56+
if test_case:
57+
command.append("--test_case")
58+
if help_text:
59+
command.extend(["--help_text", "The help text."])
60+
if doi:
61+
command.extend(["--doi", "10.1101/014043"])
62+
return command

tests/test_init_and_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from .test_utils import CliTestCase
2+
3+
4+
class InitAndTestTestCase(CliTestCase):
5+
6+
def test_init_and_test(self):
7+
with self._isolate():
8+
init_cmd = ["project_init", "--template", "demo", "basic"]
9+
self._check_exit_code(init_cmd)
10+
test_cmd = ["test", "--install_galaxy", "basic/cat.xml"]
11+
self._check_exit_code(test_cmd)

tests/test_planemo.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,50 @@
88
Tests for `planemo` module.
99
"""
1010

11-
from click.testing import CliRunner
11+
from .test_utils import CliTestCase
12+
from .test_utils import main
1213

13-
import unittest
1414

15-
from planemo import cli
16-
17-
18-
# More information on testing click applications at following link.
19-
# http://click.pocoo.org/3/testing/#basic-testing
20-
class TestPlanemo(unittest.TestCase):
15+
class TestPlanemo(CliTestCase):
2116

2217
def test_commands_have_help(self):
23-
commands = cli.list_cmds()
24-
runner = CliRunner()
18+
commands = self._cli.list_cmds()
2519
for command in commands:
26-
planemo_cli = cli.planemo
27-
result = runner.invoke(planemo_cli, [command, "--help"])
28-
if result.exit_code != 0:
29-
message = "Planemo command %s has invalid --help." % command
30-
raise AssertionError(message)
20+
self._check_exit_code([command, "--help"])
3121

3222
def test_responds_to_desired_commands(self):
33-
commands = cli.list_cmds()
23+
commands = self._cli.list_cmds()
3424

3525
def assert_responds_to(command):
3626
assert command in commands, "No command %s" % command
3727

38-
assert_responds_to("docker_shell")
39-
assert_responds_to("docker_build")
4028
assert_responds_to("brew_init")
4129
assert_responds_to("brew")
4230
assert_responds_to("brew_env")
4331
assert_responds_to("config_init")
32+
assert_responds_to("create_gist")
33+
assert_responds_to("docker_build")
34+
assert_responds_to("docker_shell")
4435
assert_responds_to("lint")
36+
assert_responds_to("normalize")
4537
assert_responds_to("project_init")
46-
assert_responds_to("shed_upload")
4738
assert_responds_to("serve")
39+
assert_responds_to("shed_diff")
40+
assert_responds_to("shed_download")
41+
assert_responds_to("shed_upload")
4842
assert_responds_to("syntax")
4943
assert_responds_to("test")
44+
assert_responds_to("tool_factory")
45+
assert_responds_to("tool_init")
5046
assert_responds_to("travis_before_install")
5147
assert_responds_to("travis_init")
5248

49+
def test_planemo_version_command(self):
50+
self._check_exit_code(["--version"])
51+
52+
def test_planemo_help_command(self):
53+
self._check_exit_code(["--help"])
54+
5355

5456
if __name__ == '__main__':
55-
unittest.main()
57+
main()

tests/test_utils.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
""" Provide abstractions over click testing of the
2+
app and unittest.
3+
"""
4+
from click.testing import CliRunner
5+
6+
from unittest import (
7+
TestCase,
8+
main,
9+
)
10+
from planemo import cli
11+
12+
EXIT_CODE_MESSAGE = ("Planemo command [%s] resulted in unexpected exit code "
13+
"[%s], expected exit code [%s]]. Command output [%s]")
14+
15+
16+
# More information on testing click applications at following link.
17+
# http://click.pocoo.org/3/testing/#basic-testing
18+
class CliTestCase(TestCase):
19+
20+
def setUp(self): # noqa
21+
self._runner = CliRunner()
22+
23+
@property
24+
def _cli(self):
25+
return cli
26+
27+
def _isolate(self):
28+
return self._runner.isolated_filesystem()
29+
30+
def _invoke(self, command_list):
31+
planemo_cli = self._cli.planemo
32+
return self._runner.invoke(planemo_cli, command_list)
33+
34+
def _check_exit_code(self, command_list, exit_code=0):
35+
expected_exit_code = exit_code
36+
result = self._invoke(command_list)
37+
result_exit_code = result.exit_code
38+
if result_exit_code != expected_exit_code:
39+
message = EXIT_CODE_MESSAGE % (
40+
" ".join(command_list),
41+
result_exit_code,
42+
expected_exit_code,
43+
result.output,
44+
)
45+
raise AssertionError(message)
46+
47+
48+
__all__ = [
49+
"TestCase",
50+
"CliTestCase",
51+
"main",
52+
]

0 commit comments

Comments
 (0)