From b48e0a4bc41a13b4467f13a67fe1bcea36f16d83 Mon Sep 17 00:00:00 2001 From: Augusto Wagner Andreoli Date: Sun, 11 Aug 2019 19:26:57 +0200 Subject: [PATCH] fix: emit warning when TOML is invalid in a style file (closes #68) --- Makefile | 8 ++++---- poetry.lock | 2 +- src/nitpick/config.py | 7 ++++++- src/nitpick/exceptions.py | 10 ++++++++++ src/nitpick/style.py | 7 ++++++- tests/test_style.py | 16 ++++++++++++++++ 6 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 src/nitpick/exceptions.py diff --git a/Makefile b/Makefile index 7fff4502..f43899cf 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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 diff --git a/poetry.lock b/poetry.lock index b3791568..4d639769 100644 --- a/poetry.lock +++ b/poetry.lock @@ -734,7 +734,7 @@ version = "0.1.7" [[package]] category = "dev" description = "Enable Unicode input and display when running Python from Windows console." -marker = "sys_platform == \"win32\" and python_version < \"3.6\" and python_version >= \"3.4\" or sys_platform == \"win32\" and python_version < \"3.6\"" +marker = "sys_platform == \"win32\" and python_version < \"3.6\" or sys_platform == \"win32\" and python_version < \"3.6\" and python_version >= \"3.4\"" name = "win-unicode-console" optional = false python-versions = "*" diff --git a/src/nitpick/config.py b/src/nitpick/config.py index 5932a3af..56cf1be3 100644 --- a/src/nitpick/config.py +++ b/src/nitpick/config.py @@ -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 @@ -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 diff --git a/src/nitpick/exceptions.py b/src/nitpick/exceptions.py new file mode 100644 index 00000000..774d974e --- /dev/null +++ b/src/nitpick/exceptions.py @@ -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) diff --git a/src/nitpick/style.py b/src/nitpick/style.py index d2c5fab2..8812676b 100644 --- a/src/nitpick/style.py +++ b/src/nitpick/style.py @@ -6,6 +6,7 @@ import requests from slugify import slugify +from toml import TomlDecodeError from nitpick.constants import ( DEFAULT_NITPICK_STYLE_URL, @@ -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 @@ -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: diff --git a/tests/test_style.py b/tests/test_style.py index 5b68dc14..189ab25a 100644 --- a/tests/test_style.py +++ b/tests/test_style.py @@ -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, + )