Skip to content

Commit

Permalink
Fix regression on assert command for tool tests (#3088)
Browse files Browse the repository at this point in the history
* revert deletion of assertCommand method to avoid regression
* deprecate method

Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
  • Loading branch information
rolnico authored and geofjamg committed Sep 18, 2024
1 parent e1fac99 commit 70ae502
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -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);
}
Expand All @@ -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<String, String> comparisonFunction) {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ByteArrayOutputStream berr = new ByteArrayOutputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <geoffroy.jamgotchian at rte-france.com>}
Expand Down Expand Up @@ -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<Tool> getTools() {
return Arrays.asList(new Tool1(), new Tool2());
return Arrays.asList(new Tool1(), new Tool2(), new Tool3());
}

@Override
Expand All @@ -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() +
Expand All @@ -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");
Expand Down Expand Up @@ -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"));
}
}

0 comments on commit 70ae502

Please sign in to comment.