Search in sources :

Example 11 with FileHashCache

use of com.facebook.buck.util.cache.FileHashCache in project buck by facebook.

the class BuildInfoRecorderTest method testGetOutputHash.

@Test
public void testGetOutputHash() throws IOException {
    FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    FileHashCache fileHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
    BuildInfoRecorder buildInfoRecorder = createBuildInfoRecorder(filesystem);
    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("file1"));
    filesystem.writeBytesToPath(contents, dir.resolve("file2"));
    buildInfoRecorder.recordArtifact(dir);
    fileHashCache.invalidateAll();
    HashCode current = buildInfoRecorder.getOutputHash(fileHashCache);
    // Test that getting the hash again results in the same hashcode.
    fileHashCache.invalidateAll();
    assertEquals(current, buildInfoRecorder.getOutputHash(fileHashCache));
    // Verify that changing a file changes the hash.
    filesystem.writeContentsToPath("something else", file);
    fileHashCache.invalidateAll();
    HashCode updated = buildInfoRecorder.getOutputHash(fileHashCache);
    assertNotEquals(current, updated);
    // Verify that changing a file under a directory changes the hash.
    filesystem.writeContentsToPath("something else", dir.resolve("file1"));
    current = updated;
    fileHashCache.invalidateAll();
    updated = buildInfoRecorder.getOutputHash(fileHashCache);
    assertNotEquals(current, updated);
    // Test that adding a file updates the hash.
    Path added = Paths.get("added");
    filesystem.writeBytesToPath(contents, added);
    buildInfoRecorder.recordArtifact(added);
    current = updated;
    fileHashCache.invalidateAll();
    updated = buildInfoRecorder.getOutputHash(fileHashCache);
    assertNotEquals(current, updated);
    // Test that adding a file under a recorded directory updates the hash.
    Path addedUnderDir = dir.resolve("added");
    filesystem.writeBytesToPath(contents, addedUnderDir);
    current = updated;
    fileHashCache.invalidateAll();
    updated = buildInfoRecorder.getOutputHash(fileHashCache);
    assertNotEquals(current, updated);
}
Also used : Path(java.nio.file.Path) BorrowablePath(com.facebook.buck.io.BorrowablePath) DefaultFileHashCache(com.facebook.buck.util.cache.DefaultFileHashCache) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) FileHashCache(com.facebook.buck.util.cache.FileHashCache) HashCode(com.google.common.hash.HashCode) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) Test(org.junit.Test)

Example 12 with FileHashCache

use of com.facebook.buck.util.cache.FileHashCache in project buck by facebook.

the class RuleKeyTest method testRuleKeyDependsOnDeps.

/**
   * Ensure that build rules with the same inputs but different deps have unique RuleKeys.
   */
@Test
public void testRuleKeyDependsOnDeps() throws Exception {
    FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    FileHashCache hashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
    BuildRuleResolver ruleResolver1 = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    BuildRuleResolver ruleResolver2 = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathRuleFinder ruleFinder1 = new SourcePathRuleFinder(ruleResolver1);
    DefaultRuleKeyFactory ruleKeyFactory = new DefaultRuleKeyFactory(0, hashCache, new SourcePathResolver(ruleFinder1), ruleFinder1);
    SourcePathRuleFinder ruleFinder2 = new SourcePathRuleFinder(ruleResolver2);
    DefaultRuleKeyFactory ruleKeyFactory2 = new DefaultRuleKeyFactory(0, hashCache, new SourcePathResolver(ruleFinder2), ruleFinder2);
    // Create a dependent build rule, //src/com/facebook/buck/cli:common.
    JavaLibraryBuilder builder = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//src/com/facebook/buck/cli:common"));
    BuildRule commonJavaLibrary = builder.build(ruleResolver1);
    builder.build(ruleResolver2);
    // Create a java_library() rule with no deps.
    Path mainSrc = Paths.get("src/com/facebook/buck/cli/Main.java");
    filesystem.mkdirs(mainSrc.getParent());
    filesystem.writeContentsToPath("hello", mainSrc);
    JavaLibraryBuilder javaLibraryBuilder = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//src/com/facebook/buck/cli:cli")).addSrc(mainSrc);
    BuildRule libraryNoCommon = javaLibraryBuilder.build(ruleResolver1, filesystem);
    // Create the same java_library() rule, but with a dep on //src/com/facebook/buck/cli:common.
    javaLibraryBuilder.addDep(commonJavaLibrary.getBuildTarget());
    BuildRule libraryWithCommon = javaLibraryBuilder.build(ruleResolver2, filesystem);
    // Assert that the RuleKeys are distinct.
    RuleKey r1 = ruleKeyFactory.build(libraryNoCommon);
    RuleKey r2 = ruleKeyFactory2.build(libraryWithCommon);
    assertThat("Rule keys should be distinct because the deps of the rules are different.", r1, not(equalTo(r2)));
}
Also used : ArchiveMemberPath(com.facebook.buck.io.ArchiveMemberPath) Path(java.nio.file.Path) NullFileHashCache(com.facebook.buck.util.cache.NullFileHashCache) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) DefaultFileHashCache(com.facebook.buck.util.cache.DefaultFileHashCache) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) FileHashCache(com.facebook.buck.util.cache.FileHashCache) DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) JavaLibraryBuilder(com.facebook.buck.jvm.java.JavaLibraryBuilder) Test(org.junit.Test)

Example 13 with FileHashCache

use of com.facebook.buck.util.cache.FileHashCache in project buck by facebook.

the class RuleKeyTest method declaredDepsAndExtraDepsGenerateDifferentRuleKeys.

@Test
public void declaredDepsAndExtraDepsGenerateDifferentRuleKeys() {
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()));
    SourcePathResolver sourcePathResolver = new SourcePathResolver(ruleFinder);
    FileHashCache hashCache = new FakeFileHashCache(ImmutableMap.of());
    DefaultRuleKeyFactory ruleKeyFactory = new DefaultRuleKeyFactory(0, hashCache, sourcePathResolver, ruleFinder);
    BuildTarget target = BuildTargetFactory.newInstance("//a:target");
    BuildTarget depTarget = BuildTargetFactory.newInstance("//some:dep");
    BuildRuleParams depParams = new FakeBuildRuleParamsBuilder(depTarget).build();
    NoopBuildRule dep = new NoopBuildRule(depParams);
    BuildRuleParams paramsWithDeclaredDep = new FakeBuildRuleParamsBuilder(target).setDeclaredDeps(ImmutableSortedSet.of(dep)).build();
    NoopBuildRule ruleWithDeclaredDep = new NoopBuildRule(paramsWithDeclaredDep);
    BuildRuleParams paramsWithExtraDep = new FakeBuildRuleParamsBuilder(target).setExtraDeps(ImmutableSortedSet.of(dep)).build();
    NoopBuildRule ruleWithExtraDep = new NoopBuildRule(paramsWithExtraDep);
    BuildRuleParams paramsWithBothDeps = new FakeBuildRuleParamsBuilder(target).setDeclaredDeps(ImmutableSortedSet.of(dep)).setExtraDeps(ImmutableSortedSet.of(dep)).build();
    NoopBuildRule ruleWithBothDeps = new NoopBuildRule(paramsWithBothDeps);
    assertNotEquals(ruleKeyFactory.build(ruleWithDeclaredDep), ruleKeyFactory.build(ruleWithExtraDep));
    assertNotEquals(ruleKeyFactory.build(ruleWithDeclaredDep), ruleKeyFactory.build(ruleWithBothDeps));
    assertNotEquals(ruleKeyFactory.build(ruleWithExtraDep), ruleKeyFactory.build(ruleWithBothDeps));
}
Also used : NullFileHashCache(com.facebook.buck.util.cache.NullFileHashCache) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) DefaultFileHashCache(com.facebook.buck.util.cache.DefaultFileHashCache) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) FileHashCache(com.facebook.buck.util.cache.FileHashCache) DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) BuildTarget(com.facebook.buck.model.BuildTarget) Test(org.junit.Test)

Example 14 with FileHashCache

use of com.facebook.buck.util.cache.FileHashCache in project buck by facebook.

the class RuleKeyTest method changingRuleKeyFieldOfDepChangesKeyWhenClassImplementsAppendToRuleKey.

@Test
public void changingRuleKeyFieldOfDepChangesKeyWhenClassImplementsAppendToRuleKey() {
    BuildTarget target = BuildTargetFactory.newInstance("//cheese:peas");
    BuildRuleParams params = new FakeBuildRuleParamsBuilder(target).build();
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()));
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    FileHashCache hashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(new FakeProjectFilesystem())));
    BuildRule buildRule1 = new TestRuleKeyAppendableBuildRule(params, "foo", "bar");
    BuildRule buildRule2 = new TestRuleKeyAppendableBuildRule(params, "foo", "xyzzy");
    BuildTarget parentTarget = BuildTargetFactory.newInstance("//cheese:milk");
    BuildRuleParams parentParams1 = new FakeBuildRuleParamsBuilder(parentTarget).setDeclaredDeps(ImmutableSortedSet.of(buildRule1)).build();
    BuildRule parentRule1 = new NoopBuildRule(parentParams1);
    BuildRuleParams parentParams2 = new FakeBuildRuleParamsBuilder(parentTarget).setDeclaredDeps(ImmutableSortedSet.of(buildRule2)).build();
    BuildRule parentRule2 = new NoopBuildRule(parentParams2);
    RuleKey ruleKey1 = new DefaultRuleKeyFactory(0, hashCache, pathResolver, ruleFinder).build(parentRule1);
    RuleKey ruleKey2 = new DefaultRuleKeyFactory(0, hashCache, pathResolver, ruleFinder).build(parentRule2);
    assertNotEquals(ruleKey1, ruleKey2);
}
Also used : NullFileHashCache(com.facebook.buck.util.cache.NullFileHashCache) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) DefaultFileHashCache(com.facebook.buck.util.cache.DefaultFileHashCache) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) FileHashCache(com.facebook.buck.util.cache.FileHashCache) DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) BuildTarget(com.facebook.buck.model.BuildTarget) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) Test(org.junit.Test)

Example 15 with FileHashCache

use of com.facebook.buck.util.cache.FileHashCache in project buck by facebook.

the class ManifestTest method lookupMissingHeader.

@Test
public void lookupMissingHeader() throws IOException {
    RuleKey key = new RuleKey("aa");
    SourcePath input = new FakeSourcePath("input.h");
    Manifest manifest = Manifest.fromMap(ImmutableMap.of(key, ImmutableMap.of(RESOLVER.getRelativePath(input).toString(), HashCode.fromInt(1))));
    FileHashCache fileHashCache = new FakeFileHashCache(ImmutableMap.of());
    assertThat(manifest.lookup(fileHashCache, RESOLVER, ImmutableSet.of(input)), Matchers.equalTo(Optional.empty()));
}
Also used : FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) FileHashCache(com.facebook.buck.util.cache.FileHashCache) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) Test(org.junit.Test)

Aggregations

FileHashCache (com.facebook.buck.util.cache.FileHashCache)46 Test (org.junit.Test)37 DefaultFileHashCache (com.facebook.buck.util.cache.DefaultFileHashCache)31 FakeFileHashCache (com.facebook.buck.testutil.FakeFileHashCache)30 StackedFileHashCache (com.facebook.buck.util.cache.StackedFileHashCache)30 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)27 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)25 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)25 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)22 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)22 DefaultRuleKeyFactory (com.facebook.buck.rules.keys.DefaultRuleKeyFactory)18 Path (java.nio.file.Path)17 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)14 BuildTarget (com.facebook.buck.model.BuildTarget)14 RuleKey (com.facebook.buck.rules.RuleKey)14 NullFileHashCache (com.facebook.buck.util.cache.NullFileHashCache)12 HashCode (com.google.common.hash.HashCode)12 BuildRule (com.facebook.buck.rules.BuildRule)11 PathSourcePath (com.facebook.buck.rules.PathSourcePath)11 SourcePath (com.facebook.buck.rules.SourcePath)10