Changeset View
Changeset View
Standalone View
Standalone View
swh/core/tests/test_cli.py
# | # Copyright (C) 2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | |||||
# License: GNU General Public License version 3, or any later version | |||||
# See top-level LICENSE file for more information | |||||
import logging | import logging | ||||
import textwrap | import textwrap | ||||
from unittest.mock import patch | |||||
import click | import click | ||||
from click.testing import CliRunner | from click.testing import CliRunner | ||||
import pytest | import pytest | ||||
help_msg = '''Usage: swh [OPTIONS] COMMAND [ARGS]... | help_msg = '''Usage: swh [OPTIONS] COMMAND [ARGS]... | ||||
Command line interface for Software Heritage. | Command line interface for Software Heritage. | ||||
Options: | Options: | ||||
-l, --log-level [NOTSET|DEBUG|INFO|WARNING|ERROR|CRITICAL] | -l, --log-level [NOTSET|DEBUG|INFO|WARNING|ERROR|CRITICAL] | ||||
Log level (defaults to INFO). | Log level (defaults to INFO). | ||||
--log-config FILENAME Python yaml logging configuration file. | --log-config FILENAME Python yaml logging configuration file. | ||||
--sentry-dsn TEXT DSN of the Sentry instance to report to | |||||
-h, --help Show this message and exit. | -h, --help Show this message and exit. | ||||
Notes: | Notes: | ||||
If both options are present, --log-level will override the root logger | If both options are present, --log-level will override the root logger | ||||
configuration set in --log-config. | configuration set in --log-config. | ||||
The --log-config YAML must conform to the logging.config.dictConfig schema | The --log-config YAML must conform to the logging.config.dictConfig schema | ||||
documented at https://docs.python.org/3/library/logging.config.html. | documented at https://docs.python.org/3/library/logging.config.html. | ||||
Show All 13 Lines | |||||
def test_command(swhmain): | def test_command(swhmain): | ||||
@swhmain.command(name='test') | @swhmain.command(name='test') | ||||
@click.pass_context | @click.pass_context | ||||
def swhtest(ctx): | def swhtest(ctx): | ||||
click.echo('Hello SWH!') | click.echo('Hello SWH!') | ||||
runner = CliRunner() | runner = CliRunner() | ||||
with patch('sentry_sdk.init') as sentry_sdk_init: | |||||
result = runner.invoke(swhmain, ['test']) | result = runner.invoke(swhmain, ['test']) | ||||
sentry_sdk_init.assert_not_called() | |||||
assert result.exit_code == 0 | assert result.exit_code == 0 | ||||
assert result.output.strip() == 'Hello SWH!' | assert result.output.strip() == 'Hello SWH!' | ||||
def test_loglevel_default(caplog, swhmain): | def test_loglevel_default(caplog, swhmain): | ||||
@swhmain.command(name='test') | @swhmain.command(name='test') | ||||
@click.pass_context | @click.pass_context | ||||
def swhtest(ctx): | def swhtest(ctx): | ||||
Show All 28 Lines | def swhtest(ctx): | ||||
click.echo('Hello SWH!') | click.echo('Hello SWH!') | ||||
runner = CliRunner() | runner = CliRunner() | ||||
result = runner.invoke(swhmain, ['-l', 'DEBUG', 'test']) | result = runner.invoke(swhmain, ['-l', 'DEBUG', 'test']) | ||||
assert result.exit_code == 0 | assert result.exit_code == 0 | ||||
assert result.output.strip() == '''Hello SWH!''' | assert result.output.strip() == '''Hello SWH!''' | ||||
def test_sentry(swhmain): | |||||
@swhmain.command(name='test') | |||||
@click.pass_context | |||||
def swhtest(ctx): | |||||
click.echo('Hello SWH!') | |||||
runner = CliRunner() | |||||
with patch('sentry_sdk.init') as sentry_sdk_init: | |||||
result = runner.invoke(swhmain, ['--sentry-dsn', 'test_dsn', 'test']) | |||||
assert result.exit_code == 0 | |||||
assert result.output.strip() == '''Hello SWH!''' | |||||
sentry_sdk_init.assert_called_once_with( | |||||
dsn='test_dsn', | |||||
debug=False, | |||||
) | |||||
def test_sentry_debug(swhmain): | |||||
@swhmain.command(name='test') | |||||
@click.pass_context | |||||
def swhtest(ctx): | |||||
click.echo('Hello SWH!') | |||||
runner = CliRunner() | |||||
with patch('sentry_sdk.init') as sentry_sdk_init: | |||||
result = runner.invoke( | |||||
swhmain, ['--sentry-dsn', 'test_dsn', '--sentry-debug', 'test']) | |||||
assert result.exit_code == 0 | |||||
assert result.output.strip() == '''Hello SWH!''' | |||||
sentry_sdk_init.assert_called_once_with( | |||||
dsn='test_dsn', | |||||
debug=True, | |||||
) | |||||
def test_sentry_env(swhmain): | |||||
@swhmain.command(name='test') | |||||
@click.pass_context | |||||
def swhtest(ctx): | |||||
click.echo('Hello SWH!') | |||||
runner = CliRunner() | |||||
with patch('sentry_sdk.init') as sentry_sdk_init: | |||||
env = { | |||||
'SWH_SENTRY_DSN': 'test_dsn', | |||||
'SWH_SENTRY_DEBUG': '1', | |||||
} | |||||
result = runner.invoke( | |||||
swhmain, ['test'], env=env, auto_envvar_prefix='SWH') | |||||
assert result.exit_code == 0 | |||||
assert result.output.strip() == '''Hello SWH!''' | |||||
sentry_sdk_init.assert_called_once_with( | |||||
dsn='test_dsn', | |||||
debug=True, | |||||
) | |||||
@pytest.fixture | @pytest.fixture | ||||
def log_config_path(tmp_path): | def log_config_path(tmp_path): | ||||
log_config = textwrap.dedent('''\ | log_config = textwrap.dedent('''\ | ||||
--- | --- | ||||
version: 1 | version: 1 | ||||
formatters: | formatters: | ||||
formatter: | formatter: | ||||
format: 'custom format:%(name)s:%(levelname)s:%(message)s' | format: 'custom format:%(name)s:%(levelname)s:%(message)s' | ||||
▲ Show 20 Lines • Show All 95 Lines • Show Last 20 Lines |