Skip to content

Commit c07d4ec

Browse files
committed
[CLI-341] HelpFormatter infinite loop with 0 width input
Add tests for negative input
1 parent 946ecc2 commit c07d4ec

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/test/java/org/apache/commons/cli/HelpFormatterTest.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
4040
import org.junit.jupiter.params.ParameterizedTest;
4141
import org.junit.jupiter.params.provider.Arguments;
4242
import org.junit.jupiter.params.provider.MethodSource;
43+
import org.junit.jupiter.params.provider.ValueSource;
4344

4445
/**
4546
* Test case for the HelpFormatter class.
@@ -154,19 +155,21 @@ void testDefaultArgName() {
154155
assertEquals("usage: app -f <argument>" + EOL, out.toString());
155156
}
156157

157-
@Test
158-
public void testDeprecatedFindWrapPosZeroWidth() {
159-
final int pos = new HelpFormatter().findWrapPos("Hello World", 0, 0);
160-
assertEquals(0, pos);
158+
@ParameterizedTest
159+
@ValueSource(ints = { -100, -1, 0 })
160+
public void testDeprecatedFindWrapPosZeroWidth(final int width) {
161+
final int pos = new HelpFormatter().findWrapPos("Hello World", width, 0);
162+
assertEquals(width, pos);
161163
}
162164

163-
@Test
164-
public void testDeprecatedPrintOptionsZeroWidth() {
165+
@ParameterizedTest
166+
@ValueSource(ints = { -100, -1, 0 })
167+
public void testDeprecatedPrintOptionsZeroWidth(final int width) {
165168
final Options options = new Options();
166169
options.addOption("h", "help", false, "Show help");
167170
final StringWriter out = new StringWriter();
168171
final PrintWriter pw = new PrintWriter(out);
169-
new HelpFormatter().printOptions(pw, 0, options, 1, 3);
172+
new HelpFormatter().printOptions(pw, width, options, 1, 3);
170173
final String result = out.toString();
171174
assertNotNull(result);
172175
}

0 commit comments

Comments
 (0)