Auto merge of #14092 - kivikakk:sort-check, r=Wafflespeanut

Sort check on JSON

<!-- Please describe your changes on the following line: -->
Check that JSON keys are ordered, and that there's no duplicates (for `resources/prefs.json` and `resources/package-prefs.json`).

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x]  🆕 `./mach test-tidy --self-test` does not report any errors
- [x] These changes fix #12283

<!-- Either: -->
- [x] There are tests for these changes 🎉

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/14092)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-11-07 07:41:34 -06:00 committed by GitHub
commit 94d0c485e1
6 changed files with 107 additions and 75 deletions

View file

@ -27,6 +27,7 @@ CONFIG_FILE_PATH = os.path.join(".", "servo-tidy.toml")
config = {
"skip-check-length": False,
"skip-check-licenses": False,
"check-ordered-json-keys": [],
"ignore": {
"files": [
"./.", # ignore hidden files
@ -636,23 +637,37 @@ def check_for_possible_duplicate_json_keys(key_value_pairs):
seen_keys = set()
for key in keys:
if key in seen_keys:
raise KeyError(key)
raise KeyError("Duplicated Key (%s)" % key)
seen_keys.add(key)
def check_for_alphabetical_sorted_json_keys(key_value_pairs):
for a, b in zip(key_value_pairs[:-1], key_value_pairs[1:]):
if a[0] > b[0]:
raise KeyError("Unordered key (found %s before %s)" % (a[0], b[0]))
def check_json_requirements(filename):
def check_fn(key_value_pairs):
check_for_possible_duplicate_json_keys(key_value_pairs)
if filename in config["check-ordered-json-keys"]:
check_for_alphabetical_sorted_json_keys(key_value_pairs)
return check_fn
def check_json(filename, contents):
if not filename.endswith(".json"):
raise StopIteration
try:
json.loads(contents, object_pairs_hook=check_for_possible_duplicate_json_keys)
json.loads(contents, object_pairs_hook=check_json_requirements(filename))
except ValueError as e:
match = re.search(r"line (\d+) ", e.message)
line_no = match and match.group(1)
yield (line_no, e.message)
except KeyError as e:
yield (None, "Duplicated Key (%s)" % e.message)
yield (None, e.message)
def check_spec(file_name, lines):

View file

@ -171,6 +171,12 @@ class CheckTidiness(unittest.TestCase):
self.assertEqual('Duplicated Key (the_duplicated_key)', errors.next()[2])
self.assertNoMoreErrors(errors)
def test_json_with_unordered_keys(self):
tidy.config["check-ordered-json-keys"].append('python/tidy/servo_tidy_tests/unordered_key.json')
errors = tidy.collect_errors_for_files(iterFile('unordered_key.json'), [tidy.check_json], [], print_text=False)
self.assertEqual('Unordered key (found b before a)', errors.next()[2])
self.assertNoMoreErrors(errors)
def test_lock(self):
errors = tidy.collect_errors_for_files(iterFile('duplicated_package.lock'), [tidy.check_lock], [], print_text=False)
msg = """duplicate versions for package "test"

View file

@ -0,0 +1,7 @@
{
"key": "value",
"other_key": {
"b": 1,
"a": 2
}
}