blob: 51fbcb376b28b19a30baf115e544804b34fbab85 [file] [log] [blame]
Prathmesh Prabhu42207542020-04-20 23:13:23 -07001# Copyright 2020 The Chromium OS 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
5# Copyright 2020 The Chromium OS Authors. All rights reserved.
6# Use of this source code is governed by a BSD-style license that can be
7# found in the LICENSE file.
8
9# For details on the depot tools provided presubmit API see:
10# http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
11
12
13def CheckGenerated(input_api, output_api):
14 """Runs a generate.sh script as a presubmit check.
15
16 Runs a generate.sh script as a presubmit check checking for successful
17 exit and no diff generated. Thus it expects a generate.sh to exist in
18 the root of the repo.
19
20 Args:
21 input_api: InputApi, provides information about the change.
22 output_api: OutputApi, provides the mechanism for returning a response.
23
24 Returns:
25 list of PresubmitError, or empty list if no errors.
26 """
27 results = []
28
29 if input_api.subprocess.call(
30 "./generate.sh",
31 shell=True,
32 stdout=input_api.subprocess.PIPE,
33 stderr=input_api.subprocess.PIPE,
34 ):
35 msg = "Error: generate.sh failed. Please fix and try again."
36 results.append(output_api.PresubmitError(msg))
37 elif input_api.subprocess.call(
38 "git diff --exit-code",
39 shell=True,
40 stdout=input_api.subprocess.PIPE,
41 stderr=input_api.subprocess.PIPE,
42 ):
43 msg = (
44 "Error: Running generate.sh produced a diff. Please "
45 "run the script, amend your changes, and try again."
46 )
47 results.append(output_api.PresubmitError(msg))
48
49 return results
50
51
52def CheckExamples(input_api, output_api):
53 results = []
54 ret = input_api.subprocess.call(
55 ["./check_examples.sh"],
56 stdout=input_api.subprocess.PIPE,
57 stderr=input_api.subprocess.PIPE,
58 )
59 if ret:
60 results.append(
61 output_api.PresubmitError(
62 "go test failed. Please run check_examples.sh for details."
63 )
64 )
65 return results
66
67
68def CheckChangeOnUpload(input_api, output_api):
69 results = []
70 results.extend(CheckGenerated(input_api, output_api))
71 results.extend(CheckExamples(input_api, output_api))
72 return results
73
74
75def CheckChangeOnCommit(input_api, output_api):
76 results = []
77 results.extend(CheckGenerated(input_api, output_api))
78 results.extend(CheckExamples(input_api, output_api))
79 return results
80