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)));
}
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);
}
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);
}
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);
}
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);
}
Aggregations