From 68d33d4b1c0016bfc698b708459345749edfd563 Mon Sep 17 00:00:00 2001 From: Remko Popma Date: Sat, 26 Aug 2023 20:14:47 +0900 Subject: [PATCH] [#2087] Test trace log when echo=false for interactive options Closes #2087 --- src/test/java/picocli/InteractiveArgTest.java | 188 ++++++++++++++++++ 1 file changed, 188 insertions(+) diff --git a/src/test/java/picocli/InteractiveArgTest.java b/src/test/java/picocli/InteractiveArgTest.java index fe25dff3b..275bfc4d4 100644 --- a/src/test/java/picocli/InteractiveArgTest.java +++ b/src/test/java/picocli/InteractiveArgTest.java @@ -1,5 +1,6 @@ package picocli; +import org.junit.After; import org.junit.Rule; import org.junit.Test; import org.junit.contrib.java.lang.system.ProvideSystemProperty; @@ -87,6 +88,11 @@ String err() { } } + @After + public void afterEachTest() { +// CommandLine.tracer().setLevel(CommandLine.TraceLevel.WARN); + } + @Test public void testInteractiveOptionReadsFromStdIn() { class App { @@ -899,4 +905,186 @@ class App { } } + @Test + public void testIssue2087InteractiveEchoOffOptionWithDefault() { + class Issue2087a { + @Option(names = "-a", interactive = true, echo = false, defaultValue = "DEFAULT") String a; + } + + System.setProperty("picocli.trace", "DEBUG"); + Streams streams = new Streams(); + try { + Capture capture = new Capture(); + + Issue2087a app = new Issue2087a(); + new CommandLine(app).parseArgs(); + assertEquals("DEFAULT", app.a); + + assertEquals("", capture.out()); + assertThat(capture.err(), containsString("Applying defaultValue (*****(masked)) to field String picocli.InteractiveArgTest$1Issue2087a.a")); + assertThat(capture.err(), containsString("Setting field String picocli.InteractiveArgTest$1Issue2087a.a to '*****(masked)' (was 'null') for")); + } finally { + streams.reset(); + } + } + + @Test + public void testIssue2087InteractiveEchoOnOptionWithDefault() { + class Issue2087b { + @Option(names = "-a", interactive = true, echo = true, defaultValue = "DEFAULT") String a; + } + + System.setProperty("picocli.trace", "DEBUG"); + Streams streams = new Streams(); + try { + Capture capture = new Capture(); + + Issue2087b app = new Issue2087b(); + new CommandLine(app).parseArgs(); + assertEquals("DEFAULT", app.a); + + assertEquals("", capture.out()); + assertThat(capture.err(), containsString("Applying defaultValue (DEFAULT) to field String picocli.InteractiveArgTest$1Issue2087b.a")); + assertThat(capture.err(), containsString("Setting field String picocli.InteractiveArgTest$1Issue2087b.a to 'DEFAULT' (was 'null') for")); + } finally { + streams.reset(); + } + } + + static class Issue2087DefaultProvider1 implements CommandLine.IDefaultValueProvider { + public String defaultValue(ArgSpec argSpec) throws Exception { + return "DEFAULT1"; + } + } + + @Test + public void testIssue2087InteractiveOptionEchoOffWithDefaultProvider() { + @CommandLine.Command(defaultValueProvider = Issue2087DefaultProvider1.class) + class Issue2087c { + @Option(names = "-a", interactive = true, echo = false) String a; + } + System.setProperty("picocli.trace", "DEBUG"); + Streams streams = new Streams(); + try { + Capture capture = new Capture(); + Issue2087c app = new Issue2087c(); + new CommandLine(app).parseArgs(); + assertEquals("DEFAULT1", app.a); + + assertEquals("", capture.out()); + assertThat(capture.err(), containsString("Applying defaultValue (*****(masked)) from picocli.InteractiveArgTest$Issue2087DefaultProvider1")); + assertThat(capture.err(), containsString("Setting field String picocli.InteractiveArgTest$1Issue2087c.a to '*****(masked)' (was 'null') for")); + } finally { + streams.reset(); + } + } + + @Test + public void testIssue2087InteractiveOptionEchoOnWithDefaultProvider() { + @CommandLine.Command(defaultValueProvider = Issue2087DefaultProvider1.class) + class Issue2087d { + @Option(names = "-a", interactive = true, echo = true) String a; + } + System.setProperty("picocli.trace", "DEBUG"); + Streams streams = new Streams(); + try { + Capture capture = new Capture(); + Issue2087d app = new Issue2087d(); + new CommandLine(app).parseArgs(); + assertEquals("DEFAULT1", app.a); + + assertEquals("", capture.out()); + assertThat(capture.err(), containsString("Applying defaultValue (DEFAULT1) from picocli.InteractiveArgTest$Issue2087DefaultProvider1")); + assertThat(capture.err(), containsString("Setting field String picocli.InteractiveArgTest$1Issue2087d.a to 'DEFAULT1' (was 'null') for")); + } finally { + streams.reset(); + } + } + + @Test + public void testIssue2087InteractiveEchoOnPositionalWithDefault() { + class Issue2087e { + @Parameters(interactive = true, echo = true, defaultValue = "DEFAULT") String a; + } + System.setProperty("picocli.trace", "DEBUG"); + Streams streams = new Streams(); + try { + Capture capture = new Capture(); + Issue2087e app = new Issue2087e(); + new CommandLine(app).parseArgs(); + assertEquals("DEFAULT", app.a); + + assertEquals("", capture.out()); + assertThat(capture.err(), containsString("Applying defaultValue (DEFAULT) ")); + assertThat(capture.err(), containsString("Setting field String picocli.InteractiveArgTest$1Issue2087e.a to 'DEFAULT' (was 'null') for")); + } finally { + streams.reset(); + } + } + + @Test + public void testIssue2087InteractiveEchoOffPositionalWithDefault() { + class Issue2087g { + @Parameters(interactive = true, echo = false, defaultValue = "DEFAULT") String a; + } + System.setProperty("picocli.trace", "DEBUG"); + Streams streams = new Streams(); + try { + Capture capture = new Capture(); + Issue2087g app = new Issue2087g(); + new CommandLine(app).parseArgs(); + assertEquals("DEFAULT", app.a); + + assertEquals("", capture.out()); + assertThat(capture.err(), containsString("Applying defaultValue (*****(masked)) ")); + assertThat(capture.err(), containsString("Setting field String picocli.InteractiveArgTest$1Issue2087g.a to '*****(masked)' (was 'null') for")); + } finally { + streams.reset(); + } + } + + @Test + public void testIssue2087InteractiveEchoOnPositionalWithDefaultProvider() { + @CommandLine.Command(defaultValueProvider = Issue2087DefaultProvider1.class) + class Issue2087f { + @Parameters(interactive = true, echo = true) String a; + } + System.setProperty("picocli.trace", "DEBUG"); + Streams streams = new Streams(); + try { + Capture capture = new Capture(); + Issue2087f app = new Issue2087f(); + new CommandLine(app).parseArgs(); + assertEquals("DEFAULT1", app.a); + + assertEquals("", capture.out()); + assertThat(capture.err(), containsString("Applying defaultValue (DEFAULT1) from picocli.InteractiveArgTest$Issue2087DefaultProvider1")); + assertThat(capture.err(), containsString("Setting field String picocli.InteractiveArgTest$1Issue2087f.a to 'DEFAULT1' (was 'null') for")); + } finally { + streams.reset(); + } + } + + @Test + public void testIssue2087InteractiveEchoOffPositionalWithDefaultProvider() { + @CommandLine.Command(defaultValueProvider = Issue2087DefaultProvider1.class) + class Issue2087h { + @Parameters(interactive = true, echo = false) String a; + } + System.setProperty("picocli.trace", "DEBUG"); + Streams streams = new Streams(); + try { + Capture capture = new Capture(); + Issue2087h app = new Issue2087h(); + new CommandLine(app).parseArgs(); + assertEquals("DEFAULT1", app.a); + + assertEquals("", capture.out()); + assertThat(capture.err(), containsString("Applying defaultValue (*****(masked)) from picocli.InteractiveArgTest$Issue2087DefaultProvider1")); + assertThat(capture.err(), containsString("Setting field String picocli.InteractiveArgTest$1Issue2087h.a to '*****(masked)' (was 'null') for")); + } finally { + streams.reset(); + } + } + }