PRESUBMIT.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. # CIPD ensure manifest for checking CIPD client itself.
  12. CIPD_CLIENT_ENSURE_FILE_TEMPLATE = r'''
  13. # Full supported.
  14. $VerifiedPlatform linux-amd64 mac-amd64 windows-amd64 windows-386
  15. # Best effort support.
  16. $VerifiedPlatform linux-386 linux-ppc64 linux-ppc64le linux-s390x
  17. $VerifiedPlatform linux-arm64 linux-armv6l
  18. $VerifiedPlatform linux-mips64 linux-mips64le linux-mipsle
  19. %s %s
  20. '''
  21. # Timeout for a test to be executed.
  22. TEST_TIMEOUT_S = 330 # 5m 30s
  23. def DepotToolsPylint(input_api, output_api):
  24. """Gather all the pylint logic into one place to make it self-contained."""
  25. white_list = [
  26. r'^[^/]*\.py$',
  27. r'^testing_support/[^/]*\.py$',
  28. r'^tests/[^/]*\.py$',
  29. r'^recipe_modules/.*\.py$', # Allow recursive search in recipe modules.
  30. ]
  31. black_list = list(input_api.DEFAULT_BLACK_LIST)
  32. if os.path.exists('.gitignore'):
  33. with open('.gitignore') as fh:
  34. lines = [l.strip() for l in fh.readlines()]
  35. black_list.extend([fnmatch.translate(l) for l in lines if
  36. l and not l.startswith('#')])
  37. if os.path.exists('.git/info/exclude'):
  38. with open('.git/info/exclude') as fh:
  39. lines = [l.strip() for l in fh.readlines()]
  40. black_list.extend([fnmatch.translate(l) for l in lines if
  41. l and not l.startswith('#')])
  42. disabled_warnings = [
  43. 'R0401', # Cyclic import
  44. 'W0613', # Unused argument
  45. ]
  46. return input_api.canned_checks.GetPylint(
  47. input_api,
  48. output_api,
  49. white_list=white_list,
  50. black_list=black_list,
  51. disabled_warnings=disabled_warnings)
  52. def CommonChecks(input_api, output_api, tests_to_black_list, run_on_python3):
  53. input_api.SetTimeout(TEST_TIMEOUT_S)
  54. results = []
  55. results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
  56. results.extend(input_api.canned_checks.CheckOwnersFormat(
  57. input_api, output_api))
  58. results.extend(input_api.canned_checks.CheckJsonParses(
  59. input_api, output_api))
  60. # Run only selected tests on Windows.
  61. tests_to_white_list = [r'.*test\.py$']
  62. if input_api.platform.startswith(('cygwin', 'win32')):
  63. print('Warning: skipping most unit tests on Windows')
  64. tests_to_black_list = [
  65. r'.*auth_test\.py$',
  66. r'.*git_common_test\.py$',
  67. r'.*git_hyper_blame_test\.py$',
  68. r'.*git_map_test\.py$',
  69. r'.*ninjalog_uploader_test\.py$',
  70. r'.*recipes_test\.py$',
  71. ]
  72. # TODO(maruel): Make sure at least one file is modified first.
  73. # TODO(maruel): If only tests are modified, only run them.
  74. tests = DepotToolsPylint(input_api, output_api)
  75. tests.extend(input_api.canned_checks.GetUnitTestsInDirectory(
  76. input_api,
  77. output_api,
  78. 'tests',
  79. whitelist=tests_to_white_list,
  80. blacklist=tests_to_black_list,
  81. run_on_python3=run_on_python3))
  82. # Validate CIPD manifests.
  83. root = input_api.os_path.normpath(
  84. input_api.os_path.abspath(input_api.PresubmitLocalPath()))
  85. rel_file = lambda rel: input_api.os_path.join(root, rel)
  86. cipd_manifests = set(rel_file(input_api.os_path.join(*x)) for x in (
  87. ('cipd_manifest.txt',),
  88. ('bootstrap', 'manifest.txt'),
  89. ('bootstrap', 'manifest_bleeding_edge.txt'),
  90. # Also generate a file for the cipd client itself.
  91. ('cipd_client_version',),
  92. ))
  93. affected_manifests = input_api.AffectedFiles(
  94. include_deletes=False,
  95. file_filter=lambda x:
  96. input_api.os_path.normpath(x.AbsoluteLocalPath()) in cipd_manifests)
  97. for path in affected_manifests:
  98. path = path.AbsoluteLocalPath()
  99. if path.endswith('.txt'):
  100. tests.append(input_api.canned_checks.CheckCIPDManifest(
  101. input_api, output_api, path=path))
  102. else:
  103. pkg = 'infra/tools/cipd/${platform}'
  104. ver = input_api.ReadFile(path)
  105. tests.append(input_api.canned_checks.CheckCIPDManifest(
  106. input_api, output_api,
  107. content=CIPD_CLIENT_ENSURE_FILE_TEMPLATE % (pkg, ver)))
  108. tests.append(input_api.canned_checks.CheckCIPDClientDigests(
  109. input_api, output_api, client_version_file=path))
  110. results.extend(input_api.RunTests(tests))
  111. return results
  112. def CheckChangeOnUpload(input_api, output_api):
  113. # Do not run integration tests on upload since they are way too slow.
  114. tests_to_black_list = [
  115. r'^checkout_test\.py$',
  116. r'^cipd_bootstrap_test\.py$',
  117. r'^gclient_smoketest\.py$',
  118. ]
  119. # TODO(ehmaldonado): Run Python 3 tests on upload once Python 3 is
  120. # bootstrapped on Linux and Mac.
  121. return CommonChecks(input_api, output_api, tests_to_black_list, False)
  122. def CheckChangeOnCommit(input_api, output_api):
  123. output = []
  124. output.extend(CommonChecks(input_api, output_api, [], True))
  125. output.extend(input_api.canned_checks.CheckDoNotSubmit(
  126. input_api,
  127. output_api))
  128. return output