Skip to content

Commit

Permalink
Add --version option to Console Launcher
Browse files Browse the repository at this point in the history
Resolves #3795.
  • Loading branch information
marcphilipp committed May 16, 2024
1 parent 5b13209 commit e07cbb6
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ repository on GitHub.
modify or query the store after it has been closed. In addition, an attempt to close a
store that has already been closed will have no effect.
- See link:https://github.com/junit-team/junit5/issues/3614[issue 3614] for details.

* The Console Launcher now provides a `--version` option.

[[release-notes-5.11.0-M2-junit-jupiter]]
=== JUnit Jupiter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ abstract class BaseCommand<T> implements Callable<T> {
@Option(names = { "-h", "--help" }, usageHelp = true, description = "Display help information.")
private boolean helpRequested;

@SuppressWarnings("unused")
@Option(names = "--version", versionHelp = true, description = "Display version information.")
private boolean versionHelpRequested;

void execute(String... args) {
toCommandLine().execute(args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,22 @@
+ "@|underline https://junit.org/junit5/docs/current/user-guide/|@", //
scope = CommandLine.ScopeType.INHERIT, //
exitCodeOnInvalidInput = CommandResult.FAILURE, //
exitCodeOnExecutionException = CommandResult.FAILURE //
exitCodeOnExecutionException = CommandResult.FAILURE, //
versionProvider = ManifestVersionProvider.class //
)
class MainCommand implements Callable<Object>, IExitCodeGenerator {

private final ConsoleTestExecutor.Factory consoleTestExecutorFactory;

@Option(names = { "-h", "--help" }, help = true, hidden = true)
@Option(names = { "-h", "--help" }, help = true, description = "Display help information.")
private boolean helpRequested;

@Option(names = { "--h", "-help" }, help = true, hidden = true)
private boolean helpRequested2;

@Option(names = "--version", versionHelp = true, description = "Display version information.")
private boolean versionHelpRequested;

@Unmatched
private final List<String> allParameters = new ArrayList<>();

Expand All @@ -71,6 +75,11 @@ public Object call() {
commandResult = CommandResult.success();
return null;
}
if (versionHelpRequested) {
commandSpec.commandLine().printVersionHelp(commandSpec.commandLine().getOut());
commandResult = CommandResult.success();
return null;
}
if (allParameters.contains("--list-engines")) {
return runCommand("engines", Optional.of("--list-engines"));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2015-2024 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

package org.junit.platform.console.options;

import picocli.CommandLine;

class ManifestVersionProvider implements CommandLine.IVersionProvider {

@Override
public String[] getVersion() {
String version = getClass().getPackage().getImplementationVersion();
return new String[] { //
"@|bold JUnit Platform Console Launcher " + version + "|@", //
"JVM: ${java.version} (${java.vendor} ${java.vm.name} ${java.vm.version})", //
"OS: ${os.name} ${os.version} ${os.arch}" //
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.EmptySource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.platform.console.tasks.ConsoleTestExecutor;

Expand All @@ -31,14 +32,26 @@ class ConsoleLauncherTests {
private final StringWriter stringWriter = new StringWriter();
private final PrintWriter printSink = new PrintWriter(stringWriter);

@ParameterizedTest(name = "{0}")
@ParameterizedTest(name = "cmd={0}")
@EmptySource
@MethodSource("commandsWithEmptyOptionExitCodes")
void displayHelp(String command) {
var consoleLauncher = new ConsoleLauncher(ConsoleTestExecutor::new, printSink, printSink);
var exitCode = consoleLauncher.run(command, "--help").getExitCode();

assertEquals(0, exitCode);
assertThat(stringWriter.toString()).contains("--help", "--disable-banner" /* ... */);
assertThat(output()).contains("--help");
}

@ParameterizedTest(name = "cmd={0}")
@EmptySource
@MethodSource("commandsWithEmptyOptionExitCodes")
void displayVersion(String command) {
var consoleLauncher = new ConsoleLauncher(ConsoleTestExecutor::new, printSink, printSink);
var exitCode = consoleLauncher.run(command, "--version").getExitCode();

assertEquals(0, exitCode);
assertThat(output()).contains("JUnit Platform Console Launcher");
}

@ParameterizedTest(name = "{0}")
Expand All @@ -47,7 +60,7 @@ void displayBanner(String command) {
var consoleLauncher = new ConsoleLauncher(ConsoleTestExecutor::new, printSink, printSink);
consoleLauncher.run(command);

assertThat(stringWriter.toString()).contains("Thanks for using JUnit!");
assertThat(output()).contains("Thanks for using JUnit!");
}

@ParameterizedTest(name = "{0}")
Expand All @@ -57,7 +70,7 @@ void disableBanner(String command, int expectedExitCode) {
var exitCode = consoleLauncher.run(command, "--disable-banner").getExitCode();

assertEquals(expectedExitCode, exitCode);
assertThat(stringWriter.toString()).doesNotContain("Thanks for using JUnit!");
assertThat(output()).doesNotContain("Thanks for using JUnit!");
}

@ParameterizedTest(name = "{0}")
Expand All @@ -67,7 +80,11 @@ void executeWithUnknownCommandLineOption(String command) {
var exitCode = consoleLauncher.run(command, "--all").getExitCode();

assertEquals(-1, exitCode);
assertThat(stringWriter.toString()).contains("Unknown option: '--all'").contains("Usage:");
assertThat(output()).contains("Unknown option: '--all'").contains("Usage:");
}

private String output() {
return stringWriter.toString();
}

@ParameterizedTest(name = "{0}")
Expand Down

0 comments on commit e07cbb6

Please sign in to comment.