Search in sources :

Example 1 with TeeInputStream

use of com.facebook.buck.io.TeeInputStream in project buck by facebook.

the class XctestRunTestsStep method execute.

@Override
public StepExecutionResult execute(ExecutionContext context) throws InterruptedException {
    ProcessExecutorParams.Builder builder = ProcessExecutorParams.builder().addAllCommand(getCommand()).setDirectory(filesystem.getRootPath().toAbsolutePath()).setRedirectErrorStream(true).setEnvironment(getEnv(context));
    ProcessExecutorParams params = builder.build();
    LOG.debug("xctest command: %s", Joiner.on(" ").join(params.getCommand()));
    try {
        ProcessExecutor executor = context.getProcessExecutor();
        ProcessExecutor.LaunchedProcess launchedProcess = executor.launchProcess(params);
        int exitCode;
        try (OutputStream outputStream = filesystem.newFileOutputStream(outputPath);
            TeeInputStream outputWrapperStream = new TeeInputStream(launchedProcess.getInputStream(), outputStream)) {
            if (outputReadingCallback.isPresent()) {
                // The caller is responsible for reading all the data, which TeeInputStream will
                // copy to outputStream.
                outputReadingCallback.get().readOutput(outputWrapperStream);
            } else {
                // Nobody's going to read from outputWrapperStream, so close it and copy
                // the process's stdout and stderr to outputPath directly.
                outputWrapperStream.close();
                ByteStreams.copy(launchedProcess.getInputStream(), outputStream);
            }
            exitCode = executor.waitForLaunchedProcess(launchedProcess).getExitCode();
            // There's no way to distinguish a test failure from an xctest issue. We don't
            // want to fail the step on a test failure, so return 0 for any xctest exit code.
            exitCode = 0;
            LOG.debug("Finished running command, exit code %d", exitCode);
        } finally {
            context.getProcessExecutor().destroyLaunchedProcess(launchedProcess);
            context.getProcessExecutor().waitForLaunchedProcess(launchedProcess);
        }
        if (exitCode != 0) {
            context.getConsole().printErrorText(String.format(Locale.US, "xctest failed with exit code %d", exitCode));
        }
        return StepExecutionResult.of(exitCode);
    } catch (IOException e) {
        LOG.error(e, "Exception while running %s", params.getCommand());
        MoreThrowables.propagateIfInterrupt(e);
        context.getConsole().printBuildFailureWithStacktrace(e);
        return StepExecutionResult.ERROR;
    }
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) OutputStream(java.io.OutputStream) IOException(java.io.IOException) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) TeeInputStream(com.facebook.buck.io.TeeInputStream)

Aggregations

TeeInputStream (com.facebook.buck.io.TeeInputStream)1 ProcessExecutor (com.facebook.buck.util.ProcessExecutor)1 ProcessExecutorParams (com.facebook.buck.util.ProcessExecutorParams)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1