use of com.facebook.buck.rules.keys.DefaultRuleKeyFactory in project buck by facebook.
the class RuleKeyTest method testRuleKeyDependsOnDeps.
/**
* Ensure that build rules with the same inputs but different deps have unique RuleKeys.
*/
@Test
public void testRuleKeyDependsOnDeps() throws Exception {
FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
FileHashCache hashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
BuildRuleResolver ruleResolver1 = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
BuildRuleResolver ruleResolver2 = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder1 = new SourcePathRuleFinder(ruleResolver1);
DefaultRuleKeyFactory ruleKeyFactory = new DefaultRuleKeyFactory(0, hashCache, new SourcePathResolver(ruleFinder1), ruleFinder1);
SourcePathRuleFinder ruleFinder2 = new SourcePathRuleFinder(ruleResolver2);
DefaultRuleKeyFactory ruleKeyFactory2 = new DefaultRuleKeyFactory(0, hashCache, new SourcePathResolver(ruleFinder2), ruleFinder2);
// Create a dependent build rule, //src/com/facebook/buck/cli:common.
JavaLibraryBuilder builder = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//src/com/facebook/buck/cli:common"));
BuildRule commonJavaLibrary = builder.build(ruleResolver1);
builder.build(ruleResolver2);
// Create a java_library() rule with no deps.
Path mainSrc = Paths.get("src/com/facebook/buck/cli/Main.java");
filesystem.mkdirs(mainSrc.getParent());
filesystem.writeContentsToPath("hello", mainSrc);
JavaLibraryBuilder javaLibraryBuilder = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//src/com/facebook/buck/cli:cli")).addSrc(mainSrc);
BuildRule libraryNoCommon = javaLibraryBuilder.build(ruleResolver1, filesystem);
// Create the same java_library() rule, but with a dep on //src/com/facebook/buck/cli:common.
javaLibraryBuilder.addDep(commonJavaLibrary.getBuildTarget());
BuildRule libraryWithCommon = javaLibraryBuilder.build(ruleResolver2, filesystem);
// Assert that the RuleKeys are distinct.
RuleKey r1 = ruleKeyFactory.build(libraryNoCommon);
RuleKey r2 = ruleKeyFactory2.build(libraryWithCommon);
assertThat("Rule keys should be distinct because the deps of the rules are different.", r1, not(equalTo(r2)));
}
use of com.facebook.buck.rules.keys.DefaultRuleKeyFactory in project buck by facebook.
the class RuleKeyTest method declaredDepsAndExtraDepsGenerateDifferentRuleKeys.
@Test
public void declaredDepsAndExtraDepsGenerateDifferentRuleKeys() {
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()));
SourcePathResolver sourcePathResolver = new SourcePathResolver(ruleFinder);
FileHashCache hashCache = new FakeFileHashCache(ImmutableMap.of());
DefaultRuleKeyFactory ruleKeyFactory = new DefaultRuleKeyFactory(0, hashCache, sourcePathResolver, ruleFinder);
BuildTarget target = BuildTargetFactory.newInstance("//a:target");
BuildTarget depTarget = BuildTargetFactory.newInstance("//some:dep");
BuildRuleParams depParams = new FakeBuildRuleParamsBuilder(depTarget).build();
NoopBuildRule dep = new NoopBuildRule(depParams);
BuildRuleParams paramsWithDeclaredDep = new FakeBuildRuleParamsBuilder(target).setDeclaredDeps(ImmutableSortedSet.of(dep)).build();
NoopBuildRule ruleWithDeclaredDep = new NoopBuildRule(paramsWithDeclaredDep);
BuildRuleParams paramsWithExtraDep = new FakeBuildRuleParamsBuilder(target).setExtraDeps(ImmutableSortedSet.of(dep)).build();
NoopBuildRule ruleWithExtraDep = new NoopBuildRule(paramsWithExtraDep);
BuildRuleParams paramsWithBothDeps = new FakeBuildRuleParamsBuilder(target).setDeclaredDeps(ImmutableSortedSet.of(dep)).setExtraDeps(ImmutableSortedSet.of(dep)).build();
NoopBuildRule ruleWithBothDeps = new NoopBuildRule(paramsWithBothDeps);
assertNotEquals(ruleKeyFactory.build(ruleWithDeclaredDep), ruleKeyFactory.build(ruleWithExtraDep));
assertNotEquals(ruleKeyFactory.build(ruleWithDeclaredDep), ruleKeyFactory.build(ruleWithBothDeps));
assertNotEquals(ruleKeyFactory.build(ruleWithExtraDep), ruleKeyFactory.build(ruleWithBothDeps));
}
use of com.facebook.buck.rules.keys.DefaultRuleKeyFactory in project buck by facebook.
the class RuleKeyTest method changingRuleKeyFieldOfDepChangesKeyWhenClassImplementsAppendToRuleKey.
@Test
public void changingRuleKeyFieldOfDepChangesKeyWhenClassImplementsAppendToRuleKey() {
BuildTarget target = BuildTargetFactory.newInstance("//cheese:peas");
BuildRuleParams params = new FakeBuildRuleParamsBuilder(target).build();
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()));
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
FileHashCache hashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(new FakeProjectFilesystem())));
BuildRule buildRule1 = new TestRuleKeyAppendableBuildRule(params, "foo", "bar");
BuildRule buildRule2 = new TestRuleKeyAppendableBuildRule(params, "foo", "xyzzy");
BuildTarget parentTarget = BuildTargetFactory.newInstance("//cheese:milk");
BuildRuleParams parentParams1 = new FakeBuildRuleParamsBuilder(parentTarget).setDeclaredDeps(ImmutableSortedSet.of(buildRule1)).build();
BuildRule parentRule1 = new NoopBuildRule(parentParams1);
BuildRuleParams parentParams2 = new FakeBuildRuleParamsBuilder(parentTarget).setDeclaredDeps(ImmutableSortedSet.of(buildRule2)).build();
BuildRule parentRule2 = new NoopBuildRule(parentParams2);
RuleKey ruleKey1 = new DefaultRuleKeyFactory(0, hashCache, pathResolver, ruleFinder).build(parentRule1);
RuleKey ruleKey2 = new DefaultRuleKeyFactory(0, hashCache, pathResolver, ruleFinder).build(parentRule2);
assertNotEquals(ruleKey1, ruleKey2);
}
use of com.facebook.buck.rules.keys.DefaultRuleKeyFactory in project buck by facebook.
the class ToolTest method hashFileToolsCreatedWithTheSamePathAreEqual.
@Test
public void hashFileToolsCreatedWithTheSamePathAreEqual() {
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()));
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
DefaultRuleKeyFactory ruleKeyFactory = new DefaultRuleKeyFactory(0, FakeFileHashCache.createFromStrings(ImmutableMap.<String, String>builder().put("path", Strings.repeat("a", 40)).put("other-path", Strings.repeat("b", 40)).put("same", Strings.repeat("a", 40)).build()), pathResolver, ruleFinder);
Path path = Paths.get("path");
Path otherPath = Paths.get("other-path");
Path same = Paths.get("same");
Tool tool1 = new HashedFileTool(path);
RuleKey tool1RuleKey = createRuleKeyBuilder(ruleKeyFactory, pathResolver).setReflectively("tool", tool1).build().result;
Tool tool2 = new HashedFileTool(path);
RuleKey tool2RuleKey = createRuleKeyBuilder(ruleKeyFactory, pathResolver).setReflectively("tool", tool2).build().result;
// Same name, same sha1
assertEquals(tool1RuleKey, tool2RuleKey);
Tool tool3 = new HashedFileTool(otherPath);
RuleKey tool3RuleKey = createRuleKeyBuilder(ruleKeyFactory, pathResolver).setReflectively("tool", tool3).build().result;
// Different name, different sha1
assertNotEquals(tool1RuleKey, tool3RuleKey);
Tool tool4 = new HashedFileTool(same);
RuleKey tool4RuleKey = createRuleKeyBuilder(ruleKeyFactory, pathResolver).setReflectively("tool", tool4).build().result;
// Different name, same sha1
assertNotEquals(tool1RuleKey, tool4RuleKey);
}
use of com.facebook.buck.rules.keys.DefaultRuleKeyFactory in project buck by facebook.
the class ToolTest method customVersion.
@Test
public void customVersion() {
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()));
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
DefaultRuleKeyFactory ruleKeyFactory = new DefaultRuleKeyFactory(0, FakeFileHashCache.createFromStrings(ImmutableMap.of()), pathResolver, ruleFinder);
String tool = "tool";
String version = "version";
Tool tool1 = VersionedTool.of(Paths.get("something"), tool, version);
RuleKey tool1RuleKey = createRuleKeyBuilder(ruleKeyFactory, pathResolver).setReflectively("tool", tool1).build().result;
Tool tool2 = VersionedTool.of(Paths.get("something-else"), tool, version);
RuleKey tool2RuleKey = createRuleKeyBuilder(ruleKeyFactory, pathResolver).setReflectively("tool", tool2).build().result;
assertEquals(tool1RuleKey, tool2RuleKey);
}
Aggregations