From b2fbb532e359985142a71354b5b648ae560a80ac Mon Sep 17 00:00:00 2001 From: David Luevano Alvarado Date: Sun, 19 Feb 2023 04:33:46 -0600 Subject: add final arg parser tests, refactor pyssg error msgs --- src/pyssg/pyssg.py | 8 ++++---- tests/conftest.py | 4 ++-- tests/test_arg_parser.py | 39 +++++++++++++++++++++++++++++++++------ 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/pyssg/pyssg.py b/src/pyssg/pyssg.py index 678000b..9b46a66 100644 --- a/src/pyssg/pyssg.py +++ b/src/pyssg/pyssg.py @@ -24,17 +24,17 @@ def main() -> None: arg_parser.print_usage() # even if it's an error, print it as info # as it is not critical, only config related - log.info(message) + log.info(f'pyssg: error: {message}, --help for more') sys.exit(1) # -1 as first argument is program path num_args = len(sys.argv) - 1 if num_args == 2 and args['config']: - _log_perror('pyssg: error: only config argument passed, --help for more') + _log_perror('only config argument passed') elif not num_args > 0 or (num_args == 1 and args['debug']): - _log_perror('pyssg: error: no arguments passed, --help for more') + _log_perror('no arguments passed') elif num_args == 3 and (args['debug'] and args['config']): - _log_perror("pyssg: error: no arguments passed other than 'debug' and 'config', --help for more") + _log_perror('no arguments passed other than "debug" and "config"') if args['version']: log.info('pyssg v%s', VERSION) diff --git a/tests/conftest.py b/tests/conftest.py index 9368432..fcf4189 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,6 +2,6 @@ import pytest from pyssg.arg_parser import get_parser -@pytest.fixture +@pytest.fixture(scope='session') def arg_parser(): - return get_parser() \ No newline at end of file + return get_parser() diff --git a/tests/test_arg_parser.py b/tests/test_arg_parser.py index 45263ce..1a5b48c 100644 --- a/tests/test_arg_parser.py +++ b/tests/test_arg_parser.py @@ -1,16 +1,43 @@ +import sys import pytest +from unittest.mock import patch from argparse import ArgumentParser +# these tests don't really show any coverage, so not sure how useful they're +# but I'm including them as at least these should work @pytest.mark.parametrize('args, arg_name, exp_result', [ (['--version'], 'version', True), (['-v'], 'version', True), + # config really just inputs a string (['--config', 'value'], 'config', 'value'), (['-c', 'value'], 'config', 'value'), + (['--copy-default-config'], 'copy_default_config', True), + (['--init'], 'init', True), + (['-i'], 'init', True), + (['--build'], 'build', True), + (['-b'], 'build', True), + (['--debug'], 'debug', True) ]) -def test_individual_args(args: list[str], - arg_name: str, - exp_result: str | bool, - arg_parser: ArgumentParser) -> None: - parsed_args: dict[str, str | bool] = vars(arg_parser.parse_args(args)) - assert parsed_args[arg_name] == exp_result +def test_valid_args(args: list[str], + arg_name: str, + exp_result: str | bool, + arg_parser: ArgumentParser) -> None: + with patch.object(sys, 'argv', ['pyssg'] + args): + parsed_args: dict[str, str | bool] = vars(arg_parser.parse_args()) + assert parsed_args[arg_name] == exp_result + + +@pytest.mark.parametrize('args', [ + (['--something-random']), + (['-z']), + (['hello']), + (['help']), + (['h']) +]) +def test_invalid_args(args: list[str], + arg_parser: ArgumentParser) -> None: + with pytest.raises(SystemExit) as system_exit: + arg_parser.parse_args(args) + assert system_exit.type == SystemExit + assert system_exit.value.code == 2 -- cgit v1.2.3-54-g00ecf