Skip to content
This repository has been archived by the owner on Feb 17, 2021. It is now read-only.

Strict mypy #110

Merged
merged 4 commits into from
Jul 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
[mypy]
check_untyped_defs = True
disallow_any_generics = True
disallow_incomplete_defs = True
disallow_subclassing_any = True
disallow_untyped_calls = True
disallow_untyped_decorators = True
disallow_untyped_defs = True
no_implicit_optional = True
no_implicit_reexport = True
pretty = True
show_column_numbers = True
show_error_codes = True
show_error_context = True
strict_equality = True
warn_redundant_casts = True
warn_return_any = True
warn_unreachable = True
warn_unused_configs = True
warn_unused_ignores = True

[mypy-tomlkit.*,pytest,pytest_mock,_pytest.*]
[mypy-_pytest.*]
ignore_missing_imports = True

[mypy-pytest]
ignore_missing_imports = True

[mypy-pytest_mock]
ignore_missing_imports = True

[mypy-tests.*]
disallow_untyped_decorators = False

[mypy-tomlkit.*]
ignore_missing_imports = True
Empty file added src/toml_validator/py.typed
Empty file.
27 changes: 15 additions & 12 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,53 @@
"""Test cases for the __main__ module."""
from typing import Any
from unittest.mock import Mock

import click.testing
import pytest
from click.testing import CliRunner
from pytest_mock import MockFixture

from toml_validator import __main__


@pytest.fixture
def runner() -> CliRunner:
def runner() -> click.testing.CliRunner:
"""Fixture for invoking command-line interfaces."""
return CliRunner()
return click.testing.CliRunner()


@pytest.fixture
def mock_validation_validate_extension(mocker: MockFixture) -> Mock:
def mock_validation_validate_extension(mocker: MockFixture) -> Any:
"""Fixture for mocking validation.validate_extension."""
return mocker.patch("toml_validator.validation.validate_extension")


@pytest.fixture
def mock_validation_validate_toml_no_error(mocker: MockFixture) -> Mock:
def mock_validation_validate_toml_no_error(mocker: MockFixture) -> Any:
"""Fixture for mocking validation.validate_toml with no errors."""
mock = mocker.patch("toml_validator.validation.validate_toml")
mock.return_value = ""
return mock


@pytest.fixture
def mock_validation_validate_toml_with_error(mocker: MockFixture) -> Mock:
def mock_validation_validate_toml_with_error(mocker: MockFixture) -> Any:
"""Fixture for mocking validation.validate_toml with error."""
mock = mocker.patch("toml_validator.validation.validate_toml")
mock.return_value = "|some error description|"
return mock


def test_main_without_argument(runner: CliRunner):
def test_main_without_argument(runner: click.testing.CliRunner) -> None:
"""It exits with a status code of 2."""
result = runner.invoke(__main__.main)
assert result.exit_code == 2


def test_main_with_argument_success(
runner: CliRunner,
runner: click.testing.CliRunner,
mock_validation_validate_extension: Mock,
mock_validation_validate_toml_no_error: Mock,
):
) -> None:
"""It exits with a status code of zero."""
with runner.isolated_filesystem():
with open("file.toml", "w") as f:
Expand All @@ -60,10 +61,10 @@ def test_main_with_argument_success(


def test_main_with_argument_fail(
runner: CliRunner,
runner: click.testing.CliRunner,
mock_validation_validate_extension: Mock,
mock_validation_validate_toml_with_error: Mock,
):
) -> None:
"""It outputs error."""
with runner.isolated_filesystem():
with open("file.toml", "w") as f:
Expand All @@ -77,7 +78,9 @@ def test_main_with_argument_fail(


@pytest.mark.e2e
def test_main_without_arguments_in_production_env(runner: CliRunner):
def test_main_without_arguments_in_production_env(
runner: click.testing.CliRunner,
) -> None:
"""It exits with a status code of 2 (e2e)."""
result = runner.invoke(__main__.main)
assert result.exit_code == 2
13 changes: 7 additions & 6 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test cases for the validation module."""
from typing import Any
from unittest.mock import Mock

import pytest
Expand All @@ -9,27 +10,27 @@


@pytest.fixture
def mock_tomlkit_parse(mocker: MockFixture) -> Mock:
def mock_tomlkit_parse(mocker: MockFixture) -> Any:
"""Fixture for mocking tomlkit.parse."""
return mocker.patch("tomlkit.parse")


@pytest.fixture
def mock_tomlkit_parse_exception(mocker: MockFixture) -> Mock:
def mock_tomlkit_parse_exception(mocker: MockFixture) -> Any:
"""Fixture for mocking tomlkit.parse."""
mock = mocker.patch("tomlkit.parse")
mock.side_effect = TOMLKitError("|some tomlkit error|")
return mock


@pytest.fixture
def mock_open_valid_file(mocker: MockFixture) -> Mock:
def mock_open_valid_file(mocker: MockFixture) -> Any:
"""Fixture for mocking build-in open for valid TOML file."""
return mocker.patch("builtins.open", mocker.mock_open(read_data="[x]\na = 3"))


@pytest.fixture
def mock_open_invalid_file(mocker: MockFixture) -> Mock:
def mock_open_invalid_file(mocker: MockFixture) -> Any:
"""Fixture for mocking build-in open for valid TOML file."""
return mocker.patch(
"builtins.open", mocker.mock_open(read_data="[x]\na = 3\n[x]\na = 3")
Expand Down Expand Up @@ -62,12 +63,12 @@ def test_validate_toml_with_error(


@pytest.mark.e2e
def test_validate_toml_no_error_production(mock_open_valid_file) -> None:
def test_validate_toml_no_error_production(mock_open_valid_file: Mock) -> None:
"""It returns no errors when valid TOML (e2e)."""
assert validation.validate_toml("file.toml") == ""


@pytest.mark.e2e
def test_validate_toml_with_error_production(mock_open_invalid_file) -> None:
def test_validate_toml_with_error_production(mock_open_invalid_file: Mock) -> None:
"""It returns errors when invalid TOML (e2e)."""
assert validation.validate_toml("file.toml") == 'Key "x" already exists.'