diff --git a/presubmit_canned_checks.py b/presubmit_canned_checks.py index a6f6b4ca9d..3ea20725c0 100644 --- a/presubmit_canned_checks.py +++ b/presubmit_canned_checks.py @@ -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 diff --git a/tests/presubmit_unittest.py b/tests/presubmit_unittest.py index dfbb8bf22d..c06c5d88bc 100755 --- a/tests/presubmit_unittest.py +++ b/tests/presubmit_unittest.py @@ -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.