aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTreehugger Robot <treehugger-gerrit@google.com>2020-06-11 20:06:48 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2020-06-11 20:06:48 +0000
commit5f0342508595cc2a4e660a9d7c8d09d1d76bc2ce (patch)
tree221d2fed5e655c57f2d2cbf504b53312ed00cb43
parent7ac64a99b14a74f7f75fc22fe86e71a04d18b99b (diff)
parentb13418d22ccee5899839e1df371e7e8c9dfeff9c (diff)
downloadrepohooks-5f0342508595cc2a4e660a9d7c8d09d1d76bc2ce.tar.gz
Merge "Add rustfmt checks" am: b13418d22c
Original change: https://android-review.googlesource.com/c/platform/tools/repohooks/+/1294805 Change-Id: I7abaa949416092ef8314725dc749a2b6a68472df
-rw-r--r--README.md2
-rw-r--r--rh/hooks.py13
-rwxr-xr-xrh/hooks_unittest.py4
3 files changed, 19 insertions, 0 deletions
diff --git a/README.md b/README.md
index c093776..83572ac 100644
--- a/README.md
+++ b/README.md
@@ -190,6 +190,7 @@ canned hooks already included geared towards AOSP style guidelines.
* `pylint`: Alias of `pylint2`. Will change to `pylint3` by end of 2019.
* `pylint2`: Run Python code through `pylint` using Python 2.
* `pylint3`: Run Python code through `pylint` using Python 3.
+* `rustfmt`: Run Rust code through `rustfmt`.
* `xmllint`: Run XML code through `xmllint`.
* `android_test_mapping_format`: Validate TEST_MAPPING files in Android source
code. Refer to go/test-mapping for more details.
@@ -239,6 +240,7 @@ distros/versions. The following tools are recognized:
* `google-java-format`: used for the `google_java_format` builtin hook.
* `google-java-format-diff`: used for the `google_java_format` builtin hook.
* `pylint`: used for the `pylint` builtin hook.
+* `rustfmt`: used for the `rustfmt` builtin hook.
* `android-test-mapping-format`: used for the `android_test_mapping_format`
builtin hook.
diff --git a/rh/hooks.py b/rh/hooks.py
index 5422494..21be3cc 100644
--- a/rh/hooks.py
+++ b/rh/hooks.py
@@ -730,6 +730,17 @@ def check_pylint3(project, commit, desc, diff, options=None):
options=options)
+def check_rustfmt(project, commit, desc, diff, options=None):
+ """Run "rustfmt --check" on diffed rust files"""
+ filtered = _filter_diff(diff, [r'\.rs$'])
+ if not filtered:
+ return None
+
+ rustfmt = options.tool_path('rustfmt')
+ cmd = [rustfmt] + options.args(('--check', '${PREUPLOAD_FILES}',), filtered)
+ return _check_cmd('rustfmt', project, commit, cmd)
+
+
def check_xmllint(project, commit, _desc, diff, options=None):
"""Run xmllint."""
# XXX: Should we drop most of these and probe for <?xml> tags?
@@ -808,6 +819,7 @@ BUILTIN_HOOKS = {
'pylint': check_pylint2,
'pylint2': check_pylint2,
'pylint3': check_pylint3,
+ 'rustfmt': check_rustfmt,
'xmllint': check_xmllint,
}
@@ -824,4 +836,5 @@ TOOL_PATHS = {
'google-java-format': 'google-java-format',
'google-java-format-diff': 'google-java-format-diff.py',
'pylint': 'pylint',
+ 'rustfmt': 'rustfmt',
}
diff --git a/rh/hooks_unittest.py b/rh/hooks_unittest.py
index 754a16e..0442f4e 100755
--- a/rh/hooks_unittest.py
+++ b/rh/hooks_unittest.py
@@ -617,6 +617,10 @@ class BuiltinHooksTests(unittest.TestCase):
self._test_file_filter(mock_check, rh.hooks.check_pylint3,
('foo.py',))
+ def test_rustfmt(self, mock_check, _mock_run):
+ self._test_file_filter(mock_check, rh.hooks.check_rustfmt,
+ ('foo.rs',))
+
def test_xmllint(self, mock_check, _mock_run):
"""Verify the xmllint builtin hook."""
self._test_file_filter(mock_check, rh.hooks.check_xmllint,