Skip to content

Commit

Permalink
[#1103] bugfix in stripAnsiTrace logic for handling nested closing br…
Browse files Browse the repository at this point in the history
…ace; added test
  • Loading branch information
remkop committed Jun 8, 2020
1 parent c93746c commit 8c763df
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
31 changes: 31 additions & 0 deletions src/test/java/picocli/CommandLineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2110,6 +2110,37 @@ public void testTracingDebugWithSubCommands() throws Exception {
}
assertEquals(stripAnsiTrace(stripHashcodes(expected)), stripAnsiTrace(stripHashcodes(actual)));
}

@Test
public void testStripAnsiTrace() {
String original = "[picocli INFO] Picocli version: 4.3.3-SNAPSHOT, JVM: 1.5.0_22 (Sun Microsystems Inc. Java HotSpot(TM) Client VM 1.5.0_22-b03), OS: Windows NT (unknown) 6.2 x86\n" +
"[picocli INFO] Parsing 8 command line args [--git-dir=/home/rpopma/picocli, commit, -m, \"Fixed typos\", --, src1.java, src2.java, src3.java]\n" +
"[picocli DEBUG] Parser configuration: optionsCaseInsensitive=false, subcommandsCaseInsensitive=false, abbreviatedOptionsAllowed=false, abbreviatedSubcommandsAllowed=false, aritySatisfiedByAttachedOptionParam=false, atFileCommentChar=#, caseInsensitiveEnumValuesAllowed=false, collectErrors=false, endOfOptionsDelimiter=--, expandAtFiles=true, limitSplit=false, overwrittenOptionsAllowed=false, posixClusteredShortOptionsAllowed=true, separator=null, splitQuotedStrings=false, stopAtPositional=false, stopAtUnmatched=false, toggleBooleanFlags=false, trimQuotes=false, unmatchedArgumentsAllowed=false, unmatchedOptionsArePositionalParams=false, useSimplifiedAtFiles=false\n" +
"[picocli DEBUG] (ANSI is disabled by default: systemproperty[picocli.ansi]=false, isatty=true, TERM=null, OSTYPE=null, isWindows=true, JansiConsoleInstalled=false, ANSICON=null, ConEmuANSI=null, NO_COLOR=null, CLICOLOR=null, CLICOLOR_FORCE=null)\n" +
"[picocli DEBUG] Initializing command 'git' (user object: picocli.Demo$Git@10f8ee4): 3 options, 0 positional parameters, 0 required, 0 groups, 12 subcommands.\n" +
"[picocli DEBUG] Set initial value for field java.io.File picocli.Demo$Git.gitDir of type class java.io.File to null.\n";

String expected = "[picocli INFO] Picocli version: 4.3.3-SNAPSHOT, JVM: 1.5.0_22 (Sun Microsystems Inc. Java HotSpot(TM) Client VM 1.5.0_22-b03), OS: Windows NT (unknown) 6.2 x86\n" +
"[picocli INFO] Parsing 8 command line args [--git-dir=/home/rpopma/picocli, commit, -m, \"Fixed typos\", --, src1.java, src2.java, src3.java]\n" +
"[picocli DEBUG] Parser configuration: optionsCaseInsensitive=false, subcommandsCaseInsensitive=false, abbreviatedOptionsAllowed=false, abbreviatedSubcommandsAllowed=false, aritySatisfiedByAttachedOptionParam=false, atFileCommentChar=#, caseInsensitiveEnumValuesAllowed=false, collectErrors=false, endOfOptionsDelimiter=--, expandAtFiles=true, limitSplit=false, overwrittenOptionsAllowed=false, posixClusteredShortOptionsAllowed=true, separator=null, splitQuotedStrings=false, stopAtPositional=false, stopAtUnmatched=false, toggleBooleanFlags=false, trimQuotes=false, unmatchedArgumentsAllowed=false, unmatchedOptionsArePositionalParams=false, useSimplifiedAtFiles=false\n" +
"[picocli DEBUG] (ANSI is disabled ...)\n" +
"[picocli DEBUG] Initializing command 'git' (user object: picocli.Demo$Git@10f8ee4): 3 options, 0 positional parameters, 0 required, 0 groups, 12 subcommands.\n" +
"[picocli DEBUG] Set initial value for field java.io.File picocli.Demo$Git.gitDir of type class java.io.File to null.\n";

assertEquals(expected, stripAnsiTrace(original));

// Now use environment value with closing brace: ANSICON=80x1000 (80x25)
// https://github.com/remkop/picocli/issues/1103
String original2 = "[picocli INFO] Picocli version: 4.3.3-SNAPSHOT, JVM: 1.5.0_22 (Sun Microsystems Inc. Java HotSpot(TM) Client VM 1.5.0_22-b03), OS: Windows NT (unknown) 6.2 x86\n" +
"[picocli INFO] Parsing 8 command line args [--git-dir=/home/rpopma/picocli, commit, -m, \"Fixed typos\", --, src1.java, src2.java, src3.java]\n" +
"[picocli DEBUG] Parser configuration: optionsCaseInsensitive=false, subcommandsCaseInsensitive=false, abbreviatedOptionsAllowed=false, abbreviatedSubcommandsAllowed=false, aritySatisfiedByAttachedOptionParam=false, atFileCommentChar=#, caseInsensitiveEnumValuesAllowed=false, collectErrors=false, endOfOptionsDelimiter=--, expandAtFiles=true, limitSplit=false, overwrittenOptionsAllowed=false, posixClusteredShortOptionsAllowed=true, separator=null, splitQuotedStrings=false, stopAtPositional=false, stopAtUnmatched=false, toggleBooleanFlags=false, trimQuotes=false, unmatchedArgumentsAllowed=false, unmatchedOptionsArePositionalParams=false, useSimplifiedAtFiles=false\n" +
"[picocli DEBUG] (ANSI is disabled by default: systemproperty[picocli.ansi]=false, isatty=true, TERM=null, OSTYPE=null, isWindows=true, JansiConsoleInstalled=false, ANSICON=80x1000 (80x25), ConEmuANSI=null, NO_COLOR=null, CLICOLOR=null, CLICOLOR_FORCE=null)\n" +
"[picocli DEBUG] Initializing command 'git' (user object: picocli.Demo$Git@10f8ee4): 3 options, 0 positional parameters, 0 required, 0 groups, 12 subcommands.\n" +
"[picocli DEBUG] Set initial value for field java.io.File picocli.Demo$Git.gitDir of type class java.io.File to null.\n";

assertEquals(expected, stripAnsiTrace(original2));
}

@Test
public void testTraceWarningIfOptionOverwrittenWhenOverwrittenOptionsAllowed() throws Exception {
PrintStream originalErr = System.err;
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/picocli/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,14 @@ public static String stripAnsiTrace(String original) {
private static String stripAnsiTraceOnce(String original, String prefix, String prefix2, String suffix, String replacement) {
int pos = original.indexOf(prefix);
if (pos > 0) {
int endPos = pos;
if (prefix2 != null) { // this allows us to skip over any intermediate closing brackets ')' in the "ANSI is disabled by default" line (https://github.com/remkop/picocli/issues/1103#issuecomment-640204473)
int pos2 = original.indexOf(prefix2, pos);
if (pos2 > 0) {
pos = pos2;
endPos = pos2;
}
}
int to = original.indexOf(suffix, pos);
int to = original.indexOf(suffix, endPos);
return original.substring(0, pos) + replacement + original.substring(to);
}
return original;
Expand Down

0 comments on commit 8c763df

Please sign in to comment.