Search in sources :

Example 41 with ProcessExecutorParams

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

the class HgCmdLineInterface method executeCommand.

private String executeCommand(Iterable<String> command) throws VersionControlCommandFailedException, InterruptedException {
    command = replaceTemplateValue(command, HG_CMD_TEMPLATE, hgCmd);
    String commandString = commandAsString(command);
    LOG.debug("Executing command: " + commandString);
    ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().setCommand(command).setDirectory(projectRoot).setEnvironment(environment).build();
    ProcessExecutor.Result result;
    try (PrintStream stdout = new PrintStream(new ByteArrayOutputStream());
        PrintStream stderr = new PrintStream(new ByteArrayOutputStream())) {
        ProcessExecutor processExecutor = processExecutorFactory.createProcessExecutor(stdout, stderr);
        result = processExecutor.launchAndExecute(processExecutorParams);
    } catch (IOException e) {
        throw new VersionControlCommandFailedException(e);
    }
    Optional<String> resultString = result.getStdout();
    if (!resultString.isPresent()) {
        throw new VersionControlCommandFailedException("Received no output from launched process for command: " + commandString);
    }
    if (result.getExitCode() != 0) {
        throw new VersionControlCommandFailedException(result.getMessageForUnexpectedResult(commandString));
    }
    return cleanResultString(resultString.get());
}
Also used : PrintStream(java.io.PrintStream) ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ProcessExecutor(com.facebook.buck.util.ProcessExecutor)

Example 42 with ProcessExecutorParams

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

the class XctoolRunTestsStepTest method xctoolCommandWithTestSelectorMatchingNothingDoesNotFail.

@Test
public void xctoolCommandWithTestSelectorMatchingNothingDoesNotFail() throws Exception {
    FakeProjectFilesystem projectFilesystem = new FakeProjectFilesystem();
    XctoolRunTestsStep step = new XctoolRunTestsStep(projectFilesystem, Paths.get("/path/to/xctool"), ImmutableMap.of(), Optional.empty(), "iphonesimulator", Optional.empty(), ImmutableSet.of(Paths.get("/path/to/FooTest.xctest"), Paths.get("/path/to/BarTest.xctest")), ImmutableMap.of(), Paths.get("/path/to/output.json"), Optional.empty(), Suppliers.ofInstance(Optional.of(Paths.get("/path/to/developer/dir"))), TestSelectorList.builder().addRawSelectors("Blargh#Xyzzy").build(), false, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty());
    ProcessExecutorParams xctoolListOnlyParams = ProcessExecutorParams.builder().setCommand(ImmutableList.of("/path/to/xctool", "-reporter", "json-stream", "-sdk", "iphonesimulator", "run-tests", "-logicTest", "/path/to/FooTest.xctest", "-logicTest", "/path/to/BarTest.xctest", "-listTestsOnly")).setEnvironment(ImmutableMap.of("DEVELOPER_DIR", "/path/to/developer/dir")).setDirectory(projectFilesystem.getRootPath().toAbsolutePath()).setRedirectOutput(ProcessBuilder.Redirect.PIPE).build();
    try (InputStream stdout = getClass().getResourceAsStream("testdata/xctool-output/list-tests-only.json");
        InputStream stderr = new ByteArrayInputStream(new byte[0])) {
        assertThat(stdout, not(nullValue()));
        FakeProcess fakeXctoolListTestsProcess = new FakeProcess(0, ByteStreams.nullOutputStream(), stdout, stderr);
        FakeProcessExecutor processExecutor = new FakeProcessExecutor(ImmutableMap.of(xctoolListOnlyParams, fakeXctoolListTestsProcess));
        ExecutionContext executionContext = TestExecutionContext.newBuilder().setProcessExecutor(processExecutor).setEnvironment(ImmutableMap.of()).build();
        assertThat(step.execute(executionContext).getExitCode(), equalTo(0));
    }
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) ByteArrayInputStream(java.io.ByteArrayInputStream) FakeProcessExecutor(com.facebook.buck.util.FakeProcessExecutor) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FakeProcess(com.facebook.buck.util.FakeProcess) Test(org.junit.Test)

Example 43 with ProcessExecutorParams

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

the class XctoolRunTestsStepTest method xctoolCommandWhichFailsPrintsStderrToConsole.

@Test
public void xctoolCommandWhichFailsPrintsStderrToConsole() throws Exception {
    FakeProjectFilesystem projectFilesystem = new FakeProjectFilesystem();
    XctoolRunTestsStep step = new XctoolRunTestsStep(projectFilesystem, Paths.get("/path/to/xctool"), ImmutableMap.of(), Optional.empty(), "iphonesimulator", Optional.empty(), ImmutableSet.of(Paths.get("/path/to/Foo.xctest")), ImmutableMap.of(), Paths.get("/path/to/output.json"), Optional.empty(), Suppliers.ofInstance(Optional.of(Paths.get("/path/to/developer/dir"))), TestSelectorList.EMPTY, false, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty());
    ProcessExecutorParams xctoolParams = ProcessExecutorParams.builder().setCommand(ImmutableList.of("/path/to/xctool", "-reporter", "json-stream", "-sdk", "iphonesimulator", "run-tests", "-logicTest", "/path/to/Foo.xctest")).setEnvironment(ImmutableMap.of("DEVELOPER_DIR", "/path/to/developer/dir")).setDirectory(projectFilesystem.getRootPath().toAbsolutePath()).setRedirectOutput(ProcessBuilder.Redirect.PIPE).build();
    FakeProcess fakeXctoolFailure = new FakeProcess(42, "", "Something went terribly wrong\n");
    FakeProcessExecutor processExecutor = new FakeProcessExecutor(ImmutableMap.of(xctoolParams, fakeXctoolFailure));
    TestConsole testConsole = new TestConsole();
    ExecutionContext executionContext = TestExecutionContext.newBuilder().setProcessExecutor(processExecutor).setEnvironment(ImmutableMap.of()).setConsole(testConsole).build();
    assertThat(step.execute(executionContext).getExitCode(), equalTo(42));
    assertThat(testConsole.getTextWrittenToStdErr(), equalTo("xctool failed with exit code 42: Something went terribly wrong\n"));
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) FakeProcessExecutor(com.facebook.buck.util.FakeProcessExecutor) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) FakeProcess(com.facebook.buck.util.FakeProcess) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Example 44 with ProcessExecutorParams

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

the class KnownBuildRuleTypesTest method createExecutor.

private ProcessExecutor createExecutor(String javac, String version) {
    Map<ProcessExecutorParams, FakeProcess> processMap = new HashMap<>();
    FakeProcess process = new FakeProcess(0, "", version);
    ProcessExecutorParams params = ProcessExecutorParams.builder().setCommand(ImmutableList.of(javac, "-version")).build();
    processMap.put(params, process);
    addXcodeSelectProcess(processMap, FAKE_XCODE_DEV_PATH);
    processMap.putAll(KnownBuildRuleTypesTestUtil.getPythonProcessMap(KnownBuildRuleTypesTestUtil.getPaths(environment)));
    return new FakeProcessExecutor(processMap);
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) HashMap(java.util.HashMap) FakeProcessExecutor(com.facebook.buck.util.FakeProcessExecutor) FakeProcess(com.facebook.buck.util.FakeProcess)

Example 45 with ProcessExecutorParams

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

the class KnownBuildRuleTypesTestUtil method getPythonProcessMap.

protected static ImmutableMap<ProcessExecutorParams, FakeProcess> getPythonProcessMap(List<String> paths) {
    Set<String> uniquePaths = new HashSet<>(paths);
    ImmutableMap.Builder<ProcessExecutorParams, FakeProcess> processMap = ImmutableMap.builder();
    for (Map.Entry<String, String> python : PYTHONS.entrySet()) {
        for (String path : uniquePaths) {
            for (String extension : new String[] { "", ".exe", ".EXE" }) {
                processMap.put(ProcessExecutorParams.builder().setCommand(ImmutableList.of(path + File.separator + python.getKey() + extension, "-")).build(), new FakeProcess(0, "CPython " + python.getValue().replace('.', ' '), ""));
            }
        }
    }
    return processMap.build();
}
Also used : ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) FakeProcess(com.facebook.buck.util.FakeProcess) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashSet(java.util.HashSet)

Aggregations

ProcessExecutorParams (com.facebook.buck.util.ProcessExecutorParams)72 ProcessExecutor (com.facebook.buck.util.ProcessExecutor)32 FakeProcess (com.facebook.buck.util.FakeProcess)30 Test (org.junit.Test)30 FakeProcessExecutor (com.facebook.buck.util.FakeProcessExecutor)21 ExecutionContext (com.facebook.buck.step.ExecutionContext)19 TestExecutionContext (com.facebook.buck.step.TestExecutionContext)19 IOException (java.io.IOException)18 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)12 TestConsole (com.facebook.buck.testutil.TestConsole)12 Path (java.nio.file.Path)11 DefaultProcessExecutor (com.facebook.buck.util.DefaultProcessExecutor)8 ListeningProcessExecutor (com.facebook.buck.util.ListeningProcessExecutor)6 HumanReadableException (com.facebook.buck.util.HumanReadableException)5 ImmutableList (com.google.common.collect.ImmutableList)5 ImmutableSet (com.google.common.collect.ImmutableSet)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 InputStream (java.io.InputStream)4 InputStreamReader (java.io.InputStreamReader)4 Matcher (java.util.regex.Matcher)4