Skip to content

Commit 34d541d

Browse files
sormurasmarcphilipp
andcommitted
Centralize exit code declaration
Co-authored-by: Marc Philipp <mail@marcphilipp.de>
1 parent 8b5d089 commit 34d541d

File tree

6 files changed

+60
-36
lines changed

6 files changed

+60
-36
lines changed

documentation/src/docs/asciidoc/user-guide/running-tests.adoc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -748,10 +748,12 @@ include::{consoleLauncherDiscoverOptionsFile}[]
748748
===== Executing tests
749749

750750
.Exit Code
751-
NOTE: The `{ConsoleLauncher}` exits with a status code of `1` if any containers or tests
752-
failed. If no tests are discovered and the `--fail-if-no-tests` command-line option is
753-
supplied, the `ConsoleLauncher` exits with a status code of `2`. Otherwise, the exit code
754-
is `0`.
751+
NOTE: On successful runs, the `{ConsoleLauncher}` exits with a status code of `0`.
752+
All non-zero codes indicate an error of some sort. For example, status code `1`
753+
is returned if any containers or tests failed. If no tests are discovered and the
754+
`--fail-if-no-tests` command-line option is supplied, the `ConsoleLauncher` exits
755+
with a status code of `2`. Unexpected or invalid user input yields a status code
756+
of `3`. An exit code of `-1` indicates an unspecified error condition.
755757

756758
----
757759
include::{consoleLauncherExecuteOptionsFile}[]

junit-platform-console/src/main/java/org/junit/platform/console/command/BaseCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ static CommandLine initialize(CommandLine commandLine) {
5959
return commandLine //
6060
.setParameterExceptionHandler((ex, args) -> {
6161
defaultParameterExceptionHandler.handleParseException(ex, args);
62-
return CommandResult.FAILURE;
62+
return ExitCode.ANY_ERROR;
6363
}) //
6464
.setExecutionExceptionHandler((ex, cmd, __) -> {
6565
commandLine.getErr().println(cmd.getColorScheme().richStackTraceString(ex));
6666
commandLine.getErr().println();
6767
commandLine.getErr().flush();
6868
cmd.usage(commandLine.getOut());
69-
return CommandResult.FAILURE;
69+
return ExitCode.ANY_ERROR;
7070
}) //
7171
.setCaseInsensitiveEnumValuesAllowed(true) //
7272
.setAtFileCommentChar(null);

junit-platform-console/src/main/java/org/junit/platform/console/command/CommandResult.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,8 @@
2222
*/
2323
@API(status = INTERNAL, since = "1.10")
2424
public class CommandResult<T> {
25-
26-
/**
27-
* Exit code indicating successful execution.
28-
*/
29-
public static final int SUCCESS = 0;
30-
31-
/**
32-
* Exit code indicating any failure(s).
33-
*/
34-
protected static final int FAILURE = -1;
35-
3625
public static <T> CommandResult<T> success() {
37-
return create(SUCCESS, null);
38-
}
39-
40-
public static <T> CommandResult<T> failure() {
41-
return create(FAILURE, null);
26+
return create(ExitCode.SUCCESS, null);
4227
}
4328

4429
public static <T> CommandResult<T> create(int exitCode, @Nullable T value) {

junit-platform-console/src/main/java/org/junit/platform/console/command/ExecuteTestsCommand.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
package org.junit.platform.console.command;
1212

13-
import static org.junit.platform.console.command.CommandResult.SUCCESS;
13+
import static org.junit.platform.console.command.ExitCode.NO_TESTS_FOUND;
14+
import static org.junit.platform.console.command.ExitCode.SUCCESS;
15+
import static org.junit.platform.console.command.ExitCode.TEST_FAILED;
1416

1517
import java.io.PrintWriter;
1618
import java.nio.file.Path;
@@ -34,17 +36,6 @@
3436
description = "Execute tests" //
3537
)
3638
class ExecuteTestsCommand extends BaseCommand<TestExecutionSummary> implements CommandLine.IExitCodeGenerator {
37-
38-
/**
39-
* Exit code indicating test failure(s)
40-
*/
41-
private static final int TEST_FAILED = 1;
42-
43-
/**
44-
* Exit code indicating no tests found
45-
*/
46-
private static final int NO_TESTS_FOUND = 2;
47-
4839
private final ConsoleTestExecutor.Factory consoleTestExecutorFactory;
4940

5041
@Mixin
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2015-2025 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package org.junit.platform.console.command;
12+
13+
/**
14+
* Well-known exit codes of the {@code junit} tool.
15+
*
16+
* @since 6.0.1
17+
*/
18+
final class ExitCode {
19+
/**
20+
* Exit code indicating a successful tool run.
21+
*/
22+
public static final int SUCCESS = 0;
23+
24+
/**
25+
* Exit code indicating an unsuccessful run.
26+
*/
27+
public static final int ANY_ERROR = -1;
28+
29+
/**
30+
* Exit code indicating test failure(s).
31+
*/
32+
public static final int TEST_FAILED = 1;
33+
34+
/**
35+
* Exit code indicating no tests found.
36+
*/
37+
public static final int NO_TESTS_FOUND = 2;
38+
39+
/**
40+
* Exit code indicating invalid user input.
41+
*/
42+
public static final int INVALID_INPUT = 3;
43+
44+
private ExitCode() {
45+
}
46+
}

junit-platform-console/src/main/java/org/junit/platform/console/command/MainCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
footer = "For more information, please refer to the JUnit User Guide at%n" //
4040
+ "@|underline https://docs.junit.org/${junit.docs.version}/user-guide/|@", //
4141
scope = CommandLine.ScopeType.INHERIT, //
42-
exitCodeOnInvalidInput = CommandResult.FAILURE, //
43-
exitCodeOnExecutionException = CommandResult.FAILURE, //
42+
exitCodeOnInvalidInput = ExitCode.INVALID_INPUT, //
43+
exitCodeOnExecutionException = ExitCode.ANY_ERROR, //
4444
versionProvider = ManifestVersionProvider.class //
4545
)
4646
class MainCommand implements Runnable, IExitCodeGenerator {

0 commit comments

Comments
 (0)