mirror of
https://github.com/servo/servo.git
synced 2025-06-12 10:24:43 +00:00
Check for JSON key non-duplication and order
This commit is contained in:
parent
c62ce53efa
commit
d47aca18c4
4 changed files with 35 additions and 3 deletions
|
@ -27,6 +27,7 @@ CONFIG_FILE_PATH = os.path.join(".", "servo-tidy.toml")
|
||||||
config = {
|
config = {
|
||||||
"skip-check-length": False,
|
"skip-check-length": False,
|
||||||
"skip-check-licenses": False,
|
"skip-check-licenses": False,
|
||||||
|
"check-ordered-json-keys": [],
|
||||||
"ignore": {
|
"ignore": {
|
||||||
"files": [
|
"files": [
|
||||||
"./.", # ignore hidden files
|
"./.", # ignore hidden files
|
||||||
|
@ -636,23 +637,37 @@ def check_for_possible_duplicate_json_keys(key_value_pairs):
|
||||||
seen_keys = set()
|
seen_keys = set()
|
||||||
for key in keys:
|
for key in keys:
|
||||||
if key in seen_keys:
|
if key in seen_keys:
|
||||||
raise KeyError(key)
|
raise KeyError("Duplicated Key (%s)" % key)
|
||||||
|
|
||||||
seen_keys.add(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):
|
def check_json(filename, contents):
|
||||||
if not filename.endswith(".json"):
|
if not filename.endswith(".json"):
|
||||||
raise StopIteration
|
raise StopIteration
|
||||||
|
|
||||||
try:
|
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:
|
except ValueError as e:
|
||||||
match = re.search(r"line (\d+) ", e.message)
|
match = re.search(r"line (\d+) ", e.message)
|
||||||
line_no = match and match.group(1)
|
line_no = match and match.group(1)
|
||||||
yield (line_no, e.message)
|
yield (line_no, e.message)
|
||||||
except KeyError as e:
|
except KeyError as e:
|
||||||
yield (None, "Duplicated Key (%s)" % e.message)
|
yield (None, e.message)
|
||||||
|
|
||||||
|
|
||||||
def check_spec(file_name, lines):
|
def check_spec(file_name, lines):
|
||||||
|
|
|
@ -171,6 +171,12 @@ class CheckTidiness(unittest.TestCase):
|
||||||
self.assertEqual('Duplicated Key (the_duplicated_key)', errors.next()[2])
|
self.assertEqual('Duplicated Key (the_duplicated_key)', errors.next()[2])
|
||||||
self.assertNoMoreErrors(errors)
|
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):
|
def test_lock(self):
|
||||||
errors = tidy.collect_errors_for_files(iterFile('duplicated_package.lock'), [tidy.check_lock], [], print_text=False)
|
errors = tidy.collect_errors_for_files(iterFile('duplicated_package.lock'), [tidy.check_lock], [], print_text=False)
|
||||||
msg = """duplicate versions for package "test"
|
msg = """duplicate versions for package "test"
|
||||||
|
|
7
python/tidy/servo_tidy_tests/unordered_key.json
Normal file
7
python/tidy/servo_tidy_tests/unordered_key.json
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"key": "value",
|
||||||
|
"other_key": {
|
||||||
|
"b": 1,
|
||||||
|
"a": 2
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,10 @@
|
||||||
[configs]
|
[configs]
|
||||||
skip-check-length = false
|
skip-check-length = false
|
||||||
skip-check-licenses = false
|
skip-check-licenses = false
|
||||||
|
check-ordered-json-keys = [
|
||||||
|
"./resources/prefs.json",
|
||||||
|
"./resources/package-prefs.json",
|
||||||
|
]
|
||||||
|
|
||||||
[ignore]
|
[ignore]
|
||||||
# Ignored packages with duplicated versions
|
# Ignored packages with duplicated versions
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue