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

Commit 08b05d2

Browse files
author
Thiago C. D'Ávila
authored
Merge pull request #29 from staticdev/pre-commit
Pre-commit
2 parents c96462e + 0ec9be4 commit 08b05d2

9 files changed

Lines changed: 64 additions & 11 deletions

File tree

.pre-commit-config.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,31 @@ repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
33
rev: v2.5.0
44
hooks:
5+
- id: check-toml
56
- id: check-yaml
67
- id: end-of-file-fixer
78
- id: trailing-whitespace
89
- id: check-added-large-files
10+
- repo: https://github.com/prettier/prettier
11+
rev: 2.0.5
12+
hooks:
13+
- id: prettier
14+
- repo: https://github.com/psf/black
15+
rev: 19.10b0
16+
hooks:
17+
- id: black
18+
- repo: https://gitlab.com/pycqa/flake8
19+
rev: 3.7.9
20+
hooks:
21+
- id: flake8
22+
additional_dependencies:
23+
- flake8-bandit==2.1.2
24+
- flake8-bugbear==20.1.4
25+
- flake8-docstrings==1.5.0
26+
- pep8-naming==0.10.0
27+
- darglint==1.2.3
28+
- repo: https://github.com/asottile/reorder_python_imports
29+
rev: v2.3.0
30+
hooks:
31+
- id: reorder-python-imports
32+
args: [--application-directories=src]

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"python.pythonPath": "/home/static/.cache/pypoetry/virtualenvs/toml-validator-zr_HNKMd-py3.7/bin/python"
2+
"python.pythonPath": "/home/static/.cache/pypoetry/virtualenvs/toml-validator-zr_HNKMd-py3.7/bin/python"
33
}

mypy.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[mypy]
22

3-
[mypy-nox.*,tomlkit.*,pytest,pytest_mock]
3+
[mypy-nox.*,tomlkit.*,pytest,pytest_mock,_pytest.*]
44
ignore_missing_imports = True

noxfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,5 @@ def tests(session: Session) -> None:
147147
def typeguard(session: Session) -> None:
148148
"""Runtime type checking using Typeguard."""
149149
install_package(session)
150-
install(session, "pytest", "typeguard")
150+
install(session, "pytest", "typeguard", "pytest_mock")
151151
session.run("pytest", f"--typeguard-packages={package}", *session.posargs)

src/toml_validator/.vscode/settings.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/toml_validator/validation.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
"""Toml Validator validations."""
2-
32
import sys
43

54
import tomlkit
6-
from tomlkit.exceptions import ParseError, TOMLKitError
5+
from tomlkit.exceptions import ParseError
6+
from tomlkit.exceptions import TOMLKitError
77

88

99
def validate_extension(filename: str) -> bool:
10+
"""Validates extension in filename.
11+
12+
Args:
13+
filename (str): name of the file.
14+
15+
Returns:
16+
bool: if extension is valid.
17+
"""
1018
valid_extensions = [".toml"]
1119
for extension in valid_extensions:
1220
if filename.endswith(extension):
@@ -15,6 +23,14 @@ def validate_extension(filename: str) -> bool:
1523

1624

1725
def validate_toml(filename: str) -> str:
26+
"""It validates the TOML.
27+
28+
Args:
29+
filename (str): name of the file.
30+
31+
Returns:
32+
str: error messages.
33+
"""
1834
with open(filename) as toml:
1935
lines = toml.read()
2036

tests/conftest.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
def pytest_configure(config):
1+
"""Package-wide test fixtures."""
2+
from _pytest.config import Config
3+
4+
5+
def pytest_configure(config: Config) -> None:
6+
"""Pytest configuration hook."""
27
config.addinivalue_line("markers", "e2e: mark as end-to-end test.")

tests/test_main.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
"""Test cases for the __main__ module."""
22
from unittest.mock import Mock
33

4-
from click.testing import CliRunner
54
import pytest
5+
from click.testing import CliRunner
66
from pytest_mock import MockFixture
77

88
from toml_validator import __main__
99

1010

1111
@pytest.fixture
12-
def runner():
12+
def runner() -> CliRunner:
13+
"""Fixture for invoking command-line interfaces."""
1314
return CliRunner()
1415

1516

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

3738

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

@@ -45,6 +47,7 @@ def test_main_with_argument_success(
4547
mock_validation_validate_extension: Mock,
4648
mock_validation_validate_toml_no_error: Mock,
4749
):
50+
"""It exits with a status code of zero."""
4851
with runner.isolated_filesystem():
4952
with open("file.toml", "w") as f:
5053
f.write("content doesnt matter")
@@ -61,6 +64,7 @@ def test_main_with_argument_fail(
6164
mock_validation_validate_extension: Mock,
6265
mock_validation_validate_toml_with_error: Mock,
6366
):
67+
"""It outputs error."""
6468
with runner.isolated_filesystem():
6569
with open("file.toml", "w") as f:
6670
f.write("content doesnt matter")
@@ -74,5 +78,6 @@ def test_main_with_argument_fail(
7478

7579
@pytest.mark.e2e
7680
def test_main_without_arguments_in_production_env(runner: CliRunner):
81+
"""It exits with a status code of 2 (e2e)."""
7782
result = runner.invoke(__main__.main)
7883
assert result.exit_code == 2

tests/test_validation.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,37 @@ def mock_open_invalid_file(mocker: MockFixture) -> Mock:
3737

3838

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

4243

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

4749

4850
def test_validate_toml_no_error(
4951
mock_open_valid_file: Mock, mock_tomlkit_parse: Mock
5052
) -> None:
53+
"""It returns no errors when valid TOML."""
5154
assert validation.validate_toml("file.toml") == ""
5255

5356

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

5963

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

6469

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

0 commit comments

Comments
 (0)