Skip to content

Commit

Permalink
feat(json): suggest content when file doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoliwa committed Aug 5, 2019
1 parent 6591fab commit 502eb3d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
25 changes: 17 additions & 8 deletions nitpick/files/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from nitpick.files.base import BaseFile
from nitpick.formats import JsonFormat
from nitpick.generic import flatten, unflatten
from nitpick.typedefs import YieldFlake8Error
from nitpick.typedefs import JsonDict, YieldFlake8Error

KEY_CONTAINS_KEYS = "contains_keys"
KEY_CONTAINS_JSON = "contains_json"
Expand All @@ -16,25 +16,34 @@ class JsonFile(BaseFile):
has_multiple_files = True
error_base_number = 340

SOME_VALUE_PLACEHOLDER = "<some value here>"

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()

def get_suggested_json(self, raw_actual: JsonDict = None) -> JsonDict:
"""Return the suggested JSON based on actual values."""
actual = set(flatten(raw_actual).keys()) if raw_actual else set()
expected = set(self.file_dict.get(KEY_CONTAINS_KEYS) or [])
missing = expected - actual
if not missing:
return {}

return SortedDict(unflatten({key: self.SOME_VALUE_PLACEHOLDER for key in missing}))

def suggest_initial_contents(self) -> str:
"""Suggest the initial content for this missing file."""
return "" # FIXME:
suggestion = self.get_suggested_json()
return JsonFormat(data=suggestion).reformatted if suggestion else ""

def _check_contained_keys(self) -> YieldFlake8Error:
json_f = JsonFormat(path=self.file_path)
actual = set(flatten(json_f.as_data).keys())
expected = set(self.file_dict.get(KEY_CONTAINS_KEYS) or [])
missing = expected - actual
if not missing:
suggested_json = self.get_suggested_json(json_f.as_data)
if not suggested_json:
return

suggested_json = SortedDict(unflatten({key: "?" for key in missing}))
yield self.flake8_error(8, " has missing keys:", JsonFormat(data=suggested_json).reformatted)

# def _check_contained_json(self) -> YieldFlake8Error:
Expand Down
32 changes: 26 additions & 6 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,31 @@

def test_suggest_initial_contents(request):
"""Suggest initial contents for missing JSON file."""
pass # FIXME:
ProjectMock(request).load_styles("package-json").pyproject_toml(
"""
[tool.nitpick]
style = ["package-json"]
"""
).lint().assert_errors_contain(
"""
NIP341 File package.json was not found. Create it with this content:\x1b[92m
{
"name": "<some value here>",
"release": {
"plugins": "<some value here>"
},
"repository": {
"type": "<some value here>",
"url": "<some value here>"
},
"version": "<some value here>"
}\x1b[0m
"""
)


def test_missing_different_values(request):
"""Test missing and different values."""
def test_json_file_contains_keys(request):
"""Test if JSON file contains keys."""
ProjectMock(request).load_styles("package-json").pyproject_toml(
"""
[tool.nitpick]
Expand All @@ -19,11 +39,11 @@ def test_missing_different_values(request):
NIP348 File package.json has missing keys:\x1b[92m
{
"release": {
"plugins": "?"
"plugins": "<some value here>"
},
"repository": {
"type": "?",
"url": "?"
"type": "<some value here>",
"url": "<some value here>"
}
}\x1b[0m
"""
Expand Down

0 comments on commit 502eb3d

Please sign in to comment.