Search in sources :

Example 11 with AddToRuleKey

use of com.facebook.buck.rules.AddToRuleKey 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 12 with AddToRuleKey

use of com.facebook.buck.rules.AddToRuleKey in project buck by facebook.

the class DefaultRuleKeyFactoryTest method fieldsAreAddedInAlphabeticalOrder.

@Test
public void fieldsAreAddedInAlphabeticalOrder() {
    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("alpha", "stilton");
    builder.setReflectively("beta", 1);
    builder.setReflectively("gamma", "stinking bishop");
    RuleKey expected = builder.build().result;
    class UnsortedFields extends EmptyRule {

        @AddToRuleKey
        private String gamma = "stinking bishop";

        @AddToRuleKey
        private int beta = 1;

        @AddToRuleKey
        private String alpha = "stilton";

        public UnsortedFields(BuildTarget target) {
            super(target);
        }
    }
    RuleKey seen = factory.build(new UnsortedFields(target));
    assertEquals(expected, seen);
}
Also used : 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 13 with AddToRuleKey

use of com.facebook.buck.rules.AddToRuleKey in project buck by facebook.

the class InputBasedRuleKeyFactoryTest method ruleKeyChangesIfInputContentsFromBuildTargetSourcePathInRuleKeyAppendableChanges.

@Test
public void ruleKeyChangesIfInputContentsFromBuildTargetSourcePathInRuleKeyAppendableChanges() throws Exception {
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    final FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    final BuildRule dep = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:dep")).setOut("out").build(resolver, filesystem);
    BuildRuleParams params = new FakeBuildRuleParamsBuilder("//:rule").setDeclaredDeps(ImmutableSortedSet.of(dep)).setProjectFilesystem(filesystem).build();
    BuildRule rule = new NoopBuildRule(params) {

        @AddToRuleKey
        RuleKeyAppendableWithInput input = new RuleKeyAppendableWithInput(dep.getSourcePathToOutput());
    };
    // Build a rule key with a particular hash set for the output for the above rule.
    FakeFileHashCache hashCache = new FakeFileHashCache(ImmutableMap.of(pathResolver.getAbsolutePath(Preconditions.checkNotNull(dep.getSourcePathToOutput())), HashCode.fromInt(0)));
    RuleKey inputKey1 = new InputBasedRuleKeyFactory(0, hashCache, pathResolver, ruleFinder).build(rule);
    // Now, build a rule key with a different hash for the output for the above rule.
    hashCache = new FakeFileHashCache(ImmutableMap.of(pathResolver.getAbsolutePath(Preconditions.checkNotNull(dep.getSourcePathToOutput())), HashCode.fromInt(1)));
    RuleKey inputKey2 = new InputBasedRuleKeyFactory(0, hashCache, pathResolver, ruleFinder).build(rule);
    assertThat(inputKey1, Matchers.not(Matchers.equalTo(inputKey2)));
}
Also used : NoopBuildRule(com.facebook.buck.rules.NoopBuildRule) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) RuleKey(com.facebook.buck.rules.RuleKey) AddToRuleKey(com.facebook.buck.rules.AddToRuleKey) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) 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

AddToRuleKey (com.facebook.buck.rules.AddToRuleKey)13 BuildRule (com.facebook.buck.rules.BuildRule)12 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)12 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)12 NoopBuildRule (com.facebook.buck.rules.NoopBuildRule)12 RuleKey (com.facebook.buck.rules.RuleKey)12 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)12 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)12 Test (org.junit.Test)12 BuildTarget (com.facebook.buck.model.BuildTarget)10 NullFileHashCache (com.facebook.buck.util.cache.NullFileHashCache)10 FakeBuildRuleParamsBuilder (com.facebook.buck.rules.FakeBuildRuleParamsBuilder)4 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)4 DefaultFileHashCache (com.facebook.buck.util.cache.DefaultFileHashCache)4 FileHashCache (com.facebook.buck.util.cache.FileHashCache)4 StackedFileHashCache (com.facebook.buck.util.cache.StackedFileHashCache)4 PathSourcePath (com.facebook.buck.rules.PathSourcePath)3 RuleKeyObjectSink (com.facebook.buck.rules.RuleKeyObjectSink)3 SourcePath (com.facebook.buck.rules.SourcePath)3 ImmutableList (com.google.common.collect.ImmutableList)3