@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(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 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); }
protected CommandExecutor createExecutor() { return CommandExecutor.create(); } }
/** * Executes the given NDeps command. * * @param nDepsCommandBuilder * the NDepsCommandBuilder * @param timeoutMinutes * the timeout for the command * @throws NDepsException * if NDeps fails to execute */ public void execute(NDepsCommandBuilder nDepsCommandBuilder, int timeoutMinutes) throws NDepsException { LOG.debug("Executing NDeps program..."); int exitCode = CommandExecutor.create().execute(nDepsCommandBuilder.toCommand(), timeoutMinutes * MINUTES_TO_MILLISECONDS); if (exitCode != 0) { throw NDepsException.createFromCode(exitCode); } }
/** * Executes the given ReSharper command. * * @param resharperCommandBuilder * the resharperCommandBuilder * @param timeoutMinutes * the timeout for the command * @throws ReSharperException * if ReSharper fails to execute */ public void execute(ReSharperCommandBuilder resharperCommandBuilder, int timeoutMinutes) throws ReSharperException { LOG.debug("Executing ReSharper program..."); int exitCode = CommandExecutor.create().execute(resharperCommandBuilder.toCommand(), timeoutMinutes * MINUTES_TO_MILLISECONDS); if (exitCode != 0 ) { throw new ReSharperException("ReSharper execution failed with return code '" + exitCode + "'. Check ReSharper documentation for more information."); } }
/** * Executes the given FxCop command. * * @param fxCopCommandBuilder * the fxCopCommandBuilder * @param timeoutMinutes * the timeout for the command * @throws FxCopException * if FxCop fails to execute */ public void execute(FxCopCommandBuilder fxCopCommandBuilder, int timeoutMinutes) throws FxCopException { LOG.debug("Executing FxCop program..."); int exitCode = CommandExecutor.create().execute(fxCopCommandBuilder.toCommand(), timeoutMinutes * MINUTES_TO_MILLISECONDS); if (exitCode != 0 && exitCode != 512) { throw new FxCopException("FxCop execution failed with return code '" + exitCode + "'. Check FxCop documentation for more information."); } }
/** * Executes the given Gendarme command. * * @param gendarmeCommandBuilder * the gendarmeCommandBuilder * @param timeoutMinutes * the timeout for the command * @throws GendarmeException * if Gendarme fails to execute */ public void execute(GendarmeCommandBuilder gendarmeCommandBuilder, int timeoutMinutes) throws GendarmeException { LOG.debug("Executing Gendarme program..."); try { int exitCode = CommandExecutor.create().execute(gendarmeCommandBuilder.toCommand(), timeoutMinutes * MINUTES_TO_MILLISECONDS); // Gendarme returns 1 when the analysis is successful but contains violations, so 1 is valid if (exitCode != 0 && exitCode != 1) { throw new GendarmeException(exitCode); } } finally { cleanupFiles(gendarmeCommandBuilder.getBuildConfiguration(), gendarmeCommandBuilder.getBuildPlatform()); } }
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."); }