aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThiƩbaud Weksteen <tweek@google.com>2020-08-19 15:41:36 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2020-08-19 15:41:36 +0000
commita4350d0cc24b53d77b2cea9b8a9a2e2ff9e828f7 (patch)
tree05ab9d99624ca6c7d6bb713de89fae7e170b6afb
parent4e726de5b66ba3d23cf4e51c244ced972236e380 (diff)
parentf773756405c17b50e2ab9a5e45a0ced873708947 (diff)
downloadrepohooks-a4350d0cc24b53d77b2cea9b8a9a2e2ff9e828f7.tar.gz
hooks: rustfmt: handle multiple commits am: f773756405
Original change: https://android-review.googlesource.com/c/platform/tools/repohooks/+/1399689 Change-Id: Ib0b684b692726004efd8859569e3a3f513078498
-rw-r--r--rh/hooks.py23
-rwxr-xr-xrh/hooks_unittest.py13
2 files changed, 32 insertions, 4 deletions
diff --git a/rh/hooks.py b/rh/hooks.py
index 7c13bf1..42427d5 100644
--- a/rh/hooks.py
+++ b/rh/hooks.py
@@ -792,8 +792,27 @@ def check_rustfmt(project, commit, _desc, diff, options=None):
return None
rustfmt = options.tool_path('rustfmt')
- cmd = [rustfmt] + options.args(('--check', '${PREUPLOAD_FILES}',), filtered)
- return _check_cmd('rustfmt', project, commit, cmd)
+ cmd = [rustfmt] + options.args((), filtered)
+ ret = []
+ for d in filtered:
+ data = rh.git.get_file_content(commit, d.file)
+ result = _run(cmd, input=data)
+ # If the parsing failed, stdout will contain enough details on the
+ # location of the error.
+ if result.returncode:
+ ret.append(rh.results.HookResult(
+ 'rustfmt', project, commit, error=result.stdout,
+ files=(d.file,)))
+ continue
+ # TODO(b/164111102): rustfmt stable does not support --check on stdin.
+ # If no error is reported, compare stdin with stdout.
+ if data != result.stdout:
+ msg = ('To fix, please run: %s' %
+ rh.shell.cmd_to_str(cmd + [d.file]))
+ ret.append(rh.results.HookResult(
+ 'rustfmt', project, commit, error=msg,
+ files=(d.file,)))
+ return ret
def check_xmllint(project, commit, _desc, diff, options=None):
diff --git a/rh/hooks_unittest.py b/rh/hooks_unittest.py
index be3a4b3..daa240b 100755
--- a/rh/hooks_unittest.py
+++ b/rh/hooks_unittest.py
@@ -725,8 +725,17 @@ class BuiltinHooksTests(unittest.TestCase):
('foo.py',))
def test_rustfmt(self, mock_check, _mock_run):
- self._test_file_filter(mock_check, rh.hooks.check_rustfmt,
- ('foo.rs',))
+ # First call should do nothing as there are no files to check.
+ ret = rh.hooks.check_rustfmt(
+ self.project, 'commit', 'desc', (), options=self.options)
+ self.assertEqual(ret, None)
+ self.assertFalse(mock_check.called)
+
+ # Second call will have some results.
+ diff = [rh.git.RawDiffEntry(file='lib.rs')]
+ ret = rh.hooks.check_rustfmt(
+ self.project, 'commit', 'desc', diff, options=self.options)
+ self.assertNotEqual(ret, None)
def test_xmllint(self, mock_check, _mock_run):
"""Verify the xmllint builtin hook."""