From 502eb3d6cd13e16f9aa8ebcfa894a3d141435b3d Mon Sep 17 00:00:00 2001 From: Augusto Wagner Andreoli Date: Mon, 5 Aug 2019 21:33:23 +0200 Subject: [PATCH] feat(json): suggest content when file doesn't exist --- nitpick/files/json.py | 25 +++++++++++++++++-------- tests/test_json.py | 32 ++++++++++++++++++++++++++------ 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/nitpick/files/json.py b/nitpick/files/json.py index 87214ef7..5909df7e 100644 --- a/nitpick/files/json.py +++ b/nitpick/files/json.py @@ -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" @@ -16,25 +16,34 @@ class JsonFile(BaseFile): has_multiple_files = True error_base_number = 340 + SOME_VALUE_PLACEHOLDER = "" + 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: diff --git a/tests/test_json.py b/tests/test_json.py index 35f1e2f4..b6c71319 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -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": "", + "release": { + "plugins": "" + }, + "repository": { + "type": "", + "url": "" + }, + "version": "" + }\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] @@ -19,11 +39,11 @@ def test_missing_different_values(request): NIP348 File package.json has missing keys:\x1b[92m { "release": { - "plugins": "?" + "plugins": "" }, "repository": { - "type": "?", - "url": "?" + "type": "", + "url": "" } }\x1b[0m """