use of com.facebook.buck.rules.AddToRuleKey in project buck by facebook.
the class DefaultRuleKeyFactoryTest method testFactoryReportsInputsAndDependenciesToCacheForRuleKeyAppendable.
@Test
public void testFactoryReportsInputsAndDependenciesToCacheForRuleKeyAppendable() throws IOException {
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()));
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
NoopRuleKeyCache<RuleKey> noopRuleKeyCache = new NoopRuleKeyCache<>();
ProjectFilesystem filesystem = new FakeProjectFilesystem();
DefaultRuleKeyFactory factory = new DefaultRuleKeyFactory(new RuleKeyFieldLoader(0), new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem))), pathResolver, ruleFinder, noopRuleKeyCache);
// Create a sample input.
PathSourcePath input = new PathSourcePath(filesystem, filesystem.getPath("input"));
filesystem.touch(input.getRelativePath());
// Create a sample dep rule.
BuildRule dep = new EmptyRule(BuildTargetFactory.newInstance("//:dep"));
// Create a sample dep appendable.
RuleKeyAppendable depAppendable = sink -> {
};
// Create a sample rule key appendable.
RuleKeyAppendable appendable = sink -> {
sink.setReflectively("input", input);
sink.setReflectively("dep", dep);
sink.setReflectively("depAppendable", depAppendable);
};
// Create a dummy build rule that uses the input.
BuildRule rule = new NoopBuildRule(new FakeBuildRuleParamsBuilder("//:target").setProjectFilesystem(filesystem).build()) {
@AddToRuleKey
private final RuleKeyAppendable appendableField = appendable;
};
// Build the rule key.
factory.build(rule);
// Verify the input was properly reported to the rule key cache.
RuleKeyResult<RuleKey> result = noopRuleKeyCache.results.get(appendable);
assertThat(result, Matchers.notNullValue());
assertThat(result.inputs, Matchers.containsInAnyOrder(RuleKeyInput.of(filesystem, input.getRelativePath())));
assertThat(result.deps, Matchers.containsInAnyOrder(dep, depAppendable));
}
use of com.facebook.buck.rules.AddToRuleKey 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.rules.AddToRuleKey in project buck by facebook.
the class DefaultRuleKeyFactoryTest method shouldAllowAFieldToBeStringified.
@Test
public void shouldAllowAFieldToBeStringified() {
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);
DefaultRuleKeyFactory factory = new DefaultRuleKeyFactory(0, new NullFileHashCache(), pathResolver, ruleFinder);
RuleKeyBuilder<RuleKeyResult<RuleKey>> builder = factory.newBuilderForTesting(rule);
builder.setReflectively("field", "sausages");
RuleKey expected = builder.build().result;
class Stringifiable {
@Override
public String toString() {
return "sausages";
}
}
class StringifiedField extends EmptyRule {
@AddToRuleKey(stringify = true)
private Stringifiable field = new Stringifiable();
public StringifiedField(BuildTarget target) {
super(target);
}
}
RuleKey seen = factory.build(new StringifiedField(target));
assertEquals(expected, seen);
}
use of com.facebook.buck.rules.AddToRuleKey in project buck by facebook.
the class DefaultRuleKeyFactoryTest method fieldsFromParentClassesAreAlsoAdded.
@Test
public void fieldsFromParentClassesAreAlsoAdded() {
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);
DefaultRuleKeyFactory factory = new DefaultRuleKeyFactory(0, new NullFileHashCache(), pathResolver, ruleFinder);
RuleKeyBuilder<RuleKeyResult<RuleKey>> builder = factory.newBuilderForTesting(rule);
builder.setReflectively("key", "child");
builder.setReflectively("key", "parent");
RuleKey expected = builder.build().result;
class Parent extends EmptyRule {
@AddToRuleKey
private String key = "parent";
public Parent(BuildTarget target) {
super(target);
}
}
class Child extends Parent {
@AddToRuleKey
private String key = "child";
public Child(BuildTarget target) {
super(target);
}
}
RuleKey seen = factory.build(new Child(target));
assertEquals(expected, seen);
}
use of com.facebook.buck.rules.AddToRuleKey in project buck by facebook.
the class DefaultRuleKeyFactoryTest method testFactoryReportsInputsAndDependenciesToCacheForBuildRule.
@Test
public void testFactoryReportsInputsAndDependenciesToCacheForBuildRule() throws IOException {
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()));
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
NoopRuleKeyCache<RuleKey> noopRuleKeyCache = new NoopRuleKeyCache<>();
ProjectFilesystem filesystem = new FakeProjectFilesystem();
DefaultRuleKeyFactory factory = new DefaultRuleKeyFactory(new RuleKeyFieldLoader(0), new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem))), pathResolver, ruleFinder, noopRuleKeyCache);
// Create a sample input.
PathSourcePath input = new PathSourcePath(filesystem, filesystem.getPath("input"));
filesystem.touch(input.getRelativePath());
// Create a sample dep rule.
BuildRule dep = new EmptyRule(BuildTargetFactory.newInstance("//:dep"));
// Create a sample rule key appendable.
RuleKeyAppendable appendable = sink -> {
};
// Create a dummy build rule that uses the input.
BuildRule rule = new NoopBuildRule(new FakeBuildRuleParamsBuilder("//:target").setProjectFilesystem(filesystem).setDeclaredDeps(ImmutableSortedSet.of(dep)).build()) {
@AddToRuleKey
private final SourcePath inputField = input;
@AddToRuleKey
private final RuleKeyAppendable appendableField = appendable;
};
// Build the rule key.
factory.build(rule);
// Verify the input was properly reported to the rule key cache.
RuleKeyResult<RuleKey> result = noopRuleKeyCache.results.get(rule);
assertThat(result, Matchers.notNullValue());
assertThat(result.inputs, Matchers.containsInAnyOrder(RuleKeyInput.of(filesystem, input.getRelativePath())));
assertThat(result.deps, Matchers.containsInAnyOrder(dep, appendable));
}
Aggregations