Skip to content

Commit

Permalink
test: ensure the config log level is set correctly
Browse files Browse the repository at this point in the history
Test a command using a config file producing logs with various levels of
logging and ensure the output contains only the expected messages.
  • Loading branch information
Yannick-Dayer committed Sep 26, 2024
1 parent 2098ee8 commit a093bec
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 1 deletion.
9 changes: 9 additions & 0 deletions tests/data/complex.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import logging

logger = logging.getLogger("clapper_test.config_with_logs")

logger.debug("Debug level message")
logger.info("Info level message")
logger.warning("Warning level message")
logger.error("Error level message")

cplx = dict(
a="test",
b=42,
Expand Down
121 changes: 120 additions & 1 deletion tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

import clapper.logging
import click
import pytest

from clapper.click import verbosity_option
from clapper.click import ConfigCommand, ResourceOption, verbosity_option
from click.testing import CliRunner


Expand Down Expand Up @@ -195,3 +196,121 @@ def cli(**_):

assert "debug message\ninfo message\n" in lo.getvalue()
assert hi.getvalue() == "warning message\nerror message\n"


# Testing the logger is also set correctly during the loading of config files.


@pytest.fixture
def cli_config():
messages = io.StringIO()
logger = clapper.logging.setup(
"clapper_test",
format="[%(levelname)s] %(message)s",
low_level_stream=messages,
high_level_stream=messages,
)
logger.setLevel("ERROR") # Enforce a default level

@click.command(entry_point_group="clapper.test.config", cls=ConfigCommand)
@click.option("--cmp", entry_point_group="clapper.test.config", cls=ResourceOption)
@verbosity_option(logger, expose_value=False)
def cli(**_):
"""This is the documentation provided by the user."""
pass

return (cli, messages)


# Loading configs using ResourceOption


def test_logger_click_option_config_q(cli_config):
cli, log_output = cli_config
runner = CliRunner()
result = runner.invoke(cli, ["--cmp", "complex-var"])
expected = "[ERROR] Error level message\n"
assert result.exit_code == 0, result.output
assert log_output.getvalue() == expected


def test_logger_click_option_config_v(cli_config):
cli, log_output = cli_config
runner = CliRunner()
result = runner.invoke(cli, ["--cmp", "complex-var", "-v"])
expected = "[WARNING] Warning level message\n" "[ERROR] Error level message\n"
assert result.exit_code == 0, result.output
assert log_output.getvalue() == expected


def test_logger_click_option_config_vv(cli_config):
cli, log_output = cli_config
runner = CliRunner()
result = runner.invoke(cli, ["--cmp", "complex-var", "-vv"])
expected = (
"[INFO] Info level message\n"
"[WARNING] Warning level message\n"
"[ERROR] Error level message\n"
)
assert result.exit_code == 0, result.output
assert log_output.getvalue() == expected


def test_logger_click_option_config_vvv(cli_config):
cli, log_output = cli_config
runner = CliRunner()
result = runner.invoke(cli, ["--cmp", "complex-var", "-vvv"])
expected = (
"[DEBUG] Debug level message\n"
"[INFO] Info level message\n"
"[WARNING] Warning level message\n"
"[ERROR] Error level message\n"
)
assert result.exit_code == 0, result.output
assert log_output.getvalue().endswith(expected)


# Loading configs using ConfigCommand
def test_logger_click_command_config_q(cli_config):
cli, log_output = cli_config
runner = CliRunner()
result = runner.invoke(cli, ["complex"])
expected = "[ERROR] Error level message\n"
assert result.exit_code == 0, result.output
assert log_output.getvalue() == expected


def test_logger_click_command_config_v(cli_config):
cli, log_output = cli_config
runner = CliRunner()
result = runner.invoke(cli, ["complex", "-v"])
expected = "[WARNING] Warning level message\n" "[ERROR] Error level message\n"
assert result.exit_code == 0, result.output
assert log_output.getvalue() == expected


def test_logger_click_command_config_vv(cli_config):
cli, log_output = cli_config
runner = CliRunner()
result = runner.invoke(cli, ["complex", "-vv"])
expected = (
"[INFO] Info level message\n"
"[WARNING] Warning level message\n"
"[ERROR] Error level message\n"
)
assert result.exit_code == 0, result.output
assert log_output.getvalue() == expected


def test_logger_click_command_config_vvv(cli_config):
cli, log_output = cli_config
runner = CliRunner()
result = runner.invoke(cli, ["complex", "-vvv"])
expected = (
"[DEBUG] Debug level message\n"
"[INFO] Info level message\n"
"[WARNING] Warning level message\n"
"[ERROR] Error level message\n"
)
assert result.exit_code == 0, result.output
assert log_output.getvalue().endswith(expected)

0 comments on commit a093bec

Please sign in to comment.