Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kdeldycke committed Feb 1, 2025
1 parent 04cae30 commit 4c3049a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
6 changes: 3 additions & 3 deletions click_extra/pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
37 changes: 32 additions & 5 deletions tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"),
(
Expand All @@ -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(
Expand Down Expand Up @@ -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"
Expand All @@ -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"
),
Expand All @@ -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)


Expand Down

0 comments on commit 4c3049a

Please sign in to comment.