1# Copyright 2019 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
5import re
6"""Presubmit for build/util"""
7
8
9def _GetBlacklist(input_api):
10  files_to_skip = []
11  affected_files = input_api.change.AffectedFiles()
12  version_script_change = next(
13      (f for f in affected_files
14       if re.search('\\/version\\.py$|\\/version_test\\.py$', f.LocalPath())),
15      None)
16
17  if version_script_change is None:
18    files_to_skip.append('version_test\\.py$')
19
20  android_chrome_version_script_change = next(
21      (f for f in affected_files if re.search(
22          '\\/android_chrome_version\\.py$|'
23          '\\/android_chrome_version_test\\.py$', f.LocalPath())), None)
24
25  if android_chrome_version_script_change is None:
26    files_to_skip.append('android_chrome_version_test\\.py$')
27
28  return files_to_skip
29
30
31def _GetPythonUnitTests(input_api, output_api):
32  # No need to test if files are unchanged
33  files_to_skip = _GetBlacklist(input_api)
34
35  return input_api.canned_checks.GetUnitTestsRecursively(
36      input_api,
37      output_api,
38      input_api.PresubmitLocalPath(),
39      files_to_check=['.*_test\\.py$'],
40      files_to_skip=files_to_skip)
41
42
43def CommonChecks(input_api, output_api):
44  """Presubmit checks run on both upload and commit.
45  """
46  checks = []
47  checks.extend(_GetPythonUnitTests(input_api, output_api))
48  return input_api.RunTests(checks, False)
49
50
51def CheckChangeOnUpload(input_api, output_api):
52  """Presubmit checks on CL upload."""
53  return CommonChecks(input_api, output_api)
54
55
56def CheckChangeOnCommit(input_api, output_api):
57  """Presubmit checks on commit."""
58  return CommonChecks(input_api, output_api)
59