@Test public void output() throws BadExitCodeException, CommandTimeoutException, BuildException, IOException { if (!SrcdepsCoreUtils.isWindows()) { final LineConsumer out = LineConsumer.string(); final ShellCommand cmd = ShellCommand.builder() .id("myCommand") .executable("/bin/bash") .arguments("-c", "echo stdout-msg; >&2 echo stderr-msg") .output(() -> out) .workingDirectory(Paths.get(".")) .build(); Shell.execute(cmd).assertSuccess(); final String actual = out.toString(); Assert.assertTrue("stdout-msg\nstderr-msg\n".equals(actual) || "stderr-msg\nstdout-msg\n".equals(actual)); } }
public static CommandResult execute(ShellCommand command) throws BuildException, CommandTimeoutException { final String[] cmdArray = command.asCmdArray(); final String cmdArrayString = Arrays.stream(cmdArray).collect(Collectors.joining(" ")); final Map<String, String> env = command.getEnvironment(); log.info("srcdeps[{}]: Executing command [{}] using env {}", command.getId(), cmdArrayString, env); ProcessBuilder builder = new ProcessBuilder(cmdArray) .directory(command.getWorkingDirectory().toFile()) .redirectErrorStream(true); if (!env.isEmpty()) { builder.environment().putAll(env); } try (LineConsumer out = command.getOutput().get(); CommandProcess process = new CommandProcess(command.getId(), builder.start(), out)) { return process.waitFor(command.getTimeoutMs(), TimeUnit.MILLISECONDS, cmdArray).assertSuccess(); } catch (IOException | InterruptedException e) { throw new BuildException(String.format("Could not start command [%s]", cmdArrayString), e); } }
Shell { public static CommandResult execute(ShellCommand command) throws BuildException, CommandTimeoutException { final String[] cmdArray = command.asCmdArray(); final String cmdArrayString = Arrays.stream(cmdArray).collect(Collectors.joining(" ")); final Map<String, String> env = command.getEnvironment(); log.info("srcdeps[{}]: Executing command [{}] using env {}", command.getId(), cmdArrayString, env); ProcessBuilder builder = new ProcessBuilder(cmdArray) .directory(command.getWorkingDirectory().toFile()) .redirectErrorStream(true); if (!env.isEmpty()) { builder.environment().putAll(env); } try (LineConsumer out = command.getOutput().get(); CommandProcess process = new CommandProcess(command.getId(), builder.start(), out)) { return process.waitFor(command.getTimeoutMs(), TimeUnit.MILLISECONDS, cmdArray).assertSuccess(); } catch (IOException | InterruptedException e) { throw new BuildException(String.format("Could not start command [%s]", cmdArrayString), e); } } }
Shell { public static CommandResult execute(ShellCommand command) throws BuildException, CommandTimeoutException { final String[] cmdArray = command.asCmdArray(); final String cmdArrayString = Arrays.stream(cmdArray).collect(Collectors.joining(" ")); final Map<String, String> env = command.getEnvironment(); log.info("srcdeps[{}]: Executing command [{}] using env {}", command.getId(), cmdArrayString, env); ProcessBuilder builder = new ProcessBuilder(cmdArray) .directory(command.getWorkingDirectory().toFile()) .redirectErrorStream(true); if (!env.isEmpty()) { builder.environment().putAll(env); } try (LineConsumer out = command.getOutput().get(); CommandProcess process = new CommandProcess(command.getId(), builder.start(), out)) { return process.waitFor(command.getTimeoutMs(), TimeUnit.MILLISECONDS, cmdArray).assertSuccess(); } catch (IOException | InterruptedException e) { throw new BuildException(String.format("Could not start command [%s]", cmdArrayString), e); } } private  Shell(); }
Shell { public static CommandResult execute(ShellCommand command) throws BuildException, CommandTimeoutException { final String[] cmdArray = command.asCmdArray(); final String cmdArrayString = Arrays.stream(cmdArray).collect(Collectors.joining(" ")); final Map<String, String> env = command.getEnvironment(); log.info("srcdeps[{}]: Executing command [{}] using env {}", command.getId(), cmdArrayString, env); ProcessBuilder builder = new ProcessBuilder(cmdArray) .directory(command.getWorkingDirectory().toFile()) .redirectErrorStream(true); if (!env.isEmpty()) { builder.environment().putAll(env); } try (LineConsumer out = command.getOutput().get(); CommandProcess process = new CommandProcess(command.getId(), builder.start(), out)) { return process.waitFor(command.getTimeoutMs(), TimeUnit.MILLISECONDS, cmdArray).assertSuccess(); } catch (IOException | InterruptedException e) { throw new BuildException(String.format("Could not start command [%s]", cmdArrayString), e); } } private  Shell(); static CommandResult execute(ShellCommand command); }
Shell { public static CommandResult execute(ShellCommand command) throws BuildException, CommandTimeoutException { final String[] cmdArray = command.asCmdArray(); final String cmdArrayString = Arrays.stream(cmdArray).collect(Collectors.joining(" ")); final Map<String, String> env = command.getEnvironment(); log.info("srcdeps[{}]: Executing command [{}] using env {}", command.getId(), cmdArrayString, env); ProcessBuilder builder = new ProcessBuilder(cmdArray) .directory(command.getWorkingDirectory().toFile()) .redirectErrorStream(true); if (!env.isEmpty()) { builder.environment().putAll(env); } try (LineConsumer out = command.getOutput().get(); CommandProcess process = new CommandProcess(command.getId(), builder.start(), out)) { return process.waitFor(command.getTimeoutMs(), TimeUnit.MILLISECONDS, cmdArray).assertSuccess(); } catch (IOException | InterruptedException e) { throw new BuildException(String.format("Could not start command [%s]", cmdArrayString), e); } } private  Shell(); static CommandResult execute(ShellCommand command);  }