From 86c76005d60282ee539c367aac5735820512edd8 Mon Sep 17 00:00:00 2001 From: Nils Homer Date: Tue, 11 Apr 2023 15:05:52 -0700 Subject: [PATCH] add tools test --- README.md | 14 ++++----- src/python/pyclient/tools/tests/__init__.py | 18 ++++++++++++ src/python/pyclient/tools/tests/test_main.py | 31 ++++++++++++++++++++ 3 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 src/python/pyclient/tools/tests/__init__.py create mode 100644 src/python/pyclient/tools/tests/test_main.py diff --git a/README.md b/README.md index a71b63f..40006f9 100644 --- a/README.md +++ b/README.md @@ -55,16 +55,10 @@ mamba create -n pyclient \ conda activate pyclient ``` -- Install all non-conda dependencies via pip - -```bash -pip install -r pip-requirements.txt -``` - - Install `pyclient` (in developer mode) ```bash -python setup.py develop + pip install --editable . ``` - Validate the install via the help message @@ -79,6 +73,12 @@ client-tools -h snakemake --snakefile src/snakemake/hello_world.smk -j 1 ``` +- Run the precommit script (unit tests, linting, code-style, type checking) + +```bash +bash scripts/precommit.sh +``` + [fulcrum-genomics-link]: https://www.fulcrumgenomics.com [conda-link]: https://docs.conda.io/projects/conda/en/latest/user-guide/install/ diff --git a/src/python/pyclient/tools/tests/__init__.py b/src/python/pyclient/tools/tests/__init__.py new file mode 100644 index 0000000..ea4d9e6 --- /dev/null +++ b/src/python/pyclient/tools/tests/__init__.py @@ -0,0 +1,18 @@ +"""Testing utilities for :module:`~pyclient.tools`""" + +import pytest + + +def _to_name(tool) -> str: # type: ignore + """Gives the tool name for a function by taking the function name and replacing + underscores with hyphens.""" + return tool.__name__.replace("_", "-") + + +def test_tool_funcs(tool, main) -> None: # type: ignore + name = _to_name(tool) + argv = [name, "-h"] + with pytest.raises(SystemExit) as e: + main(argv=argv) + assert e.type == SystemExit + assert e.value.code == 0 # code should be 0 for help diff --git a/src/python/pyclient/tools/tests/test_main.py b/src/python/pyclient/tools/tests/test_main.py new file mode 100644 index 0000000..d9c0578 --- /dev/null +++ b/src/python/pyclient/tools/tests/test_main.py @@ -0,0 +1,31 @@ +"""Tests for :module:`~pyclient.tools.__main__ +Motivation +~~~~~~~~~~ +The idea is to run help on the main tools method (`-h`) as well as on +each tool (ex. `tool-name -h`). This should force :module:`~defopt` +to parse the docstring for the method. Since :module:`~defopt` uses +:module:`~argparse` underneath, `SystemExit`s are raised, which are +different than regular `Exceptions`. The exit code returned by help +(the usage) is 0. An improperly formatted docstring throws a +:class:`NotImplementedException`. We add a few tests below to show +this. +""" + +import pytest +from pyclient.tools.__main__ import TOOLS +from pyclient.tools.__main__ import main +from pyclient.tools.tests import test_tool_funcs as _test_tool_funcs + + +def test_tools_help() -> None: + """Tests that running pyclient-tools with -h exits OK""" + argv = ["-h"] + with pytest.raises(SystemExit) as e: + main(argv=argv) + assert e.type == SystemExit + assert e.value.code == 0 # code should be 0 for help + + +@pytest.mark.parametrize("tool", TOOLS) +def test_tool_funcs(tool) -> None: # type: ignore + _test_tool_funcs(tool, main)