Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(369)

Side by Side Diff: PRESUBMIT.py

Issue 2761653003: Add presubmit check that runs dartfmt (Closed)
Patch Set: Add presubmit check that runs dartfmt. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 # for details. All rights reserved. Use of this source code is governed by a 2 # for details. All rights reserved. Use of this source code is governed by a
3 # BSD-style license that can be found in the LICENSE file. 3 # BSD-style license that can be found in the LICENSE file.
4 4
5 """Top-level presubmit script for Dart. 5 """Top-level presubmit script for Dart.
6 6
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts 7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8 for more details about the presubmit API built into gcl. 8 for more details about the presubmit API built into gcl.
9 """ 9 """
10 10
11 import imp
12 import os
13 import scm
14 import subprocess
15 import tempfile
16
17 def _CheckBuildStatus(input_api, output_api):
18 results = []
19 status_check = input_api.canned_checks.CheckTreeIsOpen(
20 input_api,
21 output_api,
22 json_url='http://dart-status.appspot.com/current?format=json')
23 results.extend(status_check)
24 return results
25
26 def _CheckDartFormat(input_api, output_api):
27 local_root = input_api.change.RepositoryRoot()
28 upstream = input_api.change._upstream
29 utils = imp.load_source('utils',
30 os.path.join(local_root, 'tools', 'utils.py'))
31
32 prebuilt_dartfmt = os.path.join(utils.CheckedInSdkPath(), 'bin', 'dartfmt')
33
34 windows = utils.GuessOS() == 'win32'
35 if windows:
36 prebuilt_dartfmt += '.bat'
37
38 if not os.path.isfile(prebuilt_dartfmt):
39 print('WARNING: dartfmt not found: %s' % (prebuilt_dartfmt))
40 return []
41
42 def HasFormatErrors(filename=None, contents=None):
43 args = [prebuilt_dartfmt, '--set-exit-if-changed']
44 if contents:
45 process = subprocess.Popen(args,
46 stdout=subprocess.PIPE,
47 stdin=subprocess.PIPE
48 )
49 out, err = process.communicate(input=contents)
50
51 # There was a bug in the return code dartfmt returns when reading from
52 # stdin so we have to check whether the content matches rather than using
53 # the return code. When the next version of the dartfmt lands in the sdk
54 # we can switch this line to "return process.returncode != 0"
55 return out != contents
56 else:
57 try:
58 subprocess.check_output(args + [filename, '-n'])
59 except subprocess.CalledProcessError:
60 return True
61 return False
62
63 unformatted_files = []
64 for git_file in input_api.AffectedTextFiles():
65 filename = git_file.AbsoluteLocalPath()
66 if filename.endswith('.dart'):
67 if HasFormatErrors(filename=filename):
68 old_version_has_errors = False
69 try:
70 path = git_file.LocalPath()
71 if windows:
72 # Git expects a linux style path.
73 path = path.replace(os.sep, '/')
74 old_contents = scm.GIT.Capture(
75 ['show', upstream + ':' + path],
76 cwd=local_root,
77 strip_out=False)
78 if HasFormatErrors(contents=old_contents):
79 old_version_has_errors = True
80 except subprocess.CalledProcessError as e:
81 # TODO(jacobr): verify that the error really is that the file was
82 # added for this CL.
83 old_version_has_errors = False
84
85 if old_version_has_errors:
86 print("WARNING: %s has existing and possibly new dartfmt issues" %
87 git_file.LocalPath())
88 else:
89 unformatted_files.append(filename)
90
91 if unformatted_files:
92 return [output_api.PresubmitError(
93 'File output does not match dartfmt.\n'
94 'Fix these issues with:\n'
95 'dartfmt -w \\\n%s' % ' \\\n'.join(unformatted_files))]
96
97 return []
98
11 def CheckChangeOnCommit(input_api, output_api): 99 def CheckChangeOnCommit(input_api, output_api):
12 results = [] 100 return (_CheckBuildStatus(input_api, output_api) +
13 status_check = input_api.canned_checks.CheckTreeIsOpen( 101 _CheckDartFormat(input_api, output_api))
14 input_api, 102
15 output_api, 103 def CheckChangeOnUpload(input_api, output_api):
16 json_url='http://dart-status.appspot.com/current?format=json') 104 return _CheckDartFormat(input_api, output_api)
17 results.extend(status_check)
18 return results
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698