diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2016-07-10 19:15:35 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-10 19:15:35 -0700 |
commit | 638eb80110cf07ec2f07e43fd64a48042fe2925b (patch) | |
tree | b8da18826d968115b0596db2c92cedf7a749cfce | |
parent | 68aca87e305138b2851c853149e0606756eb02b8 (diff) | |
parent | ff4cb7f80a0560e0d4d72e44de71d39fada1d171 (diff) | |
download | servo-638eb80110cf07ec2f07e43fd64a48042fe2925b.tar.gz servo-638eb80110cf07ec2f07e43fd64a48042fe2925b.zip |
Auto merge of #12318 - tallowen:tidy-features, r=frewsxcv
[tidy] prevent duplicate json keys
Throws a tidy error when duplicated keys are present.

---
<!-- 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] These changes fix #12283 (github issue number if applicable).
<!-- Either: -->
- [X] There are tests for these changes OR
- [ ] These changes do not require tests because _____
<!-- 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/12318)
<!-- Reviewable:end -->
-rw-r--r-- | python/tidy/servo_tidy/tidy.py | 14 | ||||
-rw-r--r-- | python/tidy/servo_tidy_tests/duplicate_key.json | 7 | ||||
-rw-r--r-- | python/tidy/servo_tidy_tests/test_tidy.py | 5 |
3 files changed, 25 insertions, 1 deletions
diff --git a/python/tidy/servo_tidy/tidy.py b/python/tidy/servo_tidy/tidy.py index 234c50727d5..093e8f3aeee 100644 --- a/python/tidy/servo_tidy/tidy.py +++ b/python/tidy/servo_tidy/tidy.py @@ -524,16 +524,28 @@ def check_webidl_spec(file_name, contents): yield (0, "No specification link found.") +def check_for_possible_duplicate_json_keys(key_value_pairs): + keys = [x[0] for x in key_value_pairs] + seen_keys = set() + for key in keys: + if key in seen_keys: + raise KeyError(key) + + seen_keys.add(key) + + def check_json(filename, contents): if not filename.endswith(".json"): raise StopIteration try: - json.loads(contents) + json.loads(contents, object_pairs_hook=check_for_possible_duplicate_json_keys) 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) def check_spec(file_name, lines): diff --git a/python/tidy/servo_tidy_tests/duplicate_key.json b/python/tidy/servo_tidy_tests/duplicate_key.json new file mode 100644 index 00000000000..6998c789ac9 --- /dev/null +++ b/python/tidy/servo_tidy_tests/duplicate_key.json @@ -0,0 +1,7 @@ +{ + "key": "value", + "other_key": { + "the_duplicated_key": 1, + "the_duplicated_key": 2 + } +} diff --git a/python/tidy/servo_tidy_tests/test_tidy.py b/python/tidy/servo_tidy_tests/test_tidy.py index 096fb5676b2..8db19a5efd7 100644 --- a/python/tidy/servo_tidy_tests/test_tidy.py +++ b/python/tidy/servo_tidy_tests/test_tidy.py @@ -109,6 +109,11 @@ class CheckTidiness(unittest.TestCase): self.assertEqual('Invalid control character at: line 3 column 40 (char 61)', errors.next()[2]) self.assertNoMoreErrors(errors) + def test_json_with_duplicate_key(self): + errors = tidy.collect_errors_for_files(iterFile('duplicate_key.json'), [tidy.check_json], [], print_text=False) + self.assertEqual('Duplicated Key (the_duplicated_key)', 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" |