From a5d24cf48e5e04af0f6f5fcf8de95aaad24cc0ab Mon Sep 17 00:00:00 2001 From: Joris van Vugt Date: Mon, 9 Sep 2019 22:48:27 +0200 Subject: [PATCH] Add test script and pass tests --- auto_cli.py | 1 - auto_cli/cli.py | 4 ++-- auto_cli/parsing.py | 7 +++---- auto_cli/tests/test_parsing.py | 14 +++++++------- script/test | 17 +++++++++++++++++ setup.py | 1 - 6 files changed, 29 insertions(+), 15 deletions(-) create mode 100755 script/test diff --git a/auto_cli.py b/auto_cli.py index 60c17f9..8d91fd1 100644 --- a/auto_cli.py +++ b/auto_cli.py @@ -1,5 +1,4 @@ import auto_cli - auto_cli.register_command("auto_cli.cli.apps") auto_cli.register_command("auto_cli.cli.register_app") diff --git a/auto_cli/cli.py b/auto_cli/cli.py index 84fe063..660610e 100644 --- a/auto_cli/cli.py +++ b/auto_cli/cli.py @@ -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 @@ -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 "" diff --git a/auto_cli/parsing.py b/auto_cli/parsing.py index c827dba..f3a8f91 100644 --- a/auto_cli/parsing.py +++ b/auto_cli/parsing.py @@ -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 @@ -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 diff --git a/auto_cli/tests/test_parsing.py b/auto_cli/tests/test_parsing.py index 414f43d..af9dea3 100644 --- a/auto_cli/tests/test_parsing.py +++ b/auto_cli/tests/test_parsing.py @@ -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"} @@ -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} @@ -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} @@ -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} @@ -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} diff --git a/script/test b/script/test new file mode 100755 index 0000000..efd3834 --- /dev/null +++ b/script/test @@ -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" \ No newline at end of file diff --git a/setup.py b/setup.py index c2dca4b..75f280f 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,5 @@ from setuptools import setup - setup( name="auto_cli", packages=[],