Search in sources :

Example 36 with FileHashCache

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

the class CalculateAbiTest method ruleKeysChangeIfGeneratedBinaryJarChanges.

@Test
public void ruleKeysChangeIfGeneratedBinaryJarChanges() throws Exception {
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    ProjectFilesystem filesystem = new FakeProjectFilesystem();
    // Setup the initial java library.
    Path input = Paths.get("input.java");
    BuildTarget javaLibraryTarget = BuildTargetFactory.newInstance("//:library");
    JavaLibraryBuilder builder = JavaLibraryBuilder.createBuilder(javaLibraryTarget).addSrc(new PathSourcePath(filesystem, input));
    DefaultJavaLibrary javaLibrary = builder.build(resolver, filesystem);
    // Write something to the library source and geneated JAR, so they exist to generate rule keys.
    filesystem.writeContentsToPath("stuff", input);
    filesystem.writeContentsToPath("stuff", pathResolver.getRelativePath(javaLibrary.getSourcePathToOutput()));
    BuildTarget target = BuildTargetFactory.newInstance("//:library-abi");
    CalculateAbi calculateAbi = CalculateAbi.of(target, ruleFinder, builder.createBuildRuleParams(resolver, filesystem), new DefaultBuildTargetSourcePath(javaLibraryTarget));
    FileHashCache initialHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
    DefaultRuleKeyFactory initialRuleKeyFactory = new DefaultRuleKeyFactory(0, initialHashCache, pathResolver, ruleFinder);
    RuleKey initialKey = initialRuleKeyFactory.build(calculateAbi);
    RuleKey initialInputKey = new InputBasedRuleKeyFactory(0, initialHashCache, pathResolver, ruleFinder).build(calculateAbi);
    // Write something to the library source and geneated JAR, so they exist to generate rule keys.
    filesystem.writeContentsToPath("new stuff", input);
    filesystem.writeContentsToPath("new stuff", pathResolver.getRelativePath(javaLibrary.getSourcePathToOutput()));
    // Re-setup some entities to drop internal rule key caching.
    resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    ruleFinder = new SourcePathRuleFinder(resolver);
    pathResolver = new SourcePathResolver(ruleFinder);
    builder.build(resolver, filesystem);
    FileHashCache alteredHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
    DefaultRuleKeyFactory alteredRuleKeyFactory = new DefaultRuleKeyFactory(0, alteredHashCache, pathResolver, ruleFinder);
    RuleKey alteredKey = alteredRuleKeyFactory.build(calculateAbi);
    RuleKey alteredInputKey = new InputBasedRuleKeyFactory(0, alteredHashCache, pathResolver, ruleFinder).build(calculateAbi);
    assertThat(initialKey, Matchers.not(Matchers.equalTo(alteredKey)));
    assertThat(initialInputKey, Matchers.not(Matchers.equalTo(alteredInputKey)));
}
Also used : Path(java.nio.file.Path) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) 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) InputBasedRuleKeyFactory(com.facebook.buck.rules.keys.InputBasedRuleKeyFactory) RuleKey(com.facebook.buck.rules.RuleKey) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildTarget(com.facebook.buck.model.BuildTarget) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 37 with FileHashCache

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

the class DefaultRuleKeyFactoryTest method shouldAllowRuleKeyAppendablesToAppendToRuleKey.

@Test
public void shouldAllowRuleKeyAppendablesToAppendToRuleKey() {
    BuildTarget target = BuildTargetFactory.newInstance("//cheese:peas");
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()));
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    BuildRule rule = new EmptyRule(target);
    FileHashCache fileHashCache = new NullFileHashCache();
    DefaultRuleKeyFactory factory = new DefaultRuleKeyFactory(0, fileHashCache, pathResolver, ruleFinder);
    RuleKey subKey = new UncachedRuleKeyBuilder(ruleFinder, pathResolver, fileHashCache, factory).setReflectively("cheese", "brie").build();
    RuleKeyBuilder<RuleKeyResult<RuleKey>> builder = factory.newBuilderForTesting(rule);
    try (RuleKeyScopedHasher.Scope keyScope = builder.getScopedHasher().keyScope("field")) {
        try (RuleKeyScopedHasher.Scope appendableScope = builder.getScopedHasher().wrapperScope(RuleKeyHasher.Wrapper.APPENDABLE)) {
            builder.getScopedHasher().getHasher().putRuleKey(subKey);
        }
    }
    RuleKey expected = builder.build().result;
    class AppendingField extends EmptyRule {

        @AddToRuleKey
        private Appender field = new Appender();

        public AppendingField(BuildTarget target) {
            super(target);
        }
    }
    RuleKey seen = factory.build(new AppendingField(target));
    assertEquals(expected, seen);
}
Also used : DefaultFileHashCache(com.facebook.buck.util.cache.DefaultFileHashCache) NullFileHashCache(com.facebook.buck.util.cache.NullFileHashCache) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) FileHashCache(com.facebook.buck.util.cache.FileHashCache) RuleKey(com.facebook.buck.rules.RuleKey) AddToRuleKey(com.facebook.buck.rules.AddToRuleKey) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildTarget(com.facebook.buck.model.BuildTarget) NullFileHashCache(com.facebook.buck.util.cache.NullFileHashCache) NoopBuildRule(com.facebook.buck.rules.NoopBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 38 with FileHashCache

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

the class DefaultRuleKeyFactoryTest method annotatedAppendableBuildRulesIncludeTheirRuleKey.

@Test
public void annotatedAppendableBuildRulesIncludeTheirRuleKey() {
    BuildTarget target = BuildTargetFactory.newInstance("//cheese:peas");
    BuildTarget depTarget = BuildTargetFactory.newInstance("//cheese:more-peas");
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()));
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    BuildRule rule = new EmptyRule(target);
    FileHashCache fileHashCache = new NullFileHashCache();
    DefaultRuleKeyFactory factory = new DefaultRuleKeyFactory(0, fileHashCache, pathResolver, ruleFinder);
    class AppendableRule extends EmptyRule {

        public AppendableRule(BuildTarget target) {
            super(target);
        }

        @Override
        public void appendToRuleKey(RuleKeyObjectSink sink) {
            sink.setReflectively("cheese", "brie");
        }
    }
    AppendableRule appendableRule = new AppendableRule(depTarget);
    RuleKey appendableSubKey = new UncachedRuleKeyBuilder(ruleFinder, pathResolver, fileHashCache, factory).setReflectively("cheese", "brie").build();
    RuleKey ruleSubKey = factory.build(appendableRule);
    RuleKeyBuilder<RuleKeyResult<RuleKey>> builder = factory.newBuilderForTesting(rule);
    try (RuleKeyScopedHasher.Scope keyScope = builder.getScopedHasher().keyScope("field")) {
        try (RuleKeyScopedHasher.Scope appendableScope = builder.getScopedHasher().wrapperScope(RuleKeyHasher.Wrapper.BUILD_RULE)) {
            builder.getScopedHasher().getHasher().putRuleKey(ruleSubKey);
        }
    }
    RuleKey expected = builder.build().result;
    class RuleContainingAppendableRule extends EmptyRule {

        @AddToRuleKey
        private final AppendableRule field;

        public RuleContainingAppendableRule(BuildTarget target, AppendableRule appendableRule) {
            super(target);
            this.field = appendableRule;
        }
    }
    RuleKey seen = factory.build(new RuleContainingAppendableRule(target, appendableRule));
    assertEquals(expected, seen);
}
Also used : DefaultFileHashCache(com.facebook.buck.util.cache.DefaultFileHashCache) NullFileHashCache(com.facebook.buck.util.cache.NullFileHashCache) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) FileHashCache(com.facebook.buck.util.cache.FileHashCache) RuleKey(com.facebook.buck.rules.RuleKey) AddToRuleKey(com.facebook.buck.rules.AddToRuleKey) RuleKeyObjectSink(com.facebook.buck.rules.RuleKeyObjectSink) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildTarget(com.facebook.buck.model.BuildTarget) NullFileHashCache(com.facebook.buck.util.cache.NullFileHashCache) NoopBuildRule(com.facebook.buck.rules.NoopBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 39 with FileHashCache

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

the class InputBasedRuleKeyFactoryTest method ruleKeysForUndersizedRules.

@Test
public void ruleKeysForUndersizedRules() throws Exception {
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    RuleKeyFieldLoader fieldLoader = new RuleKeyFieldLoader(0);
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    FileHashCache hashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
    final int sizeLimit = 200;
    InputBasedRuleKeyFactory factory = new InputBasedRuleKeyFactory(fieldLoader, hashCache, pathResolver, ruleFinder, sizeLimit);
    // Create a rule that doesn't pass the size limit and verify it creates a rule key.
    final int smallEnoughRuleSize = 100;
    assertThat(smallEnoughRuleSize, Matchers.lessThan(sizeLimit));
    Path input = filesystem.getPath("small_enough_input");
    filesystem.writeBytesToPath(new byte[smallEnoughRuleSize], input);
    BuildRule smallEnoughRule = ExportFileBuilder.newExportFileBuilder(BuildTargetFactory.newInstance("//:small_rule")).setOut("out").setSrc(new PathSourcePath(filesystem, input)).build(resolver, filesystem);
    factory.build(smallEnoughRule);
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) 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) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) FakeBuildRule(com.facebook.buck.rules.FakeBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) NoopBuildRule(com.facebook.buck.rules.NoopBuildRule) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 40 with FileHashCache

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

the class InputBasedRuleKeyFactoryTest method ruleKeyNotCalculatedIfSizeLimitHit.

@Test
public void ruleKeyNotCalculatedIfSizeLimitHit() throws Exception {
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    RuleKeyFieldLoader fieldLoader = new RuleKeyFieldLoader(0);
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    FileHashCache hashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
    // Create input that passes size limit.
    Path input = filesystem.getPath("input");
    filesystem.writeBytesToPath(new byte[1024], input);
    // Construct rule which uses input.
    BuildRule rule = ExportFileBuilder.newExportFileBuilder(BuildTargetFactory.newInstance("//:rule")).setOut("out").setSrc(new PathSourcePath(filesystem, input)).build(resolver, filesystem);
    // Verify rule key isn't calculated.
    expectedException.expect(SizeLimiter.SizeLimitException.class);
    new InputBasedRuleKeyFactory(fieldLoader, hashCache, pathResolver, ruleFinder, 200).build(rule);
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) 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) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) FakeBuildRule(com.facebook.buck.rules.FakeBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) NoopBuildRule(com.facebook.buck.rules.NoopBuildRule) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) 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