1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
"""Test cases for the __main__ module."""
import os
import pathlib
from collections.abc import Generator
from datetime import datetime
from importlib import metadata as imp_metadata
from typing import Any
from _pytest.fixtures import fixture
from _pytest.monkeypatch import MonkeyPatch
from click.testing import CliRunner
from tests.const import TEST_LOCALE
from renault_api.cli import __main__
PATCH_TODAY = datetime(2018, 12, 25)
@fixture
def patch_root(tmpdir: pathlib.Path) -> Generator[None, None, None]:
"""Update current directory to test log folder."""
root_dir = os.getcwd()
os.chdir(tmpdir)
yield
os.chdir(root_dir)
@fixture
def patch_datetime(monkeypatch: MonkeyPatch) -> Generator[None, None, None]:
"""Allow override of __main__.datetime methods."""
class MyDatetime:
@classmethod
def today(cls) -> datetime:
"""Force today to return a static date."""
return PATCH_TODAY
monkeypatch.setattr("renault_api.cli.__main__.datetime", MyDatetime)
yield
def test_main_succeeds(cli_runner: CliRunner) -> None:
"""It exits with a status code of zero."""
# Click behavior was changed in click 8.2
# See https://github.com/pallets/click/pull/1489
click_version_str = imp_metadata.version("click")
click_version_parts = tuple(int(part) for part in click_version_str.split(".")[:2])
result = cli_runner.invoke(__main__.main)
assert result.exit_code == (2 if click_version_parts >= (8, 2) else 0), (
result.exception
)
def test_debug(cli_runner: CliRunner, caplog: Any) -> None:
"""Test enable debug."""
result = cli_runner.invoke(__main__.main, f"--debug set --locale {TEST_LOCALE}")
assert result.exit_code == 0
assert __main__._WARNING_DEBUG_ENABLED in caplog.text
def test_log_no_folder(
cli_runner: CliRunner, patch_root: Any, patch_datetime: Any
) -> None:
"""Test enable log."""
assert not os.path.exists("logs")
os.makedirs("logs")
assert os.path.exists("logs")
result = cli_runner.invoke(__main__.main, f"--log set --locale {TEST_LOCALE}")
assert result.exit_code == 0
with open("logs/2018-12-25.log") as myfile:
assert __main__._WARNING_DEBUG_ENABLED in myfile.read()
def test_log_existing_folder(
cli_runner: CliRunner, patch_root: Any, patch_datetime: Any
) -> None:
"""Test enable log."""
assert not os.path.exists("logs")
result = cli_runner.invoke(__main__.main, f"--log set --locale {TEST_LOCALE}")
assert result.exit_code == 0
with open("logs/2018-12-25.log") as myfile:
assert __main__._WARNING_DEBUG_ENABLED in myfile.read()
|