diff options
author | Zach Lee <leezach@google.com> | 2022-10-19 19:49:07 +0000 |
---|---|---|
committer | Zach Lee <leezach@google.com> | 2022-10-25 21:19:55 +0000 |
commit | 3f7deee5b180403bc01dc66d57c64eb570643213 (patch) | |
tree | 92b71ae16083293f8802dae30f2297af1bfd7545 | |
parent | 787e6a349a829e46460352e93696ec72f28289f3 (diff) | |
download | repohooks-3f7deee5b180403bc01dc66d57c64eb570643213.tar.gz |
Propagate google-java-format-diff.py's failures
This adds error handling to expose failures in the script's dependencies. It also adds a `--verbose` flag to aid in debugging.
Repo upload hooks will now properly understand that the fixing script has failed instead of passing silently.
Bug: 254300364
Test: Added invalid flags for google-java-format in google-java-format-diff.py and verified that `repo upload` failed with expected output
Change-Id: I88003dec942b220f112ba80875a6c027e6c2f5c7
-rwxr-xr-x | tools/google-java-format.py | 45 |
1 files changed, 37 insertions, 8 deletions
diff --git a/tools/google-java-format.py b/tools/google-java-format.py index 8a4f739..e25d781 100755 --- a/tools/google-java-format.py +++ b/tools/google-java-format.py @@ -28,8 +28,8 @@ 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.shell -import rh.utils +import rh.shell # pylint: disable=import-error +import rh.utils # pylint: disable=import-error def get_parser(): @@ -52,6 +52,8 @@ def get_parser(): parser.add_argument('files', nargs='*', help='If specified, only consider differences in ' 'these files.') + parser.add_argument('--verbose', action='store_true', + help='Explain what is being done.') return parser @@ -74,18 +76,45 @@ def main(argv): diff_cmd.extend(['--'] + opts.files) diff = rh.utils.run(diff_cmd, capture_output=True).stdout - cmd = [opts.google_java_format_diff, '-p1', '--aosp', '-b', format_path] + format_cmd = [ + opts.google_java_format_diff, + '-p1', + '--aosp', + '-b', + format_path, + ] if opts.fix: - cmd.extend(['-i']) + format_cmd.extend(['-i']) if not opts.sort_imports: - cmd.extend(['--skip-sorting-imports']) + format_cmd.extend(['--skip-sorting-imports']) - stdout = rh.utils.run(cmd, input=diff, capture_output=True).stdout - if stdout: + format_cmd_result = rh.utils.run( + format_cmd, input=diff, capture_output=True) + + if format_cmd_result.returncode != 0: + print("Failed due to non-zero exit code.") + if opts.verbose: + # print out the full command that was called, including pipes + print("Called:") + print(f" {' '.join(diff_cmd)} |") + print(f" {' '.join(format_cmd)}") + for line in format_cmd_result.stdout.splitlines(): + print(f"[captured stdout] {line}") + for line in format_cmd_result.stderr.splitlines(): + print(f"[captured stderr] {line}") + return format_cmd_result.returncode + if format_cmd_result.stdout: print('One or more files in your commit have Java formatting errors.') print(f'You can run: {sys.argv[0]} --fix {rh.shell.cmd_to_str(argv)}') return 1 - + if format_cmd_result.stderr: + # We need to use stderr to catch errors in google-java-format since we + # cannot listen for a non-zero error code until + # https://github.com/google/google-java-format/pull/848 is merged. + print("Errors have been captured in stderr.") + for line in format_cmd_result.stderr.splitlines(): + print(f"[captured stderr] {line}") + return 1 return 0 |