PRESUBMIT.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright 2018 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. """Presubmit tests for android_webview/support_library/
  5. Runs various style checks before upload.
  6. """
  7. USE_PYTHON3 = True
  8. def CheckChangeOnUpload(input_api, output_api):
  9. results = []
  10. results.extend(_CheckAnnotatedInvocationHandlers(input_api, output_api))
  11. results.extend(_CheckFeatureDevSuffix(input_api, output_api))
  12. return results
  13. def _CheckAnnotatedInvocationHandlers(input_api, output_api):
  14. """Checks that all references to InvocationHandlers are annotated with a
  15. comment describing the class the InvocationHandler represents. This does not
  16. check .../support_lib_boundary/util/, because this has legitimate reasons to
  17. refer to InvocationHandlers without them standing for a specific type.
  18. """
  19. invocation_handler_str = r'\bInvocationHandler\b'
  20. annotation_str = r'/\* \w+ \*/\s+'
  21. invocation_handler_import_pattern = input_api.re.compile(
  22. r'^import.*' + invocation_handler_str + ';$')
  23. possibly_annotated_handler_pattern = input_api.re.compile(
  24. r'(' + annotation_str + r')?(' + invocation_handler_str + r')')
  25. errors = []
  26. sources = lambda affected_file: input_api.FilterSourceFile(
  27. affected_file,
  28. files_to_skip=(input_api.DEFAULT_FILES_TO_SKIP +
  29. (r'.*support_lib_boundary[\\\/]util[\\\/].*',)),
  30. files_to_check=(r'.*\.java$',))
  31. for f in input_api.AffectedSourceFiles(sources):
  32. for line_num, line in f.ChangedContents():
  33. if not invocation_handler_import_pattern.search(line):
  34. for match in possibly_annotated_handler_pattern.findall(line):
  35. annotation = match[0]
  36. if not annotation:
  37. # Note: we intentionally double-count lines which have multiple
  38. # mistakes, since we require each mention of 'InvocationHandler' to
  39. # be annotated.
  40. errors.append("%s:%d" % (f.LocalPath(), line_num))
  41. results = []
  42. if errors:
  43. results.append(output_api.PresubmitPromptWarning("""
  44. All references to InvocationHandlers should be annotated with the type they
  45. represent using a comment, e.g.:
  46. /* RetType */ InvocationHandler method(/* ParamType */ InvocationHandler param);
  47. """,
  48. errors))
  49. return results
  50. def _CheckFeatureDevSuffix(input_api, output_api):
  51. """Checks that Features.DEV_SUFFIX is not used in boundary_interfaces. The
  52. right place to use it is SupportLibWebViewChromiumFactory.
  53. """
  54. pattern = input_api.re.compile(r'\bDEV_SUFFIX\b')
  55. problems = []
  56. filt = lambda f: 'boundary_interfaces' in f.LocalPath()
  57. for f in input_api.AffectedFiles(file_filter=filt):
  58. for line_num, line in f.ChangedContents():
  59. m = pattern.search(line)
  60. if m:
  61. problems.append(' %s:%d\n %s\n' % (f.LocalPath(), line_num, line))
  62. if not problems:
  63. return []
  64. return [output_api.PresubmitPromptWarning(
  65. 'DEV_SUFFIX should not be used in boundary_interfaces.\n' + '\n'
  66. .join(problems))]