Skip to content

Commit 68d33d4

Browse files
committed
[#2087] Test trace log when echo=false for interactive options
Closes #2087
1 parent bb94344 commit 68d33d4

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed

src/test/java/picocli/InteractiveArgTest.java

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package picocli;
22

3+
import org.junit.After;
34
import org.junit.Rule;
45
import org.junit.Test;
56
import org.junit.contrib.java.lang.system.ProvideSystemProperty;
@@ -87,6 +88,11 @@ String err() {
8788
}
8889
}
8990

91+
@After
92+
public void afterEachTest() {
93+
// CommandLine.tracer().setLevel(CommandLine.TraceLevel.WARN);
94+
}
95+
9096
@Test
9197
public void testInteractiveOptionReadsFromStdIn() {
9298
class App {
@@ -899,4 +905,186 @@ class App {
899905
}
900906
}
901907

908+
@Test
909+
public void testIssue2087InteractiveEchoOffOptionWithDefault() {
910+
class Issue2087a {
911+
@Option(names = "-a", interactive = true, echo = false, defaultValue = "DEFAULT") String a;
912+
}
913+
914+
System.setProperty("picocli.trace", "DEBUG");
915+
Streams streams = new Streams();
916+
try {
917+
Capture capture = new Capture();
918+
919+
Issue2087a app = new Issue2087a();
920+
new CommandLine(app).parseArgs();
921+
assertEquals("DEFAULT", app.a);
922+
923+
assertEquals("", capture.out());
924+
assertThat(capture.err(), containsString("Applying defaultValue (*****(masked)) to field String picocli.InteractiveArgTest$1Issue2087a.a"));
925+
assertThat(capture.err(), containsString("Setting field String picocli.InteractiveArgTest$1Issue2087a.a to '*****(masked)' (was 'null') for"));
926+
} finally {
927+
streams.reset();
928+
}
929+
}
930+
931+
@Test
932+
public void testIssue2087InteractiveEchoOnOptionWithDefault() {
933+
class Issue2087b {
934+
@Option(names = "-a", interactive = true, echo = true, defaultValue = "DEFAULT") String a;
935+
}
936+
937+
System.setProperty("picocli.trace", "DEBUG");
938+
Streams streams = new Streams();
939+
try {
940+
Capture capture = new Capture();
941+
942+
Issue2087b app = new Issue2087b();
943+
new CommandLine(app).parseArgs();
944+
assertEquals("DEFAULT", app.a);
945+
946+
assertEquals("", capture.out());
947+
assertThat(capture.err(), containsString("Applying defaultValue (DEFAULT) to field String picocli.InteractiveArgTest$1Issue2087b.a"));
948+
assertThat(capture.err(), containsString("Setting field String picocli.InteractiveArgTest$1Issue2087b.a to 'DEFAULT' (was 'null') for"));
949+
} finally {
950+
streams.reset();
951+
}
952+
}
953+
954+
static class Issue2087DefaultProvider1 implements CommandLine.IDefaultValueProvider {
955+
public String defaultValue(ArgSpec argSpec) throws Exception {
956+
return "DEFAULT1";
957+
}
958+
}
959+
960+
@Test
961+
public void testIssue2087InteractiveOptionEchoOffWithDefaultProvider() {
962+
@CommandLine.Command(defaultValueProvider = Issue2087DefaultProvider1.class)
963+
class Issue2087c {
964+
@Option(names = "-a", interactive = true, echo = false) String a;
965+
}
966+
System.setProperty("picocli.trace", "DEBUG");
967+
Streams streams = new Streams();
968+
try {
969+
Capture capture = new Capture();
970+
Issue2087c app = new Issue2087c();
971+
new CommandLine(app).parseArgs();
972+
assertEquals("DEFAULT1", app.a);
973+
974+
assertEquals("", capture.out());
975+
assertThat(capture.err(), containsString("Applying defaultValue (*****(masked)) from picocli.InteractiveArgTest$Issue2087DefaultProvider1"));
976+
assertThat(capture.err(), containsString("Setting field String picocli.InteractiveArgTest$1Issue2087c.a to '*****(masked)' (was 'null') for"));
977+
} finally {
978+
streams.reset();
979+
}
980+
}
981+
982+
@Test
983+
public void testIssue2087InteractiveOptionEchoOnWithDefaultProvider() {
984+
@CommandLine.Command(defaultValueProvider = Issue2087DefaultProvider1.class)
985+
class Issue2087d {
986+
@Option(names = "-a", interactive = true, echo = true) String a;
987+
}
988+
System.setProperty("picocli.trace", "DEBUG");
989+
Streams streams = new Streams();
990+
try {
991+
Capture capture = new Capture();
992+
Issue2087d app = new Issue2087d();
993+
new CommandLine(app).parseArgs();
994+
assertEquals("DEFAULT1", app.a);
995+
996+
assertEquals("", capture.out());
997+
assertThat(capture.err(), containsString("Applying defaultValue (DEFAULT1) from picocli.InteractiveArgTest$Issue2087DefaultProvider1"));
998+
assertThat(capture.err(), containsString("Setting field String picocli.InteractiveArgTest$1Issue2087d.a to 'DEFAULT1' (was 'null') for"));
999+
} finally {
1000+
streams.reset();
1001+
}
1002+
}
1003+
1004+
@Test
1005+
public void testIssue2087InteractiveEchoOnPositionalWithDefault() {
1006+
class Issue2087e {
1007+
@Parameters(interactive = true, echo = true, defaultValue = "DEFAULT") String a;
1008+
}
1009+
System.setProperty("picocli.trace", "DEBUG");
1010+
Streams streams = new Streams();
1011+
try {
1012+
Capture capture = new Capture();
1013+
Issue2087e app = new Issue2087e();
1014+
new CommandLine(app).parseArgs();
1015+
assertEquals("DEFAULT", app.a);
1016+
1017+
assertEquals("", capture.out());
1018+
assertThat(capture.err(), containsString("Applying defaultValue (DEFAULT) "));
1019+
assertThat(capture.err(), containsString("Setting field String picocli.InteractiveArgTest$1Issue2087e.a to 'DEFAULT' (was 'null') for"));
1020+
} finally {
1021+
streams.reset();
1022+
}
1023+
}
1024+
1025+
@Test
1026+
public void testIssue2087InteractiveEchoOffPositionalWithDefault() {
1027+
class Issue2087g {
1028+
@Parameters(interactive = true, echo = false, defaultValue = "DEFAULT") String a;
1029+
}
1030+
System.setProperty("picocli.trace", "DEBUG");
1031+
Streams streams = new Streams();
1032+
try {
1033+
Capture capture = new Capture();
1034+
Issue2087g app = new Issue2087g();
1035+
new CommandLine(app).parseArgs();
1036+
assertEquals("DEFAULT", app.a);
1037+
1038+
assertEquals("", capture.out());
1039+
assertThat(capture.err(), containsString("Applying defaultValue (*****(masked)) "));
1040+
assertThat(capture.err(), containsString("Setting field String picocli.InteractiveArgTest$1Issue2087g.a to '*****(masked)' (was 'null') for"));
1041+
} finally {
1042+
streams.reset();
1043+
}
1044+
}
1045+
1046+
@Test
1047+
public void testIssue2087InteractiveEchoOnPositionalWithDefaultProvider() {
1048+
@CommandLine.Command(defaultValueProvider = Issue2087DefaultProvider1.class)
1049+
class Issue2087f {
1050+
@Parameters(interactive = true, echo = true) String a;
1051+
}
1052+
System.setProperty("picocli.trace", "DEBUG");
1053+
Streams streams = new Streams();
1054+
try {
1055+
Capture capture = new Capture();
1056+
Issue2087f app = new Issue2087f();
1057+
new CommandLine(app).parseArgs();
1058+
assertEquals("DEFAULT1", app.a);
1059+
1060+
assertEquals("", capture.out());
1061+
assertThat(capture.err(), containsString("Applying defaultValue (DEFAULT1) from picocli.InteractiveArgTest$Issue2087DefaultProvider1"));
1062+
assertThat(capture.err(), containsString("Setting field String picocli.InteractiveArgTest$1Issue2087f.a to 'DEFAULT1' (was 'null') for"));
1063+
} finally {
1064+
streams.reset();
1065+
}
1066+
}
1067+
1068+
@Test
1069+
public void testIssue2087InteractiveEchoOffPositionalWithDefaultProvider() {
1070+
@CommandLine.Command(defaultValueProvider = Issue2087DefaultProvider1.class)
1071+
class Issue2087h {
1072+
@Parameters(interactive = true, echo = false) String a;
1073+
}
1074+
System.setProperty("picocli.trace", "DEBUG");
1075+
Streams streams = new Streams();
1076+
try {
1077+
Capture capture = new Capture();
1078+
Issue2087h app = new Issue2087h();
1079+
new CommandLine(app).parseArgs();
1080+
assertEquals("DEFAULT1", app.a);
1081+
1082+
assertEquals("", capture.out());
1083+
assertThat(capture.err(), containsString("Applying defaultValue (*****(masked)) from picocli.InteractiveArgTest$Issue2087DefaultProvider1"));
1084+
assertThat(capture.err(), containsString("Setting field String picocli.InteractiveArgTest$1Issue2087h.a to '*****(masked)' (was 'null') for"));
1085+
} finally {
1086+
streams.reset();
1087+
}
1088+
}
1089+
9021090
}

0 commit comments

Comments
 (0)