PRESUBMIT.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. import fnmatch
  9. import os
  10. import sys
  11. # Whether to run the checks under Python2 or Python3.
  12. # TODO: Uncomment this to run the checks under Python3, and change the tests
  13. # in _CommonChecks in this file and the values and tests in
  14. # //tests/PRESUBMIT.py as well to keep the test coverage of all three cases
  15. # (true, false, and default/not specified).
  16. # USE_PYTHON3 = False
  17. # CIPD ensure manifest for checking CIPD client itself.
  18. CIPD_CLIENT_ENSURE_FILE_TEMPLATE = r'''
  19. # Full supported.
  20. $VerifiedPlatform linux-amd64 mac-amd64 windows-amd64 windows-386
  21. # Best effort support.
  22. $VerifiedPlatform linux-386 linux-ppc64 linux-ppc64le linux-s390x
  23. $VerifiedPlatform linux-arm64 linux-armv6l
  24. $VerifiedPlatform linux-mips64 linux-mips64le linux-mipsle
  25. %s %s
  26. '''
  27. # Timeout for a test to be executed.
  28. TEST_TIMEOUT_S = 330 # 5m 30s
  29. def DepotToolsPylint(input_api, output_api):
  30. """Gather all the pylint logic into one place to make it self-contained."""
  31. files_to_check = [
  32. r'^[^/]*\.py$',
  33. r'^testing_support/[^/]*\.py$',
  34. r'^tests/[^/]*\.py$',
  35. r'^recipe_modules/.*\.py$', # Allow recursive search in recipe modules.
  36. ]
  37. files_to_skip = list(input_api.DEFAULT_FILES_TO_SKIP)
  38. if os.path.exists('.gitignore'):
  39. with open('.gitignore') as fh:
  40. lines = [l.strip() for l in fh.readlines()]
  41. files_to_skip.extend([fnmatch.translate(l) for l in lines if
  42. l and not l.startswith('#')])
  43. if os.path.exists('.git/info/exclude'):
  44. with open('.git/info/exclude') as fh:
  45. lines = [l.strip() for l in fh.readlines()]
  46. files_to_skip.extend([fnmatch.translate(l) for l in lines if
  47. l and not l.startswith('#')])
  48. disabled_warnings = [
  49. 'R0401', # Cyclic import
  50. 'W0613', # Unused argument
  51. ]
  52. return input_api.canned_checks.GetPylint(
  53. input_api,
  54. output_api,
  55. files_to_check=files_to_check,
  56. files_to_skip=files_to_skip,
  57. disabled_warnings=disabled_warnings)
  58. def CommonChecks(input_api, output_api, tests_to_skip_list):
  59. input_api.SetTimeout(TEST_TIMEOUT_S)
  60. file_filter = lambda x: x.LocalPath() == 'infra/config/recipes.cfg'
  61. results = input_api.canned_checks.CheckJsonParses(input_api, output_api,
  62. file_filter=file_filter)
  63. # The tests here are assuming this is not defined, so raise an error
  64. # if it is.
  65. if 'USE_PYTHON3' in globals():
  66. results.append(output_api.PresubmitError(
  67. 'USE_PYTHON3 is defined; update the tests in //PRESUBMIT.py and '
  68. '//tests/PRESUBMIT.py.'))
  69. elif sys.version_info.major != 2:
  70. results.append(output_api.PresubmitError(
  71. 'Did not use Python2 for //PRESUBMIT.py by default.'))
  72. results.extend(input_api.canned_checks.CheckJsonParses(
  73. input_api, output_api))
  74. # Run only selected tests on Windows.
  75. test_to_run_list = [r'.*test\.py$']
  76. if input_api.platform.startswith(('cygwin', 'win32')):
  77. print('Warning: skipping most unit tests on Windows')
  78. tests_to_skip_list.extend([
  79. r'.*auth_test\.py$',
  80. r'.*git_common_test\.py$',
  81. r'.*git_hyper_blame_test\.py$',
  82. r'.*git_map_test\.py$',
  83. r'.*ninjalog_uploader_test\.py$',
  84. r'.*recipes_test\.py$',
  85. ])
  86. tests_to_skip_list.append(r'.*my_activity_test\.py')
  87. # TODO(maruel): Make sure at least one file is modified first.
  88. # TODO(maruel): If only tests are modified, only run them.
  89. tests = DepotToolsPylint(input_api, output_api)
  90. tests.extend(input_api.canned_checks.GetUnitTestsInDirectory(
  91. input_api,
  92. output_api,
  93. 'tests',
  94. files_to_check=test_to_run_list,
  95. files_to_skip=tests_to_skip_list,
  96. run_on_python3=False))
  97. tests.extend(input_api.canned_checks.GetUnitTestsInDirectory(
  98. input_api,
  99. output_api,
  100. 'tests',
  101. files_to_check=[r'.*my_activity_test\.py'],
  102. run_on_python3=True))
  103. # Validate CIPD manifests.
  104. root = input_api.os_path.normpath(
  105. input_api.os_path.abspath(input_api.PresubmitLocalPath()))
  106. rel_file = lambda rel: input_api.os_path.join(root, rel)
  107. cipd_manifests = set(rel_file(input_api.os_path.join(*x)) for x in (
  108. ('cipd_manifest.txt',),
  109. ('bootstrap', 'manifest.txt'),
  110. ('bootstrap', 'manifest_bleeding_edge.txt'),
  111. # Also generate a file for the cipd client itself.
  112. ('cipd_client_version',),
  113. ))
  114. affected_manifests = input_api.AffectedFiles(
  115. include_deletes=False,
  116. file_filter=lambda x:
  117. input_api.os_path.normpath(x.AbsoluteLocalPath()) in cipd_manifests)
  118. for path in affected_manifests:
  119. path = path.AbsoluteLocalPath()
  120. if path.endswith('.txt'):
  121. tests.append(input_api.canned_checks.CheckCIPDManifest(
  122. input_api, output_api, path=path))
  123. else:
  124. pkg = 'infra/tools/cipd/${platform}'
  125. ver = input_api.ReadFile(path)
  126. tests.append(input_api.canned_checks.CheckCIPDManifest(
  127. input_api, output_api,
  128. content=CIPD_CLIENT_ENSURE_FILE_TEMPLATE % (pkg, ver)))
  129. tests.append(input_api.canned_checks.CheckCIPDClientDigests(
  130. input_api, output_api, client_version_file=path))
  131. results.extend(input_api.RunTests(tests))
  132. return results
  133. def CheckChangeOnUpload(input_api, output_api):
  134. # Do not run integration tests on upload since they are way too slow.
  135. tests_to_skip_list = [
  136. r'^checkout_test\.py$',
  137. r'^cipd_bootstrap_test\.py$',
  138. r'^gclient_smoketest\.py$',
  139. ]
  140. results = []
  141. results.extend(input_api.canned_checks.CheckOwners(
  142. input_api, output_api, allow_tbr=False))
  143. results.extend(input_api.canned_checks.CheckOwnersFormat(
  144. input_api, output_api))
  145. results.extend(CommonChecks(input_api, output_api, tests_to_skip_list))
  146. return results
  147. def CheckChangeOnCommit(input_api, output_api):
  148. output = []
  149. output.extend(CommonChecks(input_api, output_api, []))
  150. output.extend(input_api.canned_checks.CheckDoNotSubmit(
  151. input_api,
  152. output_api))
  153. return output