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

Commit cc78955

Browse files
author
staticdev
committed
#1 Added tests passing
1 parent c67413d commit cc78955

10 files changed

Lines changed: 208 additions & 48 deletions

File tree

.vscode/settings.json

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

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]
3+
[mypy-nox.*,tomlkit.*,pytest,pytest_mock]
44
ignore_missing_imports = True

noxfile.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ def tests(session: Session) -> None:
8888
"""Run the test suite."""
8989
args = session.posargs or ["--cov", "-m", "not e2e"]
9090
session.run("poetry", "install", "--no-dev", external=True)
91-
install_with_constraints(session, "coverage[toml]", "pytest", "pytest-cov")
91+
install_with_constraints(
92+
session, "coverage[toml]", "pytest", "pytest-cov", "pytest-mock"
93+
)
9294
session.run("pytest", *args)
9395

9496

poetry.lock

Lines changed: 36 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ flake8-import-order = "^0.18.1"
2727
safety = "^1.8.5"
2828
mypy = "^0.770"
2929
codecov = "^2.0.22"
30+
pytest-mock = "^3.0.0"
3031

3132
[tool.poetry.scripts]
3233
toml-validator = "toml_validator.console:main"

src/toml_validator/console.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,21 @@
1-
""" Toml Validator CLI """
2-
3-
import sys
1+
"""Toml Validator CLI."""
42

53
import click
6-
from tomlkit import parse
7-
from tomlkit.exceptions import ParseError, TOMLKitError
84

9-
from . import __version__
5+
from . import __version__, validation
106

117

128
@click.command()
139
@click.argument("filename", type=click.Path(exists=True))
1410
@click.version_option(version=__version__)
15-
def main(filename) -> None:
16-
""" Gets TOML file and validate """
17-
if not filename.endswith(".toml"):
18-
sys.exit('Error: "FILANAME" %s does not have a ".toml" extension.' % filename)
19-
20-
with open(filename) as toml:
21-
lines = toml.read()
22-
23-
click.secho("Reading file %s" % filename, fg="blue")
24-
try:
25-
parse(lines)
26-
click.secho("No problems found parsing file %s!" % filename, fg="green")
27-
except (TOMLKitError, ParseError) as error:
28-
click.secho("Error found: " + str(error), fg="red")
11+
def main(filename: str) -> None:
12+
"""Makes validations and echos errors if found."""
13+
validation.validate_extension(filename)
2914

15+
click.secho("Reading file {}.".format(filename), fg="blue")
3016

31-
if __name__ == "__main__":
32-
main()
17+
errors = validation.validate_toml(filename)
18+
if errors:
19+
click.secho("Error(s) found: {}.".format(errors), fg="red")
20+
else:
21+
click.secho("No problems found parsing file {}!".format(filename), fg="green")

src/toml_validator/validation.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Toml Validator validations."""
2+
3+
import sys
4+
5+
import tomlkit
6+
from tomlkit.exceptions import ParseError, TOMLKitError
7+
8+
9+
def validate_extension(filename: str) -> bool:
10+
valid_extensions = [".toml"]
11+
for extension in valid_extensions:
12+
if filename.endswith(extension):
13+
return True
14+
sys.exit('Error: "FILANAME" {} does not have a valid extension.'.format(filename))
15+
16+
17+
def validate_toml(filename: str) -> str:
18+
with open(filename) as toml:
19+
lines = toml.read()
20+
21+
try:
22+
tomlkit.parse(lines)
23+
return ""
24+
except (TOMLKitError, ParseError) as errors:
25+
return str(errors)

tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Test suite for the toml_validator package."""

tests/test_console.py

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,78 @@
1-
import click.testing
1+
"""Test cases for the console module."""
2+
from unittest.mock import Mock
3+
4+
from click.testing import CliRunner
25
import pytest
6+
from pytest_mock import MockFixture
37

48
from toml_validator import console
59

610

711
@pytest.fixture
812
def runner():
9-
return click.testing.CliRunner()
13+
return CliRunner()
14+
15+
16+
@pytest.fixture
17+
def mock_validation_validate_extension(mocker: MockFixture) -> Mock:
18+
"""Fixture for mocking validation.validate_extension."""
19+
return mocker.patch("toml_validator.validation.validate_extension")
20+
21+
22+
@pytest.fixture
23+
def mock_validation_validate_toml_no_error(mocker: MockFixture) -> Mock:
24+
"""Fixture for mocking validation.validate_toml with no errors."""
25+
mock = mocker.patch("toml_validator.validation.validate_toml")
26+
mock.return_value = ""
27+
return mock
1028

1129

12-
# TODO make the two success cases
13-
# (one using mock file and one running a valid file in e2e)
30+
@pytest.fixture
31+
def mock_validation_validate_toml_with_error(mocker: MockFixture) -> Mock:
32+
"""Fixture for mocking validation.validate_toml with error."""
33+
mock = mocker.patch("toml_validator.validation.validate_toml")
34+
mock.return_value = "|some error description|"
35+
return mock
1436

1537

16-
def test_main_without_arguments(runner):
38+
def test_main_without_argument(runner: CliRunner):
1739
result = runner.invoke(console.main)
1840
assert result.exit_code == 2
1941

2042

43+
def test_main_with_argument_success(
44+
runner: CliRunner,
45+
mock_validation_validate_extension: Mock,
46+
mock_validation_validate_toml_no_error: Mock,
47+
):
48+
with runner.isolated_filesystem():
49+
with open("file.toml", "w") as f:
50+
f.write("content doesnt matter")
51+
52+
result = runner.invoke(console.main, ["file.toml"])
53+
assert result.output == (
54+
"Reading file file.toml.\n" "No problems found parsing file file.toml!\n"
55+
)
56+
assert result.exit_code == 0
57+
58+
59+
def test_main_with_argument_fail(
60+
runner: CliRunner,
61+
mock_validation_validate_extension: Mock,
62+
mock_validation_validate_toml_with_error: Mock,
63+
):
64+
with runner.isolated_filesystem():
65+
with open("file.toml", "w") as f:
66+
f.write("content doesnt matter")
67+
68+
result = runner.invoke(console.main, ["file.toml"])
69+
assert result.output == (
70+
"Reading file file.toml.\n" "Error(s) found: |some error description|.\n"
71+
)
72+
assert result.exit_code == 0
73+
74+
2175
@pytest.mark.e2e
22-
def test_main_without_arguments_in_production_env(runner):
76+
def test_main_without_arguments_in_production_env(runner: CliRunner):
2377
result = runner.invoke(console.main)
2478
assert result.exit_code == 2

0 commit comments

Comments
 (0)