Search in sources :

Example 11 with CommandRunner

use of com.google.copybara.util.CommandRunner in project copybara by google.

the class GitRepository method gitAllowNonZeroExit.

/**
 * Execute git allowing non-zero exit codes. This will only allow program non-zero exit codes
 * (0-10. The upper bound is arbitrary). And will still fail for exit codes like 127 (Command not
 * found).
 */
private CommandOutputWithStatus gitAllowNonZeroExit(byte[] stdin, Iterable<String> params, Duration defaultTimeout) throws RepoException {
    try {
        List<String> allParams = new ArrayList<>();
        allParams.add(gitEnv.resolveGitBinary());
        allParams.addAll(addGitDirAndWorkTreeParams(params));
        Command cmd = new Command(Iterables.toArray(allParams, String.class), gitEnv.getEnvironment(), getCwd().toFile());
        return new CommandRunner(cmd, defaultTimeout).withVerbose(verbose).withInput(stdin).execute();
    } catch (BadExitStatusWithOutputException e) {
        CommandOutputWithStatus output = e.getOutput();
        int exitCode = e.getOutput().getTerminationStatus().getExitCode();
        if (NON_CRASH_ERROR_EXIT_CODES.contains(exitCode)) {
            return output;
        }
        throw throwUnknownGitError(output, params);
    } catch (CommandException e) {
        throw new RepoException("Error executing 'git': " + e.getMessage(), e);
    }
}
Also used : BadExitStatusWithOutputException(com.google.copybara.util.BadExitStatusWithOutputException) Command(com.google.copybara.shell.Command) CommandOutputWithStatus(com.google.copybara.util.CommandOutputWithStatus) ArrayList(java.util.ArrayList) CommandException(com.google.copybara.shell.CommandException) RepoException(com.google.copybara.exception.RepoException) CommandRunner(com.google.copybara.util.CommandRunner)

Example 12 with CommandRunner

use of com.google.copybara.util.CommandRunner in project copybara by google.

the class PatchingOptions method getPatchVersion.

Version getPatchVersion(String patchBin) throws CommandException, ValidationException {
    String out = new CommandRunner(new Command(new String[] { patchBin, "-v" })).withVerbose(generalOptions.isVerbose()).execute().getStdout().trim();
    Matcher matcher = PATCH_VERSION_FORMAT.matcher(out);
    checkCondition(matcher.matches(), "Unknown version of GNU Patch. Path used: %s. Use %s to use a different path", this.patchBin, PATCH_BIN_FLAG);
    int major = Integer.parseInt(matcher.group("major"));
    int minor = Integer.parseInt(matcher.group("minor"));
    return new Version(major, minor);
}
Also used : Command(com.google.copybara.shell.Command) Matcher(java.util.regex.Matcher) CommandRunner(com.google.copybara.util.CommandRunner)

Example 13 with CommandRunner

use of com.google.copybara.util.CommandRunner in project copybara by google.

the class CommandRunnerTest method testCommandWithMaxLogLines.

@Test
public void testCommandWithMaxLogLines() throws Exception {
    Command command = new Command(new String[] { "echo", "hello\n", "world" });
    CommandOutputWithStatus result = runCommand(new CommandRunner(command).withMaxStdOutLogLines(1));
    assertThat(result.getTerminationStatus().success()).isTrue();
    assertThat(result.getStdout()).isEqualTo("hello\n world\n");
    assertThat(outContent.toByteArray()).isEmpty();
    assertLogContains("Executing [echo 'hello\n' world]", "'echo' STDOUT: hello", "'echo' STDOUT: ... truncated after 1 line(s)");
}
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 14 with CommandRunner

use of com.google.copybara.util.CommandRunner in project copybara by google.

the class CommandRunnerTest method testTimeout.

@Test
public void testTimeout() throws Exception {
    Command command = bashCommand("" + "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.getOutput().getStdout()).contains("stdout msg");
    assertThat(e.getOutput().getStderr()).contains("stderr msg");
    assertThat(e.getMessage()).containsMatch("Command '.*' killed by Copybara after timeout \\(1s\\)");
    assertThat(e.getTimeout()).isEquivalentAccordingToCompareTo(Duration.ofSeconds(1));
}
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 15 with CommandRunner

use of com.google.copybara.util.CommandRunner in project copybara by google.

the class CommandRunnerTest method testCommand.

@Test
public void testCommand() throws Exception {
    Command command = new Command(new String[] { "echo", "hello", "world" });
    CommandOutputWithStatus result = runCommand(new CommandRunner(command));
    assertThat(result.getTerminationStatus().success()).isTrue();
    assertThat(result.getStdout()).isEqualTo("hello world\n");
    assertWithMessage("Not empty: %s", new String(outContent.toByteArray(), UTF_8)).that(outContent.toByteArray()).isEmpty();
    assertWithMessage("Not empty: %s", new String(errContent.toByteArray(), UTF_8)).that(errContent.toByteArray()).isEmpty();
    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)

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