use of com.facebook.buck.util.ProcessExecutorParams in project buck by facebook.
the class AbstractTestStep method execute.
@Override
public StepExecutionResult execute(ExecutionContext context) throws InterruptedException {
// Build the process, redirecting output to the provided output file. In general,
// it's undesirable that both stdout and stderr are being redirected to the same
// input stream. However, due to the nature of OS pipe buffering, we can't really
// maintain the natural interleaving of multiple output streams in a way that we
// can correctly associate both stdout/stderr streams to the one correct test out
// of the many that ran. So, our best bet is to just combine them all into stdout,
// so they get properly interleaved with the test start and end messages that we
// use when we parse the test output.
Map<String, String> environment = new HashMap<>(System.getenv());
if (env.isPresent()) {
environment.putAll(env.get());
}
ProcessExecutorParams params = ProcessExecutorParams.builder().setCommand(command).setDirectory(workingDirectory.map(filesystem::resolve)).setEnvironment(ImmutableMap.copyOf(environment)).setRedirectOutput(ProcessBuilder.Redirect.to(filesystem.resolve(output).toFile())).setRedirectErrorStream(true).build();
ProcessExecutor.Result result;
try {
// Run the test process, saving the exit code.
ProcessExecutor executor = context.getProcessExecutor();
ImmutableSet<ProcessExecutor.Option> options = ImmutableSet.of(ProcessExecutor.Option.EXPECTING_STD_OUT);
result = executor.launchAndExecute(params, options, /* stdin */
Optional.empty(), testRuleTimeoutMs, /* timeOutHandler */
Optional.empty());
} catch (IOException e) {
context.logError(e, "Error starting command %s", command);
return StepExecutionResult.ERROR;
}
if (result.isTimedOut()) {
throw new HumanReadableException("Timed out after %d ms running test command %s", testRuleTimeoutMs.orElse(-1L), command);
}
// to a file rather than signalling a step failure.
try (FileOutputStream fileOut = new FileOutputStream(filesystem.resolve(exitCode).toFile());
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut)) {
objectOut.writeInt(result.getExitCode());
} catch (IOException e) {
context.logError(e, "Error saving exit code to %s", exitCode);
return StepExecutionResult.ERROR;
}
return StepExecutionResult.SUCCESS;
}
use of com.facebook.buck.util.ProcessExecutorParams in project buck by facebook.
the class ShellStepTest method testStdErrPrintedOnErrorIfNotSilentEvenIfNotShouldPrintStdErr.
@Test
public void testStdErrPrintedOnErrorIfNotSilentEvenIfNotShouldPrintStdErr() throws Exception {
ShellStep command = createCommand(/*shouldPrintStdErr*/
false, /*shouldPrintStdOut*/
false);
ProcessExecutorParams params = createParams();
FakeProcess process = new FakeProcess(EXIT_FAILURE, OUTPUT_MSG, ERROR_MSG);
TestConsole console = new TestConsole(Verbosity.STANDARD_INFORMATION);
ExecutionContext context = createContext(ImmutableMap.of(params, process), console);
command.launchAndInteractWithProcess(context, params);
assertEquals(ERROR_MSG, console.getTextWrittenToStdErr());
}
use of com.facebook.buck.util.ProcessExecutorParams in project buck by facebook.
the class ShellStepTest method testStdErrPrintedOnSuccessIfShouldPrintStdErrEvenIfSilent.
@Test
public void testStdErrPrintedOnSuccessIfShouldPrintStdErrEvenIfSilent() throws Exception {
ShellStep command = createCommand(/*shouldPrintStdErr*/
true, /*shouldPrintStdOut*/
false);
ProcessExecutorParams params = createParams();
FakeProcess process = new FakeProcess(EXIT_SUCCESS, OUTPUT_MSG, ERROR_MSG);
TestConsole console = new TestConsole(Verbosity.SILENT);
ExecutionContext context = createContext(ImmutableMap.of(params, process), console);
command.launchAndInteractWithProcess(context, params);
assertEquals(ERROR_MSG, console.getTextWrittenToStdErr());
}
use of com.facebook.buck.util.ProcessExecutorParams in project buck by facebook.
the class ShellStepTest method testStdinDoesNotGetToProcessWhenAbsent.
@Test
public void testStdinDoesNotGetToProcessWhenAbsent() throws Exception {
final Optional<String> stdin = Optional.empty();
ShellStep command = createCommand(ImmutableMap.of(), ImmutableList.of("cat", "-"), null, /*shouldPrintStdErr*/
true, /*shouldPrintStdOut*/
true, stdin);
ProcessExecutorParams params = createParams();
FakeProcess process = new FakeProcess(EXIT_SUCCESS, OUTPUT_MSG, ERROR_MSG);
TestConsole console = new TestConsole(Verbosity.ALL);
ExecutionContext context = createContext(ImmutableMap.of(params, process), console);
command.launchAndInteractWithProcess(context, params);
assertEquals("", process.getOutput());
}
use of com.facebook.buck.util.ProcessExecutorParams in project buck by facebook.
the class WorkerProcessProtocolZeroTest method setUp.
@Before
public void setUp() throws IOException {
ProcessExecutorParams fakeParams = ProcessExecutorParams.ofCommand("");
fakeProcess = new FakeProcess(0);
fakeProcessExecutor = new FakeProcessExecutor(ImmutableMap.of(fakeParams, fakeProcess));
fakeLaunchedProcess = fakeProcessExecutor.launchProcess(fakeParams);
dummyJsonWriter = new JsonWriter(new StringWriter());
dummyJsonReader = new JsonReader(new StringReader(""));
}
Aggregations