Search in sources :

Example 36 with ProcessExecutor

use of com.facebook.buck.util.ProcessExecutor in project buck by facebook.

the class AppleSimulatorDiscovery method discoverAppleSimulators.

/**
   * Discovers information about Apple simulators installed on the system.
   */
public static ImmutableSet<AppleSimulator> discoverAppleSimulators(ProcessExecutor processExecutor, Path simctlPath) throws InterruptedException, IOException {
    LOG.debug("Running xcrun simctl list to get list of simulators");
    ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().setCommand(ImmutableList.of(simctlPath.toString(), "list")).build();
    ProcessExecutor.LaunchedProcess simctlListProcess = processExecutor.launchProcess(processExecutorParams);
    ImmutableSet.Builder<AppleSimulator> simulatorsBuilder = ImmutableSet.builder();
    try (InputStreamReader stdoutReader = new InputStreamReader(simctlListProcess.getInputStream(), StandardCharsets.UTF_8)) {
        LOG.debug("Parsing output of xcrun simctl list...");
        SimctlListOutputParsing.parseOutputFromReader(stdoutReader, simulatorsBuilder);
    }
    ProcessExecutor.Result result = processExecutor.waitForLaunchedProcess(simctlListProcess);
    if (result.getExitCode() != 0) {
        throw new IOException(result.getMessageForUnexpectedResult("simctl list"));
    }
    ImmutableSet<AppleSimulator> simulators = simulatorsBuilder.build();
    LOG.debug("Discovered simulators: %s", simulators);
    return simulators;
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) ImmutableSet(com.google.common.collect.ImmutableSet) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) ProcessExecutor(com.facebook.buck.util.ProcessExecutor)

Example 37 with ProcessExecutor

use of com.facebook.buck.util.ProcessExecutor 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;
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) HashMap(java.util.HashMap) HumanReadableException(com.facebook.buck.util.HumanReadableException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) ObjectOutputStream(java.io.ObjectOutputStream)

Example 38 with ProcessExecutor

use of com.facebook.buck.util.ProcessExecutor in project buck by facebook.

the class ShellStep method launchAndInteractWithProcess.

@VisibleForTesting
int launchAndInteractWithProcess(ExecutionContext context, ProcessExecutorParams params) throws InterruptedException, IOException {
    ImmutableSet.Builder<Option> options = ImmutableSet.builder();
    addOptions(context, options);
    ProcessExecutor executor = context.getProcessExecutor();
    ProcessExecutor.Result result = executor.launchAndExecute(params, options.build(), getStdin(context), getTimeout(), getTimeoutHandler(context));
    stdout = result.getStdout();
    stderr = result.getStderr();
    Verbosity verbosity = context.getVerbosity();
    if (stdout.isPresent() && !stdout.get().isEmpty() && (result.getExitCode() != 0 || shouldPrintStdout(verbosity))) {
        context.postEvent(ConsoleEvent.info("%s", stdout.get()));
    }
    if (stderr.isPresent() && !stderr.get().isEmpty() && (result.getExitCode() != 0 || shouldPrintStderr(verbosity))) {
        context.postEvent(ConsoleEvent.warning("%s", stderr.get()));
    }
    return getExitCodeFromResult(context, result);
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Option(com.facebook.buck.util.ProcessExecutor.Option) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) Verbosity(com.facebook.buck.util.Verbosity) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

ProcessExecutor (com.facebook.buck.util.ProcessExecutor)38 DefaultProcessExecutor (com.facebook.buck.util.DefaultProcessExecutor)20 ProcessExecutorParams (com.facebook.buck.util.ProcessExecutorParams)18 TestConsole (com.facebook.buck.testutil.TestConsole)13 IOException (java.io.IOException)13 Path (java.nio.file.Path)12 Test (org.junit.Test)11 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)8 HumanReadableException (com.facebook.buck.util.HumanReadableException)6 ImmutableMap (com.google.common.collect.ImmutableMap)5 FakeBuckConfig (com.facebook.buck.cli.FakeBuckConfig)4 Verbosity (com.facebook.buck.util.Verbosity)4 ImmutableSet (com.google.common.collect.ImmutableSet)4 FakeAndroidDirectoryResolver (com.facebook.buck.android.FakeAndroidDirectoryResolver)3 BuckConfig (com.facebook.buck.cli.BuckConfig)3 FakeProcessExecutor (com.facebook.buck.util.FakeProcessExecutor)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InputStreamReader (java.io.InputStreamReader)3 PrintStream (java.io.PrintStream)3 NSDate (com.dd.plist.NSDate)2