From 4c3049a01dfe1e35dcb6168ea5935c1936182b76 Mon Sep 17 00:00:00 2001 From: Kevin Deldycke Date: Sat, 1 Feb 2025 17:44:11 +0400 Subject: [PATCH] Increase test coverage --- click_extra/pytest.py | 6 +++--- tests/test_logging.py | 37 ++++++++++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/click_extra/pytest.py b/click_extra/pytest.py index 3a2e3f6d8..c1d613438 100644 --- a/click_extra/pytest.py +++ b/click_extra/pytest.py @@ -266,13 +266,13 @@ def _create_config(filename: str | Path, content: str) -> Path: ) +default_debug_uncolored_verbose_log = ( + r"debug: Increased log verbosity by \d+ levels: from WARNING to [A-Z]+.\n" +) default_debug_colored_verbose_log = ( r"\x1b\[34mdebug\x1b\[0m: Increased log verbosity " r"by \d+ levels: from WARNING to [A-Z]+.\n" ) -default_debug_uncolored_verbose_log = ( - r"debug: Increased log verbosity by \d+ levels: from WARNING to [A-Z]+.\n" -) default_debug_uncolored_config = ( diff --git a/tests/test_logging.py b/tests/test_logging.py index 93c0ea69c..6f4683673 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -138,8 +138,15 @@ def logging_cli3(): # Duplicate options with different levels: the last always win. ("--blah", "INFO", "-B", "DEBUG"), ("-B", "INFO", "--blah", "DEBUG"), - # ("--blah", "DEBUG", "-B", "INFO"), - # ("-B", "DEBUG", "--blah", "INFO"), + # Click's argument parser deduplicate options before invoking the callback, so the following cases fails. + pytest.param( + ("--blah", "DEBUG", "-B", "INFO"), + marks=pytest.mark.xfail(reason="Last value of the same option wins"), + ), + pytest.param( + ("-B", "DEBUG", "--blah", "INFO"), + marks=pytest.mark.xfail(reason="Last value of the same option wins"), + ), ), ) def test_custom_verbosity_option_name(invoke, args): @@ -226,7 +233,15 @@ def logging_cli1(): # Skip click extra's commands, as verbosity option is already part of the default. command_decorators(no_groups=True, no_extra=True), ) -@pytest.mark.parametrize("option_decorator", (verbosity_option, verbosity_option())) +@pytest.mark.parametrize( + "option_decorator", + ( + verbosity_option, + verbosity_option(), + verbose_option, + verbose_option(), + ), +) @pytest.mark.parametrize( ("args", "expected_level"), ( @@ -236,6 +251,10 @@ def logging_cli1(): (("--verbosity", "WARNING"), "WARNING"), (("--verbosity", "INFO"), "INFO"), (("--verbosity", "DEBUG"), "DEBUG"), + (("-v",), "INFO"), + (("-v", "-v"), "DEBUG"), + (("-v", "-v", "-v"), "DEBUG"), + (("-v", "-v", "-v", "-v", "-v", "-v"), "DEBUG"), ), ) def test_standalone_option_default_logger( @@ -265,6 +284,12 @@ def logging_cli2(): logging.error("my error message.") logging.critical("my critical message.") + logging_option = logging_cli2.params[0] + if args and not set(logging_option.opts).intersection(args): + pytest.skip( + reason=f"Test case for {' '.join(args)!r} does not apply to {logging_option}" + ) + result = invoke(logging_cli2, args, color=True) assert result.exit_code == 0 assert result.stdout == "It works!\n" @@ -286,7 +311,6 @@ def logging_cli2(): messages = ( ( - rf"{default_debug_colored_logging}" r"\x1b\[34mdebug\x1b\[0m: my random message.\n" r"\x1b\[34mdebug\x1b\[0m: my debug message.\n" ), @@ -301,7 +325,10 @@ def logging_cli2(): log_records = r"".join(messages[-level_index - 1 :]) if expected_level == "DEBUG": - log_records += default_debug_colored_log_end + log_start = default_debug_colored_logging + if "-v" in args: + log_start += default_debug_colored_verbose_log + log_records = log_start + log_records + default_debug_colored_log_end assert re.fullmatch(log_records, result.stderr)