Skip to content

Commit 7f17af9

Browse files
cushongoogle-java-format Team
authored and
google-java-format Team
committed
Return a non-zero exit code upon error
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 FUTURE_COPYBARA_INTEGRATE_REVIEW=#848 from abstractlyZach:return_nonzero 06b2d36 PiperOrigin-RevId: 502211842
1 parent 90f9e5a commit 7f17af9

File tree

3 files changed

+4
-3
lines changed

3 files changed

+4
-3
lines changed

core/src/main/java/com/google/googlejavaformat/java/Main.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ static int main(PrintWriter out, PrintWriter err, String... args) {
7979
return formatter.format(args);
8080
} catch (UsageException e) {
8181
err.print(e.getMessage());
82-
return 0;
82+
// We return exit code 2 to differentiate usage issues from code formatting issues.
83+
return 2;
8384
} finally {
8485
err.flush();
8586
out.flush();

core/src/test/java/com/google/googlejavaformat/java/GoogleJavaFormatToolProviderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void testUsageOutputAfterLoadingViaToolName() {
4646

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

49-
assertThat(result).isEqualTo(0);
49+
assertThat(result).isEqualTo(2);
5050

5151
String usage = err.toString();
5252

core/src/test/java/com/google/googlejavaformat/java/MainTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public void testMain() throws Exception {
132132
process.waitFor();
133133
String err = new String(ByteStreams.toByteArray(process.getErrorStream()), UTF_8);
134134
assertThat(err).contains("Usage: google-java-format");
135-
assertThat(process.exitValue()).isEqualTo(0);
135+
assertThat(process.exitValue()).isEqualTo(2);
136136
}
137137

138138
// end to end javadoc formatting test

0 commit comments

Comments
 (0)