Search in sources :

Example 51 with RuleKey

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

the class RuleKeyBuilderTest method testNoOp.

@Test
public void testNoOp() {
    RuleKey noop = newBuilder().build();
    assertEquals(noop, calcRuleKey("key", ImmutableList.of()));
    assertEquals(noop, calcRuleKey("key", ImmutableList.of().iterator()));
    assertEquals(noop, calcRuleKey("key", ImmutableMap.of()));
    assertEquals(noop, calcRuleKey("key", ImmutableList.of(IGNORED_RULE)));
    assertEquals(noop, calcRuleKey("key", Suppliers.ofInstance(IGNORED_RULE)));
    assertEquals(noop, calcRuleKey("key", Optional.of(IGNORED_RULE)));
    assertEquals(noop, calcRuleKey("key", Either.ofLeft(IGNORED_RULE)));
    assertEquals(noop, calcRuleKey("key", Either.ofRight(IGNORED_RULE)));
    assertEquals(noop, calcRuleKey("key", IGNORED_RULE));
    assertEquals(noop, calcRuleKey("key", IGNORED_APPENDABLE));
}
Also used : RuleKey(com.facebook.buck.rules.RuleKey) Test(org.junit.Test)

Example 52 with RuleKey

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

the class RuleKeyBuilderTest method newBuilder.

private RuleKeyBuilder<RuleKey> newBuilder() {
    Map<BuildTarget, BuildRule> ruleMap = ImmutableMap.of(TARGET_1, RULE_1, TARGET_2, RULE_2);
    Map<BuildRule, RuleKey> ruleKeyMap = ImmutableMap.of(RULE_1, RULE_KEY_1, RULE_2, RULE_KEY_2);
    Map<RuleKeyAppendable, RuleKey> appendableKeys = ImmutableMap.of(APPENDABLE_1, RULE_KEY_1, APPENDABLE_2, RULE_KEY_2);
    BuildRuleResolver ruleResolver = new FakeBuildRuleResolver(ruleMap);
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    FakeFileHashCache hashCache = new FakeFileHashCache(ImmutableMap.of(FILESYSTEM.resolve(PATH_1), HashCode.fromInt(0), FILESYSTEM.resolve(PATH_2), HashCode.fromInt(42)), ImmutableMap.of(pathResolver.getAbsoluteArchiveMemberPath(ARCHIVE_PATH_1), HashCode.fromInt(0), pathResolver.getAbsoluteArchiveMemberPath(ARCHIVE_PATH_2), HashCode.fromInt(42)), ImmutableMap.of());
    RuleKeyHasher<HashCode> hasher = new GuavaRuleKeyHasher(Hashing.sha1().newHasher());
    return new RuleKeyBuilder<RuleKey>(ruleFinder, pathResolver, hashCache, hasher) {

        @Override
        protected RuleKeyBuilder<RuleKey> setBuildRule(BuildRule rule) {
            if (rule == IGNORED_RULE) {
                return this;
            }
            return setBuildRuleKey(ruleKeyMap.get(rule));
        }

        @Override
        public RuleKeyBuilder<RuleKey> setAppendableRuleKey(RuleKeyAppendable appendable) {
            if (appendable == IGNORED_APPENDABLE) {
                return this;
            }
            return setAppendableRuleKey(appendableKeys.get(appendable));
        }

        @Override
        protected RuleKeyBuilder<RuleKey> setSourcePath(SourcePath sourcePath) throws IOException {
            if (sourcePath instanceof BuildTargetSourcePath) {
                return setSourcePathAsRule((BuildTargetSourcePath<?>) sourcePath);
            } else {
                return setSourcePathDirectly(sourcePath);
            }
        }

        @Override
        protected RuleKeyBuilder<RuleKey> setNonHashingSourcePath(SourcePath sourcePath) {
            return setNonHashingSourcePathDirectly(sourcePath);
        }

        @Override
        public RuleKey build() {
            return buildRuleKey();
        }
    };
}
Also used : RuleKey(com.facebook.buck.rules.RuleKey) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) ArchiveMemberSourcePath(com.facebook.buck.rules.ArchiveMemberSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) Sha1HashCode(com.facebook.buck.util.sha1.Sha1HashCode) HashCode(com.google.common.hash.HashCode) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule) RuleKeyAppendable(com.facebook.buck.rules.RuleKeyAppendable)

Example 53 with RuleKey

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

the class FakeInputBasedRuleKeyFactory method newInstance.

private RuleKeyBuilder<RuleKey> newInstance(final BuildRule buildRule) {
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()));
    SourcePathResolver resolver = new SourcePathResolver(ruleFinder);
    return new RuleKeyBuilder<RuleKey>(ruleFinder, resolver, fileHashCache) {

        @Override
        protected RuleKeyBuilder<RuleKey> setBuildRule(BuildRule rule) {
            return this;
        }

        @Override
        protected RuleKeyBuilder<RuleKey> setReflectively(@Nullable Object val) {
            return this;
        }

        @Override
        public RuleKeyBuilder<RuleKey> setAppendableRuleKey(RuleKeyAppendable appendable) {
            return this;
        }

        @Override
        protected RuleKeyBuilder<RuleKey> setSourcePath(SourcePath sourcePath) {
            return this;
        }

        @Override
        protected RuleKeyBuilder<RuleKey> setNonHashingSourcePath(SourcePath sourcePath) {
            return setNonHashingSourcePathDirectly(sourcePath);
        }

        @Override
        public RuleKey build() {
            if (oversized.contains(buildRule.getBuildTarget())) {
                throw new SizeLimiter.SizeLimitException();
            }
            return ruleKeys.get(buildRule.getBuildTarget());
        }
    };
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) RuleKey(com.facebook.buck.rules.RuleKey) 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) Nullable(javax.annotation.Nullable) RuleKeyAppendable(com.facebook.buck.rules.RuleKeyAppendable)

Example 54 with RuleKey

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

the class DefaultRuleKeyFactoryTest method fieldsFromParentClassesShouldBeAddedAndFieldsRetainOverallAlphabeticalOrdering.

@Test
public void fieldsFromParentClassesShouldBeAddedAndFieldsRetainOverallAlphabeticalOrdering() {
    BuildTarget topLevelTarget = BuildTargetFactory.newInstance("//cheese:peas");
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()));
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    BuildRule rule = new EmptyRule(topLevelTarget);
    DefaultRuleKeyFactory factory = new DefaultRuleKeyFactory(0, new NullFileHashCache(), pathResolver, ruleFinder);
    RuleKeyBuilder<RuleKeyResult<RuleKey>> builder = factory.newBuilderForTesting(rule);
    builder.setReflectively("exoticCheese", "bavarian smoked");
    builder.setReflectively("target", topLevelTarget);
    RuleKey expected = builder.build().result;
    class Parent extends EmptyRule {

        @AddToRuleKey
        private BuildTarget target;

        public Parent(BuildTarget target) {
            super(target);
            this.target = target;
        }
    }
    class Child extends Parent {

        @AddToRuleKey
        private String exoticCheese = "bavarian smoked";

        public Child(BuildTarget target) {
            super(target);
        }
    }
    RuleKey seen = factory.build(new Child(topLevelTarget));
    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 55 with RuleKey

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

the class DefaultRuleKeyFactoryTest method testBothKeysAndValuesGetHashed.

@Test
public void testBothKeysAndValuesGetHashed() {
    assertKeysGetHashed(null);
    assertBothKeysAndValuesGetHashed(true, false);
    assertBothKeysAndValuesGetHashed((double) 123, (double) 42);
    assertBothKeysAndValuesGetHashed((float) 123, (float) 42);
    // (int)
    assertBothKeysAndValuesGetHashed(123, 42);
    assertBothKeysAndValuesGetHashed((long) 123, (long) 42);
    assertBothKeysAndValuesGetHashed((short) 123, (short) 42);
    assertBothKeysAndValuesGetHashed((byte) 123, (byte) 42);
    assertBothKeysAndValuesGetHashed(new byte[] { 1, 2, 3 }, new byte[] { 4, 2 });
    assertBothKeysAndValuesGetHashed(DummyEnum.BLACK, DummyEnum.WHITE);
    assertBothKeysAndValuesGetHashed("abc", "def");
    assertBothKeysAndValuesGetHashed(Pattern.compile("regex1"), Pattern.compile("regex2"));
    assertBothKeysAndValuesGetHashed(Sha1HashCode.of("a002b39af204cdfaa5fdb67816b13867c32ac52c"), Sha1HashCode.of("b67816b13867c32ac52ca002b39af204cdfaa5fd"));
    assertBothKeysAndValuesGetHashed(new RuleKey("a002b39af204cdfaa5fdb67816b13867c32ac52c"), new RuleKey("b67816b13867c32ac52ca002b39af204cdfaa5fd"));
    assertBothKeysAndValuesGetHashed(BuildRuleType.of("rule_type"), BuildRuleType.of("type2"));
    assertBothKeysAndValuesGetHashed(BuildTargetFactory.newInstance(Paths.get("/root"), "//example/base:one"), BuildTargetFactory.newInstance(Paths.get("/root"), "//example/base:two"));
    assertBothKeysAndValuesGetHashed(new SourceRoot("root1"), new SourceRoot("root2"));
    // wrapper types
    assertBothKeysAndValuesGetHashed(Optional.of("abc"), Optional.of("def"));
    assertBothKeysAndValuesGetHashed(Either.ofLeft("abc"), Either.ofLeft("def"));
    assertBothKeysAndValuesGetHashed(Either.ofRight("def"), Either.ofRight("ghi"));
    assertBothKeysAndValuesGetHashed(Suppliers.ofInstance("abc"), Suppliers.ofInstance("def"));
    // iterables & maps
    assertBothKeysAndValuesGetHashed(Arrays.asList(1, 2, 3), Arrays.asList(4, 2));
    assertBothKeysAndValuesGetHashed(ImmutableList.of("abc", "xy"), ImmutableList.of("xy", "abc"));
    assertBothKeysAndValuesGetHashed(ImmutableMap.of("key", "v1"), ImmutableMap.of("key", "v2"));
    // nested
    assertBothKeysAndValuesGetHashed(ImmutableMap.of("key", Optional.of(ImmutableList.of(1, 2, 3))), ImmutableMap.of("key", Optional.of(ImmutableList.of(1, 2, 4))));
}
Also used : RuleKey(com.facebook.buck.rules.RuleKey) AddToRuleKey(com.facebook.buck.rules.AddToRuleKey) SourceRoot(com.facebook.buck.rules.SourceRoot) 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