Search in sources :

Example 11 with FakeClock

use of com.facebook.buck.timing.FakeClock in project buck by facebook.

the class ProjectBuildFileParserTest method whenSubprocessReturnsNonSyntaxErrorThenExceptionContainsFullStackTrace.

@Test
public void whenSubprocessReturnsNonSyntaxErrorThenExceptionContainsFullStackTrace() 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());
    thrown.expect(BuildFileParseException.class);
    thrown.expectMessage("Parse error for build file foo/BUCK:\n" + "ZeroDivisionError: integer division or modulo by zero\n" + "Call stack:\n" + "  File \"bar/BUCK\", line 42, in lets_divide_by_zero\n" + "    foo / bar\n" + "\n" + "  File \"foo/BUCK\", line 23\n" + "    lets_divide_by_zero()\n" + "\n");
    try (ProjectBuildFileParser buildFileParser = buildFileParserFactory.createNoopParserThatAlwaysReturnsErrorWithException(BuckEventBusFactory.newInstance(new FakeClock(0)), "This is an error", "parser", ImmutableMap.<String, Object>builder().put("type", "ZeroDivisionError").put("value", "integer division or modulo by zero").put("filename", "bar/BUCK").put("lineno", 42).put("offset", 24).put("text", "foo / bar\n").put("traceback", ImmutableList.of(ImmutableMap.of("filename", "bar/BUCK", "line_number", 42, "function_name", "lets_divide_by_zero", "text", "foo / bar\n"), ImmutableMap.of("filename", "foo/BUCK", "line_number", 23, "function_name", "<module>", "text", "lets_divide_by_zero()\n"))).build())) {
        buildFileParser.initIfNeeded();
        buildFileParser.getAllRulesAndMetaRules(Paths.get("foo/BUCK"));
    }
}
Also used : ProjectBuildFileParser(com.facebook.buck.json.ProjectBuildFileParser) FakeClock(com.facebook.buck.timing.FakeClock) Test(org.junit.Test)

Example 12 with FakeClock

use of com.facebook.buck.timing.FakeClock 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 13 with FakeClock

use of com.facebook.buck.timing.FakeClock in project buck by facebook.

the class BuildInfoRecorderTest method testPerformUploadToArtifactCache.

@Test
public void testPerformUploadToArtifactCache() throws IOException, InterruptedException {
    FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    BuildInfoRecorder buildInfoRecorder = createBuildInfoRecorder(filesystem);
    BuckEventBus bus = new BuckEventBus(new FakeClock(0), new BuildId("BUILD"));
    final byte[] contents = "contents".getBytes();
    Path file = Paths.get("file");
    filesystem.writeBytesToPath(contents, file);
    buildInfoRecorder.recordArtifact(file);
    Path dir = Paths.get("dir");
    filesystem.mkdirs(dir);
    filesystem.writeBytesToPath(contents, dir.resolve("file"));
    buildInfoRecorder.recordArtifact(dir);
    // Record some metadata.
    buildInfoRecorder.addMetadata("metadata", "metadata");
    // Record some build metadata.
    buildInfoRecorder.addBuildMetadata("build-metadata", "build-metadata");
    buildInfoRecorder.writeMetadataToDisk(true);
    final AtomicBoolean stored = new AtomicBoolean(false);
    final ArtifactCache cache = new NoopArtifactCache() {

        @Override
        public boolean isStoreSupported() {
            return true;
        }

        @Override
        public ListenableFuture<Void> store(ArtifactInfo info, BorrowablePath output) {
            stored.set(true);
            // Verify the build metadata.
            assertThat(info.getMetadata().get("build-metadata"), Matchers.equalTo("build-metadata"));
            // Verify zip contents
            try (Zip zip = new Zip(output.getPath(), /* forWriting */
            false)) {
                assertEquals(ImmutableSet.of("", "dir/", "buck-out/", "buck-out/bin/", "buck-out/bin/foo/", "buck-out/bin/foo/.bar/", "buck-out/bin/foo/.bar/metadata/"), zip.getDirNames());
                assertEquals(ImmutableSet.of("dir/file", "file", "buck-out/bin/foo/.bar/metadata/metadata"), zip.getFileNames());
                assertArrayEquals(contents, zip.readFully("file"));
                assertArrayEquals(contents, zip.readFully("dir/file"));
            } catch (IOException e) {
                Throwables.throwIfUnchecked(e);
                throw new RuntimeException(e);
            }
            return Futures.immediateFuture(null);
        }
    };
    buildInfoRecorder.performUploadToArtifactCache(ImmutableSet.of(new RuleKey("aa")), cache, bus);
    assertTrue(stored.get());
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) Path(java.nio.file.Path) BorrowablePath(com.facebook.buck.io.BorrowablePath) Zip(com.facebook.buck.testutil.Zip) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) FakeClock(com.facebook.buck.timing.FakeClock) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArtifactInfo(com.facebook.buck.artifact_cache.ArtifactInfo) BuildId(com.facebook.buck.model.BuildId) NoopArtifactCache(com.facebook.buck.artifact_cache.NoopArtifactCache) BorrowablePath(com.facebook.buck.io.BorrowablePath) ArtifactCache(com.facebook.buck.artifact_cache.ArtifactCache) NoopArtifactCache(com.facebook.buck.artifact_cache.NoopArtifactCache) Test(org.junit.Test)

Example 14 with FakeClock

use of com.facebook.buck.timing.FakeClock in project buck by facebook.

the class WatchmanWatcherTest method whenExistsIsFalseThenDeleteEventIsGenerated.

@Test
public void whenExistsIsFalseThenDeleteEventIsGenerated() throws IOException, InterruptedException {
    ImmutableMap<String, Object> watchmanOutput = ImmutableMap.of("files", ImmutableList.of(ImmutableMap.<String, Object>of("name", "foo/bar/baz", "exists", false)));
    Capture<WatchEvent<Path>> eventCapture = newCapture();
    EventBus eventBus = createStrictMock(EventBus.class);
    eventBus.post(capture(eventCapture));
    replay(eventBus);
    WatchmanWatcher watcher = createWatcher(eventBus, watchmanOutput);
    watcher.postEvents(BuckEventBusFactory.newInstance(new FakeClock(0)), WatchmanWatcher.FreshInstanceAction.NONE);
    verify(eventBus);
    assertEquals("Should be delete event.", StandardWatchEventKinds.ENTRY_DELETE, eventCapture.getValue().kind());
}
Also used : FakeClock(com.facebook.buck.timing.FakeClock) EasyMock.anyObject(org.easymock.EasyMock.anyObject) WatchEvent(java.nio.file.WatchEvent) BuckEventBus(com.facebook.buck.event.BuckEventBus) EventBus(com.google.common.eventbus.EventBus) Test(org.junit.Test)

Example 15 with FakeClock

use of com.facebook.buck.timing.FakeClock in project buck by facebook.

the class WatchmanWatcherIntegrationTest method globMatchesWholeName.

@Test
public void globMatchesWholeName() throws IOException, InterruptedException {
    WatchmanWatcher watcher = createWatchmanWatcher(new PathOrGlobMatcher("*.txt"));
    // Create a dot-file which should be ignored by the above glob.
    Path path = tmp.getRoot().getFileSystem().getPath("foo/bar/hello.txt");
    Files.createDirectories(tmp.getRoot().resolve(path).getParent());
    Files.write(tmp.getRoot().resolve(path), new byte[0]);
    // Verify we still get an event for the created path.
    watcher.postEvents(new BuckEventBus(new FakeClock(0), new BuildId()), WatchmanWatcher.FreshInstanceAction.NONE);
    ImmutableList<WatchEvent<?>> events = watchmanEventCollector.getEvents();
    assertThat(events.size(), Matchers.equalTo(1));
    WatchEvent<?> event = events.get(0);
    Path eventPath = (Path) event.context();
    assertThat(eventPath, Matchers.equalTo(path));
    assertSame(event.kind(), StandardWatchEventKinds.ENTRY_CREATE);
}
Also used : Path(java.nio.file.Path) BuckEventBus(com.facebook.buck.event.BuckEventBus) BuildId(com.facebook.buck.model.BuildId) FakeClock(com.facebook.buck.timing.FakeClock) PathOrGlobMatcher(com.facebook.buck.io.PathOrGlobMatcher) WatchEvent(java.nio.file.WatchEvent) Test(org.junit.Test)

Aggregations

FakeClock (com.facebook.buck.timing.FakeClock)54 Test (org.junit.Test)48 BuckEventBus (com.facebook.buck.event.BuckEventBus)36 EventBus (com.google.common.eventbus.EventBus)19 EasyMock.anyObject (org.easymock.EasyMock.anyObject)17 WatchEvent (java.nio.file.WatchEvent)13 BuildId (com.facebook.buck.model.BuildId)12 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)9 ProjectBuildFileParser (com.facebook.buck.json.ProjectBuildFileParser)8 Path (java.nio.file.Path)8 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)7 FakeWatchmanClient (com.facebook.buck.io.FakeWatchmanClient)6 Subscribe (com.google.common.eventbus.Subscribe)6 ArrayList (java.util.ArrayList)6 IncrementingFakeClock (com.facebook.buck.timing.IncrementingFakeClock)5 SettableFakeClock (com.facebook.buck.timing.SettableFakeClock)5 ConsoleEvent (com.facebook.buck.event.ConsoleEvent)4 FakeExecutor (com.facebook.buck.testutil.FakeExecutor)4 BuckEvent (com.facebook.buck.event.BuckEvent)3 WatchmanStatusEvent (com.facebook.buck.event.WatchmanStatusEvent)2