Changeset View
Changeset View
Standalone View
Standalone View
swh/core/tests/test_cli.py
Show First 20 Lines • Show All 236 Lines • ▼ Show 20 Lines | def log_config_path(tmp_path): | ||||
""" | """ | ||||
) | ) | ||||
(tmp_path / "log_config.yml").write_text(log_config) | (tmp_path / "log_config.yml").write_text(log_config) | ||||
yield str(tmp_path / "log_config.yml") | yield str(tmp_path / "log_config.yml") | ||||
def test_log_config(caplog, log_config_path, swhmain): | def test_log_config(log_config_path, swhmain): | ||||
@swhmain.command(name="test") | @swhmain.command(name="test") | ||||
@click.pass_context | @click.pass_context | ||||
def swhtest(ctx): | def swhtest(ctx): | ||||
logging.debug("Root log debug") | logging.debug("Root log debug") | ||||
logging.info("Root log info") | logging.info("Root log info") | ||||
logging.getLogger("dontshowdebug").debug("Not shown") | logging.getLogger("dontshowdebug").debug("Not shown") | ||||
logging.getLogger("dontshowdebug").info("Shown") | logging.getLogger("dontshowdebug").info("Shown") | ||||
runner = CliRunner() | runner = CliRunner() | ||||
result = runner.invoke(swhmain, ["--log-config", log_config_path, "test",],) | result = runner.invoke(swhmain, ["--log-config", log_config_path, "test",],) | ||||
assert result.exit_code == 0 | assert result.exit_code == 0 | ||||
assert result.output.strip() == "\n".join( | assert result.output.strip() == "\n".join( | ||||
[ | [ | ||||
"custom format:root:DEBUG:Root log debug", | "custom format:root:DEBUG:Root log debug", | ||||
"custom format:root:INFO:Root log info", | "custom format:root:INFO:Root log info", | ||||
"custom format:dontshowdebug:INFO:Shown", | "custom format:dontshowdebug:INFO:Shown", | ||||
] | ] | ||||
) | ) | ||||
def test_log_config_log_level_interaction(caplog, log_config_path, swhmain): | def test_log_config_log_level_interaction(log_config_path, swhmain): | ||||
@swhmain.command(name="test") | @swhmain.command(name="test") | ||||
@click.pass_context | @click.pass_context | ||||
def swhtest(ctx): | def swhtest(ctx): | ||||
logging.debug("Root log debug") | logging.debug("Root log debug") | ||||
logging.info("Root log info") | logging.info("Root log info") | ||||
logging.getLogger("dontshowdebug").debug("Not shown") | logging.getLogger("dontshowdebug").debug("Not shown") | ||||
logging.getLogger("dontshowdebug").info("Shown") | logging.getLogger("dontshowdebug").info("Shown") | ||||
runner = CliRunner() | runner = CliRunner() | ||||
result = runner.invoke( | result = runner.invoke( | ||||
swhmain, ["--log-config", log_config_path, "--log-level", "INFO", "test",], | swhmain, ["--log-config", log_config_path, "--log-level", "INFO", "test",], | ||||
) | ) | ||||
assert result.exit_code == 0 | assert result.exit_code == 0 | ||||
assert result.output.strip() == "\n".join( | assert result.output.strip() == "\n".join( | ||||
[ | [ | ||||
"custom format:root:INFO:Root log info", | "custom format:root:INFO:Root log info", | ||||
"custom format:dontshowdebug:INFO:Shown", | "custom format:dontshowdebug:INFO:Shown", | ||||
] | ] | ||||
) | ) | ||||
def test_multiple_log_level_behavior(swhmain): | |||||
@swhmain.command(name="test") | |||||
@click.pass_context | |||||
def swhtest(ctx): | |||||
assert logging.getLevelName(logging.root.level) == "DEBUG" | |||||
assert logging.getLevelName(logging.getLogger("dontshowdebug").level) == "INFO" | |||||
return 0 | |||||
runner = CliRunner() | |||||
result = runner.invoke( | |||||
swhmain, ["--log-level", "DEBUG", "--log-level", "dontshowdebug:INFO", "test",] | |||||
) | |||||
assert result.exit_code == 0, result.output | |||||
def test_invalid_log_level(swhmain): | |||||
runner = CliRunner() | |||||
result = runner.invoke(swhmain, ["--log-level", "broken:broken:DEBUG"]) | |||||
assert result.exit_code != 0 | |||||
assert "Invalid log level specification" in result.output | |||||
runner = CliRunner() | |||||
result = runner.invoke(swhmain, ["--log-level", "UNKNOWN"]) | |||||
assert result.exit_code != 0 | |||||
assert "Log level UNKNOWN unknown" in result.output | |||||
def test_aliased_command(swhmain): | def test_aliased_command(swhmain): | ||||
@swhmain.command(name="canonical-test") | @swhmain.command(name="canonical-test") | ||||
@click.pass_context | @click.pass_context | ||||
def swhtest(ctx): | def swhtest(ctx): | ||||
"A test command." | "A test command." | ||||
click.echo("Hello SWH!") | click.echo("Hello SWH!") | ||||
swhmain.add_alias(swhtest, "othername") | swhmain.add_alias(swhtest, "othername") | ||||
Show All 18 Lines |