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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: PRESUBMIT.py
diff --git a/PRESUBMIT.py b/PRESUBMIT.py
index f850b03948c9b2e1a03a9960ef8f54a3bc2d9cb2..fb447fbb59c52fc915fda1f0d62a5f54bcf67089 100644
--- a/PRESUBMIT.py
+++ b/PRESUBMIT.py
@@ -8,11 +8,82 @@ See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
for more details about the presubmit API built into gcl.
"""
+from __future__ import print_function
+import imp
+import os
+import scm
+import subprocess
+import tempfile
+
+def _CheckBuildStatus(input_api, output_api):
+ results = []
+ status_check = input_api.canned_checks.CheckTreeIsOpen(
+ input_api,
+ json_url='http://dart-status.appspot.com/current?format=json')
+ results.extend(status_check)
+ return results
+
+def _CheckDartFormat(input_api, output_api):
+ local_root = input_api.change.RepositoryRoot()
+ upstream = input_api.change._upstream
+ utils = imp.load_source('utils',
+ os.path.join(local_root, 'tools', 'utils.py'))
+
+ prebuilt_dartfmt = os.path.join(utils.CheckedInSdkPath(), 'bin', 'dartfmt')
+
+ if not os.path.isfile(prebuilt_dartfmt):
+ print('WARNING: dartfmt not found: %s' % (prebuilt_dartfmt))
+ return []
+
+ def HasFormatErrors(filename):
+ try:
+ subprocess.check_output(
+ [prebuilt_dartfmt, '--set-exit-if-changed', '-n', filename])
+ except subprocess.CalledProcessError:
+ return True
+ return False
+
+ unformatted_files = []
+ for git_file in input_api.AffectedTextFiles():
+ filename = git_file.AbsoluteLocalPath()
+ if filename.endswith('.dart'):
+ if HasFormatErrors(filename):
+ old_version_has_errors = False
+ try:
+ old_contents = scm.GIT.Capture(
+ ['show', upstream + ':' + git_file.LocalPath()], cwd=local_root)
+ fd, temp_path = tempfile.mkstemp()
+ # Use print to make sure file is terminated with a newline as
+ # "git show" is losing the trailing newline.
+ with open(temp_path, 'w') as f:
+ print(old_contents, file=f)
+
+ if HasFormatErrors(temp_path):
+ old_version_has_errors = True
+
+ os.close(fd)
zra 2017/03/21 20:47:49 It might also be a good idea to manually delete th
Jacob 2017/03/21 22:04:21 Worked around a bug in how the formatter handles r
+ except subprocess.CalledProcessError as e:
zra 2017/03/21 20:47:49 Can this still happen? It looks like HasFormatErro
Jacob 2017/03/21 22:04:22 scm.GIT.Capture is throwing this error.
+ # TODO(jacobr): verify that the error really is that the file was
+ # added for this CL.
+ old_version_has_errors = False
+
+ if old_version_has_errors:
+ print("WARNING: %s has existing and possibly new dartfmt issues" %
+ git_file.LocalPath())
+ else:
+ unformatted_files.append(filename)
+
+ if unformatted_files:
+ return [output_api.PresubmitError(
+ 'File output does not match dartfmt.\n'
+ 'Fix these issues with:\n'
+ 'dartfmt -w \\\n%s' % ' \\\n'.join(unformatted_files))]
+
+ return []
+
def CheckChangeOnCommit(input_api, output_api):
- results = []
- status_check = input_api.canned_checks.CheckTreeIsOpen(
- input_api,
- output_api,
- json_url='http://dart-status.appspot.com/current?format=json')
- results.extend(status_check)
- return results
+ return (_CheckBuildStatus(input_api, output_api) +
+ _CheckDartFormat(input_api, output_api))
+
+def CheckChangeOnUpload(input_api, output_api):
+ return _CheckDartFormat(input_api, output_api)
« 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