Skip to content

Commit

Permalink
feat(json): check if a JSON file contains the specified JSON data
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoliwa committed Aug 5, 2019
1 parent aa1bc1d commit 47fa133
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
20 changes: 14 additions & 6 deletions nitpick/files/json.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Checker for JSON files."""
import json

from sortedcontainers import SortedDict

from nitpick.files.base import BaseFile
Expand All @@ -22,7 +24,7 @@ def check_rules(self) -> YieldFlake8Error:
"""Check missing keys and JSON content."""
for _ in self.multiple_files:
yield from self._check_contained_keys()
# FIXME: # yield from self._check_contained_json()
yield from self._check_contained_json()

def get_suggested_json(self, raw_actual: JsonDict = None) -> JsonDict:
"""Return the suggested JSON based on actual values."""
Expand All @@ -40,12 +42,18 @@ def suggest_initial_contents(self) -> str:
return JsonFormat(data=suggestion).reformatted if suggestion else ""

def _check_contained_keys(self) -> YieldFlake8Error:
json_f = JsonFormat(path=self.file_path)
suggested_json = self.get_suggested_json(json_f.as_data)
json_fmt = JsonFormat(path=self.file_path)
suggested_json = self.get_suggested_json(json_fmt.as_data)
if not suggested_json:
return
yield self.flake8_error(8, " has missing keys:", JsonFormat(data=suggested_json).reformatted)

# def _check_contained_json(self) -> YieldFlake8Error:
# # fixme key is a jmespath expression, value is valid JSON
# return
def _check_contained_json(self) -> YieldFlake8Error:
actual_fmt = JsonFormat(path=self.file_path)
expected = {
# FIXME: deal with invalid JSON
# TODO: accept key as a jmespath expression, value is valid JSON
key: json.loads(json_string)
for key, json_string in (self.file_dict.get(KEY_CONTAINS_JSON) or {}).items()
}
yield from self.warn_missing_different(JsonFormat(data=actual_fmt.as_data).compare_with_dictdiffer(expected))
7 changes: 7 additions & 0 deletions nitpick/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ def compare_with_dictdiffer(self, expected: Union[JsonDict, "BaseFormat"] = None
return comparison


# FIXME: rename to TomlFormat
class Toml(BaseFormat):
"""TOML configuration format."""

Expand All @@ -216,6 +217,7 @@ def load(self) -> bool:
return True


# FIXME: rename to YamlFormat
class Yaml(BaseFormat):
"""YAML configuration format."""

Expand Down Expand Up @@ -281,3 +283,8 @@ def load(self) -> bool:
self._reformatted = json.dumps(self._data, sort_keys=True, indent=2)
self._loaded = True
return True

@classmethod
def cleanup(cls, *args: List[Any]) -> List[Any]:
"""Cleanup similar values according to the specific format. E.g.: Yaml accepts 'True' or 'true'."""
return list(args)
41 changes: 41 additions & 0 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,44 @@ def test_json_file_contains_keys(request):
}\x1b[0m
"""
)


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_root_key = """
{ "valid": "JSON", "content": ["should", "be", "here"] }
"""
formatting = """ {"doesnt":"matter","here":true,"on the": "config file"} """
'''
).save_file("my.json", '{"name":"myproject","formatting":{"on the":"actual file"}}').lint().assert_errors_contain(
"""
NIP348 File my.json has missing values:\x1b[92m
{
"formatting.doesnt": "matter",
"formatting.here": true,
"some_root_key.content": [
"should",
"be",
"here"
],
"some_root_key.valid": "JSON"
}\x1b[0m
"""
# FIXME:
# ).assert_errors_contain(
# """
# NIP349 File my.json has different values. Use this:\x1b[92m
# {
# "formatting": {
# "here": true,
# "on the": "config file"
# }
# }\x1b[0m
# """
)

0 comments on commit 47fa133

Please sign in to comment.