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);
}
}
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);
}
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)");
}
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));
}
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");
}
Aggregations