use of com.google.copybara.util.CommandRunner in project copybara by google.
the class CommandRunnerTest method testCommandWithVerbose.
@Test
public void testCommandWithVerbose() throws Exception {
Command command = new Command(new String[] { "echo", "hello", "world" });
CommandOutputWithStatus result = runCommand(new CommandRunner(command).withVerbose(true));
assertThat(result.getTerminationStatus().success()).isTrue();
assertThat(result.getStdout()).isEqualTo("hello world\n");
assertThat(outContent.toByteArray()).isEmpty();
String stderr = new String(errContent.toByteArray(), UTF_8);
// Using contains() because stderr also gets all the logs
assertThat(stderr).contains("hello world\n");
// Verify that logging is redirected, even with verbose
assertLogContains("Executing [echo hello world]", "'echo' STDOUT: hello world", "Command 'echo' finished");
}
use of com.google.copybara.util.CommandRunner in project copybara by google.
the class CommandRunnerTest method testTimeoutCustomExitCode.
@Test
public void testTimeoutCustomExitCode() throws Exception {
Command command = bashCommand("" + "trap 'exit 42' SIGTERM SIGINT\n" + "echo stdout msg\n" + ">&2 echo stderr msg\n" + "sleep 10\n");
CommandTimeoutException e = assertThrows(CommandTimeoutException.class, () -> runCommand(new CommandRunner(command, Duration.ofSeconds(1))));
assertThat(e.getTimeout()).isEquivalentAccordingToCompareTo(Duration.ofSeconds(1));
assertThat(e.getOutput().getStderr()).contains("stderr msg");
assertThat(e.getMessage()).containsMatch("Command '.*' killed by Copybara after timeout \\(1s\\)");
assertThat(e.getOutput().getStdout()).contains("stdout msg");
}
use of com.google.copybara.util.CommandRunner in project copybara by google.
the class CommandRunnerTest method runCommand.
private CommandOutputWithStatus runCommand(CommandRunner commandRunner) throws CommandException {
Logger logger = Logger.getLogger(CommandRunner.class.getName());
boolean useParentLogger = logger.getUseParentHandlers();
logger.setUseParentHandlers(false);
StreamHandler handler = new StreamHandler() {
@Override
public synchronized void publish(LogRecord record) {
logLines.add(record.getMessage());
}
};
logger.addHandler(handler);
PrintStream outRestore = System.out;
PrintStream errRestore = System.err;
System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
try {
return commandRunner.execute();
} finally {
System.setOut(outRestore);
System.setErr(errRestore);
logger.removeHandler(handler);
logger.setUseParentHandlers(useParentLogger);
}
}
use of com.google.copybara.util.CommandRunner in project copybara by google.
the class CommandRunnerTest method testCommandWithExtraOutput.
@Test
public void testCommandWithExtraOutput() throws Exception {
Command command = bashCommand("" + "echo hello\n" + "echo >&2 world\n");
ByteArrayOutputStream stdoutCollector = new ByteArrayOutputStream();
ByteArrayOutputStream stderrCollector = new ByteArrayOutputStream();
runCommand(new CommandRunner(command).withStdErrStream(stderrCollector).withStdOutStream(stdoutCollector));
String stderr = new String(stderrCollector.toByteArray(), UTF_8);
String stdout = new String(stdoutCollector.toByteArray(), UTF_8);
assertThat(stderr).contains("world");
assertThat(stdout).contains("hello");
}
use of com.google.copybara.util.CommandRunner in project copybara by google.
the class CommandRunnerTest method testCommandWithVerboseLargeOutput.
@Test
public void testCommandWithVerboseLargeOutput() throws Exception {
Path largeFile = Files.createTempDirectory("test").resolve("large_file.txt");
String singleLine = Strings.repeat("A", 100);
try (BufferedWriter writer = Files.newBufferedWriter(largeFile)) {
for (int i = 0; i < LINES_SIZE; i++) {
writer.write(singleLine + "\n");
}
}
Command command = new Command(new String[] { "cat", largeFile.toAbsolutePath().toString() });
CommandOutputWithStatus result = runCommand(new CommandRunner(command).withVerbose(true));
assertThat(result.getTerminationStatus().success()).isTrue();
for (int i = 1; i < LINES_SIZE - 1; i++) {
assertThat(logLines.get(i)).endsWith(singleLine);
}
}
Aggregations