PRESUBMIT.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Copyright 2020 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. """Chromium presubmit script for src/components/autofill.
  5. See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
  6. for more details on the presubmit API built into depot_tools.
  7. """
  8. USE_PYTHON3 = True
  9. def _CheckNoBaseTimeCalls(input_api, output_api):
  10. """Checks that no files call base::Time::Now() or base::TimeTicks::Now()."""
  11. pattern = input_api.re.compile(
  12. r'(base::(Time|TimeTicks)::Now)\(\)',
  13. input_api.re.MULTILINE)
  14. files = []
  15. for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
  16. if (f.LocalPath().startswith('components/autofill/') and
  17. not f.LocalPath().endswith("PRESUBMIT.py")):
  18. contents = input_api.ReadFile(f)
  19. if pattern.search(contents):
  20. files.append(f)
  21. if len(files):
  22. return [ output_api.PresubmitPromptWarning(
  23. 'Consider to not call base::Time::Now() or base::TimeTicks::Now() ' +
  24. 'directly but use AutofillClock::Now() and '+
  25. 'Autofill::TickClock::NowTicks(), respectively. These clocks can be ' +
  26. 'manipulated through TestAutofillClock and TestAutofillTickClock '+
  27. 'for testing purposes, and using AutofillClock and AutofillTickClock '+
  28. 'throughout Autofill code makes sure Autofill tests refers to the '+
  29. 'same (potentially manipulated) clock.',
  30. files) ]
  31. return []
  32. def _CheckNoServerFieldTypeCasts(input_api, output_api):
  33. """Checks that no files cast (e.g., raw integers to) ServerFieldTypes."""
  34. pattern = input_api.re.compile(
  35. r'_cast<\s*ServerFieldType\b',
  36. input_api.re.MULTILINE)
  37. files = []
  38. for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
  39. if (f.LocalPath().startswith('components/autofill/') and
  40. not f.LocalPath().endswith("PRESUBMIT.py")):
  41. contents = input_api.ReadFile(f)
  42. if pattern.search(contents):
  43. files.append(f)
  44. if len(files):
  45. return [ output_api.PresubmitPromptWarning(
  46. 'Do not cast raw integers to ServerFieldType to prevent values that ' +
  47. 'have no corresponding enum constant or are deprecated. Use '+
  48. 'ToSafeServerFieldType() instead.',
  49. files) ]
  50. return []
  51. def _CheckFeatureNames(input_api, output_api):
  52. """Checks that no features are enabled."""
  53. pattern = input_api.re.compile(
  54. r'\bbase::Feature\s+k(\w*)\s*{\s*"(\w*)"',
  55. input_api.re.MULTILINE)
  56. warnings = []
  57. def exception(constant, feature):
  58. if constant == "AutofillAddressEnhancementVotes" and \
  59. feature == "kAutofillAddressEnhancementVotes":
  60. return True
  61. return False
  62. for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
  63. if (f.LocalPath().startswith('components/autofill/') and
  64. f.LocalPath().endswith('features.cc')):
  65. contents = input_api.ReadFile(f)
  66. mismatches = [(constant, feature)
  67. for (constant, feature) in pattern.findall(contents)
  68. if constant != feature and not exception(constant, feature)]
  69. if mismatches:
  70. mismatch_strings = ['\t{} -- {}'.format(*m) for m in mismatches]
  71. mismatch_string = format('\n').join(mismatch_strings)
  72. warnings += [ output_api.PresubmitPromptWarning(
  73. 'Feature names should be identical to variable names:\n{}'
  74. .format(mismatch_string),
  75. [f]) ]
  76. return warnings
  77. def _CheckWebViewExposedExperiments(input_api, output_api):
  78. """Checks that changes to autofill features are exposed to webview."""
  79. _PRODUCTION_SUPPORT_FILE = ('android_webview/java/src/org/chromium/' +
  80. 'android_webview/common/ProductionSupportedFlagList.java')
  81. _GENERATE_FLAG_LABELS_PY = 'android_webview/tools/generate_flag_labels.py'
  82. def is_autofill_features_file(f):
  83. return (f.LocalPath().startswith('components/autofill/') and
  84. f.LocalPath().endswith('features.cc'))
  85. def is_webview_features_file(f):
  86. return f.LocalPath() == _PRODUCTION_SUPPORT_FILE
  87. def any_file_matches(matcher):
  88. return any(matcher(f) for f in input_api.change.AffectedTestableFiles())
  89. warnings = []
  90. if (any_file_matches(is_autofill_features_file)
  91. and not any_file_matches(is_webview_features_file)):
  92. warnings += [
  93. output_api.PresubmitPromptWarning((
  94. 'You may need to modify {} and run {} and follow its '+
  95. 'instructions if your feature affects WebView.'
  96. ).format(_PRODUCTION_SUPPORT_FILE, _GENERATE_FLAG_LABELS_PY))
  97. ]
  98. return warnings
  99. def _CommonChecks(input_api, output_api):
  100. """Checks common to both upload and commit."""
  101. results = []
  102. results.extend(_CheckNoBaseTimeCalls(input_api, output_api))
  103. results.extend(_CheckNoServerFieldTypeCasts(input_api, output_api))
  104. results.extend(_CheckFeatureNames(input_api, output_api))
  105. results.extend(_CheckWebViewExposedExperiments(input_api, output_api))
  106. return results
  107. def CheckChangeOnUpload(input_api, output_api):
  108. results = []
  109. results.extend(_CommonChecks(input_api, output_api))
  110. return results
  111. def CheckChangeOnCommit(input_api, output_api):
  112. results = []
  113. results.extend(_CommonChecks(input_api, output_api))
  114. return results