diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2024-03-25 16:29:08 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2024-03-25 16:29:08 +0000 |
commit | 89d566c34f2dae1ef0949a2a91737354efd010fc (patch) | |
tree | 52cec87e70718b4efea41504bde845addb0022e3 | |
parent | f517423041e70e83f1b4aca9991928b7dba35299 (diff) | |
parent | e106ee3301113116bdc4e11cdb9af60ea946d12b (diff) | |
download | repohooks-androidx-core-release.tar.gz |
Snap for 11610999 from e106ee3301113116bdc4e11cdb9af60ea946d12b to androidx-core-releaseandroidx-core-release
Change-Id: Ibe77cad9d9a8d40aa30d8d3ebf78b17ab3058103
-rw-r--r-- | PREUPLOAD.cfg | 1 | ||||
-rwxr-xr-x | pre-upload.py | 8 | ||||
-rw-r--r-- | rh/hooks.py | 10 | ||||
-rw-r--r-- | rh/results.py | 2 | ||||
-rwxr-xr-x | rh/results_unittest.py | 105 |
5 files changed, 116 insertions, 10 deletions
diff --git a/PREUPLOAD.cfg b/PREUPLOAD.cfg index 8ee46ac..31de3b0 100644 --- a/PREUPLOAD.cfg +++ b/PREUPLOAD.cfg @@ -2,6 +2,7 @@ # Only list fast unittests here. config_unittest = ./rh/config_unittest.py hooks_unittest = ./rh/hooks_unittest.py +results_unittest = ./rh/results_unittest.py shell_unittest = ./rh/shell_unittest.py terminal_unittest = ./rh/terminal_unittest.py utils_unittest = ./rh/utils_unittest.py diff --git a/pre-upload.py b/pre-upload.py index 90d16f1..18bf11f 100755 --- a/pre-upload.py +++ b/pre-upload.py @@ -222,7 +222,7 @@ def _process_hook_results(results): error_ret = '' warning_ret = '' for result in results: - if result: + if result or result.is_warning(): ret = '' if result.files: ret += f' FILES: {rh.shell.cmd_to_str(result.files)}\n' @@ -359,8 +359,7 @@ def _run_project_hooks_in_cwd( config = _get_project_config(from_git) except rh.config.ValidationError as e: output.error('Loading config files', str(e)) - ret.internal_failure = True - return ret + return ret._replace(internal_failure=True) # If the repo has no pre-upload hooks enabled, then just return. hooks = list(config.callable_hooks()) @@ -374,8 +373,7 @@ def _run_project_hooks_in_cwd( except rh.utils.CalledProcessError as e: output.error('Upstream remote/tracking branch lookup', f'{e}\nDid you run repo start? Is your HEAD detached?') - ret.internal_failure = True - return ret + return ret._replace(internal_failure=True) project = rh.Project(name=project_name, dir=proj_dir) rel_proj_dir = os.path.relpath(proj_dir, rh.git.find_repo_root()) diff --git a/rh/hooks.py b/rh/hooks.py index 1168720..6cb92a0 100644 --- a/rh/hooks.py +++ b/rh/hooks.py @@ -346,15 +346,17 @@ def check_bpfmt(project, commit, _desc, diff, options=None): bpfmt = options.tool_path('bpfmt') bpfmt_options = options.args((), filtered) - cmd = [bpfmt, '-l'] + bpfmt_options + cmd = [bpfmt, '-d'] + bpfmt_options + fixup_cmd = [bpfmt, '-w'] + if '-s' in bpfmt_options: + fixup_cmd.append('-s') + fixup_cmd.append('--') + ret = [] for d in filtered: data = rh.git.get_file_content(commit, d.file) result = _run(cmd, input=data) if result.stdout: - fixup_cmd = [bpfmt, '-w'] - if '-s' in bpfmt_options: - fixup_cmd.append('-s') ret.append(rh.results.HookResult( 'bpfmt', project, commit, error=result.stdout, diff --git a/rh/results.py b/rh/results.py index 8adcf6b..65e0052 100644 --- a/rh/results.py +++ b/rh/results.py @@ -70,7 +70,7 @@ class HookCommandResult(HookResult): def __bool__(self): """Whether this result is an error.""" - return self.result.returncode not in (None, 0) + return self.result.returncode not in (None, 0, 77) def is_warning(self): """Whether this result is a non-fatal warning.""" diff --git a/rh/results_unittest.py b/rh/results_unittest.py new file mode 100755 index 0000000..93d909e --- /dev/null +++ b/rh/results_unittest.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python3 +# Copyright 2023 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Unittests for the results module.""" + +import os +import sys +import unittest + +_path = os.path.realpath(__file__ + '/../..') +if sys.path[0] != _path: + sys.path.insert(0, _path) +del _path + +# We have to import our local modules after the sys.path tweak. We can't use +# relative imports because this is an executable program, not a module. +# pylint: disable=wrong-import-position +import rh +import rh.results +import rh.utils + + +COMPLETED_PROCESS_PASS = rh.utils.CompletedProcess(returncode=0) +COMPLETED_PROCESS_FAIL = rh.utils.CompletedProcess(returncode=1) +COMPLETED_PROCESS_WARN = rh.utils.CompletedProcess(returncode=77) + + +class HookResultTests(unittest.TestCase): + """Verify behavior of HookResult object.""" + + def test_error_warning(self): + """Check error & warning handling.""" + # No errors. + result = rh.results.HookResult('hook', 'project', 'HEAD', False) + self.assertFalse(result) + self.assertFalse(result.is_warning()) + + # An error. + result = rh.results.HookResult('hook', 'project', 'HEAD', True) + self.assertTrue(result) + self.assertFalse(result.is_warning()) + + +class HookCommandResultTests(unittest.TestCase): + """Verify behavior of HookCommandResult object.""" + + def test_error_warning(self): + """Check error & warning handling.""" + # No errors. + result = rh.results.HookCommandResult( + 'hook', 'project', 'HEAD', COMPLETED_PROCESS_PASS) + self.assertFalse(result) + self.assertFalse(result.is_warning()) + + # An error. + result = rh.results.HookCommandResult( + 'hook', 'project', 'HEAD', COMPLETED_PROCESS_FAIL) + self.assertTrue(result) + self.assertFalse(result.is_warning()) + + # A warning. + result = rh.results.HookCommandResult( + 'hook', 'project', 'HEAD', COMPLETED_PROCESS_WARN) + self.assertFalse(result) + self.assertTrue(result.is_warning()) + + +class ProjectResultsTests(unittest.TestCase): + """Verify behavior of ProjectResults object.""" + + def test_error_warning(self): + """Check error & warning handling.""" + # No errors. + result = rh.results.ProjectResults('project', 'workdir') + self.assertFalse(result) + + # Warnings are not errors. + result.add_results([ + rh.results.HookResult('hook', 'project', 'HEAD', False), + rh.results.HookCommandResult( + 'hook', 'project', 'HEAD', COMPLETED_PROCESS_WARN), + ]) + self.assertFalse(result) + + # Errors are errors. + result.add_results([ + rh.results.HookResult('hook', 'project', 'HEAD', True), + ]) + self.assertTrue(result) + + +if __name__ == '__main__': + unittest.main() |