Search in sources :

Example 1 with ConsoleEvent

use of com.facebook.buck.event.ConsoleEvent in project buck by facebook.

the class HttpArtifactCacheTest method errorTextReplaced.

@Test
public void errorTextReplaced() throws InterruptedException {
    FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    final String cacheName = "http cache";
    final RuleKey ruleKey = new RuleKey("00000000000000000000000000000000");
    final RuleKey otherRuleKey = new RuleKey("11111111111111111111111111111111");
    final String data = "data";
    final AtomicBoolean consoleEventReceived = new AtomicBoolean(false);
    argsBuilder.setCacheName(cacheName).setProjectFilesystem(filesystem).setBuckEventBus(new BuckEventBus(new IncrementingFakeClock(), new BuildId()) {

        @Override
        public void post(BuckEvent event) {
            if (event instanceof ConsoleEvent) {
                consoleEventReceived.set(true);
                ConsoleEvent consoleEvent = (ConsoleEvent) event;
                assertThat(consoleEvent.getMessage(), Matchers.containsString(cacheName));
                assertThat(consoleEvent.getMessage(), Matchers.containsString("incorrect key name"));
            }
        }
    }).setFetchClient(withMakeRequest((path, requestBuilder) -> {
        Request request = requestBuilder.url(SERVER + path).build();
        Response response = new Response.Builder().request(request).protocol(Protocol.HTTP_1_1).code(HttpURLConnection.HTTP_OK).body(createResponseBody(ImmutableSet.of(otherRuleKey), ImmutableMap.of(), ByteSource.wrap(data.getBytes(Charsets.UTF_8)), data)).build();
        return new OkHttpResponseWrapper(response);
    }));
    HttpArtifactCache cache = new HttpArtifactCache(argsBuilder.build());
    Path output = Paths.get("output/file");
    CacheResult result = cache.fetch(ruleKey, LazyPath.ofInstance(output));
    assertEquals(CacheResultType.ERROR, result.getType());
    assertEquals(Optional.empty(), filesystem.readFileIfItExists(output));
    assertTrue(consoleEventReceived.get());
    cache.close();
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) BuckEventBus(com.facebook.buck.event.BuckEventBus) HttpURLConnection(java.net.HttpURLConnection) DataInputStream(java.io.DataInputStream) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BuckEvent(com.facebook.buck.event.BuckEvent) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) Assert.assertThat(org.junit.Assert.assertThat) Lists(com.google.common.collect.Lists) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) BuildId(com.facebook.buck.model.BuildId) RuleKey(com.facebook.buck.rules.RuleKey) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Response(okhttp3.Response) ByteSource(com.google.common.io.ByteSource) HttpResponse(com.facebook.buck.slb.HttpResponse) Path(java.nio.file.Path) MediaType(okhttp3.MediaType) ResponseBody(okhttp3.ResponseBody) HttpService(com.facebook.buck.slb.HttpService) Before(org.junit.Before) Charsets(com.google.common.base.Charsets) Request(okhttp3.Request) Buffer(okio.Buffer) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Assert.assertTrue(org.junit.Assert.assertTrue) OkHttpResponseWrapper(com.facebook.buck.slb.OkHttpResponseWrapper) Matchers(org.hamcrest.Matchers) Set(java.util.Set) Test(org.junit.Test) IOException(java.io.IOException) Sets(com.google.common.collect.Sets) List(java.util.List) Paths(java.nio.file.Paths) LazyPath(com.facebook.buck.io.LazyPath) IncrementingFakeClock(com.facebook.buck.timing.IncrementingFakeClock) Optional(java.util.Optional) Protocol(okhttp3.Protocol) Assert.assertEquals(org.junit.Assert.assertEquals) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Path(java.nio.file.Path) LazyPath(com.facebook.buck.io.LazyPath) RuleKey(com.facebook.buck.rules.RuleKey) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) Request(okhttp3.Request) IncrementingFakeClock(com.facebook.buck.timing.IncrementingFakeClock) OkHttpResponseWrapper(com.facebook.buck.slb.OkHttpResponseWrapper) BuckEvent(com.facebook.buck.event.BuckEvent) Response(okhttp3.Response) HttpResponse(com.facebook.buck.slb.HttpResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BuildId(com.facebook.buck.model.BuildId) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) Test(org.junit.Test)

Example 2 with ConsoleEvent

use of com.facebook.buck.event.ConsoleEvent in project buck by facebook.

the class ProjectBuildFileParserTest method whenSubprocessReturnsWarningThenConsoleEventPublished.

@Test
public void whenSubprocessReturnsWarningThenConsoleEventPublished() throws IOException, BuildFileParseException, InterruptedException {
    // This test depends on unix utilities that don't exist on Windows.
    assumeTrue(Platform.detect() != Platform.WINDOWS);
    TestProjectBuildFileParserFactory buildFileParserFactory = new TestProjectBuildFileParserFactory(cell.getRoot(), cell.getKnownBuildRuleTypes());
    BuckEventBus buckEventBus = BuckEventBusFactory.newInstance(new FakeClock(0));
    final List<ConsoleEvent> consoleEvents = new ArrayList<>();
    final List<WatchmanDiagnosticEvent> watchmanDiagnosticEvents = new ArrayList<>();
    class EventListener {

        @Subscribe
        public void on(ConsoleEvent consoleEvent) {
            consoleEvents.add(consoleEvent);
        }

        @Subscribe
        public void on(WatchmanDiagnosticEvent event) {
            watchmanDiagnosticEvents.add(event);
        }
    }
    EventListener eventListener = new EventListener();
    buckEventBus.register(eventListener);
    try (ProjectBuildFileParser buildFileParser = buildFileParserFactory.createNoopParserThatAlwaysReturnsSuccessWithWarning(buckEventBus, "This is a warning", "parser")) {
        buildFileParser.initIfNeeded();
        buildFileParser.getAllRulesAndMetaRules(Paths.get("foo"));
    }
    assertThat(consoleEvents, Matchers.contains(Matchers.hasToString("Warning raised by BUCK file parser: This is a warning")));
    assertThat("Should not receive any watchman diagnostic events", watchmanDiagnosticEvents, Matchers.empty());
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) FakeClock(com.facebook.buck.timing.FakeClock) WatchmanDiagnosticEvent(com.facebook.buck.io.WatchmanDiagnosticEvent) ProjectBuildFileParser(com.facebook.buck.json.ProjectBuildFileParser) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 3 with ConsoleEvent

use of com.facebook.buck.event.ConsoleEvent in project buck by facebook.

the class ProjectBuildFileParserTest method whenSubprocessPrintsWarningToStderrThenConsoleEventPublished.

@Test
public void whenSubprocessPrintsWarningToStderrThenConsoleEventPublished() throws IOException, BuildFileParseException, InterruptedException {
    // This test depends on unix utilities that don't exist on Windows.
    assumeTrue(Platform.detect() != Platform.WINDOWS);
    TestProjectBuildFileParserFactory buildFileParserFactory = new TestProjectBuildFileParserFactory(cell.getRoot(), cell.getKnownBuildRuleTypes());
    BuckEventBus buckEventBus = BuckEventBusFactory.newInstance(new FakeClock(0));
    final List<ConsoleEvent> consoleEvents = new ArrayList<>();
    class EventListener {

        @Subscribe
        public void onConsoleEvent(ConsoleEvent consoleEvent) {
            consoleEvents.add(consoleEvent);
        }
    }
    EventListener eventListener = new EventListener();
    buckEventBus.register(eventListener);
    try (ProjectBuildFileParser buildFileParser = buildFileParserFactory.createNoopParserThatAlwaysReturnsSuccessAndPrintsToStderr(buckEventBus)) {
        buildFileParser.initIfNeeded();
        buildFileParser.getAllRulesAndMetaRules(Paths.get("foo"));
    }
    assertThat(consoleEvents, Matchers.contains(Matchers.hasToString("Warning raised by BUCK file parser: Don't Panic!")));
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) FakeClock(com.facebook.buck.timing.FakeClock) ProjectBuildFileParser(com.facebook.buck.json.ProjectBuildFileParser) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 4 with ConsoleEvent

use of com.facebook.buck.event.ConsoleEvent in project buck by facebook.

the class ProjectBuildFileParserTest method whenSubprocessReturnsErrorThenConsoleEventPublished.

@Test
public void whenSubprocessReturnsErrorThenConsoleEventPublished() throws IOException, BuildFileParseException, InterruptedException {
    // This test depends on unix utilities that don't exist on Windows.
    assumeTrue(Platform.detect() != Platform.WINDOWS);
    TestProjectBuildFileParserFactory buildFileParserFactory = new TestProjectBuildFileParserFactory(cell.getRoot(), cell.getKnownBuildRuleTypes());
    BuckEventBus buckEventBus = BuckEventBusFactory.newInstance(new FakeClock(0));
    final List<ConsoleEvent> consoleEvents = new ArrayList<>();
    class EventListener {

        @Subscribe
        public void onConsoleEvent(ConsoleEvent consoleEvent) {
            consoleEvents.add(consoleEvent);
        }
    }
    EventListener eventListener = new EventListener();
    buckEventBus.register(eventListener);
    try (ProjectBuildFileParser buildFileParser = buildFileParserFactory.createNoopParserThatAlwaysReturnsSuccessWithError(buckEventBus, "This is an error", "parser")) {
        buildFileParser.initIfNeeded();
        buildFileParser.getAllRulesAndMetaRules(Paths.get("foo"));
    }
    assertThat(consoleEvents, Matchers.contains(Matchers.hasToString("Error raised by BUCK file parser: This is an error")));
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) FakeClock(com.facebook.buck.timing.FakeClock) ProjectBuildFileParser(com.facebook.buck.json.ProjectBuildFileParser) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 5 with ConsoleEvent

use of com.facebook.buck.event.ConsoleEvent in project buck by facebook.

the class ShellStepTest method createContext.

private static ExecutionContext createContext(ImmutableMap<ProcessExecutorParams, FakeProcess> processes, final Console console) throws IOException {
    ExecutionContext context = TestExecutionContext.newBuilder().setConsole(console).setProcessExecutor(new FakeProcessExecutor(processes, console)).build();
    context.getBuckEventBus().register(new Object() {

        @Subscribe
        public void logEvent(ConsoleEvent event) throws IOException {
            if (event.getLevel().equals(Level.WARNING)) {
                console.getStdErr().write(event.getMessage().getBytes(Charsets.UTF_8));
            }
        }
    });
    return context;
}
Also used : ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) FakeProcessExecutor(com.facebook.buck.util.FakeProcessExecutor) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) Subscribe(com.google.common.eventbus.Subscribe) IOException(java.io.IOException)

Aggregations

ConsoleEvent (com.facebook.buck.event.ConsoleEvent)10 Test (org.junit.Test)8 BuckEventBus (com.facebook.buck.event.BuckEventBus)5 BuckEvent (com.facebook.buck.event.BuckEvent)4 ProjectBuildFileParser (com.facebook.buck.json.ProjectBuildFileParser)4 ExecutionContext (com.facebook.buck.step.ExecutionContext)4 TestExecutionContext (com.facebook.buck.step.TestExecutionContext)4 FakeClock (com.facebook.buck.timing.FakeClock)4 ArrayList (java.util.ArrayList)4 FakeBuckEventListener (com.facebook.buck.event.FakeBuckEventListener)3 IOException (java.io.IOException)2 LazyPath (com.facebook.buck.io.LazyPath)1 WatchmanDiagnosticEvent (com.facebook.buck.io.WatchmanDiagnosticEvent)1 BuildId (com.facebook.buck.model.BuildId)1 RuleKey (com.facebook.buck.rules.RuleKey)1 HttpResponse (com.facebook.buck.slb.HttpResponse)1 HttpService (com.facebook.buck.slb.HttpService)1 OkHttpResponseWrapper (com.facebook.buck.slb.OkHttpResponseWrapper)1 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)1 IncrementingFakeClock (com.facebook.buck.timing.IncrementingFakeClock)1