@Test public void shouldnt_use_new_shell_by_default() { Command command = Command.create("foo.sh"); assertThat(command.isNewShell()).isFalse(); } }
@Test public void fail_if_null_executable() { thrown.expect(IllegalArgumentException.class); Command.create(null); }
@Test public void should_use_working_directory_to_store_argument_and_environment_variable() throws Exception { Command command = Command.create(getScript("echo")) .setDirectory(workDir) .addArgument("1") .setEnvironmentVariable("ENVVAR", "2"); int exitCode = CommandExecutor.create().execute(command, 1000L); assertThat(exitCode).isEqualTo(0); File logFile = new File(workDir, "echo.log"); assertThat(logFile).exists(); String log = FileUtils.readFileToString(logFile); assertThat(log).contains(workDir.getAbsolutePath()); assertThat(log).contains("Parameter: 1"); assertThat(log).contains("Environment variable: 2"); }
@Test public void fail_if_blank_executable() { thrown.expect(IllegalArgumentException.class); Command.create(" "); }
@Test public void create_command() { Command command = Command.create("java"); command.addArgument("-Xmx512m"); command.addArguments(Arrays.asList("-a", "-b")); command.addArguments(new String[] {"-x", "-y"}); assertThat(command.getExecutable()).isEqualTo("java"); assertThat(command.getArguments()).hasSize(5); assertThat(command.toCommandLine()).isEqualTo("java -Xmx512m -a -b -x -y"); }
@Test(timeout = 3000L) public void should_consume_StdOut_and_StdErr() throws Exception { // too many false-positives on MS windows if (!SystemUtils.IS_OS_WINDOWS) { final StringBuilder stdOutBuilder = new StringBuilder(); StreamConsumer stdOutConsumer = new StreamConsumer() { public void consumeLine(String line) { stdOutBuilder.append(line).append(SystemUtils.LINE_SEPARATOR); } }; final StringBuilder stdErrBuilder = new StringBuilder(); StreamConsumer stdErrConsumer = new StreamConsumer() { public void consumeLine(String line) { stdErrBuilder.append(line).append(SystemUtils.LINE_SEPARATOR); } }; Command command = Command.create(getScript("output")).setDirectory(workDir); int exitCode = CommandExecutor.create().execute(command, stdOutConsumer, stdErrConsumer, 1000L); assertThat(exitCode).isEqualTo(0); String stdOut = stdOutBuilder.toString(); String stdErr = stdErrBuilder.toString(); assertThat(stdOut).contains("stdOut: first line"); assertThat(stdOut).contains("stdOut: second line"); assertThat(stdErr).contains("stdErr: first line"); assertThat(stdErr).contains("stdErr: second line"); } }
@Test public void create_command_with_masked_arguments() { Command command = Command.create("java"); command.addArgument("-Xmx512m"); command.addMaskedArgument("s3cr3t"); command.addArguments(new String[] {"-x", "-y"}); assertThat(command.getExecutable()).isEqualTo("java"); assertThat(command.getArguments()).hasSize(4); assertThat(command.toCommandLine()).isEqualTo("java -Xmx512m s3cr3t -x -y"); assertThat(command.toString()).isEqualTo("java -Xmx512m ******** -x -y"); }
@Test public void initialize_with_current_env() { Command command = Command.create("java"); assertThat(command.getEnvironmentVariables()).isNotEmpty(); }
@Test public void toString_is_the_command_line() { Command command = Command.create("java"); command.addArgument("-Xmx512m"); assertThat(command.toString()).isEqualTo(command.toCommandLine()); }
@Test public void should_fail_if_script_not_found() { thrown.expect(CommandException.class); CommandExecutor.create().execute(Command.create("notfound").setDirectory(workDir), 1000L); }
@Test(timeout = 6000L) public void should_stop_after_timeout() throws IOException { try { String executable = getScript("forever"); CommandExecutor.create().execute(Command.create(executable).setDirectory(workDir), 1000L); fail(); } catch (TimeoutException e) { // ok } }
@Test(timeout = 3000L) public void stdOut_consumer_can_throw_exception() throws Exception { Command command = Command.create(getScript("output")).setDirectory(workDir); thrown.expect(CommandException.class); thrown.expectMessage("Error inside stdOut stream"); CommandExecutor.create().execute(command, BAD_CONSUMER, NOP_CONSUMER, 1000L); }
@Test(timeout = 6000L) public void should_stop_after_timeout_and_new_shell() throws IOException { try { String executable = getScript("forever"); CommandExecutor.create().execute(Command.create(executable).setNewShell(true).setDirectory(workDir), 1000L); fail(); } catch (TimeoutException e) { // ok } }
@Test(timeout = 3000L) public void stdErr_consumer_can_throw_exception() throws Exception { Command command = Command.create(getScript("output")).setDirectory(workDir); thrown.expect(CommandException.class); thrown.expectMessage("Error inside stdErr stream"); CommandExecutor.create().execute(command, NOP_CONSUMER, BAD_CONSUMER, 1500L); }
PylintIssuesAnalyzer(String pylintPath, String pylintConfigPath) { this(pylintPath, pylintConfigPath, new PylintArguments(Command.create(pylintPathWithDefault(pylintPath)).addArgument("--version"))); }
PylintIssuesAnalyzer(String pylintPath, String pylintConfigPath) { this(pylintPath, pylintConfigPath, new PylintArguments(Command.create(pylintPathWithDefault(pylintPath)).addArgument("--version"))); }
Command command = Command.create(executable.getAbsolutePath());
public void execute(String executable, String assemblies, File rulesetFile, File reportFile, int timeout, boolean aspnet, List<String> directories, List<String> references) { Command command = Command.create(getExecutable(executable)) .addArgument("/file:" + assemblies) .addArgument("/ruleset:=" + rulesetFile.getAbsolutePath()) .addArgument("/out:" + reportFile.getAbsolutePath()) .addArgument("/outxsl:none") .addArgument("/forceoutput") .addArgument("/searchgac"); if (aspnet) { command.addArgument("/aspnet"); } for (String directory : directories) { command.addArgument("/directory:" + directory); } for (String reference : references) { command.addArgument("/reference:" + reference); } int exitCode = CommandExecutor.create().execute( command, TimeUnit.MINUTES.toMillis(timeout)); LOG.info("FxCopCmd.exe ended with the exit code: " + exitCode); Preconditions.checkState((exitCode & 1) == 0, "The execution of \"" + executable + "\" failed and returned " + exitCode + " as exit code. See http://msdn.microsoft.com/en-us/library/bb429400(v=vs.80).aspx for details."); }
private Command getBaseCommand(TsLintExecutorConfig config, String tempPath) { Command command = Command .create(config.getPathToNode()) .addArgument(this.preparePath(config.getPathToTsLint())) .addArgument("--format")