diff --git a/tools-test/src/main/java/com/powsybl/tools/test/AbstractToolTest.java b/tools-test/src/main/java/com/powsybl/tools/test/AbstractToolTest.java index 680651c5519..6ff033accef 100644 --- a/tools-test/src/main/java/com/powsybl/tools/test/AbstractToolTest.java +++ b/tools-test/src/main/java/com/powsybl/tools/test/AbstractToolTest.java @@ -33,6 +33,7 @@ import java.nio.file.Files; import java.util.Objects; import java.util.function.BiConsumer; +import java.util.regex.Pattern; import static org.junit.jupiter.api.Assertions.*; @@ -93,6 +94,10 @@ public static void containsTxt(String expected, String actual) { assertTrue(actual.contains(expected), () -> ASSERT_MATCH_TEXT_BLOCK.formatted(expected, actual)); } + public void matchTextOrRegex(String expected, String actual) { + assertTrue(actual.equals(expected) || Pattern.compile(expected).matcher(actual).find()); + } + protected void assertCommandSuccessful(String[] args) { assertCommand(args, CommandLineTools.COMMAND_OK_STATUS, null, "", ComparisonUtils::assertTxtEquals); } @@ -117,6 +122,18 @@ protected void assertCommandErrorMatch(String[] args, String expectedErr) { assertCommand(args, CommandLineTools.EXECUTION_ERROR_STATUS, null, expectedErr, AbstractToolTest::containsTxt); } + /** + * @deprecated use {@link AbstractToolTest#assertCommandMatchTextOrRegex} instead + */ + @Deprecated(since = "6.4.0") + protected void assertCommand(String[] args, int expectedStatus, String expectedOut, String expectedErr) { + assertCommandMatchTextOrRegex(args, expectedStatus, expectedOut, expectedErr); + } + + protected void assertCommandMatchTextOrRegex(String[] args, int expectedStatus, String expectedOut, String expectedErr) { + assertCommand(args, expectedStatus, expectedOut, expectedErr, this::matchTextOrRegex); + } + private void assertCommand(String[] args, int expectedStatus, String expectedOut, String expectedErr, BiConsumer comparisonFunction) { ByteArrayOutputStream bout = new ByteArrayOutputStream(); ByteArrayOutputStream berr = new ByteArrayOutputStream(); diff --git a/tools-test/src/test/java/com/powsybl/tools/test/CommandLineToolsTest.java b/tools-test/src/test/java/com/powsybl/tools/test/CommandLineToolsTest.java index a1cca0d839a..6b95c5da39f 100644 --- a/tools-test/src/test/java/com/powsybl/tools/test/CommandLineToolsTest.java +++ b/tools-test/src/test/java/com/powsybl/tools/test/CommandLineToolsTest.java @@ -16,8 +16,12 @@ import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.junit.jupiter.api.Test; +import org.opentest4j.AssertionFailedError; import java.util.Arrays; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertThrows; /** * @author Geoffroy Jamgotchian {@literal } @@ -114,9 +118,54 @@ public void run(CommandLine line, ToolRunningContext context) { } } + private static class Tool3 implements Tool { + + @Override + public Command getCommand() { + return new Command() { + @Override + public String getName() { + return "tool3"; + } + + @Override + public String getTheme() { + return "theme3"; + } + + @Override + public String getDescription() { + return "test tool3"; + } + + @Override + public Options getOptions() { + Options options = new Options(); + options.addOption(Option.builder() + .longOpt("option1") + .desc("this is option 1") + .hasArg() + .argName("FILE") + .build()); + return options; + } + + @Override + public String getUsageFooter() { + return "footer1"; + } + }; + } + + @Override + public void run(CommandLine line, ToolRunningContext context) { + context.getOutputStream().print(UUID.randomUUID()); + } + } + @Override protected Iterable getTools() { - return Arrays.asList(new Tool1(), new Tool2()); + return Arrays.asList(new Tool1(), new Tool2(), new Tool3()); } @Override @@ -133,6 +182,18 @@ public void assertCommand() { assertOption(command.getOptions(), "option2", false, false); } + @Test + void testRegexOutput() { + // Matches a regex + assertCommandMatchTextOrRegex(new String[] {"tool3"}, 0, "^[a-z0-9-]+$", ""); + + // Matches a regex - deprecated method + assertCommand(new String[] {"tool3"}, 0, "^[a-z0-9-]+$", ""); + + // Matches a string + assertCommandMatchTextOrRegex(new String[] {"tool1", "--option1", "file.txt"}, 0, "result1", ""); + } + @Test void test() { String scriptOptions = "Available options are:" + System.lineSeparator() + @@ -150,12 +211,15 @@ void test() { System.lineSeparator() + "theme2:" + System.lineSeparator() + " tool2 test tool2" + System.lineSeparator() + + System.lineSeparator() + + "theme3:" + System.lineSeparator() + + " tool3 test tool3" + System.lineSeparator() + System.lineSeparator(); assertCommandError(new String[] {}, CommandLineTools.COMMAND_NOT_FOUND_STATUS, usage); // usage when command does not exist - assertCommandError(new String[] {"tool3"}, CommandLineTools.COMMAND_NOT_FOUND_STATUS, usage); + assertCommandError(new String[] {"tool4"}, CommandLineTools.COMMAND_NOT_FOUND_STATUS, usage); // command success assertCommandSuccessful(new String[] {"tool1", "--option1", "file.txt"}, "result1"); @@ -202,4 +266,11 @@ void test() { "footer1" + System.lineSeparator()); } + + @Test + void testComparisonMethods() { + matchTextOrRegex("works", "works"); + matchTextOrRegex("^[a-z0-9-]+$", UUID.randomUUID().toString()); + assertThrows(AssertionFailedError.class, () -> matchTextOrRegex("^[a-z0-9-]+$", "fail test")); + } }