diff options
author | Jiyong Park <jiyong@google.com> | 2021-01-20 17:48:46 +0900 |
---|---|---|
committer | Jiyong Park <jiyong@google.com> | 2021-01-21 08:11:04 +0900 |
commit | 147b6c35e1e87edda7b8db9d28ef5079cf69878d (patch) | |
tree | 70bb2d604d556baff0aaa19fb234feef5625efcc | |
parent | 2729e11af0482bc00c1eeddc4fe6348290a1867d (diff) | |
download | repohooks-147b6c35e1e87edda7b8db9d28ef5079cf69878d.tar.gz |
Add aidl-format as a new builtin hook
aidl-format formats AIDL files.
Bug: 144540481
Test: Add `aidl_format = true` to /system/tools/aidl/REUPLOAD.cfg,
make a poorly formatted change in one of the AIDL file in the repo.
Creating a commit and executing `repo upload` caught it and did an
automatic fixup.
[FAILED] aidl_format
FILES: ('tests/android/aidl/tests/ITestService.aidl',) 17,18d16
...
[RUNNING 5/5] aidl_format
An automatic fix can be attempted for the "aidl_format" hook. Do you
want to run it? (Yes/no)? yes
Fix successfully applied. Amend the current commit before attempting to
upload again.
[FAILED] repohooks for platform/system/tools/aidl failed in 10.274s
FATAL: Preupload failed due to above error(s).
Change-Id: I3081360840dfbe989908cdffe189573799fc29ab
-rw-r--r-- | README.md | 10 | ||||
-rw-r--r-- | rh/hooks.py | 22 | ||||
-rwxr-xr-x | rh/hooks_unittest.py | 14 |
3 files changed, 42 insertions, 4 deletions
@@ -170,6 +170,9 @@ some dog = tool --no-cat-in-commit-message ${PREUPLOAD_COMMIT_MESSAGE} This section allows for turning on common/builtin hooks. There are a bunch of canned hooks already included geared towards AOSP style guidelines. +* `aidl_format`: Run AIDL files (.aidl) through `aidl-format`. +* `android_test_mapping_format`: Validate TEST_MAPPING files in Android source + code. Refer to go/test-mapping for more details. * `bpfmt`: Run Blueprint files (.bp) through `bpfmt`. * `checkpatch`: Run commits through the Linux kernel's `checkpatch.pl` script. * `clang_format`: Run git-clang-format against the commit. The default style is @@ -195,8 +198,6 @@ canned hooks already included geared towards AOSP style guidelines. * `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. Note: Builtin hooks tend to match specific filenames (e.g. `.json`). If no files match in a specific commit, then the hook will be skipped for that commit. @@ -263,6 +264,9 @@ executables can be overridden through `[Tool Paths]`. This is helpful to provide consistent behavior for developers across different OS and Linux distros/versions. The following tools are recognized: +* `aidl-format`: used for the `aidl_format` builtin hook. +* `android-test-mapping-format`: used for the `android_test_mapping_format` + builtin hook. * `bpfmt`: used for the `bpfmt` builtin hook. * `clang-format`: used for the `clang_format` builtin hook. * `cpplint`: used for the `cpplint` builtin hook. @@ -272,8 +276,6 @@ distros/versions. The following tools are recognized: * `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. See [Placeholders](#Placeholders) for variables you can expand automatically. diff --git a/rh/hooks.py b/rh/hooks.py index 8cea251..0b3bb29 100644 --- a/rh/hooks.py +++ b/rh/hooks.py @@ -974,9 +974,30 @@ def check_android_test_mapping(project, commit, _desc, diff, options=None): return _check_cmd('android-test-mapping-format', project, commit, cmd) +def check_aidl_format(project, commit, _desc, diff, options=None): + """Checks that AIDL files are formatted with aidl-format.""" + # All *.aidl files except for those under aidl_api directory. + filtered = _filter_diff(diff, [r'\.aidl$'], [r'/aidl_api/']) + if not filtered: + return None + aidl_format = options.tool_path('aidl-format') + cmd = [aidl_format, '-d'] + options.args((), filtered) + ret = [] + for d in filtered: + data = rh.git.get_file_content(commit, d.file) + result = _run(cmd, input=data) + if result.stdout: + fixup_func = _fixup_func_caller([aidl_format, '-w', d.file]) + ret.append(rh.results.HookResult( + 'aidl-format', project, commit, error=result.stdout, + files=(d.file,), fixup_func=fixup_func)) + return ret + + # Hooks that projects can opt into. # Note: Make sure to keep the top level README.md up to date when adding more! BUILTIN_HOOKS = { + 'aidl_format': check_aidl_format, 'android_test_mapping_format': check_android_test_mapping, 'bpfmt': check_bpfmt, 'checkpatch': check_checkpatch, @@ -1002,6 +1023,7 @@ BUILTIN_HOOKS = { # Additional tools that the hooks can call with their default values. # Note: Make sure to keep the top level README.md up to date when adding more! TOOL_PATHS = { + 'aidl-format': 'aidl-format', 'android-test-mapping-format': os.path.join(TOOLS_DIR, 'android_test_mapping_format.py'), 'bpfmt': 'bpfmt', diff --git a/rh/hooks_unittest.py b/rh/hooks_unittest.py index 716e1da..8466319 100755 --- a/rh/hooks_unittest.py +++ b/rh/hooks_unittest.py @@ -823,6 +823,20 @@ class BuiltinHooksTests(unittest.TestCase): self.project, 'commit', 'desc', diff, options=self.options) self.assertIsNotNone(ret) + def test_aidl_format(self, mock_check, _mock_run): + """Verify the aidl_format builtin hook.""" + # First call should do nothing as there are no files to check. + ret = rh.hooks.check_aidl_format( + self.project, 'commit', 'desc', (), options=self.options) + self.assertIsNone(ret) + self.assertFalse(mock_check.called) + + # Second call will have some results. + diff = [rh.git.RawDiffEntry(file='IFoo.go')] + ret = rh.hooks.check_gofmt( + self.project, 'commit', 'desc', diff, options=self.options) + self.assertIsNotNone(ret) + if __name__ == '__main__': unittest.main() |