2021-01-06 16:17:05 +01:00
|
|
|
"""Test the package's top-level `umd` CLI command."""
|
2020-08-04 21:14:40 +02:00
|
|
|
|
|
|
|
import click
|
|
|
|
import pytest
|
|
|
|
|
2021-01-06 16:17:05 +01:00
|
|
|
from urban_meal_delivery.console import main
|
2020-08-04 21:14:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestShowVersion:
|
2021-01-06 16:17:05 +01:00
|
|
|
"""Test `console.main.show_version()`.
|
2020-08-04 21:14:40 +02:00
|
|
|
|
|
|
|
The function is used as a callback to a click command option.
|
|
|
|
|
2021-01-06 16:17:05 +01:00
|
|
|
`show_version()` prints the name and version of the installed package to
|
2020-08-04 21:14:40 +02:00
|
|
|
stdout. The output looks like this: "{pkg_name}, version {version}".
|
|
|
|
|
|
|
|
Development (= non-final) versions are indicated by appending a
|
|
|
|
" (development)" to the output.
|
|
|
|
"""
|
|
|
|
|
2020-08-04 22:57:55 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def ctx(self) -> click.Context:
|
2021-01-06 16:17:05 +01:00
|
|
|
"""Context around the `main.entry_point` Command."""
|
|
|
|
return click.Context(main.entry_point)
|
2020-08-04 22:57:55 +02:00
|
|
|
|
|
|
|
def test_no_version(self, capsys, ctx):
|
2021-01-06 16:17:05 +01:00
|
|
|
"""Test the early exit branch without any output."""
|
|
|
|
main.show_version(ctx, _param='discarded', value=False)
|
2020-08-04 21:14:40 +02:00
|
|
|
|
|
|
|
captured = capsys.readouterr()
|
|
|
|
|
|
|
|
assert captured.out == ''
|
|
|
|
|
2020-08-04 22:57:55 +02:00
|
|
|
def test_final_version(self, capsys, ctx, monkeypatch):
|
2020-08-04 21:14:40 +02:00
|
|
|
"""For final versions, NO "development" warning is emitted."""
|
|
|
|
version = '1.2.3'
|
2021-01-06 16:17:05 +01:00
|
|
|
monkeypatch.setattr(main.urban_meal_delivery, '__version__', version)
|
2020-08-04 21:14:40 +02:00
|
|
|
|
|
|
|
with pytest.raises(click.exceptions.Exit):
|
2021-01-06 16:17:05 +01:00
|
|
|
main.show_version(ctx, _param='discarded', value=True)
|
2020-08-04 21:14:40 +02:00
|
|
|
|
|
|
|
captured = capsys.readouterr()
|
|
|
|
|
|
|
|
assert captured.out.endswith(f', version {version}\n')
|
|
|
|
|
2020-08-04 22:57:55 +02:00
|
|
|
def test_develop_version(self, capsys, ctx, monkeypatch):
|
2020-08-04 21:14:40 +02:00
|
|
|
"""For develop versions, a warning thereof is emitted."""
|
|
|
|
version = '1.2.3.dev0'
|
2021-01-06 16:17:05 +01:00
|
|
|
monkeypatch.setattr(main.urban_meal_delivery, '__version__', version)
|
2020-08-04 21:14:40 +02:00
|
|
|
|
|
|
|
with pytest.raises(click.exceptions.Exit):
|
2021-01-06 16:17:05 +01:00
|
|
|
main.show_version(ctx, _param='discarded', value=True)
|
2020-08-04 21:14:40 +02:00
|
|
|
|
|
|
|
captured = capsys.readouterr()
|
|
|
|
|
|
|
|
assert captured.out.strip().endswith(f', version {version} (development)')
|
|
|
|
|
|
|
|
|
2021-01-06 16:17:05 +01:00
|
|
|
class TestCLIWithoutCommand:
|
|
|
|
"""Test the `umd` CLI utility, invoked without any specific command.
|
2020-08-04 21:14:40 +02:00
|
|
|
|
|
|
|
The test cases are integration tests.
|
|
|
|
Therefore, they are not considered for coverage reporting.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@pytest.mark.no_cover
|
2020-08-04 22:57:55 +02:00
|
|
|
def test_no_options(self, cli):
|
2020-08-04 21:14:40 +02:00
|
|
|
"""Exit with 0 status code and no output if run without options."""
|
2021-01-06 16:17:05 +01:00
|
|
|
result = cli.invoke(main.entry_point)
|
2020-08-04 21:14:40 +02:00
|
|
|
|
|
|
|
assert result.exit_code == 0
|
|
|
|
|
|
|
|
# The following test cases validate the --version / -V option.
|
|
|
|
|
|
|
|
version_options = ('--version', '-V')
|
|
|
|
|
|
|
|
@pytest.mark.no_cover
|
2020-08-04 22:57:55 +02:00
|
|
|
@pytest.mark.parametrize('option', version_options)
|
|
|
|
def test_final_version(self, cli, monkeypatch, option):
|
2020-08-04 21:14:40 +02:00
|
|
|
"""For final versions, NO "development" warning is emitted."""
|
|
|
|
version = '1.2.3'
|
2021-01-06 16:17:05 +01:00
|
|
|
monkeypatch.setattr(main.urban_meal_delivery, '__version__', version)
|
2020-08-04 21:14:40 +02:00
|
|
|
|
2021-01-06 16:17:05 +01:00
|
|
|
result = cli.invoke(main.entry_point, option)
|
2020-08-04 21:14:40 +02:00
|
|
|
|
|
|
|
assert result.exit_code == 0
|
|
|
|
assert result.output.strip().endswith(f', version {version}')
|
|
|
|
|
|
|
|
@pytest.mark.no_cover
|
2020-08-04 22:57:55 +02:00
|
|
|
@pytest.mark.parametrize('option', version_options)
|
|
|
|
def test_develop_version(self, cli, monkeypatch, option):
|
2020-08-04 21:14:40 +02:00
|
|
|
"""For develop versions, a warning thereof is emitted."""
|
|
|
|
version = '1.2.3.dev0'
|
2021-01-06 16:17:05 +01:00
|
|
|
monkeypatch.setattr(main.urban_meal_delivery, '__version__', version)
|
2020-08-04 21:14:40 +02:00
|
|
|
|
2021-01-06 16:17:05 +01:00
|
|
|
result = cli.invoke(main.entry_point, option)
|
2020-08-04 21:14:40 +02:00
|
|
|
|
|
|
|
assert result.exit_code == 0
|
|
|
|
assert result.output.strip().endswith(f', version {version} (development)')
|