From b6623fe0b89902ef3bb4b5d9b85998a8d286f3e7 Mon Sep 17 00:00:00 2001 From: Andrey Chernykh Date: Tue, 7 Oct 2025 13:54:32 +0300 Subject: [PATCH 1/7] #152: Remove DummyTest --- src/test/java/org/eolang/aoi/DummyTest.java | 28 ------------------- .../java/org/eolang/aoi/package-info.java | 5 ---- 2 files changed, 33 deletions(-) delete mode 100644 src/test/java/org/eolang/aoi/DummyTest.java delete mode 100644 src/test/java/org/eolang/aoi/package-info.java diff --git a/src/test/java/org/eolang/aoi/DummyTest.java b/src/test/java/org/eolang/aoi/DummyTest.java deleted file mode 100644 index dbcf967..0000000 --- a/src/test/java/org/eolang/aoi/DummyTest.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com - * SPDX-License-Identifier: MIT - */ -package org.eolang.aoi; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -/** - * Temporary dummy test to prevent the "No tests to run!" error from Maven Surefire. - * - * @since 0.2 - */ -final class DummyTest { - /** - * Dummy test method. Always passes. - * - * @since 0.2 - */ - @Test - void shouldAlwaysPass() { - Assertions.assertTrue( - true, - "This test should always pass." - ); - } -} diff --git a/src/test/java/org/eolang/aoi/package-info.java b/src/test/java/org/eolang/aoi/package-info.java deleted file mode 100644 index f1c9971..0000000 --- a/src/test/java/org/eolang/aoi/package-info.java +++ /dev/null @@ -1,5 +0,0 @@ -/* - * SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com - * SPDX-License-Identifier: MIT - */ -package org.eolang.aoi; From 561a69f8cd804d37e19e96d6acd36f9879f83525 Mon Sep 17 00:00:00 2001 From: Andrey Chernykh Date: Tue, 7 Oct 2025 13:59:28 +0300 Subject: [PATCH 2/7] #152: Add CLI command interface and help command --- .../org/eolang/aoi/cli/cmd/CliCommand.java | 24 ++++++ .../org/eolang/aoi/cli/cmd/HelpCommand.java | 63 +++++++++++++++ .../org/eolang/aoi/cli/cmd/package-info.java | 14 ++++ .../eolang/aoi/cli/cmd/HelpCommandTest.java | 76 +++++++++++++++++++ .../org/eolang/aoi/cli/cmd/package-info.java | 11 +++ src/test/resources/org/eolang/aoi/help.txt | 1 + 6 files changed, 189 insertions(+) create mode 100644 src/main/java/org/eolang/aoi/cli/cmd/CliCommand.java create mode 100644 src/main/java/org/eolang/aoi/cli/cmd/HelpCommand.java create mode 100644 src/main/java/org/eolang/aoi/cli/cmd/package-info.java create mode 100644 src/test/java/org/eolang/aoi/cli/cmd/HelpCommandTest.java create mode 100644 src/test/java/org/eolang/aoi/cli/cmd/package-info.java create mode 100644 src/test/resources/org/eolang/aoi/help.txt diff --git a/src/main/java/org/eolang/aoi/cli/cmd/CliCommand.java b/src/main/java/org/eolang/aoi/cli/cmd/CliCommand.java new file mode 100644 index 0000000..ae56c59 --- /dev/null +++ b/src/main/java/org/eolang/aoi/cli/cmd/CliCommand.java @@ -0,0 +1,24 @@ +/* + * SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com + * SPDX-License-Identifier: MIT + */ +package org.eolang.aoi.cli.cmd; + +/** + * A command for the Command-Line Interface (CLI). + * + * @since 0.0.5 + */ +public interface CliCommand { + /** + * Executes the command's logic. + */ + void execute(); + + /** + * Checks if this command should handle arguments provided during its instantiation. + * + * @return Flag indicating if the command should be executed. + */ + boolean matches(); +} diff --git a/src/main/java/org/eolang/aoi/cli/cmd/HelpCommand.java b/src/main/java/org/eolang/aoi/cli/cmd/HelpCommand.java new file mode 100644 index 0000000..d61fde0 --- /dev/null +++ b/src/main/java/org/eolang/aoi/cli/cmd/HelpCommand.java @@ -0,0 +1,63 @@ +/* + * SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com + * SPDX-License-Identifier: MIT + */ +package org.eolang.aoi.cli.cmd; + +import java.io.PrintStream; +import java.util.List; +import org.cactoos.io.ResourceOf; +import org.cactoos.text.TextOf; +import org.cactoos.text.UncheckedText; + +/** + * Displays help text from resources. + *

+ * This command matches when either no arguments are provided or the {@code --help} flag is present. + * + * @since 0.0.5 + */ +public final class HelpCommand implements CliCommand { + /** + * Command-line arguments. + */ + private final List args; + + /** + * The output stream to print the help message to. + */ + private final PrintStream out; + + /** + * Primary constructor. + * + * @param args Command-line arguments. + * @param out The output stream to print the help message to. + */ + public HelpCommand(final List args, final PrintStream out) { + this.args = args; + this.out = out; + } + + /** + * Convenience constructor for string array. + * + * @param args Command-line arguments. + * @param out The output stream to print the help message to. + */ + public HelpCommand(final String[] args, final PrintStream out) { + this(List.of(args), out); + } + + @Override + public void execute() { + this.out.print( + new UncheckedText(new TextOf(new ResourceOf("org/eolang/aoi/help.txt"))).asString() + ); + } + + @Override + public boolean matches() { + return this.args.isEmpty() || this.args.contains("--help"); + } +} diff --git a/src/main/java/org/eolang/aoi/cli/cmd/package-info.java b/src/main/java/org/eolang/aoi/cli/cmd/package-info.java new file mode 100644 index 0000000..f164f55 --- /dev/null +++ b/src/main/java/org/eolang/aoi/cli/cmd/package-info.java @@ -0,0 +1,14 @@ +/* + * SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com + * SPDX-License-Identifier: MIT + */ + +/** + * Contains classes for implementing commands for the Command-Line Interface (CLI). + *

+ * The core abstraction is the {@link org.eolang.aoi.cli.cmd.CliCommand} interface, which defines + * the contract for all executable commands. + * + * @since 0.0.5 + */ +package org.eolang.aoi.cli.cmd; diff --git a/src/test/java/org/eolang/aoi/cli/cmd/HelpCommandTest.java b/src/test/java/org/eolang/aoi/cli/cmd/HelpCommandTest.java new file mode 100644 index 0000000..a205cb4 --- /dev/null +++ b/src/test/java/org/eolang/aoi/cli/cmd/HelpCommandTest.java @@ -0,0 +1,76 @@ +/* + * SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com + * SPDX-License-Identifier: MIT + */ +package org.eolang.aoi.cli.cmd; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.nio.charset.StandardCharsets; +import java.util.stream.Stream; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +/** + * Tests for {@link HelpCommand}. + * + * @since 0.0.5 + */ +final class HelpCommandTest { + @ParameterizedTest + @MethodSource("matchingArguments") + @SuppressWarnings("PMD.UseVarargs") + void matchesOnHelpRequest(final String[] args) { + MatcherAssert.assertThat( + "HelpCommand should have matched on a help request, but it did not.", + new HelpCommand(args, System.out).matches(), + Matchers.is(true) + ); + } + + @ParameterizedTest + @MethodSource("nonMatchingArguments") + @SuppressWarnings("PMD.UseVarargs") + void doesNotMatchOnOtherArguments(final String[] args) { + MatcherAssert.assertThat( + "HelpCommand should not have matched on other arguments, but it did.", + new HelpCommand(args, System.out).matches(), + Matchers.is(false) + ); + } + + @Test + void printsHelpContentToOutputStream() { + final ByteArrayOutputStream stream = new ByteArrayOutputStream(); + final String expected = "help message\n"; + try (PrintStream out = new PrintStream(stream, true, StandardCharsets.UTF_8)) { + new HelpCommand(new String[]{}, out).execute(); + MatcherAssert.assertThat( + "HelpCommand did not print the expected help content.", + stream.toString(StandardCharsets.UTF_8), + Matchers.equalTo(expected) + ); + } + } + + private static Stream matchingArguments() { + return Stream.of( + Arguments.of((Object) new String[]{}), + Arguments.of((Object) new String[]{"--help"}), + Arguments.of((Object) new String[]{"--help", "extra-arg"}), + Arguments.of((Object) new String[]{"extra-arg", "--help"}) + ); + } + + private static Stream nonMatchingArguments() { + return Stream.of( + Arguments.of((Object) new String[]{"some-arg"}), + Arguments.of((Object) new String[]{"--version"}), + Arguments.of((Object) new String[]{"/tmp/in", "/tmp/out"}) + ); + } +} diff --git a/src/test/java/org/eolang/aoi/cli/cmd/package-info.java b/src/test/java/org/eolang/aoi/cli/cmd/package-info.java new file mode 100644 index 0000000..62befdc --- /dev/null +++ b/src/test/java/org/eolang/aoi/cli/cmd/package-info.java @@ -0,0 +1,11 @@ +/* + * SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com + * SPDX-License-Identifier: MIT + */ + +/** + * Tests for {@link org.eolang.aoi.cli.cmd}. + * + * @since 0.0.5 + */ +package org.eolang.aoi.cli.cmd; diff --git a/src/test/resources/org/eolang/aoi/help.txt b/src/test/resources/org/eolang/aoi/help.txt new file mode 100644 index 0000000..2c8d142 --- /dev/null +++ b/src/test/resources/org/eolang/aoi/help.txt @@ -0,0 +1 @@ +help message From 08fba7bc01d2d4bb8182550caebe8043aa58dec2 Mon Sep 17 00:00:00 2001 From: Andrey Chernykh Date: Tue, 7 Oct 2025 14:08:21 +0300 Subject: [PATCH 3/7] #152: Add CliException class for handling CLI errors --- .../java/org/eolang/aoi/cli/CliException.java | 36 +++++++++++++++++++ .../java/org/eolang/aoi/cli/package-info.java | 13 +++++++ 2 files changed, 49 insertions(+) create mode 100644 src/main/java/org/eolang/aoi/cli/CliException.java create mode 100644 src/main/java/org/eolang/aoi/cli/package-info.java diff --git a/src/main/java/org/eolang/aoi/cli/CliException.java b/src/main/java/org/eolang/aoi/cli/CliException.java new file mode 100644 index 0000000..fb85648 --- /dev/null +++ b/src/main/java/org/eolang/aoi/cli/CliException.java @@ -0,0 +1,36 @@ +/* + * SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com + * SPDX-License-Identifier: MIT + */ +package org.eolang.aoi.cli; + +/** + * A custom runtime exception for command-line interface (CLI) errors. + * + * @since 0.0.5 + */ +public final class CliException extends RuntimeException { + /** + * The serialization version UID. + */ + private static final long serialVersionUID = 1L; + + /** + * Constructor. + * + * @param message The detail message. + */ + public CliException(final String message) { + super(message); + } + + /** + * Constructor. + * + * @param message The detail message. + * @param cause The cause of this exception. + */ + public CliException(final String message, final Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/java/org/eolang/aoi/cli/package-info.java b/src/main/java/org/eolang/aoi/cli/package-info.java new file mode 100644 index 0000000..d97cbde --- /dev/null +++ b/src/main/java/org/eolang/aoi/cli/package-info.java @@ -0,0 +1,13 @@ +/* + * SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com + * SPDX-License-Identifier: MIT + */ + +/** + * Contains classes for the Command-Line Interface (CLI) application. + *

+ * Command implementations are located in the {@link org.eolang.aoi.cli.cmd} sub-package. + * + * @since 0.0.5 + */ +package org.eolang.aoi.cli; From dfb7388d8867074b3cf11bf3cdab16e4e45dd00b Mon Sep 17 00:00:00 2001 From: Andrey Chernykh Date: Thu, 9 Oct 2025 18:49:08 +0300 Subject: [PATCH 4/7] #152: Refactor Application to use Picocli for command-line argument parsing --- pom.xml | 6 +- src/main/java/org/eolang/aoi/Application.java | 101 ++++++++---------- src/main/java/org/eolang/aoi/Main.java | 9 +- .../java/org/eolang/aoi/cli/CliException.java | 36 ------- .../org/eolang/aoi/cli/cmd/CliCommand.java | 24 ----- .../org/eolang/aoi/cli/cmd/HelpCommand.java | 63 ----------- .../org/eolang/aoi/cli/cmd/package-info.java | 14 --- .../java/org/eolang/aoi/cli/package-info.java | 13 --- src/main/resources/org/eolang/aoi/help.txt | 14 --- .../java/org/eolang/aoi/ApplicationTest.java | 99 +++++++++++++++++ .../eolang/aoi/cli/cmd/HelpCommandTest.java | 76 ------------- .../aoi/{cli/cmd => }/package-info.java | 4 +- src/test/resources/org/eolang/aoi/help.txt | 1 - 13 files changed, 155 insertions(+), 305 deletions(-) delete mode 100644 src/main/java/org/eolang/aoi/cli/CliException.java delete mode 100644 src/main/java/org/eolang/aoi/cli/cmd/CliCommand.java delete mode 100644 src/main/java/org/eolang/aoi/cli/cmd/HelpCommand.java delete mode 100644 src/main/java/org/eolang/aoi/cli/cmd/package-info.java delete mode 100644 src/main/java/org/eolang/aoi/cli/package-info.java delete mode 100644 src/main/resources/org/eolang/aoi/help.txt create mode 100644 src/test/java/org/eolang/aoi/ApplicationTest.java delete mode 100644 src/test/java/org/eolang/aoi/cli/cmd/HelpCommandTest.java rename src/test/java/org/eolang/aoi/{cli/cmd => }/package-info.java (62%) delete mode 100644 src/test/resources/org/eolang/aoi/help.txt diff --git a/pom.xml b/pom.xml index 0bc907e..ef5a54d 100644 --- a/pom.xml +++ b/pom.xml @@ -69,9 +69,9 @@ - org.cactoos - cactoos - 0.57.0 + info.picocli + picocli + 4.7.7 org.junit.jupiter diff --git a/src/main/java/org/eolang/aoi/Application.java b/src/main/java/org/eolang/aoi/Application.java index 775005a..96aa90f 100644 --- a/src/main/java/org/eolang/aoi/Application.java +++ b/src/main/java/org/eolang/aoi/Application.java @@ -4,75 +4,64 @@ */ package org.eolang.aoi; -import java.io.PrintStream; -import java.util.Arrays; -import java.util.List; -import org.cactoos.io.ResourceOf; -import org.cactoos.text.TextOf; +import java.nio.file.Path; +import java.util.concurrent.Callable; +import picocli.CommandLine.Command; +import picocli.CommandLine.Option; +import picocli.CommandLine.Parameters; /** - * Command-line interface for the AOI (Abstract Object Inference) tool. - * - *

This class handles command-line argument parsing and coordinates the execution - * of the AOI tool.

+ * Command-line interface for the AOI tool. + *

+ * Implementing {@link Callable} allows Picocli to treat this class as a command that returns an + * exit code. + *

* * @since 0.0.4 */ -public final class Application { - +@Command( + name = "aoi", + description = { + "AOI analyzes EO programs to infer object types. It finds .xmir files in the", + "input directory and generates .xml files with type information." + }, + descriptionHeading = "%nDescription:%n", + parameterListHeading = "%nArguments:%n", + optionListHeading = "%nOptions:%n" +) +public final class Application implements Callable { /** - * Command-line arguments passed to the application. + * A standard help option that triggers Picocli's built-in help display. */ - private final String[] args; + @Option( + names = "--help", + usageHelp = true, + description = "Print this message and exit." + ) + private boolean help; /** - * Output stream for printing messages and help text. + * The first positional argument, representing the input directory. */ - private final PrintStream out; + @Parameters( + index = "0", + paramLabel = "", + description = "Directory with .xmir files (searches recursively)" + ) + private Path input; /** - * Creates a new application instance. - * - *

The arguments array is defensively copied to prevent external modification.

- * - * @param args Command-line arguments - * @param out Output stream for messages + * The second positional argument, representing the output directory. */ - public Application(final String[] args, final PrintStream out) { - this.args = Arrays.copyOf(args, args.length); - this.out = out; - } + @Parameters( + index = "1", + paramLabel = "", + description = "Directory for .xml files with inferred types" + ) + private Path output; - /** - * Executes the main application logic. - * - *

Note: {@code --help} and {@code --version} flags take precedence over other arguments and - * will be processed even if other arguments are invalid.

- * - * @throws IllegalArgumentException if the number of arguments is not exactly 2 (when neither - * {@code --help} nor {@code --version} is specified) - */ - public void run() { - final List arguments = Arrays.asList(this.args); - if (arguments.contains("--help")) { - this.out.print(new TextOf(new ResourceOf("org/eolang/aoi/help.txt"))); - } else if (arguments.contains("--version")) { - this.out.printf( - "aoi version %s", - new TextOf(new ResourceOf("org/eolang/aoi/version.txt")) - ); - } else { - if (arguments.size() != 2) { - throw new IllegalArgumentException( - "Expected 2 arguments (input dir and output dir), but got %d: %s".formatted( - arguments.size(), - String.join(", ", arguments) - ) - ); - } - this.out.printf( - "Input directory: %s, Output directory: %s%n", this.args[0], this.args[1] - ); - } + @Override + public Integer call() throws Exception { + return 0; } } diff --git a/src/main/java/org/eolang/aoi/Main.java b/src/main/java/org/eolang/aoi/Main.java index 466bc76..f862720 100644 --- a/src/main/java/org/eolang/aoi/Main.java +++ b/src/main/java/org/eolang/aoi/Main.java @@ -4,6 +4,8 @@ */ package org.eolang.aoi; +import picocli.CommandLine; + /** * Main entry point for the AOI (Abstract Object Inference) application. * @@ -18,11 +20,12 @@ private Main() { } /** - * Runs the application. + * Executes the CLI and terminates the JVM with the resulting exit code. * - * @param args Command-line arguments + * @param args Command-line arguments passed to the application. */ public static void main(final String[] args) { - new Application(args, System.out).run(); + final int exit = new CommandLine(new Application()).execute(args); + System.exit(exit); } } diff --git a/src/main/java/org/eolang/aoi/cli/CliException.java b/src/main/java/org/eolang/aoi/cli/CliException.java deleted file mode 100644 index fb85648..0000000 --- a/src/main/java/org/eolang/aoi/cli/CliException.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com - * SPDX-License-Identifier: MIT - */ -package org.eolang.aoi.cli; - -/** - * A custom runtime exception for command-line interface (CLI) errors. - * - * @since 0.0.5 - */ -public final class CliException extends RuntimeException { - /** - * The serialization version UID. - */ - private static final long serialVersionUID = 1L; - - /** - * Constructor. - * - * @param message The detail message. - */ - public CliException(final String message) { - super(message); - } - - /** - * Constructor. - * - * @param message The detail message. - * @param cause The cause of this exception. - */ - public CliException(final String message, final Throwable cause) { - super(message, cause); - } -} diff --git a/src/main/java/org/eolang/aoi/cli/cmd/CliCommand.java b/src/main/java/org/eolang/aoi/cli/cmd/CliCommand.java deleted file mode 100644 index ae56c59..0000000 --- a/src/main/java/org/eolang/aoi/cli/cmd/CliCommand.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com - * SPDX-License-Identifier: MIT - */ -package org.eolang.aoi.cli.cmd; - -/** - * A command for the Command-Line Interface (CLI). - * - * @since 0.0.5 - */ -public interface CliCommand { - /** - * Executes the command's logic. - */ - void execute(); - - /** - * Checks if this command should handle arguments provided during its instantiation. - * - * @return Flag indicating if the command should be executed. - */ - boolean matches(); -} diff --git a/src/main/java/org/eolang/aoi/cli/cmd/HelpCommand.java b/src/main/java/org/eolang/aoi/cli/cmd/HelpCommand.java deleted file mode 100644 index d61fde0..0000000 --- a/src/main/java/org/eolang/aoi/cli/cmd/HelpCommand.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com - * SPDX-License-Identifier: MIT - */ -package org.eolang.aoi.cli.cmd; - -import java.io.PrintStream; -import java.util.List; -import org.cactoos.io.ResourceOf; -import org.cactoos.text.TextOf; -import org.cactoos.text.UncheckedText; - -/** - * Displays help text from resources. - *

- * This command matches when either no arguments are provided or the {@code --help} flag is present. - * - * @since 0.0.5 - */ -public final class HelpCommand implements CliCommand { - /** - * Command-line arguments. - */ - private final List args; - - /** - * The output stream to print the help message to. - */ - private final PrintStream out; - - /** - * Primary constructor. - * - * @param args Command-line arguments. - * @param out The output stream to print the help message to. - */ - public HelpCommand(final List args, final PrintStream out) { - this.args = args; - this.out = out; - } - - /** - * Convenience constructor for string array. - * - * @param args Command-line arguments. - * @param out The output stream to print the help message to. - */ - public HelpCommand(final String[] args, final PrintStream out) { - this(List.of(args), out); - } - - @Override - public void execute() { - this.out.print( - new UncheckedText(new TextOf(new ResourceOf("org/eolang/aoi/help.txt"))).asString() - ); - } - - @Override - public boolean matches() { - return this.args.isEmpty() || this.args.contains("--help"); - } -} diff --git a/src/main/java/org/eolang/aoi/cli/cmd/package-info.java b/src/main/java/org/eolang/aoi/cli/cmd/package-info.java deleted file mode 100644 index f164f55..0000000 --- a/src/main/java/org/eolang/aoi/cli/cmd/package-info.java +++ /dev/null @@ -1,14 +0,0 @@ -/* - * SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com - * SPDX-License-Identifier: MIT - */ - -/** - * Contains classes for implementing commands for the Command-Line Interface (CLI). - *

- * The core abstraction is the {@link org.eolang.aoi.cli.cmd.CliCommand} interface, which defines - * the contract for all executable commands. - * - * @since 0.0.5 - */ -package org.eolang.aoi.cli.cmd; diff --git a/src/main/java/org/eolang/aoi/cli/package-info.java b/src/main/java/org/eolang/aoi/cli/package-info.java deleted file mode 100644 index d97cbde..0000000 --- a/src/main/java/org/eolang/aoi/cli/package-info.java +++ /dev/null @@ -1,13 +0,0 @@ -/* - * SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com - * SPDX-License-Identifier: MIT - */ - -/** - * Contains classes for the Command-Line Interface (CLI) application. - *

- * Command implementations are located in the {@link org.eolang.aoi.cli.cmd} sub-package. - * - * @since 0.0.5 - */ -package org.eolang.aoi.cli; diff --git a/src/main/resources/org/eolang/aoi/help.txt b/src/main/resources/org/eolang/aoi/help.txt deleted file mode 100644 index d2a180a..0000000 --- a/src/main/resources/org/eolang/aoi/help.txt +++ /dev/null @@ -1,14 +0,0 @@ -AOI - Abstract Object Inference Tool - -Usage: aoi [options] - -AOI analyzes EO programs to infer object types. It finds .xmir files -in the input directory and generates .xml files with type information. - -Arguments: - Directory with .xmir files (searches recursively) - Output directory for .xml files with inferred types - -Options: - --help Print this message and exit. - --version Print the version of aoi and exit.. diff --git a/src/test/java/org/eolang/aoi/ApplicationTest.java b/src/test/java/org/eolang/aoi/ApplicationTest.java new file mode 100644 index 0000000..dd84f7c --- /dev/null +++ b/src/test/java/org/eolang/aoi/ApplicationTest.java @@ -0,0 +1,99 @@ +/* + * SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com + * SPDX-License-Identifier: MIT + */ +package org.eolang.aoi; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.nio.file.Path; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import picocli.CommandLine; + +/** + * Tests for {@link Application}. + * + * @since 0.0.5 + */ +final class ApplicationTest { + @Test + void applicationExitsWithZeroCodeOnSuccessfulExecution( + @TempDir final Path input, + @TempDir final Path output + ) { + final CommandLine cmd = new CommandLine(new Application()); + final int exit = cmd.execute(input.toString(), output.toString()); + MatcherAssert.assertThat( + "Application did not exit with a zero code on successful execution", + exit, + Matchers.is(0) + ); + } + + @Test + void applicationPrintsNothingToStderrOnSuccessfulExecution( + @TempDir final Path input, + @TempDir final Path output + ) { + final CommandLine cmd = new CommandLine(new Application()); + final StringWriter stderr = new StringWriter(); + cmd.setErr(new PrintWriter(stderr)); + cmd.execute(input.toString(), output.toString()); + MatcherAssert.assertThat( + "Application printed to stderr during a successful execution", + stderr.toString(), + Matchers.emptyString() + ); + } + + @Test + void applicationPrintsHelpMessageWhenHelpFlagIsPassed() { + final CommandLine cmd = new CommandLine(new Application()); + final StringWriter stdout = new StringWriter(); + cmd.setOut(new PrintWriter(stdout)); + cmd.execute("--help"); + MatcherAssert.assertThat( + "Application did not print the help message to stdout for the --help flag", + stdout.toString(), + Matchers.containsString("Usage:") + ); + } + + @Test + void applicationExitsWithZeroCodeWhenHelpFlagIsPassed() { + final CommandLine cmd = new CommandLine(new Application()); + final int exit = cmd.execute("--help"); + MatcherAssert.assertThat( + "Application did not exit with a zero code when the --help flag was used", + exit, + Matchers.is(0) + ); + } + + @Test + void applicationExitsWithNonZeroCodeWhenNoArgumentsAreProvided() { + final CommandLine cmd = new CommandLine(new Application()); + cmd.setErr(new PrintWriter(new StringWriter())); + final int exit = cmd.execute(); + MatcherAssert.assertThat( + "Application did not exit with a non-zero code when required arguments were missing", + exit, + Matchers.not(0) + ); + } + + @Test + void applicationExitsWithNonZeroCodeWhenOneArgumentIsMissing() { + final CommandLine cmd = new CommandLine(new Application()); + cmd.setErr(new PrintWriter(new StringWriter())); + final int exit = cmd.execute("input/dir"); + MatcherAssert.assertThat( + "Application did not exit with a non-zero code when one argument was missing", + exit, + Matchers.not(0) + ); + } +} diff --git a/src/test/java/org/eolang/aoi/cli/cmd/HelpCommandTest.java b/src/test/java/org/eolang/aoi/cli/cmd/HelpCommandTest.java deleted file mode 100644 index a205cb4..0000000 --- a/src/test/java/org/eolang/aoi/cli/cmd/HelpCommandTest.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com - * SPDX-License-Identifier: MIT - */ -package org.eolang.aoi.cli.cmd; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.nio.charset.StandardCharsets; -import java.util.stream.Stream; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; - -/** - * Tests for {@link HelpCommand}. - * - * @since 0.0.5 - */ -final class HelpCommandTest { - @ParameterizedTest - @MethodSource("matchingArguments") - @SuppressWarnings("PMD.UseVarargs") - void matchesOnHelpRequest(final String[] args) { - MatcherAssert.assertThat( - "HelpCommand should have matched on a help request, but it did not.", - new HelpCommand(args, System.out).matches(), - Matchers.is(true) - ); - } - - @ParameterizedTest - @MethodSource("nonMatchingArguments") - @SuppressWarnings("PMD.UseVarargs") - void doesNotMatchOnOtherArguments(final String[] args) { - MatcherAssert.assertThat( - "HelpCommand should not have matched on other arguments, but it did.", - new HelpCommand(args, System.out).matches(), - Matchers.is(false) - ); - } - - @Test - void printsHelpContentToOutputStream() { - final ByteArrayOutputStream stream = new ByteArrayOutputStream(); - final String expected = "help message\n"; - try (PrintStream out = new PrintStream(stream, true, StandardCharsets.UTF_8)) { - new HelpCommand(new String[]{}, out).execute(); - MatcherAssert.assertThat( - "HelpCommand did not print the expected help content.", - stream.toString(StandardCharsets.UTF_8), - Matchers.equalTo(expected) - ); - } - } - - private static Stream matchingArguments() { - return Stream.of( - Arguments.of((Object) new String[]{}), - Arguments.of((Object) new String[]{"--help"}), - Arguments.of((Object) new String[]{"--help", "extra-arg"}), - Arguments.of((Object) new String[]{"extra-arg", "--help"}) - ); - } - - private static Stream nonMatchingArguments() { - return Stream.of( - Arguments.of((Object) new String[]{"some-arg"}), - Arguments.of((Object) new String[]{"--version"}), - Arguments.of((Object) new String[]{"/tmp/in", "/tmp/out"}) - ); - } -} diff --git a/src/test/java/org/eolang/aoi/cli/cmd/package-info.java b/src/test/java/org/eolang/aoi/package-info.java similarity index 62% rename from src/test/java/org/eolang/aoi/cli/cmd/package-info.java rename to src/test/java/org/eolang/aoi/package-info.java index 62befdc..0839f36 100644 --- a/src/test/java/org/eolang/aoi/cli/cmd/package-info.java +++ b/src/test/java/org/eolang/aoi/package-info.java @@ -4,8 +4,8 @@ */ /** - * Tests for {@link org.eolang.aoi.cli.cmd}. + * Tests for {@link org.eolang.aoi}. * * @since 0.0.5 */ -package org.eolang.aoi.cli.cmd; +package org.eolang.aoi; diff --git a/src/test/resources/org/eolang/aoi/help.txt b/src/test/resources/org/eolang/aoi/help.txt deleted file mode 100644 index 2c8d142..0000000 --- a/src/test/resources/org/eolang/aoi/help.txt +++ /dev/null @@ -1 +0,0 @@ -help message From a95263f3ee6e4bf0d9b0c5a767a76e5ce49ed077 Mon Sep 17 00:00:00 2001 From: Andrey Chernykh Date: Fri, 10 Oct 2025 15:09:19 +0300 Subject: [PATCH 5/7] #152: Inline exit variable in main method --- src/main/java/org/eolang/aoi/Main.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/org/eolang/aoi/Main.java b/src/main/java/org/eolang/aoi/Main.java index f862720..9013d32 100644 --- a/src/main/java/org/eolang/aoi/Main.java +++ b/src/main/java/org/eolang/aoi/Main.java @@ -25,7 +25,6 @@ private Main() { * @param args Command-line arguments passed to the application. */ public static void main(final String[] args) { - final int exit = new CommandLine(new Application()).execute(args); - System.exit(exit); + System.exit(new CommandLine(new Application()).execute(args)); } } From 1d17fc6da6cdd91592152bf484eb8d9f9c9cc469 Mon Sep 17 00:00:00 2001 From: Andrey Chernykh Date: Fri, 10 Oct 2025 15:18:02 +0300 Subject: [PATCH 6/7] #152: Update help option to support both --help and -h flags --- src/main/java/org/eolang/aoi/Application.java | 2 +- .../java/org/eolang/aoi/ApplicationTest.java | 20 +++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/eolang/aoi/Application.java b/src/main/java/org/eolang/aoi/Application.java index 96aa90f..8c2f237 100644 --- a/src/main/java/org/eolang/aoi/Application.java +++ b/src/main/java/org/eolang/aoi/Application.java @@ -34,7 +34,7 @@ public final class Application implements Callable { * A standard help option that triggers Picocli's built-in help display. */ @Option( - names = "--help", + names = {"--help", "-h"}, usageHelp = true, description = "Print this message and exit." ) diff --git a/src/test/java/org/eolang/aoi/ApplicationTest.java b/src/test/java/org/eolang/aoi/ApplicationTest.java index dd84f7c..6dd899b 100644 --- a/src/test/java/org/eolang/aoi/ApplicationTest.java +++ b/src/test/java/org/eolang/aoi/ApplicationTest.java @@ -11,6 +11,8 @@ import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import picocli.CommandLine; /** @@ -49,25 +51,27 @@ void applicationPrintsNothingToStderrOnSuccessfulExecution( ); } - @Test - void applicationPrintsHelpMessageWhenHelpFlagIsPassed() { + @ParameterizedTest + @ValueSource(strings = {"--help", "-h"}) + void applicationPrintsHelpMessageWhenHelpFlagIsPassed(final String flag) { final CommandLine cmd = new CommandLine(new Application()); final StringWriter stdout = new StringWriter(); cmd.setOut(new PrintWriter(stdout)); - cmd.execute("--help"); + cmd.execute(flag); MatcherAssert.assertThat( - "Application did not print the help message to stdout for the --help flag", + "Application did not print the help message to stdout for the %s flag".formatted(flag), stdout.toString(), Matchers.containsString("Usage:") ); } - @Test - void applicationExitsWithZeroCodeWhenHelpFlagIsPassed() { + @ParameterizedTest + @ValueSource(strings = {"--help", "-h"}) + void applicationExitsWithZeroCodeWhenHelpFlagIsPassed(final String flag) { final CommandLine cmd = new CommandLine(new Application()); - final int exit = cmd.execute("--help"); + final int exit = cmd.execute(flag); MatcherAssert.assertThat( - "Application did not exit with a zero code when the --help flag was used", + "Application did not exit with a zero code when the %s flag was used".formatted(flag), exit, Matchers.is(0) ); From 70486d6e87192a2bee68209ab0843886523f77db Mon Sep 17 00:00:00 2001 From: Andrey Chernykh Date: Fri, 10 Oct 2025 16:15:34 +0300 Subject: [PATCH 7/7] #152: Disable help flag tests --- src/main/java/org/eolang/aoi/Application.java | 2 +- src/test/java/org/eolang/aoi/ApplicationTest.java | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/eolang/aoi/Application.java b/src/main/java/org/eolang/aoi/Application.java index 8c2f237..96aa90f 100644 --- a/src/main/java/org/eolang/aoi/Application.java +++ b/src/main/java/org/eolang/aoi/Application.java @@ -34,7 +34,7 @@ public final class Application implements Callable { * A standard help option that triggers Picocli's built-in help display. */ @Option( - names = {"--help", "-h"}, + names = "--help", usageHelp = true, description = "Print this message and exit." ) diff --git a/src/test/java/org/eolang/aoi/ApplicationTest.java b/src/test/java/org/eolang/aoi/ApplicationTest.java index 6dd899b..136e0de 100644 --- a/src/test/java/org/eolang/aoi/ApplicationTest.java +++ b/src/test/java/org/eolang/aoi/ApplicationTest.java @@ -9,6 +9,7 @@ import java.nio.file.Path; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.params.ParameterizedTest; @@ -19,6 +20,10 @@ * Tests for {@link Application}. * * @since 0.0.5 + * @todo #152:15min Enable help flag tests after adding support for the short `-h` flag. + * Once implemented, remove @Disabled from the following tests: + * {@link ApplicationTest#applicationPrintsHelpMessageWhenHelpFlagIsPassed}, + * {@link ApplicationTest#applicationExitsWithZeroCodeWhenHelpFlagIsPassed} */ final class ApplicationTest { @Test @@ -51,6 +56,7 @@ void applicationPrintsNothingToStderrOnSuccessfulExecution( ); } + @Disabled @ParameterizedTest @ValueSource(strings = {"--help", "-h"}) void applicationPrintsHelpMessageWhenHelpFlagIsPassed(final String flag) { @@ -65,6 +71,7 @@ void applicationPrintsHelpMessageWhenHelpFlagIsPassed(final String flag) { ); } + @Disabled @ParameterizedTest @ValueSource(strings = {"--help", "-h"}) void applicationExitsWithZeroCodeWhenHelpFlagIsPassed(final String flag) {