Skip to content

Commit

Permalink
fix: emit warning when TOML is invalid in a style file (closes #68)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoliwa committed Aug 11, 2019
1 parent 3c6b100 commit b48e0a4
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ sphinx:
$(MAKE)

# $(O) is meant as a shortcut for $(SPHINXOPTS).
.cache/make/sphinx: docs *.rst *.md
.cache/make/sphinx: src/* docs *.rst *.md
-rm -rf docs/source
sphinx-apidoc --force --module-first --separate --implicit-namespaces --output-dir docs/source src/nitpick/
sphinx-apidoc --force --module-first --separate --implicit-namespaces --output-dir docs/source src/
@$(SPHINXBUILD) "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
touch .cache/make/sphinx

.cache/make/run: .github/* .travis/* docs/* src/nitpick/* styles/* tests/* nitpick-style.toml
.cache/make/run: .github/* .travis/* docs/* src/* styles/* tests/* nitpick-style.toml
pre-commit run --all-files
flake8
touch .cache/make/run
Expand All @@ -64,6 +64,6 @@ pytest:
-rm .cache/make/pytest
$(MAKE)

.cache/make/pytest: src/nitpick/* styles/* tests/*
.cache/make/pytest: src/* styles/* tests/*
pytest
touch .cache/make/pytest
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/nitpick/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
TOOL_NITPICK,
TOOL_NITPICK_JMEX,
)
from nitpick.exceptions import StyleError
from nitpick.files.pyproject_toml import PyProjectTomlFile
from nitpick.files.setup_cfg import SetupCfgFile
from nitpick.formats import TomlFormat
Expand Down Expand Up @@ -164,7 +165,11 @@ def merge_styles(self) -> YieldFlake8Error:

configured_styles = self.tool_nitpick_dict.get("style", "") # type: StrOrList
style = Style(self)
style.find_initial_styles(configured_styles)
try:
style.find_initial_styles(configured_styles)
except StyleError as err:
yield self.style_error(err.style_file_name.name, "Invalid TOML:", err.args[0])

self.style_dict = style.merge_toml_dict()

from nitpick.plugin import NitpickChecker
Expand Down
10 changes: 10 additions & 0 deletions src/nitpick/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Nitpick exceptions."""
from pathlib import Path


class StyleError(Exception):
"""An error in a style file."""

def __init__(self, style_file_name: Path, *args: object) -> None:
self.style_file_name = style_file_name
super().__init__(*args)
7 changes: 6 additions & 1 deletion src/nitpick/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import requests
from slugify import slugify
from toml import TomlDecodeError

from nitpick.constants import (
DEFAULT_NITPICK_STYLE_URL,
Expand All @@ -14,6 +15,7 @@
NITPICK_STYLES_INCLUDE_JMEX,
TOML_EXTENSION,
)
from nitpick.exceptions import StyleError
from nitpick.files.pyproject_toml import PyProjectTomlFile
from nitpick.formats import TomlFormat
from nitpick.generic import MergeDict, climb_directory_tree, is_url, search_dict
Expand Down Expand Up @@ -60,7 +62,10 @@ def include_multiple_styles(self, chosen_styles: StrOrList) -> None:
continue

toml = TomlFormat(path=style_path)
self._all_styles.add(toml.as_data)
try:
self._all_styles.add(toml.as_data)
except TomlDecodeError as err:
raise StyleError(style_path, "{}: {}".format(err.__class__.__name__, err)) from err

sub_styles = search_dict(NITPICK_STYLES_INCLUDE_JMEX, toml.as_data, []) # type: StrOrList
if sub_styles:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,3 +499,19 @@ def test_invalid_tool_nitpick_on_pyproject_toml(request):
+ " Invalid data in [tool.nitpick]:\x1b[92m\n{}\x1b[0m".format(error_message),
1,
)


def test_invalid_toml(request):
"""Invalid TOML should emit a NIP warning, not raise TomlDecodeError."""
ProjectMock(request).style(
"""
["setup.cfg".flake8]
ignore = D100,D104,D202,E203,W503
"""
).lint().assert_errors_contain(
"""
NIP001 File nitpick-style.toml has an incorrect style. Invalid TOML:\x1b[92m
TomlDecodeError: This float doesn't have a leading digit (line 2 column 1 char 21)\x1b[0m
""",
1,
)

0 comments on commit b48e0a4

Please sign in to comment.