Search in sources :

Example 16 with BuildId

use of com.facebook.buck.model.BuildId in project buck by facebook.

the class TargetGraphHashingTest method hashChangesForDependentNodeWhenDepsChange.

@Test
public void hashChangesForDependentNodeWhenDepsChange() throws IOException, InterruptedException, AcyclicDepthFirstPostOrderTraversal.CycleException {
    FakeProjectFilesystem projectFilesystem = new FakeProjectFilesystem();
    BuckEventBus eventBus = new BuckEventBus(new IncrementingFakeClock(), new BuildId());
    BuildTarget nodeTarget = BuildTargetFactory.newInstance("//foo:lib");
    BuildTarget depTarget = BuildTargetFactory.newInstance("//dep:lib");
    TargetGraph targetGraphA = createGraphWithANodeAndADep(nodeTarget, HashCode.fromLong(12345), depTarget, HashCode.fromLong(64738));
    TargetGraph targetGraphB = createGraphWithANodeAndADep(nodeTarget, HashCode.fromLong(12345), depTarget, HashCode.fromLong(84552));
    FileHashCache fileHashCache = new FakeFileHashCache(ImmutableMap.of(projectFilesystem.resolve("foo/FooLib.java"), HashCode.fromString("abcdef"), projectFilesystem.resolve("dep/DepLib.java"), HashCode.fromString("123456")));
    Map<BuildTarget, HashCode> resultA = new TargetGraphHashing(eventBus, targetGraphA, fileHashCache, ImmutableList.of(targetGraphA.get(nodeTarget))).hashTargetGraph();
    Map<BuildTarget, HashCode> resultB = new TargetGraphHashing(eventBus, targetGraphB, fileHashCache, ImmutableList.of(targetGraphB.get(nodeTarget))).hashTargetGraph();
    assertThat(resultA, aMapWithSize(2));
    assertThat(resultA, hasKey(nodeTarget));
    assertThat(resultA, hasKey(depTarget));
    assertThat(resultB, aMapWithSize(2));
    assertThat(resultB, hasKey(nodeTarget));
    assertThat(resultB, hasKey(depTarget));
    assertThat(resultA.get(nodeTarget), not(equalTo(resultB.get(nodeTarget))));
    assertThat(resultA.get(depTarget), not(equalTo(resultB.get(depTarget))));
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) NullFileHashCache(com.facebook.buck.util.cache.NullFileHashCache) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) FileHashCache(com.facebook.buck.util.cache.FileHashCache) HashCode(com.google.common.hash.HashCode) BuildId(com.facebook.buck.model.BuildId) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) BuildTarget(com.facebook.buck.model.BuildTarget) IncrementingFakeClock(com.facebook.buck.timing.IncrementingFakeClock) Test(org.junit.Test)

Example 17 with BuildId

use of com.facebook.buck.model.BuildId 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)

Example 18 with BuildId

use of com.facebook.buck.model.BuildId in project buck by facebook.

the class WatchmanWatcherIntegrationTest method ignoreDotFileInGlob.

@Test
public void ignoreDotFileInGlob() throws IOException, InterruptedException {
    WatchmanWatcher watcher = createWatchmanWatcher(new PathOrGlobMatcher("**/*.swp"));
    // Create a dot-file which should be ignored by the above glob.
    Path path = tmp.getRoot().getFileSystem().getPath("foo/bar/.hello.swp");
    Files.createDirectories(tmp.getRoot().resolve(path).getParent());
    Files.write(tmp.getRoot().resolve(path), new byte[0]);
    // Verify we don't get an event for the path.
    watcher.postEvents(new BuckEventBus(new FakeClock(0), new BuildId()), WatchmanWatcher.FreshInstanceAction.NONE);
    assertThat(watchmanEventCollector.getEvents(), Matchers.empty());
}
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) Test(org.junit.Test)

Example 19 with BuildId

use of com.facebook.buck.model.BuildId in project buck by facebook.

the class OfflineScribeLoggerTest method unsentLinesStoredForOffline.

@Test
public void unsentLinesStoredForOffline() throws Exception {
    final String whitelistedCategory = "whitelisted_category";
    final String whitelistedCategory2 = "whitelisted_category_2";
    final String blacklistedCategory = "blacklisted_category";
    final ImmutableList<String> blacklistCategories = ImmutableList.of(blacklistedCategory);
    final int maxScribeOfflineLogsKB = 7;
    final ProjectFilesystem filesystem = new ProjectFilesystem(tmp.getRoot());
    final Path logDir = filesystem.getBuckPaths().getOfflineLogDir();
    final ObjectMapper objectMapper = ObjectMappers.newDefaultInstance();
    String[] ids = { "test1", "test2", "test3", "test4" };
    char[] longLineBytes = new char[1000];
    Arrays.fill(longLineBytes, 'A');
    String longLine = new String(longLineBytes);
    char[] tooLongLineBytes = new char[6000];
    Arrays.fill(longLineBytes, 'A');
    String tooLongLine = new String(tooLongLineBytes);
    // As we set max space for logs to 7KB, then we expect storing data with 2 'longLine' (which,
    // given UTF-8 is used, will be ~ 2 * 1KB) to succeed. We then expect subsequent attempt to
    // store data with 'tooLongLine' (~6KB) to fail. We also expect that logfile created by first
    // fakeLogger ('test1' id) will be removed as otherwise data from last logger would not fit.
    //
    // Note that code sending already stored offline logs will be triggered by the first log() as
    // it succeeds, but further failing log() (and initiating storing) will stop sending. Hence, no
    // logs will be deleted due to the sending routine.
    FakeFailingOfflineScribeLogger fakeLogger = null;
    for (String id : ids) {
        fakeLogger = new FakeFailingOfflineScribeLogger(blacklistCategories, maxScribeOfflineLogsKB, filesystem, logDir, objectMapper, new BuildId(id));
        // Simulate network issues occurring for some of sending attempts (all after first one).
        // Logging succeeds.
        fakeLogger.log(whitelistedCategory, ImmutableList.of("hello world 1", "hello world 2"));
        // Logging fails.
        fakeLogger.log(whitelistedCategory, ImmutableList.of("hello world 3", "hello world 4"));
        // Event with blacklisted category for offline logging.
        fakeLogger.log(blacklistedCategory, ImmutableList.of("hello world 5", "hello world 6"));
        // Logging fails.
        fakeLogger.log(whitelistedCategory2, ImmutableList.of(longLine, longLine));
        // Logging fails, but offline logging rejects data as well - too big.
        fakeLogger.log(whitelistedCategory2, ImmutableList.of(tooLongLine));
        fakeLogger.close();
    }
    // Check correct logs are in the directory (1st log removed).
    Path[] expectedLogPaths = FluentIterable.from(ImmutableList.copyOf(ids)).transform(id -> filesystem.resolve(logDir.resolve(LOGFILE_PREFIX + id + LOGFILE_SUFFIX))).toArray(Path.class);
    ImmutableSortedSet<Path> logs = filesystem.getMtimeSortedMatchingDirectoryContents(logDir, LOGFILE_PATTERN);
    assertThat(logs, Matchers.allOf(hasItem(expectedLogPaths[1]), hasItem(expectedLogPaths[2]), hasItem(expectedLogPaths[3]), IsIterableWithSize.<Path>iterableWithSize(3)));
    // Check that last logger logged correct data.
    assertEquals(3, fakeLogger.getAttemptStoringCategoriesWithLinesCount());
    InputStream logFile = fakeLogger.getStoredLog();
    String[] whitelistedCategories = { whitelistedCategory, whitelistedCategory2 };
    String[][] whitelistedLines = { { "hello world 3", "hello world 4" }, { longLine, longLine } };
    Iterator<ScribeData> it = null;
    try {
        it = new ObjectMapper().readValues(new JsonFactory().createParser(logFile), ScribeData.class);
    } catch (Exception e) {
        fail("Obtaining iterator for reading the log failed.");
    }
    int dataNum = 0;
    try {
        while (it.hasNext()) {
            assertTrue(dataNum < 2);
            ScribeData data = it.next();
            assertThat(data.getCategory(), is(whitelistedCategories[dataNum]));
            assertThat(data.getLines(), Matchers.allOf(hasItem(whitelistedLines[dataNum][0]), hasItem(whitelistedLines[dataNum][1]), IsIterableWithSize.<String>iterableWithSize(2)));
            dataNum++;
        }
    } catch (Exception e) {
        fail("Reading stored offline log failed.");
    }
    logFile.close();
    assertEquals(2, dataNum);
}
Also used : Path(java.nio.file.Path) LOGFILE_PREFIX(com.facebook.buck.util.network.offline.OfflineScribeLogger.LOGFILE_PREFIX) Arrays(java.util.Arrays) BufferedInputStream(java.io.BufferedInputStream) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ObjectMappers(com.facebook.buck.util.ObjectMappers) LOGFILE_SUFFIX(com.facebook.buck.util.network.offline.OfflineScribeLogger.LOGFILE_SUFFIX) TemporaryPaths(com.facebook.buck.testutil.integration.TemporaryPaths) BuckEventBusFactory(com.facebook.buck.event.BuckEventBusFactory) Matchers.arrayContainingInAnyOrder(org.hamcrest.Matchers.arrayContainingInAnyOrder) BufferedOutputStream(java.io.BufferedOutputStream) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Matchers.everyItem(org.hamcrest.Matchers.everyItem) ImmutableList(com.google.common.collect.ImmutableList) BuildId(com.facebook.buck.model.BuildId) FluentIterable(com.google.common.collect.FluentIterable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Is.is(org.hamcrest.core.Is.is) ScribeLogger(com.facebook.buck.util.network.ScribeLogger) Assert.fail(org.junit.Assert.fail) LOGFILE_PATTERN(com.facebook.buck.util.network.offline.OfflineScribeLogger.LOGFILE_PATTERN) Pair(com.facebook.buck.model.Pair) Path(java.nio.file.Path) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Charsets(com.google.common.base.Charsets) ObjectArrays(com.google.common.collect.ObjectArrays) Iterator(java.util.Iterator) FakeFailingScribeLogger(com.facebook.buck.util.network.FakeFailingScribeLogger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FutureCallback(com.google.common.util.concurrent.FutureCallback) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) Matchers.hasItem(org.hamcrest.Matchers.hasItem) JsonFactory(com.fasterxml.jackson.core.JsonFactory) Rule(org.junit.Rule) IsIterableWithSize(org.hamcrest.collection.IsIterableWithSize) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JsonFactory(com.fasterxml.jackson.core.JsonFactory) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BuildId(com.facebook.buck.model.BuildId) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 20 with BuildId

use of com.facebook.buck.model.BuildId in project buck by facebook.

the class JUnitStepTest method testGetEnvironmentVariables.

@Test
public void testGetEnvironmentVariables() {
    BuildId pretendBuildId = new BuildId("pretend-build-id");
    Path modulePath = Paths.get("module/submodule");
    Path directoryForTestResults = Paths.get("buck-out/gen/theresults/");
    Path testRunnerClasspath = Paths.get("build/classes/junit");
    ProjectFilesystem filesystem = FakeProjectFilesystem.createJavaOnlyFilesystem();
    Path classpathFile = filesystem.resolve("foo");
    JUnitJvmArgs args = JUnitJvmArgs.builder().setBuildId(pretendBuildId).setBuckModuleBaseSourceCodePath(modulePath).setClasspathFile(classpathFile).setTestRunnerClasspath(testRunnerClasspath).setExtraJvmArgs(ImmutableList.of()).setTestType(TestType.JUNIT).setDirectoryForTestResults(directoryForTestResults).addAllTestClasses(ImmutableList.of()).build();
    JUnitStep junit = new JUnitStep(filesystem, /* nativeLibsEnvironment */
    ImmutableMap.of(), /* testRuleTimeoutMs */
    Optional.empty(), /* testCaseTimeoutMs */
    Optional.empty(), ImmutableMap.of("FOO", "BAR"), new ExternalJavaRuntimeLauncher("/foo/bar/custom/java"), args);
    ImmutableMap<String, String> observedEnvironment = junit.getEnvironmentVariables(TestExecutionContext.newInstance());
    assertThat(observedEnvironment, hasEntry("FOO", "BAR"));
}
Also used : Path(java.nio.file.Path) BuildId(com.facebook.buck.model.BuildId) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Test(org.junit.Test)

Aggregations

BuildId (com.facebook.buck.model.BuildId)41 Test (org.junit.Test)25 BuckEventBus (com.facebook.buck.event.BuckEventBus)22 Path (java.nio.file.Path)13 FakeClock (com.facebook.buck.timing.FakeClock)12 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)10 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)9 IOException (java.io.IOException)8 IncrementingFakeClock (com.facebook.buck.timing.IncrementingFakeClock)7 BuildTarget (com.facebook.buck.model.BuildTarget)6 Before (org.junit.Before)6 DefaultClock (com.facebook.buck.timing.DefaultClock)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 ArtifactCache (com.facebook.buck.artifact_cache.ArtifactCache)4 BuildRuleDurationTracker (com.facebook.buck.rules.BuildRuleDurationTracker)4 Clock (com.facebook.buck.timing.Clock)4 NullFileHashCache (com.facebook.buck.util.cache.NullFileHashCache)4 ImmutableList (com.google.common.collect.ImmutableList)4 RuleKey (com.facebook.buck.rules.RuleKey)3 FakeExecutor (com.facebook.buck.testutil.FakeExecutor)3