From 1cf59446d684cf0de2b28376068314888f2096f6 Mon Sep 17 00:00:00 2001 From: Remko Popma Date: Fri, 19 Mar 2021 11:30:40 +0900 Subject: [PATCH] [#1352] add tests to attempt to reproduce NPE in AutoComplete --- src/test/java/picocli/AutoCompleteTest.java | 75 ++++++++++++++++++- .../Issue1352ResourceBundle.properties | 1 + 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/picocli/Issue1352ResourceBundle.properties diff --git a/src/test/java/picocli/AutoCompleteTest.java b/src/test/java/picocli/AutoCompleteTest.java index de5a57b89..61e3a4085 100644 --- a/src/test/java/picocli/AutoCompleteTest.java +++ b/src/test/java/picocli/AutoCompleteTest.java @@ -35,6 +35,7 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; @@ -49,9 +50,14 @@ import java.util.Collections; import java.util.Comparator; import java.util.List; +import java.util.ListResourceBundle; +import java.util.PropertyResourceBundle; +import java.util.Scanner; import java.util.concurrent.TimeUnit; import static java.lang.String.format; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.not; import static org.junit.Assert.*; /** @@ -1257,7 +1263,7 @@ class App { @Option(names = "-B") Boolean object; } String actual = AutoComplete.bash("booltest", new CommandLine(new App())); - assertThat(actual, CoreMatchers.containsString("local flag_opts=\"-b -B\"")); + assertThat(actual, containsString("local flag_opts=\"-b -B\"")); } @Test @@ -1823,4 +1829,71 @@ static class NestedLevel1 implements Runnable { public void run() { } } + + @Command(name = "${project.cli.command}", resourceBundle = "picocli.Issue1352ResourceBundle", + mixinStandardHelpOptions = true) + static class Issue1352CommandWithResourceBundle {} + + @Test + public void testIssue1352_CommandNameResourceBundle() throws FileNotFoundException { + // AutoComplete.bash("scriptname", new CommandLine(new Issue1352CommandWithResourceBundle())); + File existingScript = new File("mycommandfromresourcebundle_completion"); + if (existingScript.exists()) { + assertTrue(existingScript.delete()); + } + try { + AutoComplete.main(Issue1352CommandWithResourceBundle.class.getName()); + + assertEquals("", systemErrRule.getLog()); + assertEquals("", systemOutRule.getLog()); + + assertTrue("Expected file '" + existingScript.getAbsolutePath() + "' to exist", + existingScript.exists()); + + Scanner scanner = new Scanner(existingScript); + scanner.useDelimiter("\\Z"); // end of file + String script = scanner.next(); + scanner.close(); + assertThat(script, containsString("mycommandfromresourcebundle")); + assertThat(script, not(containsString("${project.cli.command}"))); + } finally { + existingScript.delete(); + } + } + + @Command(name = "parent", resourceBundle = "picocli.Issue1352ResourceBundle", + subcommands = Issue1352CommandWithResourceBundle.class, + mixinStandardHelpOptions = true) + static class Issue1352ParentCommand {} + + @Test + public void testIssue1352_SubcommandNameResourceBundle() throws FileNotFoundException { + File existingScript = new File("parent_completion"); + if (existingScript.exists()) { + assertTrue(existingScript.delete()); + } + try { + AutoComplete.main(Issue1352ParentCommand.class.getName()); + + assertEquals("", systemErrRule.getLog()); + assertEquals("", systemOutRule.getLog()); + + assertTrue("Expected file '" + existingScript.getAbsolutePath() + "' to exist", + existingScript.exists()); + + Scanner scanner = new Scanner(existingScript); + scanner.useDelimiter("\\Z"); // end of file + String script = scanner.next(); + scanner.close(); + assertThat(script, containsString("_picocli_parent")); + assertThat(script, containsString("mycommandfromresourcebundle")); + + // FIXME: the below assertion fails with picocli 4.6.1... + //assertThat(script, not(containsString("${project.cli.command}"))); + + } finally { + existingScript.delete(); + } + } + } diff --git a/src/test/resources/picocli/Issue1352ResourceBundle.properties b/src/test/resources/picocli/Issue1352ResourceBundle.properties new file mode 100644 index 000000000..33f52fbf2 --- /dev/null +++ b/src/test/resources/picocli/Issue1352ResourceBundle.properties @@ -0,0 +1 @@ +project.cli.command=mycommandfromresourcebundle