Skip to content

Commit

Permalink
feat: detect JSON files by extension, no need to declare them first
Browse files Browse the repository at this point in the history
Signed-off-by: Augusto W. Andreoli <andreoliwa@gmail.com>
  • Loading branch information
andreoliwa committed Jul 13, 2020
1 parent 3173bf7 commit 6f54480
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 45 deletions.
3 changes: 0 additions & 3 deletions docs/defaults.rst
Expand Up @@ -185,9 +185,6 @@ Content of `styles/package-json.toml <https://raw.githubusercontent.com/andreoli

.. code-block:: toml
[nitpick.JSONFile]
file_names = ["package.json"]
["package.json"]
contains_keys = ["name", "version", "repository.type", "repository.url", "release.plugins"]
Expand Down
8 changes: 0 additions & 8 deletions docs/nitpick_section.rst
Expand Up @@ -73,11 +73,3 @@ Styles can include other styles. Just provide a list of styles to include:
The styles will be merged following the sequence in the list.

If a key/value pair appears in more than one sub-style, it will be overridden; the last declared key/pair will prevail.

.. _nitpick-jsonfile:

[nitpick.JSONFile]
------------------

Configure the list of filenames that should be checked by the :py:class:`nitpick.plugins.json.JSONPlugin` class.
See :ref:`the default package.json style <default-package-json>` for an example of usage.
7 changes: 1 addition & 6 deletions docs/plugins.rst
Expand Up @@ -45,14 +45,9 @@ JSON files

Checker for any JSON file.

First, configure the list of files to be checked in the :ref:`[nitpick.JSONFile] section <nitpick-jsonfile>`.

Then add the configuration for the file name you just declared.
Add the configurations for the file name you wish to check.
Example: :ref:`the default config for package.json <default-package-json>`.

If a JSON file is configured on ``[nitpick.JSONFile] file_names``, then a configuration for it should exist.
Otherwise, a style validation error will be raised.

.. _textplugin:

Text files
Expand Down
7 changes: 1 addition & 6 deletions src/nitpick/plugins/json.py
Expand Up @@ -28,13 +28,8 @@ class JSONFileSchema(BaseNitpickSchema):
class JSONPlugin(NitpickPlugin):
"""Checker for any JSON file.
First, configure the list of files to be checked in the :ref:`[nitpick.JSONFile] section <nitpick-jsonfile>`.
Then add the configuration for the file name you just declared.
Add the configurations for the file name you wish to check.
Example: :ref:`the default config for package.json <default-package-json>`.
If a JSON file is configured on ``[nitpick.JSONFile] file_names``, then a configuration for it should exist.
Otherwise, a style validation error will be raised.
"""

error_base_number = 340
Expand Down
10 changes: 0 additions & 10 deletions src/nitpick/schemas.py
Expand Up @@ -56,14 +56,6 @@ class NitpickStylesSectionSchema(BaseNitpickSchema):
include = PolyField(deserialization_schema_selector=fields.string_or_list_field)


class NitpickJSONFileSectionSchema(BaseNitpickSchema):
"""Validation schema for the ``[nitpick.JSONFile]`` section on the style file."""

error_messages = {"unknown": help_message("Unknown configuration", "nitpick_section.html#nitpick-jsonfile")}

file_names = fields.List(fields.String)


class SetupCfgSchema(BaseNitpickSchema):
"""Validation schema for setup.cfg."""

Expand All @@ -89,8 +81,6 @@ class NitpickSectionSchema(BaseNitpickSchema):
minimum_version = fields.NonEmptyString()
styles = fields.Nested(NitpickStylesSectionSchema)
files = fields.Nested(NitpickFilesSectionSchema)
# TODO: deprecate this field and remove all tests using it; it's not necessary anymore
JSONFile = fields.Nested(NitpickJSONFileSectionSchema)


class BaseStyleSchema(Schema):
Expand Down
13 changes: 13 additions & 0 deletions src/nitpick/style.py
@@ -1,5 +1,6 @@
"""Style files."""
import logging
import warnings
from collections import OrderedDict
from pathlib import Path
from typing import Dict, List, Optional, Set, Type
Expand All @@ -17,6 +18,7 @@
MERGED_STYLE_TOML,
NITPICK_STYLE_TOML,
NITPICK_STYLES_INCLUDE_JMEX,
PROJECT_NAME,
RAW_GITHUB_CONTENT_BASE_URL,
TOML_EXTENSION,
)
Expand Down Expand Up @@ -67,6 +69,17 @@ def validate_style(self, style_file_name: str, original_data: JsonDict):
"""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:
has_nitpick_jsonfile_section = style_errors.get(PROJECT_NAME, {}).pop("JSONFile", None)
if has_nitpick_jsonfile_section:
warnings.warn(
"The [nitpick.JSONFile] section is not needed anymore; just declare your JSON files directly",
DeprecationWarning,
)
if not style_errors[PROJECT_NAME]:
style_errors.pop(PROJECT_NAME)

if style_errors:
NitpickApp.current().add_style_error(
style_file_name, "Invalid config:", flatten_marshmallow_errors(style_errors)
Expand Down
3 changes: 0 additions & 3 deletions styles/package-json.toml
@@ -1,6 +1,3 @@
[nitpick.JSONFile]
file_names = ["package.json"]

["package.json"]
contains_keys = ["name", "version", "repository.type", "repository.url", "release.plugins"]

Expand Down
34 changes: 25 additions & 9 deletions tests/test_json.py
@@ -1,4 +1,6 @@
"""JSON tests."""
import warnings

from tests.helpers import ProjectMock


Expand Down Expand Up @@ -54,9 +56,6 @@ def test_missing_different_values(request):
"""Test missing and different values on the JSON file."""
ProjectMock(request).style(
'''
[nitpick.JSONFile]
file_names = ["my.json"]
["my.json".contains_json]
"some.dotted.root.key" = """
{ "valid": "JSON", "content": ["should", "be", "here"] , "dotted.subkeys" : ["should be preserved",
Expand Down Expand Up @@ -108,9 +107,6 @@ def test_invalid_json(request):
# pylint: disable=line-too-long
ProjectMock(request).style(
'''
[nitpick.JSONFile]
file_names = ["another.json"]
["another.json".contains_json]
some_field = """
{ "this": "is missing the end...
Expand All @@ -133,9 +129,6 @@ def test_json_configuration(request):
"""Test configuration for JSON files."""
ProjectMock(request).style(
"""
[nitpick.JSONFile]
file_names = ["your.json"]
["your.json".has]
an_extra = "key"
Expand All @@ -150,3 +143,26 @@ def test_json_configuration(request):
""",
1,
)


def test_jsonfile_deprecated(request):
"""Test configuration for JSON files."""
with warnings.catch_warnings(record=True) as captured:
# Cause all warnings to always be triggered.
warnings.simplefilter("always")

ProjectMock(request).style(
"""
[nitpick.JSONFile]
file_names = ["my.json"]
["my.json"]
contains_keys = ["x"]
"""
).save_file("my.json", '{"x":1}').flake8().assert_no_errors()

assert len(captured) == 2 # Twice, one for the original style file, one for the merged style
assert issubclass(captured[-1].category, DeprecationWarning)
assert "The [nitpick.JSONFile] section is not needed anymore; just declare your JSON files directly" in str(
captured[-1].message
)

0 comments on commit 6f54480

Please sign in to comment.