Changeset View
Changeset View
Standalone View
Standalone View
swh/core/tests/test_cli.py
# Copyright (C) 2019 The Software Heritage developers | # Copyright (C) 2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import logging | import logging | ||||
import pkg_resources | |||||
import textwrap | import textwrap | ||||
from unittest.mock import patch | from unittest.mock import patch | ||||
import click | import click | ||||
from click.testing import CliRunner | from click.testing import CliRunner | ||||
import pytest | import pytest | ||||
▲ Show 20 Lines • Show All 91 Lines • ▼ Show 20 Lines | def test_sentry(swhmain): | ||||
runner = CliRunner() | runner = CliRunner() | ||||
with patch('sentry_sdk.init') as sentry_sdk_init: | with patch('sentry_sdk.init') as sentry_sdk_init: | ||||
result = runner.invoke(swhmain, ['--sentry-dsn', 'test_dsn', 'test']) | result = runner.invoke(swhmain, ['--sentry-dsn', 'test_dsn', 'test']) | ||||
assert result.exit_code == 0 | assert result.exit_code == 0 | ||||
assert result.output.strip() == '''Hello SWH!''' | assert result.output.strip() == '''Hello SWH!''' | ||||
sentry_sdk_init.assert_called_once_with( | sentry_sdk_init.assert_called_once_with( | ||||
dsn='test_dsn', | dsn='test_dsn', | ||||
debug=False, | debug=False, | ||||
integrations=[], | |||||
release=None, | |||||
) | ) | ||||
def test_sentry_debug(swhmain): | def test_sentry_debug(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: | with patch('sentry_sdk.init') as sentry_sdk_init: | ||||
result = runner.invoke( | result = runner.invoke( | ||||
swhmain, ['--sentry-dsn', 'test_dsn', '--sentry-debug', 'test']) | swhmain, ['--sentry-dsn', 'test_dsn', '--sentry-debug', 'test']) | ||||
assert result.exit_code == 0 | assert result.exit_code == 0 | ||||
assert result.output.strip() == '''Hello SWH!''' | assert result.output.strip() == '''Hello SWH!''' | ||||
sentry_sdk_init.assert_called_once_with( | sentry_sdk_init.assert_called_once_with( | ||||
dsn='test_dsn', | dsn='test_dsn', | ||||
debug=True, | debug=True, | ||||
integrations=[], | |||||
release=None, | |||||
) | ) | ||||
def test_sentry_env(swhmain): | def test_sentry_env(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: | with patch('sentry_sdk.init') as sentry_sdk_init: | ||||
env = { | env = { | ||||
'SWH_SENTRY_DSN': 'test_dsn', | 'SWH_SENTRY_DSN': 'test_dsn', | ||||
'SWH_SENTRY_DEBUG': '1', | 'SWH_SENTRY_DEBUG': '1', | ||||
} | } | ||||
result = runner.invoke( | result = runner.invoke( | ||||
swhmain, ['test'], env=env, auto_envvar_prefix='SWH') | swhmain, ['test'], env=env, auto_envvar_prefix='SWH') | ||||
assert result.exit_code == 0 | assert result.exit_code == 0 | ||||
assert result.output.strip() == '''Hello SWH!''' | assert result.output.strip() == '''Hello SWH!''' | ||||
sentry_sdk_init.assert_called_once_with( | sentry_sdk_init.assert_called_once_with( | ||||
dsn='test_dsn', | dsn='test_dsn', | ||||
debug=True, | debug=True, | ||||
integrations=[], | |||||
release=None, | |||||
) | |||||
def test_sentry_env_main_package(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_MAIN_PACKAGE': 'swh.core', | |||||
} | |||||
result = runner.invoke( | |||||
swhmain, ['test'], env=env, auto_envvar_prefix='SWH') | |||||
assert result.exit_code == 0 | |||||
version = pkg_resources.get_distribution('swh.core').version | |||||
assert result.output.strip() == '''Hello SWH!''' | |||||
sentry_sdk_init.assert_called_once_with( | |||||
dsn='test_dsn', | |||||
debug=False, | |||||
integrations=[], | |||||
release='swh.core@' + version, | |||||
) | ) | ||||
@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 | ||||
▲ Show 20 Lines • Show All 98 Lines • Show Last 20 Lines |