Skip to content

Commit

Permalink
ci: tox - add scripts/endpoints invokation for testing + utests - dis…
Browse files Browse the repository at this point in the history
…able catching exceptions when invokating cli_runner

Signed-off-by: ATTY Lionel <[email protected]>
  • Loading branch information
ATTY Lionel committed Aug 24, 2024
1 parent ffa0f5c commit 2794d8d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 17 deletions.
58 changes: 50 additions & 8 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ aiohttp = "*"
aiohttp_cors = "*"

tox = "*"
tox-poetry-dev-dependencies = "*"
tox-gh-actions = "*"
virtualenv-pyenv = "*"

Expand Down
31 changes: 22 additions & 9 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re

import pytest
import requests
from rich._emoji_codes import EMOJI

from cruft_helloworld import __project_name__ as application_name
Expand All @@ -9,7 +10,7 @@


def test_app_cli_option_version(cli_runner):
result = cli_runner.invoke(cli, ["--version"])
result = cli_runner.invoke(cli, ["--version"], catch_exceptions=False)
assert result.exit_code == 0
application_name_expected = application_name
application_version_expected = application_version
Expand All @@ -21,25 +22,33 @@ def test_app_cli_option_version(cli_runner):

# @pytest.mark.skip
def test_app_cli_option_show_banner(cli_runner):
result = cli_runner.invoke(cli, ["--show-banner"])
result = cli_runner.invoke(cli, ["--show-banner"], catch_exceptions=False)
assert result.exit_code == 0
version_message = f"{application_name}, version {application_version}"
assert len(result.output) > len(version_message)
assert result.output.count("\n") > 1


def test_app_cli_option_verbose(cli_runner):
result = cli_runner.invoke(cli, ["--verbose"])
def test_app_cli_option_verbose(cli_runner, mocker):
# no need to use 'internet' for testing "verbose" option
# we can raise an exception at the first internet request
# and follow the default running path if no internet available
mocker.patch(
"cruft_helloworld.services.globe_emoji_with_geoip.requests.get",
side_effect=requests.exceptions.RequestException,
)

result = cli_runner.invoke(cli, ["--verbose"], catch_exceptions=False)
assert result.exit_code == 0
assert "DEBUG" not in result.output

result = cli_runner.invoke(cli, ["-vv"])
result = cli_runner.invoke(cli, ["-vv"], catch_exceptions=False)
assert result.exit_code == 0
assert "DEBUG" in result.output


def test_app_cli_help(cli_runner):
result = cli_runner.invoke(hello_world, ["--help"])
result = cli_runner.invoke(hello_world, ["--help"], catch_exceptions=False)
assert result.exit_code == 0
assert (
"--globe-emoji [EUROPE_AFRICA|ASIA_AUSTRALIA|AMERICAS|WITH_MERIDIANS]"
Expand All @@ -60,7 +69,9 @@ def test_app_cli_hello_world(
cli_runner, input_globe_emoji_name: str, expected_globe_emoji_char: str
):
# https://github.com/willmcgugan/rich/blob/a3f5609202e9aa45751ce9baa3a72462ed1cc488/tests/test_console.py#L192
result = cli_runner.invoke(hello_world, ["--globe-emoji", input_globe_emoji_name])
result = cli_runner.invoke(
hello_world, ["--globe-emoji", input_globe_emoji_name], catch_exceptions=False
)
assert result.exit_code == 0
assert result.output == f"Hello {expected_globe_emoji_char}\n"

Expand All @@ -69,7 +80,9 @@ def test_error_app_cli_hello_world(cli_runner):
click_option_name = "globe-emoji"
wrong_emoji_name = "no-existing-click-option"
result = cli_runner.invoke(
hello_world, [f"--{click_option_name}", wrong_emoji_name]
hello_world,
[f"--{click_option_name}", wrong_emoji_name],
catch_exceptions=False,
)
assert result.exit_code == 2
assert (
Expand All @@ -80,7 +93,7 @@ def test_error_app_cli_hello_world(cli_runner):

@pytest.mark.use_internet
def test_app_cli_hello_world_without_option(cli_runner):
result = cli_runner.invoke(hello_world)
result = cli_runner.invoke(hello_world, catch_exceptions=False)
assert result.exit_code == 0
hello_world_result = result.output
regex = r"Hello (?P<globe_emoji>.)"
Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ deps =
commands =
poetry lock
poetry install
# test poetry scripts endpoints define in `pyproject.toml:[tool.poetry.scripts]`
poetry run helloworld
poetry run pytest --cov=cruft_helloworld --cov-report term

0 comments on commit 2794d8d

Please sign in to comment.