Commit aca5b6ac authored by Aaron Gable's avatar Aaron Gable Committed by Commit Bot

Add JSON-parsing presubmit check

This is followup from the malformed-json bug which took down
Sheriff-o-Matic on 2019-05-16. This CL just adds the canned check;
future CLs can either add it to PanProjectChecks or add it to
other PRESUBMIT files directly.

Change-Id: I4a445193c1744966a448b12c7eb2915873e484d9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1617941
Commit-Queue: Aaron Gable <agable@chromium.org>
Reviewed-by: 's avatarSean McCullough <seanmccullough@chromium.org>
parent d6bf517d
......@@ -1468,3 +1468,21 @@ def CheckLucicfgGenOutput(input_api, output_api, entry_script):
},
output_api.PresubmitError)
]
# TODO(agable): Add this to PanProjectChecks.
def CheckJsonParses(input_api, output_api):
"""Verifies that all JSON files at least parse as valid JSON."""
import json
affected_files = input_api.AffectedFiles(
include_deletes=False,
file_filter=lambda x: x.LocalPath().endswith('.json'))
warnings = []
for f in affected_files:
with open(f.AbsoluteLocalPath()) as j:
try:
json.load(j)
except ValueError:
# Just a warning for now, in case people are using JSON5 somewhere.
warnings.append(output_api.PresubmitPromptWarning(
'%s does not appear to be valid JSON.' % f.LocalPath()))
return warnings
......@@ -1670,7 +1670,7 @@ class CannedChecksUnittest(PresubmitTestsBase):
'GetPythonUnitTests', 'GetPylint',
'GetUnitTests', 'GetUnitTestsInDirectory', 'GetUnitTestsRecursively',
'CheckCIPDManifest', 'CheckCIPDPackages', 'CheckCIPDClientDigests',
'CheckChangedLUCIConfigs', 'CheckLucicfgGenOutput',
'CheckChangedLUCIConfigs', 'CheckLucicfgGenOutput', 'CheckJsonParses',
'print_function',
]
# If this test fails, you should add the relevant test.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment