Skip to content

Commit

Permalink
Add test script and pass tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jvanvugt committed Sep 9, 2019
1 parent 77a66b3 commit a5d24cf
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 15 deletions.
1 change: 0 additions & 1 deletion auto_cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import auto_cli


auto_cli.register_command("auto_cli.cli.apps")
auto_cli.register_command("auto_cli.cli.register_app")
4 changes: 2 additions & 2 deletions auto_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def run_func_with_argv(
function: Callable[..., ReturnType], argv: List[str]
) -> ReturnType:
parser = create_parser(function)
args = parser.parse_args(argv)
args = parser.parse(argv)
retval = function(**args)
return retval

Expand Down Expand Up @@ -98,7 +98,7 @@ def _command_help() -> str:
)


def _function_help(function) -> str:
def _function_help(function: Callable) -> str:
if function.__doc__ is not None:
return " " + function.__doc__
return ""
7 changes: 3 additions & 4 deletions auto_cli/parsing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import argparse
import inspect
from typing import Any, Callable, Dict, List, Tuple, Union
from typing import Any, Callable, Dict, List, Optional, Tuple, Union

from .utils import _print_and_quit

Expand All @@ -10,9 +10,8 @@ def __init__(self, *args: Any, **kwargs: Any):
super().__init__(*args, **kwargs)
self.post_processing: Dict[str, Callable] = {}

def parse_args(self, *args: Any, **kwargs: Any) -> Dict[str, Any]:
args = super().parse_args(*args, **kwargs)
args_as_dict = vars(args)
def parse(self, args: Optional[List[str]] = None) -> Dict[str, Any]:
args_as_dict = vars(super().parse_args(args))
for param_name, transform_func in self.post_processing.items():
args_as_dict[param_name] = transform_func(args_as_dict[param_name])
return args_as_dict
Expand Down
14 changes: 7 additions & 7 deletions auto_cli/tests/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def func_to_test(a: int, b: str) -> int:
return a + len(b)

parser = create_parser(func_to_test)
args = parser.parse_args(["--a", "42", "--b", "1234"])
args = parser.parse(["--a", "42", "--b", "1234"])
assert args == {"a": 42, "b": "1234"}


Expand All @@ -17,10 +17,10 @@ def func_to_test(a: int, b: int = 38) -> int:
return a + b

parser = create_parser(func_to_test)
args_no_default = parser.parse_args(["--a", "1", "--b", "42"])
args_no_default = parser.parse(["--a", "1", "--b", "42"])
assert args_no_default == {"a": 1, "b": 42}

args_with_default = parser.parse_args(["--a", "1"])
args_with_default = parser.parse(["--a", "1"])
assert args_with_default == {"a": 1, "b": 38}


Expand All @@ -29,10 +29,10 @@ def func_to_test(a: bool) -> bool:
return a

parser = create_parser(func_to_test)
args_with_flag = parser.parse_args(["--a"])
args_with_flag = parser.parse(["--a"])
assert args_with_flag == {"a": True}

args_without_flag = parser.parse_args([])
args_without_flag = parser.parse([])
assert args_without_flag == {"a": False}


Expand All @@ -42,7 +42,7 @@ def func_to_test(a: List[int]) -> int:

parser = create_parser(func_to_test)
nums = [1, 3, 5, 7]
args = parser.parse_args(["--a"] + list(map(str, nums)))
args = parser.parse(["--a"] + list(map(str, nums)))

assert args == {"a": nums}

Expand All @@ -53,6 +53,6 @@ def func_to_test(a: Tuple[int, int], b: bool) -> int:

parser = create_parser(func_to_test)
nums = (42, 1337)
args = parser.parse_args(["--a"] + list(map(str, nums)))
args = parser.parse(["--a"] + list(map(str, nums)))

assert args == {"a": nums, "b": False}
17 changes: 17 additions & 0 deletions script/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

# script/test: Run test suite for application.

set -e

cd "$(dirname "$0")/.."

# Formatting
black --check auto_cli
isort -c
# Type checking
mypy auto_cli --disallow-untyped-defs --warn-unreachable --warn-redundant-casts --warn-unused-ignores
# Tests
pytest -s auto_cli

echo "Done! All tests passed"
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from setuptools import setup


setup(
name="auto_cli",
packages=[],
Expand Down

0 comments on commit a5d24cf

Please sign in to comment.