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

Pre-commit #29

Merged
merged 3 commits into from
May 12, 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
24 changes: 24 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,31 @@ repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.5.0
hooks:
- id: check-toml
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-added-large-files
- repo: https://github.com/prettier/prettier
rev: 2.0.5
hooks:
- id: prettier
- repo: https://github.com/psf/black
rev: 19.10b0
hooks:
- id: black
- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.9
hooks:
- id: flake8
additional_dependencies:
- flake8-bandit==2.1.2
- flake8-bugbear==20.1.4
- flake8-docstrings==1.5.0
- pep8-naming==0.10.0
- darglint==1.2.3
- repo: https://github.com/asottile/reorder_python_imports
rev: v2.3.0
hooks:
- id: reorder-python-imports
args: [--application-directories=src]
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"python.pythonPath": "/home/static/.cache/pypoetry/virtualenvs/toml-validator-zr_HNKMd-py3.7/bin/python"
"python.pythonPath": "/home/static/.cache/pypoetry/virtualenvs/toml-validator-zr_HNKMd-py3.7/bin/python"
}
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[mypy]

[mypy-nox.*,tomlkit.*,pytest,pytest_mock]
[mypy-nox.*,tomlkit.*,pytest,pytest_mock,_pytest.*]
ignore_missing_imports = True
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,5 @@ def tests(session: Session) -> None:
def typeguard(session: Session) -> None:
"""Runtime type checking using Typeguard."""
install_package(session)
install(session, "pytest", "typeguard")
install(session, "pytest", "typeguard", "pytest_mock")
session.run("pytest", f"--typeguard-packages={package}", *session.posargs)
3 changes: 0 additions & 3 deletions src/toml_validator/.vscode/settings.json

This file was deleted.

20 changes: 18 additions & 2 deletions src/toml_validator/validation.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
"""Toml Validator validations."""

import sys

import tomlkit
from tomlkit.exceptions import ParseError, TOMLKitError
from tomlkit.exceptions import ParseError
from tomlkit.exceptions import TOMLKitError


def validate_extension(filename: str) -> bool:
"""Validates extension in filename.

Args:
filename (str): name of the file.

Returns:
bool: if extension is valid.
"""
valid_extensions = [".toml"]
for extension in valid_extensions:
if filename.endswith(extension):
Expand All @@ -15,6 +23,14 @@ def validate_extension(filename: str) -> bool:


def validate_toml(filename: str) -> str:
"""It validates the TOML.

Args:
filename (str): name of the file.

Returns:
str: error messages.
"""
with open(filename) as toml:
lines = toml.read()

Expand Down
7 changes: 6 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
def pytest_configure(config):
"""Package-wide test fixtures."""
from _pytest.config import Config


def pytest_configure(config: Config) -> None:
"""Pytest configuration hook."""
config.addinivalue_line("markers", "e2e: mark as end-to-end test.")
9 changes: 7 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
"""Test cases for the __main__ module."""
from unittest.mock import Mock

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

from toml_validator import __main__


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


Expand All @@ -36,6 +37,7 @@ def mock_validation_validate_toml_with_error(mocker: MockFixture) -> Mock:


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

Expand All @@ -45,6 +47,7 @@ def test_main_with_argument_success(
mock_validation_validate_extension: Mock,
mock_validation_validate_toml_no_error: Mock,
):
"""It exits with a status code of zero."""
with runner.isolated_filesystem():
with open("file.toml", "w") as f:
f.write("content doesnt matter")
Expand All @@ -61,6 +64,7 @@ def test_main_with_argument_fail(
mock_validation_validate_extension: Mock,
mock_validation_validate_toml_with_error: Mock,
):
"""It outputs error."""
with runner.isolated_filesystem():
with open("file.toml", "w") as f:
f.write("content doesnt matter")
Expand All @@ -74,5 +78,6 @@ def test_main_with_argument_fail(

@pytest.mark.e2e
def test_main_without_arguments_in_production_env(runner: CliRunner):
"""It exits with a status code of 2 (e2e)."""
result = runner.invoke(__main__.main)
assert result.exit_code == 2
6 changes: 6 additions & 0 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,37 @@ def mock_open_invalid_file(mocker: MockFixture) -> Mock:


def test_validate_extension_valid() -> None:
"""It returns nothing when extension is valid."""
assert validation.validate_extension("file.toml")


def test_validate_extension_invalid() -> None:
"""It raises `SystemExit` when extensio is invalid."""
with pytest.raises(SystemExit):
assert validation.validate_extension("file.xml")


def test_validate_toml_no_error(
mock_open_valid_file: Mock, mock_tomlkit_parse: Mock
) -> None:
"""It returns no errors when valid TOML."""
assert validation.validate_toml("file.toml") == ""


def test_validate_toml_with_error(
mock_open_invalid_file: Mock, mock_tomlkit_parse_exception: Mock
) -> None:
"""It returns errors when invalid TOML."""
assert validation.validate_toml("file.toml") == "|some tomlkit error|"


@pytest.mark.e2e
def test_validate_toml_no_error_production(mock_open_valid_file) -> 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:
"""It returns errors when invalid TOML (e2e)."""
assert validation.validate_toml("file.toml") == 'Key "x" already exists.'