Skip to content

Commit

Permalink
fix: handle tests passing sys.argv
Browse files Browse the repository at this point in the history
  • Loading branch information
jnoortheen committed Apr 13, 2020
1 parent 09c0c63 commit 9226d97
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
7 changes: 5 additions & 2 deletions arger/decorator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from argparse import ArgumentParser
from typing import Any, Callable, Dict, Optional

Expand Down Expand Up @@ -45,7 +46,7 @@ def __init__(self, fn: Optional[F] = None, **kwargs):
if fn: # lazily add arguments
_add_args(self, self._command.args)

def run(self, *args) -> Any:
def run(self, *args, capture_sys=True) -> Any:
"""Dispatch functions.
The arguments will be passed onto self.parse_args
Expand All @@ -57,7 +58,9 @@ def run(self, *args) -> Any:
# populate sub-parsers
_add_parsers(self, self._command)

kwargs = vars(self.parse_args(args or None)) # type: Dict[str, Any]
if not args and capture_sys:
args = tuple(sys.argv[:1])
kwargs = vars(self.parse_args(args)) # type: Dict[str, Any]

return self._command.run(**kwargs)

Expand Down
12 changes: 3 additions & 9 deletions tests/test_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,13 @@ def args(cmd: str):
def test_example(capsys, arger, args, expected: str):
if "error" in expected or expected.startswith('usage:'):
with pytest.raises(SystemExit):
arger.run(*args) # start function
arger.run(*args, capture_sys=False) # start function
else:
arger.run(*args)
arger.run(*args, capture_sys=False)

capture = capsys.readouterr()
out = capture.err or capture.out

if out != expected:
import time

tmp = Path(__file__).with_name('-'.join(args) + str(time.time()) + ".log")
tmp.write_text("\n\n\n".join([out, expected]))
assert out == expected
assert out == expected, f"\ncmd: {args}\nout: \n{out}\nexpected:\n{expected}\n"


# def test_var_positional_help(capsys, arg):
Expand Down
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ python =
3.8: py38

[testenv]
whitelist_externals = poetry
whitelist_externals =
poetry
make
skip_install = true
commands =
poetry install
Expand Down

0 comments on commit 9226d97

Please sign in to comment.