@Test public void executeRunsInTheSpecifiedDirectory() throws IOException, InterruptedException { boolean isWindows = System.getProperty("os.name").toLowerCase().startsWith("windows"); Path path = Paths.get("src", "main", "tf", "root"); FileUtils.forceMkdir(path.toFile()); Executable commandLine = new CommandLine(path, false, LoggerFactory.getLogger(CommandLine.class)); if (isWindows) { if (!System.getProperties().containsKey("shellPath") && System.getenv("SHELL_PATH") == null) { String output = commandLine.execute("cd"); Assert.assertEquals(String.format("%1$s%n", path.toAbsolutePath().toString()), output); return; } } String output = commandLine.execute("pwd"); Assert.assertEquals(String.format("%1$s%n", path.toAbsolutePath().toString().replace("C:", "/c").replace('\\', '/')), String.format("%1$s%n", output.trim())); FileUtils.forceDelete(path.getParent().toFile()); }
@Override public String execute(String command, int timeout) throws IOException, InterruptedException { logger.info("*** directory: {} ***", getDirectory().toAbsolutePath()); logger.info("*** command: {} ***", command); ProcessExecutor processExecutor = new ProcessExecutor(); List<String> cmd = new ArrayList<>(getShell()); cmd.add(command); Optional<String> output; String [] baseCommand = command.split(" "); String baseCommandName = Paths.get(baseCommand[0]).getFileName().toString(); String loggerName = baseCommand.length >= 2 ? baseCommandName + "-" + baseCommand[1] : baseCommandName; try { output = Optional.ofNullable(processExecutor.command(cmd) .directory(directory.toAbsolutePath().toFile()) .redirectOutput(Slf4jStream.of(LoggerFactory.getLogger(loggerName)).asInfo()) .timeout(timeout, TimeUnit.MILLISECONDS) .readOutput(true) .exitValueNormal() .destroyOnExit() .execute() .outputString()); } catch (TimeoutException e) { logger.error("Processed timed out", e); throw new InterruptedException(e.getMessage()); } return output.orElse(""); }
CommandLine implements Executable { @Override public String execute(String command, int timeout) throws IOException, InterruptedException { logger.info("*** directory: {} ***", getDirectory().toAbsolutePath()); logger.info("*** command: {} ***", command); ProcessExecutor processExecutor = new ProcessExecutor(); List<String> cmd = new ArrayList<>(getShell()); cmd.add(command); Optional<String> output; String [] baseCommand = command.split(" "); String baseCommandName = Paths.get(baseCommand[0]).getFileName().toString(); String loggerName = baseCommand.length >= 2 ? baseCommandName + "-" + baseCommand[1] : baseCommandName; try { output = Optional.ofNullable(processExecutor.command(cmd) .directory(directory.toAbsolutePath().toFile()) .redirectOutput(Slf4jStream.of(LoggerFactory.getLogger(loggerName)).asInfo()) .timeout(timeout, TimeUnit.MILLISECONDS) .readOutput(true) .exitValueNormal() .destroyOnExit() .execute() .outputString()); } catch (TimeoutException e) { logger.error("Processed timed out", e); throw new InterruptedException(e.getMessage()); } return output.orElse(""); } }
CommandLine implements Executable { @Override public String execute(String command, int timeout) throws IOException, InterruptedException { logger.info("*** directory: {} ***", getDirectory().toAbsolutePath()); logger.info("*** command: {} ***", command); ProcessExecutor processExecutor = new ProcessExecutor(); List<String> cmd = new ArrayList<>(getShell()); cmd.add(command); Optional<String> output; String [] baseCommand = command.split(" "); String baseCommandName = Paths.get(baseCommand[0]).getFileName().toString(); String loggerName = baseCommand.length >= 2 ? baseCommandName + "-" + baseCommand[1] : baseCommandName; try { output = Optional.ofNullable(processExecutor.command(cmd) .directory(directory.toAbsolutePath().toFile()) .redirectOutput(Slf4jStream.of(LoggerFactory.getLogger(loggerName)).asInfo()) .timeout(timeout, TimeUnit.MILLISECONDS) .readOutput(true) .exitValueNormal() .destroyOnExit() .execute() .outputString()); } catch (TimeoutException e) { logger.error("Processed timed out", e); throw new InterruptedException(e.getMessage()); } return output.orElse(""); } CommandLine(Path directory);  CommandLine(Path directory, Logger logger);  CommandLine(Path directory, boolean inheritIO, Logger logger); }
CommandLine implements Executable { @Override public String execute(String command, int timeout) throws IOException, InterruptedException { logger.info("*** directory: {} ***", getDirectory().toAbsolutePath()); logger.info("*** command: {} ***", command); ProcessExecutor processExecutor = new ProcessExecutor(); List<String> cmd = new ArrayList<>(getShell()); cmd.add(command); Optional<String> output; String [] baseCommand = command.split(" "); String baseCommandName = Paths.get(baseCommand[0]).getFileName().toString(); String loggerName = baseCommand.length >= 2 ? baseCommandName + "-" + baseCommand[1] : baseCommandName; try { output = Optional.ofNullable(processExecutor.command(cmd) .directory(directory.toAbsolutePath().toFile()) .redirectOutput(Slf4jStream.of(LoggerFactory.getLogger(loggerName)).asInfo()) .timeout(timeout, TimeUnit.MILLISECONDS) .readOutput(true) .exitValueNormal() .destroyOnExit() .execute() .outputString()); } catch (TimeoutException e) { logger.error("Processed timed out", e); throw new InterruptedException(e.getMessage()); } return output.orElse(""); } CommandLine(Path directory);  CommandLine(Path directory, Logger logger);  CommandLine(Path directory, boolean inheritIO, Logger logger); @Override String execute(String command, int timeout); @Override String execute(String command); @Override void setLogger(Logger logger); Path getDirectory(); }
CommandLine implements Executable { @Override public String execute(String command, int timeout) throws IOException, InterruptedException { logger.info("*** directory: {} ***", getDirectory().toAbsolutePath()); logger.info("*** command: {} ***", command); ProcessExecutor processExecutor = new ProcessExecutor(); List<String> cmd = new ArrayList<>(getShell()); cmd.add(command); Optional<String> output; String [] baseCommand = command.split(" "); String baseCommandName = Paths.get(baseCommand[0]).getFileName().toString(); String loggerName = baseCommand.length >= 2 ? baseCommandName + "-" + baseCommand[1] : baseCommandName; try { output = Optional.ofNullable(processExecutor.command(cmd) .directory(directory.toAbsolutePath().toFile()) .redirectOutput(Slf4jStream.of(LoggerFactory.getLogger(loggerName)).asInfo()) .timeout(timeout, TimeUnit.MILLISECONDS) .readOutput(true) .exitValueNormal() .destroyOnExit() .execute() .outputString()); } catch (TimeoutException e) { logger.error("Processed timed out", e); throw new InterruptedException(e.getMessage()); } return output.orElse(""); } CommandLine(Path directory);  CommandLine(Path directory, Logger logger);  CommandLine(Path directory, boolean inheritIO, Logger logger); @Override String execute(String command, int timeout); @Override String execute(String command); @Override void setLogger(Logger logger); Path getDirectory();  }