Search in sources :

Example 81 with RuleKey

use of com.facebook.buck.rules.RuleKey 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);
}
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 82 with RuleKey

use of com.facebook.buck.rules.RuleKey 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));
}
Also used : Arrays(java.util.Arrays) BuildRuleType(com.facebook.buck.rules.BuildRuleType) LIBRARY(com.facebook.buck.rules.BuildableProperties.Kind.LIBRARY) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourceRoot(com.facebook.buck.rules.SourceRoot) RuleKeyAppendable(com.facebook.buck.rules.RuleKeyAppendable) Assert.assertThat(org.junit.Assert.assertThat) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) NoopBuildRule(com.facebook.buck.rules.NoopBuildRule) RuleKey(com.facebook.buck.rules.RuleKey) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) Map(java.util.Map) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Objects(com.google.common.base.Objects) RuleKeyObjectSink(com.facebook.buck.rules.RuleKeyObjectSink) Path(java.nio.file.Path) ImmutableSet(com.google.common.collect.ImmutableSet) AddToRuleKey(com.facebook.buck.rules.AddToRuleKey) ImmutableMap(com.google.common.collect.ImmutableMap) BuildableProperties(com.facebook.buck.rules.BuildableProperties) BuildableContext(com.facebook.buck.rules.BuildableContext) TargetGraph(com.facebook.buck.rules.TargetGraph) DefaultFileHashCache(com.facebook.buck.util.cache.DefaultFileHashCache) BuildTarget(com.facebook.buck.model.BuildTarget) Sha1HashCode(com.facebook.buck.util.sha1.Sha1HashCode) PathSourcePath(com.facebook.buck.rules.PathSourcePath) Optional(java.util.Optional) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Pattern(java.util.regex.Pattern) CacheStats(com.google.common.cache.CacheStats) Step(com.facebook.buck.step.Step) SourcePath(com.facebook.buck.rules.SourcePath) Either(com.facebook.buck.model.Either) Function(java.util.function.Function) BuildRule(com.facebook.buck.rules.BuildRule) ImmutableList(com.google.common.collect.ImmutableList) NullFileHashCache(com.facebook.buck.util.cache.NullFileHashCache) BuildTargetFactory(com.facebook.buck.model.BuildTargetFactory) Suppliers(com.google.common.base.Suppliers) Nullable(javax.annotation.Nullable) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) IOException(java.io.IOException) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) Paths(java.nio.file.Paths) FileHashCache(com.facebook.buck.util.cache.FileHashCache) BuildContext(com.facebook.buck.rules.BuildContext) Preconditions(com.google.common.base.Preconditions) MapMaker(com.google.common.collect.MapMaker) Assert.assertEquals(org.junit.Assert.assertEquals) RuleKey(com.facebook.buck.rules.RuleKey) AddToRuleKey(com.facebook.buck.rules.AddToRuleKey) NoopBuildRule(com.facebook.buck.rules.NoopBuildRule) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) NoopBuildRule(com.facebook.buck.rules.NoopBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) RuleKeyAppendable(com.facebook.buck.rules.RuleKeyAppendable) Test(org.junit.Test)

Example 83 with RuleKey

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

the class DefaultRuleKeyFactoryTest method assertValuesGetHashed.

private void assertValuesGetHashed(@Nullable Object val1, @Nullable Object val2) {
    Preconditions.checkArgument(!Objects.equal(val1, val2), "Values should be different!");
    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);
    RuleKey key1 = factory.newBuilderForTesting(rule).setReflectively("key", val1).build().result;
    RuleKey key1again = factory.newBuilderForTesting(rule).setReflectively("key", val1).build().result;
    RuleKey key2 = factory.newBuilderForTesting(rule).setReflectively("key", val2).build().result;
    assertEquals("Rule keys should be same! " + val1, key1, key1again);
    assertNotEquals("Rule keys should be different! " + val1 + " != " + val2, key1, key2);
}
Also used : RuleKey(com.facebook.buck.rules.RuleKey) AddToRuleKey(com.facebook.buck.rules.AddToRuleKey) 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) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver)

Example 84 with RuleKey

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

use of com.facebook.buck.rules.RuleKey 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)

Aggregations

RuleKey (com.facebook.buck.rules.RuleKey)149 Test (org.junit.Test)112 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)71 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)71 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)71 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)70 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)57 Path (java.nio.file.Path)57 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)42 BuildTarget (com.facebook.buck.model.BuildTarget)42 BuildRule (com.facebook.buck.rules.BuildRule)42 LazyPath (com.facebook.buck.io.LazyPath)34 DefaultRuleKeyFactory (com.facebook.buck.rules.keys.DefaultRuleKeyFactory)34 PathSourcePath (com.facebook.buck.rules.PathSourcePath)33 AddToRuleKey (com.facebook.buck.rules.AddToRuleKey)30 SourcePath (com.facebook.buck.rules.SourcePath)28 FakeFileHashCache (com.facebook.buck.testutil.FakeFileHashCache)27 FakeBuildRule (com.facebook.buck.rules.FakeBuildRule)25 IOException (java.io.IOException)25 StackedFileHashCache (com.facebook.buck.util.cache.StackedFileHashCache)23