Skip to content

Commit

Permalink
fix: check a YAML file with the text plugin (#249)
Browse files Browse the repository at this point in the history
The merged style is not being validated anymore, only individual style files.
  • Loading branch information
andreoliwa authored Jan 15, 2021
1 parent f9090c0 commit 1821962
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 106 deletions.
135 changes: 67 additions & 68 deletions poetry.lock

Large diffs are not rendered by default.

11 changes: 1 addition & 10 deletions src/nitpick/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@
from identify import identify

from nitpick.app import NitpickApp
from nitpick.constants import (
MERGED_STYLE_TOML,
NITPICK_MINIMUM_VERSION_JMEX,
PROJECT_NAME,
TOOL_NITPICK,
TOOL_NITPICK_JMEX,
)
from nitpick.constants import NITPICK_MINIMUM_VERSION_JMEX, PROJECT_NAME, TOOL_NITPICK, TOOL_NITPICK_JMEX
from nitpick.exceptions import Deprecation
from nitpick.formats import TOMLFormat
from nitpick.generic import search_dict, version_to_tuple
Expand Down Expand Up @@ -69,9 +63,6 @@ def merge_styles(self) -> YieldFlake8Error:
style.find_initial_styles(configured_styles)

self.style_dict = style.merge_toml_dict()
if not NitpickApp.current().style_errors:
# Don't show duplicated errors: if there are style errors already, don't validate the merged style.
style.validate_style(MERGED_STYLE_TOML, self.style_dict, True)

from nitpick.flake8 import NitpickExtension # pylint: disable=import-outside-toplevel

Expand Down
4 changes: 4 additions & 0 deletions src/nitpick/plugins/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class NitpickPlugin(NitpickMixin, metaclass=abc.ABCMeta):
#: Which ``identify`` tags this :py:class:`nitpick.plugins.base.NitpickPlugin` child recognises.
identify_tags = set() # type: Set[str]

skip_empty_suggestion = False

def __init__(self, path_from_root: str = None) -> None:
if path_from_root is not None:
self.file_name = path_from_root
Expand Down Expand Up @@ -75,6 +77,8 @@ def process(self, config: JsonDict) -> YieldFlake8Error:

if config_data_exists and not file_exists:
suggestion = self.suggest_initial_contents()
if not suggestion and self.skip_empty_suggestion:
return
phrases = [" was not found"]
message = NitpickApp.current().config.nitpick_files_section.get(self.file_name)
if message and isinstance(message, str):
Expand Down
8 changes: 6 additions & 2 deletions src/nitpick/plugins/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ class TextPlugin(NitpickPlugin):
"""

error_base_number = 350
identify_tags = {"plain-text"}
identify_tags = {"text"}
validation_schema = TextSchema

#: All other files are also text files, and they already have a suggested content message
# TODO: this is a hack to avoid rethinking the whole schema validation now (this will have to be done some day)
skip_empty_suggestion = True

def _expected_lines(self):
return [obj.get("line") for obj in self.file_dict.get("contains", {})]

Expand All @@ -73,6 +77,6 @@ def plugin_class() -> Type["NitpickPlugin"]:
@hookimpl
def handler(file_name: str, tags: Set[str]) -> Optional["NitpickPlugin"]:
"""Handle text files."""
if "plain-text" in tags:
if "text" in tags:
return TextPlugin(file_name)
return None
44 changes: 18 additions & 26 deletions src/nitpick/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,39 +67,18 @@ def find_initial_styles(self, configured_styles: StrOrList):

self.include_multiple_styles(chosen_styles)

def validate_style(self, style_file_name: str, original_data: JsonDict, is_merged_style: bool):
"""Validate a style file (TOML) against a Marshmallow schema."""
self.rebuild_dynamic_schema(original_data)
style_errors = self._dynamic_schema_class().validate(original_data)

if style_errors:
Deprecation.jsonfile_section(style_errors, is_merged_style)

if style_errors:
NitpickApp.current().add_style_error(
style_file_name, "Invalid config:", flatten_marshmallow_errors(style_errors)
)

def validate_schema(
self,
path_from_root: str,
schema: Type[Schema],
original_data: JsonDict,
is_merged_style: bool,
):
@staticmethod
def validate_schema(schema: Type[Schema], path_from_root: str, original_data: JsonDict) -> Dict[str, List[str]]:
"""Validate the schema for the file."""
if not schema:
return
return {}

inherited_schema = schema is not BaseStyleSchema
data_to_validate = original_data if inherited_schema else {path_from_root: None}
local_errors = schema().validate(data_to_validate)
if local_errors and inherited_schema:
local_errors = {path_from_root: local_errors}

if local_errors:
Deprecation.jsonfile_section(local_errors, is_merged_style)
self.style_errors.update(local_errors)
return local_errors

def include_multiple_styles(self, chosen_styles: StrOrList) -> None:
"""Include a list of styles (or just one) into this style tree."""
Expand Down Expand Up @@ -154,8 +133,20 @@ def _validate_config(self, config_dict: dict) -> dict:
]
if not schemas:
self.style_errors[key] = [BaseStyleSchema.error_messages["unknown"]]

all_errors = {}
valid_schema = False
for schema in schemas:
self.validate_schema(cleaner.path_from_root, schema, value_dict, False)
errors = self.validate_schema(schema, cleaner.path_from_root, value_dict)
if not errors:
# When multiple schemas match a file type, exit when a valid schema is found
valid_schema = True
break
all_errors.update(errors)

if not valid_schema:
Deprecation.jsonfile_section(all_errors, False)
self.style_errors.update(all_errors)
return toml_dict

def get_style_path(self, style_uri: str) -> Optional[Path]:
Expand Down Expand Up @@ -254,6 +245,7 @@ def merge_toml_dict(self) -> JsonDict:
if not app.cache_dir:
return {}
merged_dict = self._all_styles.merge()
# TODO: check if the merged style file is still needed
merged_style_path = app.cache_dir / MERGED_STYLE_TOML # type: Path
toml = TOMLFormat(data=merged_dict)

Expand Down
15 changes: 15 additions & 0 deletions tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,18 @@ def test_text_file_contains_line(request):
qqq\x1b[0m
"""
)


def test_yaml_file_as_text(request):
"""A YAML file is also a text file, so it could be checked with the text plugin."""
ProjectMock(request).style(
"""
[[".gitlab-ci.yml".contains]]
line = " - mypy -p ims --junit-xml report-mypy.xml"
"""
).save_file(".gitlab-ci.yml", "def\nghi\nwww").flake8().assert_errors_contain(
"""
NIP352 File .gitlab-ci.yml has missing lines:\x1b[32m
- mypy -p ims --junit-xml report-mypy.xml\x1b[0m
"""
)

0 comments on commit 1821962

Please sign in to comment.