Search in sources :

Example 6 with CommandRunner

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");
}
Also used : Command(com.google.copybara.shell.Command) CommandOutputWithStatus(com.google.copybara.util.CommandOutputWithStatus) CommandRunner(com.google.copybara.util.CommandRunner) Test(org.junit.Test)

Example 7 with CommandRunner

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");
}
Also used : CommandTimeoutException(com.google.copybara.util.CommandTimeoutException) Command(com.google.copybara.shell.Command) CommandRunner(com.google.copybara.util.CommandRunner) Test(org.junit.Test)

Example 8 with CommandRunner

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);
    }
}
Also used : PrintStream(java.io.PrintStream) LogRecord(java.util.logging.LogRecord) StreamHandler(java.util.logging.StreamHandler) Logger(java.util.logging.Logger) CommandRunner(com.google.copybara.util.CommandRunner)

Example 9 with CommandRunner

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");
}
Also used : Command(com.google.copybara.shell.Command) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CommandRunner(com.google.copybara.util.CommandRunner) Test(org.junit.Test)

Example 10 with CommandRunner

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);
    }
}
Also used : Path(java.nio.file.Path) Command(com.google.copybara.shell.Command) CommandOutputWithStatus(com.google.copybara.util.CommandOutputWithStatus) CommandRunner(com.google.copybara.util.CommandRunner) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Aggregations

CommandRunner (com.google.copybara.util.CommandRunner)18 Command (com.google.copybara.shell.Command)17 Test (org.junit.Test)10 CommandOutputWithStatus (com.google.copybara.util.CommandOutputWithStatus)7 ArrayList (java.util.ArrayList)5 CommandException (com.google.copybara.shell.CommandException)4 BadExitStatusWithOutputException (com.google.copybara.util.BadExitStatusWithOutputException)3 RepoException (com.google.copybara.exception.RepoException)2 KillableObserver (com.google.copybara.shell.KillableObserver)2 CommandTimeoutException (com.google.copybara.util.CommandTimeoutException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ImmutableList (com.google.common.collect.ImmutableList)1 GitEnvironment (com.google.copybara.git.GitEnvironment)1 AbnormalTerminationException (com.google.copybara.shell.AbnormalTerminationException)1 Killable (com.google.copybara.shell.Killable)1 TerminationStatus (com.google.copybara.shell.TerminationStatus)1 CommandExecutor (com.google.copybara.util.CommandRunner.CommandExecutor)1 BufferedWriter (java.io.BufferedWriter)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1