@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 should_use_cmd_for_new_shell_on_windows() { System2 system = mock(System2.class); when(system.isOsWindows()).thenReturn(true); Command command = new Command("foo.bat", system); command.setNewShell(true); assertThat(command.toCommandLine()).isEqualTo("cmd /C call foo.bat"); assertThat(command.isNewShell()).isTrue(); }
StreamGobbler errorGobbler = null; try { ProcessBuilder builder = new ProcessBuilder(command.toStrings(false)); if (command.getDirectory() != null) { builder.directory(command.getDirectory()); builder.environment().putAll(command.getEnvironmentVariables()); process = builder.start();
@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 public void toString_is_the_command_line() { Command command = Command.create("java"); command.addArgument("-Xmx512m"); assertThat(command.toString()).isEqualTo(command.toCommandLine()); }
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."); }
@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 should_fail_if_script_not_found() { thrown.expect(CommandException.class); CommandExecutor.create().execute(Command.create("notfound").setDirectory(workDir), 1000L); }
private Command getBaseCommand(TsLintExecutorConfig config, String tempPath) { Command command = Command .create(config.getPathToNode()) .addArgument(this.preparePath(config.getPathToTsLint())) .addArgument("--format") .addArgument("json"); .addArgument("--rules-dir") .addArgument(this.preparePath(rulesDir)); .addArgument("--out") .addArgument(this.preparePath(tempPath)); .addArgument("--config") .addArgument(this.preparePath(config.getConfigFile())); .addArgument("--project") .addArgument(this.preparePath(config.getPathToTsConfig())); .addArgument("--type-check"); command.setNewShell(false);
@Test public void working_directory() { Command command = Command.create("java"); assertThat(command.getDirectory()).isNull(); File working = new File("working"); command = Command.create("java").setDirectory(working); assertThat(command.getDirectory()).isEqualTo(working); }
@Test public void initialize_with_current_env() { Command command = Command.create("java"); assertThat(command.getEnvironmentVariables()).isNotEmpty(); }
@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 public void shouldnt_use_new_shell_by_default() { Command command = Command.create("foo.sh"); assertThat(command.isNewShell()).isFalse(); } }
@Test public void override_env_variables() { System2 system = mock(System2.class); when(system.envVariables()).thenReturn(ImmutableMap.of("JAVA_HOME", "/default/path/to/java")); Command command = new Command("java", system); command.setEnvironmentVariable("JAVA_HOME", "/new/path/to/java"); assertThat(command.getEnvironmentVariables().get("JAVA_HOME")).isEqualTo("/new/path/to/java"); }
@Test public void fail_if_null_executable() { thrown.expect(IllegalArgumentException.class); Command.create(null); }
int baseCommandLength = baseCommand.toCommandLine().length(); int availableForBatching = MAX_COMMAND_LENGTH - baseCommandLength; thisCommand.addArgument(thisBatch.get(fileIndex)); LOG.debug("Executing TsLint with command: {}", thisCommand.toCommandLine());
@Override public String toString() { return Joiner.on(" ").join(toStrings(true)); } }
/** * Create a command line without any arguments * * @param executable */ public static Command create(String executable) { return new Command(executable, System2.INSTANCE); }
PylintIssuesAnalyzer(String pylintPath, String pylintConfigPath) { this(pylintPath, pylintConfigPath, new PylintArguments(Command.create(pylintPathWithDefault(pylintPath)).addArgument("--version"))); }
@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"); } }