Skip to content

Commit

Permalink
Return a non-zero exit code upon error
Browse files Browse the repository at this point in the history
This brings us in line with the Unix standard of returning non-zero for a failed command:
https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html

Also, this will allow any callers (like google-java-format-diff.py) to respond appropriately.
[A change](http://r.android.com/2257232) needed to be reverted because `google-java-format-diff.py`
was failing silently, so the author wasn't aware that their change caused a pre-upload hook to stop working.

Fixes #848

COPYBARA_INTEGRATE_REVIEW=#848 from abstractlyZach:return_nonzero 06b2d36
PiperOrigin-RevId: 502399096
  • Loading branch information
abstractlyZach authored and google-java-format Team committed Jan 16, 2023
1 parent 90f9e5a commit 4a22aab
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ static int main(PrintWriter out, PrintWriter err, String... args) {
return formatter.format(args);
} catch (UsageException e) {
err.print(e.getMessage());
return 0;
// We return exit code 2 to differentiate usage issues from code formatting issues.
return 2;
} finally {
err.flush();
out.flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void testUsageOutputAfterLoadingViaToolName() {

int result = format.run(new PrintWriter(out, true), new PrintWriter(err, true), "--help");

assertThat(result).isEqualTo(0);
assertThat(result).isEqualTo(2);

String usage = err.toString();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void testMain() throws Exception {
process.waitFor();
String err = new String(ByteStreams.toByteArray(process.getErrorStream()), UTF_8);
assertThat(err).contains("Usage: google-java-format");
assertThat(process.exitValue()).isEqualTo(0);
assertThat(process.exitValue()).isEqualTo(2);
}

// end to end javadoc formatting test
Expand Down

0 comments on commit 4a22aab

Please sign in to comment.