From e07cbb616254fb290d6bd9558580f667db3d799b Mon Sep 17 00:00:00 2001 From: Marc Philipp Date: Thu, 16 May 2024 16:39:21 +0200 Subject: [PATCH] Add --version option to Console Launcher Resolves #3795. --- .../release-notes-5.11.0-M2.adoc | 2 +- .../platform/console/options/BaseCommand.java | 4 +++ .../platform/console/options/MainCommand.java | 13 +++++++-- .../options/ManifestVersionProvider.java | 27 +++++++++++++++++++ .../console/ConsoleLauncherTests.java | 27 +++++++++++++++---- 5 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 junit-platform-console/src/main/java/org/junit/platform/console/options/ManifestVersionProvider.java diff --git a/documentation/src/docs/asciidoc/release-notes/release-notes-5.11.0-M2.adoc b/documentation/src/docs/asciidoc/release-notes/release-notes-5.11.0-M2.adoc index 06f97c9d06da..520bd43a045e 100644 --- a/documentation/src/docs/asciidoc/release-notes/release-notes-5.11.0-M2.adoc +++ b/documentation/src/docs/asciidoc/release-notes/release-notes-5.11.0-M2.adoc @@ -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 diff --git a/junit-platform-console/src/main/java/org/junit/platform/console/options/BaseCommand.java b/junit-platform-console/src/main/java/org/junit/platform/console/options/BaseCommand.java index 2bb6ed9ec8b7..e8e377c27f13 100644 --- a/junit-platform-console/src/main/java/org/junit/platform/console/options/BaseCommand.java +++ b/junit-platform-console/src/main/java/org/junit/platform/console/options/BaseCommand.java @@ -34,6 +34,10 @@ abstract class BaseCommand implements Callable { @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); } diff --git a/junit-platform-console/src/main/java/org/junit/platform/console/options/MainCommand.java b/junit-platform-console/src/main/java/org/junit/platform/console/options/MainCommand.java index b87755b8e996..f83ba2b243d0 100644 --- a/junit-platform-console/src/main/java/org/junit/platform/console/options/MainCommand.java +++ b/junit-platform-console/src/main/java/org/junit/platform/console/options/MainCommand.java @@ -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, 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 allParameters = new ArrayList<>(); @@ -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")); } diff --git a/junit-platform-console/src/main/java/org/junit/platform/console/options/ManifestVersionProvider.java b/junit-platform-console/src/main/java/org/junit/platform/console/options/ManifestVersionProvider.java new file mode 100644 index 000000000000..18ab5aa19cd3 --- /dev/null +++ b/junit-platform-console/src/main/java/org/junit/platform/console/options/ManifestVersionProvider.java @@ -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}" // + }; + } + +} diff --git a/platform-tests/src/test/java/org/junit/platform/console/ConsoleLauncherTests.java b/platform-tests/src/test/java/org/junit/platform/console/ConsoleLauncherTests.java index b10db9682eb2..ef30a6d3db59 100644 --- a/platform-tests/src/test/java/org/junit/platform/console/ConsoleLauncherTests.java +++ b/platform-tests/src/test/java/org/junit/platform/console/ConsoleLauncherTests.java @@ -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; @@ -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}") @@ -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}") @@ -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}") @@ -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}")