Search in sources :

Example 1 with CapturingPrintStream

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

the class BuildCommandTest method testGenerateBuildReportForConsole.

@Test
public void testGenerateBuildReportForConsole() {
    String expectedReport = "OK   //fake:rule1 " + "BUILT_LOCALLY " + MorePaths.pathWithPlatformSeparators("buck-out/gen/fake/rule1.txt") + "\n" + "FAIL //fake:rule2\n" + "OK   //fake:rule3 FETCHED_FROM_CACHE\n" + "FAIL //fake:rule4\n";
    String observedReport = new BuildReport(buildExecutionResult, resolver).generateForConsole(new Console(Verbosity.STANDARD_INFORMATION, new CapturingPrintStream(), new CapturingPrintStream(), Ansi.forceTty()));
    assertEquals(expectedReport, observedReport);
}
Also used : BuildReport(com.facebook.buck.command.BuildReport) CapturingPrintStream(com.facebook.buck.util.CapturingPrintStream) Console(com.facebook.buck.util.Console) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Example 2 with CapturingPrintStream

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

the class DaemonIntegrationTest method whenClientDisconnectionDetectedThenTestIsInterrupted.

/**
   * This verifies that a client disconnection will be detected by a Nailgun
   * NGInputStream reading from an empty heartbeat stream and that the generated
   * InterruptedException will interrupt command execution causing it to fail.
   */
@Test
public void whenClientDisconnectionDetectedThenTestIsInterrupted() throws InterruptedException, IOException {
    // Sub process interruption not supported on Windows.
    assumeTrue(Platform.detect() != Platform.WINDOWS);
    // Stream timeout > test timeout.
    final long timeoutMillis = 2000;
    // Disconnect before test timeout.
    final long disconnectMillis = 100;
    final ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "exclusive_execution", tmp);
    workspace.setUp();
    // Start with an input stream that sends heartbeats at a regular rate.
    final DelegatingInputStream inputStream = new DelegatingInputStream(TestContext.createHeartBeatStream(timeoutMillis / 10));
    // Build an NGContext connected to an NGInputStream reading from stream that will timeout.
    try (TestContext context = new TestContext(ImmutableMap.copyOf(System.getenv()), inputStream, timeoutMillis)) {
        ProcessResult result = workspace.runBuckdCommand(context, new CapturingPrintStream() {

            @Override
            public void println(String x) {
                if (x.contains("TESTING //:test")) {
                    // When tests start running, make the heartbeat stream simulate a disconnection.
                    inputStream.setDelegate(TestContext.createDisconnectionStream(disconnectMillis));
                }
                super.println(x);
            }
        }, "test", "//:test");
        result.assertFailure();
        assertThat(result.getStderr(), containsString("InterruptedException"));
    }
}
Also used : ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) CapturingPrintStream(com.facebook.buck.util.CapturingPrintStream) DelegatingInputStream(com.facebook.buck.testutil.integration.DelegatingInputStream) TestContext(com.facebook.buck.testutil.integration.TestContext) ProcessResult(com.facebook.buck.testutil.integration.ProjectWorkspace.ProcessResult) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 3 with CapturingPrintStream

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

the class StepFailedException method createForFailingStepWithException.

static StepFailedException createForFailingStepWithException(Step step, Throwable throwable, Optional<BuildTarget> buildTarget) {
    CapturingPrintStream printStream = new CapturingPrintStream();
    throwable.printStackTrace(printStream);
    String stackTrace = printStream.getContentsAsString(Charsets.UTF_8);
    String message;
    if (buildTarget.isPresent()) {
        message = String.format("%s failed on step %s with an exception:\n%s\n%s", buildTarget.get().getFullyQualifiedName(), step.getShortName(), throwable.getMessage(), stackTrace);
    } else {
        message = String.format("Failed on step %s with an exception:\n%s\n%s", step.getShortName(), throwable.getMessage(), stackTrace);
    }
    return new StepFailedException(message, step, 1);
}
Also used : CapturingPrintStream(com.facebook.buck.util.CapturingPrintStream)

Example 4 with CapturingPrintStream

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

the class JavacStep method tryBuildWithFirstOrderDeps.

private StepExecutionResult tryBuildWithFirstOrderDeps(ExecutionContext context, ProjectFilesystem filesystem) throws InterruptedException, IOException {
    try {
        javacOptions.validateOptions(classpathChecker::validateClasspath);
    } catch (IOException e) {
        context.postEvent(ConsoleEvent.severe("Invalid Java compiler options: %s", e.getMessage()));
        return StepExecutionResult.ERROR;
    }
    Verbosity verbosity = context.getVerbosity().isSilent() ? Verbosity.STANDARD_INFORMATION : context.getVerbosity();
    try (CapturingPrintStream stdout = new CapturingPrintStream();
        CapturingPrintStream stderr = new CapturingPrintStream();
        ExecutionContext firstOrderContext = context.createSubContext(stdout, stderr, Optional.of(verbosity))) {
        Javac javac = getJavac();
        JavacExecutionContext javacExecutionContext = JavacExecutionContext.of(new JavacEventSinkToBuckEventBusBridge(firstOrderContext.getBuckEventBus()), stderr, firstOrderContext.getClassLoaderCache(), firstOrderContext.getObjectMapper(), verbosity, firstOrderContext.getCellPathResolver(), firstOrderContext.getJavaPackageFinder(), filesystem, usedClassesFileWriter, firstOrderContext.getEnvironment(), firstOrderContext.getProcessExecutor(), getAbsolutePathsForJavacInputs(javac), directToJarOutputSettings);
        return performBuild(context, stdout, stderr, javac, javacExecutionContext);
    }
}
Also used : ExecutionContext(com.facebook.buck.step.ExecutionContext) CapturingPrintStream(com.facebook.buck.util.CapturingPrintStream) IOException(java.io.IOException) Verbosity(com.facebook.buck.util.Verbosity)

Example 5 with CapturingPrintStream

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

the class ProjectWorkspace method runBuckCommandWithEnvironmentOverridesAndContext.

public ProcessResult runBuckCommandWithEnvironmentOverridesAndContext(Path repoRoot, Optional<NGContext> context, ImmutableMap<String, String> environmentOverrides, CapturingPrintStream stderr, String... args) throws IOException {
    assertTrue("setUp() must be run before this method is invoked", isSetUp);
    CapturingPrintStream stdout = new CapturingPrintStream();
    InputStream stdin = new ByteArrayInputStream("".getBytes());
    // Construct a limited view of the parent environment for the child.
    // TODO(#5754812): we should eventually get tests working without requiring these be set.
    ImmutableList<String> inheritedEnvVars = ImmutableList.of("ANDROID_HOME", "ANDROID_NDK", "ANDROID_NDK_REPOSITORY", "ANDROID_SDK", // scripts provided by the groovy distribution in order to remove these two.
    "GROOVY_HOME", "JAVA_HOME", "NDK_HOME", "PATH", "PATHEXT", // Needed by ndk-build on Windows
    "OS", "ProgramW6432", "ProgramFiles(x86)", // The haskell integration tests call into GHC, which needs HOME to be set.
    "HOME", // TODO(#6586154): set TMP variable for ShellSteps
    "TMP");
    Map<String, String> envBuilder = new HashMap<>();
    for (String variable : inheritedEnvVars) {
        String value = System.getenv(variable);
        if (value != null) {
            envBuilder.put(variable, value);
        }
    }
    envBuilder.putAll(environmentOverrides);
    ImmutableMap<String, String> sanizitedEnv = ImmutableMap.copyOf(envBuilder);
    Main main = new Main(stdout, stderr, stdin);
    int exitCode;
    try {
        exitCode = main.runMainWithExitCode(new BuildId(), repoRoot, context, sanizitedEnv, CommandMode.TEST, WatchmanWatcher.FreshInstanceAction.NONE, System.nanoTime(), args);
    } catch (InterruptedException e) {
        e.printStackTrace(stderr);
        exitCode = Main.FAIL_EXIT_CODE;
        Thread.currentThread().interrupt();
    }
    return new ProcessResult(exitCode, stdout.getContentsAsString(Charsets.UTF_8), stderr.getContentsAsString(Charsets.UTF_8));
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CapturingPrintStream(com.facebook.buck.util.CapturingPrintStream) ByteArrayInputStream(java.io.ByteArrayInputStream) BuildId(com.facebook.buck.model.BuildId) Main(com.facebook.buck.cli.Main)

Aggregations

CapturingPrintStream (com.facebook.buck.util.CapturingPrintStream)8 Test (org.junit.Test)4 TestContext (com.facebook.buck.testutil.integration.TestContext)3 BuildId (com.facebook.buck.model.BuildId)2 DelegatingInputStream (com.facebook.buck.testutil.integration.DelegatingInputStream)2 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)2 ProcessResult (com.facebook.buck.testutil.integration.ProjectWorkspace.ProcessResult)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Main (com.facebook.buck.cli.Main)1 BuildReport (com.facebook.buck.command.BuildReport)1 ExecutionContext (com.facebook.buck.step.ExecutionContext)1 TestConsole (com.facebook.buck.testutil.TestConsole)1 Console (com.facebook.buck.util.Console)1 Verbosity (com.facebook.buck.util.Verbosity)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1