| 1 |
from inspect import signature |
| 2 |
import pytest |
| 3 |
from autocommand.autoparse import make_parser |
| 4 |
|
| 5 |
|
| 6 |
# TODO: This doesn't really need to be a fixture; replace it with a normal |
| 7 |
# helper |
| 8 |
@pytest.fixture(scope='session') |
| 9 |
def check_parse(): |
| 10 |
def check_parse_impl(function, *args, add_nos=False, **kwargs): |
| 11 |
''' |
| 12 |
Helper for generalized parser testing. This function takes a function, |
| 13 |
a set of positional arguments, and a set of keyword argumnets. It |
| 14 |
creates an argparse parser using `autocommand.autoparse:make_parser` on |
| 15 |
the signature of the provided function. It then parses the positional |
| 16 |
arguments using this parser, and asserts that the returned set of |
| 17 |
parsed arguments matches the given keyword arguments exactly. |
| 18 |
|
| 19 |
Arguments: |
| 20 |
- function: The function to generate a parser for |
| 21 |
- *args: The set of positional arguments to pass to the generated |
| 22 |
parser |
| 23 |
- add_nos: If True, "-no-" versions of the option flags will be |
| 24 |
created, |
| 25 |
as per the `autoparse` docs. |
| 26 |
- **kwargs: The set of parsed argument values to check for. |
| 27 |
''' |
| 28 |
parser = make_parser( |
| 29 |
func_sig=signature(function), |
| 30 |
description=None, |
| 31 |
epilog=None, |
| 32 |
add_nos=add_nos) |
| 33 |
|
| 34 |
parsed_args = vars(parser.parse_args(args)) |
| 35 |
assert parsed_args == kwargs |
| 36 |
|
| 37 |
return check_parse_impl |
| 38 |
|
| 39 |
|
| 40 |
@pytest.fixture |
| 41 |
def check_help_text(capsys): |
| 42 |
def check_help_text_impl(func, *texts, reject=()): |
| 43 |
''' |
| 44 |
This helper checks that some set of text is written to stdout or stderr |
| 45 |
after the called function raises a SystemExit. It is used to test that |
| 46 |
the underlying ArgumentParser was correctly configured to output a |
| 47 |
given set of help text(s). |
| 48 |
|
| 49 |
func: This should be a wrapped autoparse function that causes a |
| 50 |
SystemExit exception to be raised (most commonly a function with the |
| 51 |
-h flag, or with an invalid set of positional arguments). This |
| 52 |
Exception should be accompanied by some kind of printout from |
| 53 |
argparse to stderr or stdout. |
| 54 |
*texts: A set of strings to test for. All of the provided strings will |
| 55 |
be checked for in the captured stdout/stderr using a standard |
| 56 |
substring search. |
| 57 |
reject: A string or set of strings to check do NOT exist anywhere in |
| 58 |
the output. Currently used as a cludge to test the docstring split |
| 59 |
behavior. |
| 60 |
''' |
| 61 |
with pytest.raises(SystemExit): |
| 62 |
func() |
| 63 |
|
| 64 |
out, err = capsys.readouterr() |
| 65 |
|
| 66 |
# TODO: be wary of argparse's text formatting |
| 67 |
# TODO: test that the text appears in the order given |
| 68 |
for text in texts: |
| 69 |
assert text in out or text in err |
| 70 |
|
| 71 |
if isinstance(reject, str): |
| 72 |
reject = [reject] |
| 73 |
|
| 74 |
for text in reject: |
| 75 |
assert text not in out and text not in err |
| 76 |
|
| 77 |
return check_help_text_impl |