mirror of
https://github.com/servo/servo.git
synced 2025-06-06 16:45:39 +00:00
[tidy] check for duplicate keys in .json files
This commit is contained in:
parent
9b2b101d19
commit
ff4cb7f80a
3 changed files with 25 additions and 1 deletions
|
@ -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):
|
||||
|
|
7
python/tidy/servo_tidy_tests/duplicate_key.json
Normal file
7
python/tidy/servo_tidy_tests/duplicate_key.json
Normal file
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"key": "value",
|
||||
"other_key": {
|
||||
"the_duplicated_key": 1,
|
||||
"the_duplicated_key": 2
|
||||
}
|
||||
}
|
|
@ -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"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue