PRESUBMIT.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # Copyright (c) 2012 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. """Top-level presubmit script for depot tools.
  5. See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts for
  6. details on the presubmit API built into depot_tools.
  7. """
  8. PRESUBMIT_VERSION = '2.0.0'
  9. USE_PYTHON3 = True
  10. import fnmatch
  11. import os
  12. import sys
  13. # Whether to run the checks under Python2 or Python3.
  14. # TODO: Uncomment this to run the checks under Python3, and change the tests
  15. # in _CommonChecks in this file and the values and tests in
  16. # //tests/PRESUBMIT.py as well to keep the test coverage of all three cases
  17. # (true, false, and default/not specified).
  18. # USE_PYTHON3 = False
  19. # CIPD ensure manifest for checking CIPD client itself.
  20. CIPD_CLIENT_ENSURE_FILE_TEMPLATE = r'''
  21. # Full supported.
  22. $VerifiedPlatform linux-amd64 mac-amd64 windows-amd64 windows-386
  23. # Best effort support.
  24. $VerifiedPlatform linux-386 linux-ppc64 linux-ppc64le linux-s390x
  25. $VerifiedPlatform linux-arm64 linux-armv6l
  26. $VerifiedPlatform linux-mips64 linux-mips64le linux-mipsle
  27. %s %s
  28. '''
  29. # Timeout for a test to be executed.
  30. TEST_TIMEOUT_S = 330 # 5m 30s
  31. def CheckPylint(input_api, output_api):
  32. """Gather all the pylint logic into one place to make it self-contained."""
  33. files_to_check = [
  34. r'^[^/]*\.py$',
  35. r'^testing_support/[^/]*\.py$',
  36. r'^tests/[^/]*\.py$',
  37. r'^recipe_modules/.*\.py$', # Allow recursive search in recipe modules.
  38. ]
  39. files_to_skip = list(input_api.DEFAULT_FILES_TO_SKIP)
  40. if os.path.exists('.gitignore'):
  41. with open('.gitignore') as fh:
  42. lines = [l.strip() for l in fh.readlines()]
  43. files_to_skip.extend([fnmatch.translate(l) for l in lines if
  44. l and not l.startswith('#')])
  45. if os.path.exists('.git/info/exclude'):
  46. with open('.git/info/exclude') as fh:
  47. lines = [l.strip() for l in fh.readlines()]
  48. files_to_skip.extend([fnmatch.translate(l) for l in lines if
  49. l and not l.startswith('#')])
  50. disabled_warnings = [
  51. 'R0401', # Cyclic import
  52. 'W0613', # Unused argument
  53. 'C0415', # import-outside-toplevel
  54. 'R1710', # inconsistent-return-statements
  55. 'E1101', # no-member
  56. 'E1120', # no-value-for-parameter
  57. 'R1708', # stop-iteration-return
  58. 'W1510', # subprocess-run-check
  59. # Checks which should be re-enabled after Python 2 support is removed.
  60. 'R0205', # useless-object-inheritance
  61. 'R1725', # super-with-arguments
  62. 'W0707', # raise-missing-from
  63. 'W1113', # keyword-arg-before-vararg
  64. ]
  65. return input_api.RunTests(input_api.canned_checks.GetPylint(
  66. input_api,
  67. output_api,
  68. files_to_check=files_to_check,
  69. files_to_skip=files_to_skip,
  70. disabled_warnings=disabled_warnings,
  71. version='2.7'), parallel=False)
  72. def CheckRecipes(input_api, output_api):
  73. file_filter = lambda x: x.LocalPath() == 'infra/config/recipes.cfg'
  74. return input_api.canned_checks.CheckJsonParses(input_api, output_api,
  75. file_filter=file_filter)
  76. def CheckUsePython3(input_api, output_api):
  77. results = []
  78. if sys.version_info.major != 3:
  79. results.append(
  80. output_api.PresubmitError(
  81. 'Did not use Python3 for //tests/PRESUBMIT.py.'))
  82. return results
  83. def CheckJsonFiles(input_api, output_api):
  84. return input_api.canned_checks.CheckJsonParses(
  85. input_api, output_api)
  86. def CheckUnitTestsOnCommit(input_api, output_api):
  87. """ Do not run integration tests on upload since they are way too slow."""
  88. input_api.SetTimeout(TEST_TIMEOUT_S)
  89. # We still support python2 presubmit tests, so we need to test them. We don't
  90. # run py3 here as the code below will start those tests
  91. tests = input_api.canned_checks.GetUnitTestsInDirectory(
  92. input_api,
  93. output_api,
  94. 'tests',
  95. files_to_check=[
  96. r'.*presubmit_unittest\.py$',
  97. ],
  98. run_on_python2=True,
  99. run_on_python3=False)
  100. # Run only selected tests on Windows.
  101. test_to_run_list = [r'.*test\.py$']
  102. tests_to_skip_list = []
  103. if input_api.platform.startswith(('cygwin', 'win32')):
  104. print('Warning: skipping most unit tests on Windows')
  105. tests_to_skip_list.extend([
  106. r'.*auth_test\.py$',
  107. r'.*git_common_test\.py$',
  108. r'.*git_hyper_blame_test\.py$',
  109. r'.*git_map_test\.py$',
  110. r'.*ninjalog_uploader_test\.py$',
  111. r'.*recipes_test\.py$',
  112. ])
  113. tests.extend(
  114. input_api.canned_checks.GetUnitTestsInDirectory(
  115. input_api,
  116. output_api,
  117. 'tests',
  118. files_to_check=test_to_run_list,
  119. files_to_skip=tests_to_skip_list,
  120. run_on_python3=True,
  121. run_on_python2=False))
  122. return input_api.RunTests(tests)
  123. def CheckCIPDManifest(input_api, output_api):
  124. # Validate CIPD manifests.
  125. root = input_api.os_path.normpath(
  126. input_api.os_path.abspath(input_api.PresubmitLocalPath()))
  127. rel_file = lambda rel: input_api.os_path.join(root, rel)
  128. cipd_manifests = set(rel_file(input_api.os_path.join(*x)) for x in (
  129. ('cipd_manifest.txt',),
  130. ('bootstrap', 'manifest.txt'),
  131. ('bootstrap', 'manifest_bleeding_edge.txt'),
  132. # Also generate a file for the cipd client itself.
  133. ('cipd_client_version',),
  134. ))
  135. affected_manifests = input_api.AffectedFiles(
  136. include_deletes=False,
  137. file_filter=lambda x:
  138. input_api.os_path.normpath(x.AbsoluteLocalPath()) in cipd_manifests)
  139. tests = []
  140. for path in affected_manifests:
  141. path = path.AbsoluteLocalPath()
  142. if path.endswith('.txt'):
  143. tests.append(input_api.canned_checks.CheckCIPDManifest(
  144. input_api, output_api, path=path))
  145. else:
  146. pkg = 'infra/tools/cipd/${platform}'
  147. ver = input_api.ReadFile(path)
  148. tests.append(input_api.canned_checks.CheckCIPDManifest(
  149. input_api, output_api,
  150. content=CIPD_CLIENT_ENSURE_FILE_TEMPLATE % (pkg, ver)))
  151. tests.append(input_api.canned_checks.CheckCIPDClientDigests(
  152. input_api, output_api, client_version_file=path))
  153. return input_api.RunTests(tests)
  154. def CheckOwnersFormat(input_api, output_api):
  155. return input_api.canned_checks.CheckOwnersFormat(input_api, output_api)
  156. def CheckOwnersOnUpload(input_api, output_api):
  157. return input_api.canned_checks.CheckOwners(input_api, output_api,
  158. allow_tbr=False)
  159. def CheckDoNotSubmitOnCommit(input_api, output_api):
  160. return input_api.canned_checks.CheckDoNotSubmit(input_api, output_api)
  161. def CheckPatchFormatted(input_api, output_api):
  162. # TODO(https://crbug.com/979330) If clang-format is fixed for non-chromium
  163. # repos, remove check_clang_format=False so that proto files can be formatted
  164. return input_api.canned_checks.CheckPatchFormatted(input_api,
  165. output_api,
  166. check_clang_format=False)