@Test public void testAutoCompleteAppCompletionScriptFileWillNotOverwrite_NoSystemExit() throws Exception { File dir = new File(System.getProperty("java.io.tmpdir")); final File completionScript = new File(dir, "App_completion"); if (completionScript.exists()) {assertTrue(completionScript.delete());} completionScript.deleteOnExit(); // create the file FileOutputStream fous = new FileOutputStream(completionScript, false); fous.close(); AutoComplete.main(String.format("-o=%s", completionScript.getAbsolutePath()), "picocli.AutoComplete$App"); String expected = String.format("" + "ERROR: picocli.AutoComplete: %s exists. Specify --force to overwrite.%n" + "%s", completionScript.getAbsolutePath(), AUTO_COMPLETE_APP_USAGE); assertTrue(systemErrRule.getLog().startsWith(expected)); }
@Test public void testAutoCompleteAppCommandScriptFileWillNotOverwrite_NoSystemExit() throws Exception { File dir = new File(System.getProperty("java.io.tmpdir")); final File commandScript = new File(dir, "picocli.AutoComplete"); if (commandScript.exists()) {assertTrue(commandScript.delete());} commandScript.deleteOnExit(); // create the file FileOutputStream fous = new FileOutputStream(commandScript, false); fous.close(); File completionScript = new File(dir, commandScript.getName() + "_completion"); AutoComplete.main("--writeCommandScript", String.format("-o=%s", completionScript.getAbsolutePath()), "picocli.AutoComplete$App"); String expected = String.format("" + "ERROR: picocli.AutoComplete: %s exists. Specify --force to overwrite.%n" + "%s", commandScript.getAbsolutePath(), AUTO_COMPLETE_APP_USAGE); assertTrue(systemErrRule.getLog().startsWith(expected)); }
@Test public void test306_SupportGeneratingAutocompletionScriptForNonPublicCommandClasses() { File dir = new File(System.getProperty("java.io.tmpdir")); final File completionScript = new File(dir, "App_completion"); if (completionScript.exists()) {assertTrue(completionScript.delete());} completionScript.deleteOnExit(); exit.expectSystemExitWithStatus(AutoComplete.EXIT_CODE_SUCCESS); exit.checkAssertionAfterwards(new Assertion() { public void checkAssertion() { assertEquals("", systemErrRule.getLog()); assertEquals("", systemOutRule.getLog()); completionScript.delete(); } }); System.setProperty("picocli.autocomplete.systemExitOnSuccess", ""); AutoComplete.main(String.format("-o=%s", completionScript.getAbsolutePath()), PrivateCommandClass.class.getName()); }
@Test public void testAutoCompleteAppGeneratesScriptNameBasedOnCommandName_NoSystemExit() throws Exception { final String commandName = "bestCommandEver"; final File completionScript = new File(commandName + "_completion"); if (completionScript.exists()) {assertTrue(completionScript.delete());} completionScript.deleteOnExit(); AutoComplete.main(String.format("--name=%s", commandName), "picocli.AutoComplete$App"); byte[] completion = readBytes(completionScript); assertTrue(completionScript.delete()); String expected = expectedCompletionScriptForAutoCompleteApp().replaceAll("picocli\\.AutoComplete", commandName); assertEquals(expected, new String(completion, "UTF8")); }
@Test public void testAutoCompleteAppCompletionScriptFileWillNotOverwrite() throws Exception { File dir = new File(System.getProperty("java.io.tmpdir")); final File completionScript = new File(dir, "App_completion"); if (completionScript.exists()) {assertTrue(completionScript.delete());} completionScript.deleteOnExit(); // create the file FileOutputStream fous = new FileOutputStream(completionScript, false); fous.close(); exit.expectSystemExitWithStatus(AutoComplete.EXIT_CODE_COMPLETION_SCRIPT_EXISTS); exit.checkAssertionAfterwards(new Assertion() { public void checkAssertion() { String expected = String.format("" + "ERROR: picocli.AutoComplete: %s exists. Specify --force to overwrite.%n" + "%s", completionScript.getAbsolutePath(), AUTO_COMPLETE_APP_USAGE); assertTrue(systemErrRule.getLog().startsWith(expected)); } }); System.setProperty("picocli.autocomplete.systemExitOnError", ""); AutoComplete.main(String.format("-o=%s", completionScript.getAbsolutePath()), "picocli.AutoComplete$App"); }
@Test public void testAutoCompleteAppCommandScriptFileWillNotOverwrite() throws Exception { File dir = new File(System.getProperty("java.io.tmpdir")); final File commandScript = new File(dir, "picocli.AutoComplete"); if (commandScript.exists()) {assertTrue(commandScript.delete());} commandScript.deleteOnExit(); // create the file FileOutputStream fous = new FileOutputStream(commandScript, false); fous.close(); File completionScript = new File(dir, commandScript.getName() + "_completion"); exit.expectSystemExitWithStatus(AutoComplete.EXIT_CODE_COMMAND_SCRIPT_EXISTS); exit.checkAssertionAfterwards(new Assertion() { public void checkAssertion() { String expected = String.format("" + "ERROR: picocli.AutoComplete: %s exists. Specify --force to overwrite.%n" + "%s", commandScript.getAbsolutePath(), AUTO_COMPLETE_APP_USAGE); assertTrue(systemErrRule.getLog().startsWith(expected)); } }); System.setProperty("picocli.autocomplete.systemExitOnError", "true"); AutoComplete.main("--writeCommandScript", String.format("-o=%s", completionScript.getAbsolutePath()), "picocli.AutoComplete$App"); }
@Test public void testAutoCompleteAppCannotInstantiate_NoSystemExit() { @Command(name = "test") class TestApp { public TestApp(String noDefaultConstructor) { throw new RuntimeException();} } AutoComplete.main(TestApp.class.getName()); String actual = systemErrRule.getLog(); assertTrue(actual.startsWith("java.lang.NoSuchMethodException: picocli.AutoCompleteTest$2TestApp.<init>()")); assertTrue(actual.contains(AUTO_COMPLETE_APP_USAGE)); }
@Test public void testAutoCompleteAppCommandScriptFileWillOverwriteIfRequested() throws Exception { File dir = new File(System.getProperty("java.io.tmpdir")); final File commandScript = new File(dir, "picocli.AutoComplete"); if (commandScript.exists()) {assertTrue(commandScript.delete());} commandScript.deleteOnExit(); // create the file FileOutputStream fous = new FileOutputStream(commandScript, false); fous.close(); assertEquals(0, commandScript.length()); File completionScript = new File(dir, commandScript.getName() + "_completion"); exit.expectSystemExitWithStatus(AutoComplete.EXIT_CODE_SUCCESS); exit.checkAssertionAfterwards(new Assertion() { public void checkAssertion() { assertEquals("", systemErrRule.getLog()); assertNotEquals(0, commandScript.length()); assertTrue(commandScript.delete()); } }); System.setProperty("picocli.autocomplete.systemExitOnSuccess", "true"); AutoComplete.main("--writeCommandScript", "--force", String.format("-o=%s", completionScript.getAbsolutePath()), "picocli.AutoComplete$App"); }
@Test public void testAutoCompleteAppUsesCustomFactory() throws Exception { final String commandName = "nondefault"; final File completionScript = new File(commandName + "_completion"); if (completionScript.exists()) {assertTrue(completionScript.delete());} completionScript.deleteOnExit(); exit.expectSystemExitWithStatus(AutoComplete.EXIT_CODE_SUCCESS); exit.checkAssertionAfterwards(new Assertion() { public void checkAssertion() throws Exception { byte[] completion = readBytes(completionScript); assertTrue(completionScript.delete()); String expected = expectedCompletionScriptForNonDefault().replaceAll("picocli\\.AutoComplete", commandName); assertEquals(expected, new String(completion, "UTF8")); } }); System.setProperty("picocli.autocomplete.systemExitOnSuccess", "true"); AutoComplete.main(String.format("--factory=%s", MyFactory.class.getName()), String.format("--name=%s", commandName), NonDefaultCommand.class.getName()); }
@Test public void testAutoCompleteRequiresCommandLineFQCN() { exit.expectSystemExitWithStatus(AutoComplete.EXIT_CODE_INVALID_INPUT); exit.checkAssertionAfterwards(new Assertion() { public void checkAssertion() { String expected = String.format("Missing required parameter: <commandLineFQCN>%n") + AUTO_COMPLETE_APP_USAGE; assertEquals(expected, systemErrRule.getLog()); } }); System.setProperty("picocli.autocomplete.systemExitOnError", "true"); AutoComplete.main(); }
@Test public void testAutoCompleteAppGeneratesScriptNameBasedOnCommandName() throws Exception { final String commandName = "bestCommandEver"; final File completionScript = new File(commandName + "_completion"); if (completionScript.exists()) {assertTrue(completionScript.delete());} completionScript.deleteOnExit(); exit.expectSystemExitWithStatus(AutoComplete.EXIT_CODE_SUCCESS); exit.checkAssertionAfterwards(new Assertion() { public void checkAssertion() throws Exception { byte[] completion = readBytes(completionScript); assertTrue(completionScript.delete()); String expected = expectedCompletionScriptForAutoCompleteApp().replaceAll("picocli\\.AutoComplete", commandName); assertEquals(expected, new String(completion, "UTF8")); } }); System.setProperty("picocli.autocomplete.systemExitOnSuccess", "YES"); AutoComplete.main(String.format("--name=%s", commandName), "picocli.AutoComplete$App"); }
@Test public void testAutoCompleteRequiresCommandLineFQCN_NoSystemExit() { AutoComplete.main(); String expected = String.format("Missing required parameter: <commandLineFQCN>%n") + AUTO_COMPLETE_APP_USAGE; assertEquals(expected, systemErrRule.getLog()); }
@Test public void testAutoCompleteAppCannotInstantiate() { @Command(name = "test") class TestApp { public TestApp(String noDefaultConstructor) { throw new RuntimeException();} } exit.expectSystemExitWithStatus(AutoComplete.EXIT_CODE_EXECUTION_ERROR); exit.checkAssertionAfterwards(new Assertion() { public void checkAssertion() { String actual = systemErrRule.getLog(); assertTrue(actual.startsWith("java.lang.NoSuchMethodException: picocli.AutoCompleteTest$1TestApp.<init>()")); assertTrue(actual.contains(AUTO_COMPLETE_APP_USAGE)); } }); System.setProperty("picocli.autocomplete.systemExitOnSuccess", "false"); System.setProperty("picocli.autocomplete.systemExitOnError", "YES"); AutoComplete.main(TestApp.class.getName()); }
AutoComplete.main("--force", "--writeCommandScript", String.format("-o=%s", completionScript.getAbsolutePath()), "picocli.AutoComplete$App");
@Test public void testAutoCompleteAppBothScriptFilesForceOverwrite_NoSystemExit() throws Exception { File dir = new File(System.getProperty("java.io.tmpdir")); final File commandScript = new File(dir, "picocli.AutoComplete"); if (commandScript.exists()) {assertTrue(commandScript.delete());} commandScript.deleteOnExit(); // create the file FileOutputStream fous1 = new FileOutputStream(commandScript, false); fous1.close(); final File completionScript = new File(dir, commandScript.getName() + "_completion"); if (completionScript.exists()) {assertTrue(completionScript.delete());} completionScript.deleteOnExit(); // create the file FileOutputStream fous2 = new FileOutputStream(completionScript, false); fous2.close(); AutoComplete.main("--force", "--writeCommandScript", String.format("-o=%s", completionScript.getAbsolutePath()), "picocli.AutoComplete$App"); byte[] command = readBytes(commandScript); assertEquals(("" + "#!/usr/bin/env bash\n" + "\n" + "LIBS=path/to/libs\n" + "CP=\"${LIBS}/myApp.jar\"\n" + "java -cp \"${CP}\" 'picocli.AutoComplete$App' $@"), new String(command, "UTF8")); byte[] completion = readBytes(completionScript); String expected = expectedCompletionScriptForAutoCompleteApp(); assertEquals(expected, new String(completion, "UTF8")); }
@Test public void testAutoCompleteAppHelp() { String[][] argsList = new String[][] { {"-h"}, {"--help"}, }; for (final String[] args : argsList) { exit.expectSystemExitWithStatus(AutoComplete.EXIT_CODE_SUCCESS); exit.checkAssertionAfterwards(new Assertion() { public void checkAssertion() { assertEquals(args[0], AUTO_COMPLETE_APP_USAGE, systemOutRule.getLog()); systemOutRule.clearLog(); } }); System.setProperty("picocli.autocomplete.systemExitOnSuccess", "YES"); AutoComplete.main(args); } }
@Test public void testAutoCompleteAppHelp_NoSystemExit() { String[][] argsList = new String[][] { {"-h"}, {"--help"}, }; System.setProperty("picocli.autocomplete.systemExitOnSuccess", "false"); for (final String[] args : argsList) { AutoComplete.main(args); assertEquals(args[0], AUTO_COMPLETE_APP_USAGE, systemOutRule.getLog()); systemOutRule.clearLog(); } }
@Test public void testAutoCompleteAppCannotInstantiate_NoSystemExit() { @Command(name = "test") class TestApp { public TestApp(String noDefaultConstructor) { throw new RuntimeException();} } AutoComplete.main(TestApp.class.getName()); String actual = systemErrRule.getLog(); assertTrue(actual.startsWith("java.lang.NoSuchMethodException: picocli.AutoCompleteTest$2TestApp.<init>()")); assertTrue(actual.contains(AUTO_COMPLETE_APP_USAGE)); }
@Test public void testAutoCompleteRequiresCommandLineFQCN() { exit.expectSystemExitWithStatus(AutoComplete.EXIT_CODE_INVALID_INPUT); exit.checkAssertionAfterwards(new Assertion() { public void checkAssertion() { String expected = String.format("Missing required parameter: <commandLineFQCN>%n") + AUTO_COMPLETE_APP_USAGE; assertEquals(expected, systemErrRule.getLog()); } }); System.setProperty("picocli.autocomplete.systemExitOnError", "true"); AutoComplete.main(); }
@Test public void testAutoCompleteRequiresCommandLineFQCN_NoSystemExit() { AutoComplete.main(); String expected = String.format("Missing required parameter: <commandLineFQCN>%n") + AUTO_COMPLETE_APP_USAGE; assertEquals(expected, systemErrRule.getLog()); }