use of com.facebook.buck.rules.keys.DefaultRuleKeyFactory in project buck by facebook.
the class HeaderSymlinkTreeWithHeaderMapTest method testSymlinkTreeRuleKeyChangesIfLinkMapChanges.
@Test
public void testSymlinkTreeRuleKeyChangesIfLinkMapChanges() throws Exception {
Path aFile = tmpDir.newFile();
Files.write(aFile, "hello world".getBytes(Charsets.UTF_8));
HeaderSymlinkTreeWithHeaderMap modifiedSymlinkTreeBuildRule = HeaderSymlinkTreeWithHeaderMap.create(buildTarget, projectFilesystem, symlinkTreeRoot, ImmutableMap.of(Paths.get("different/link"), new PathSourcePath(projectFilesystem, MorePaths.relativize(tmpDir.getRoot(), aFile))), ruleFinder);
// Calculate their rule keys and verify they're different.
DefaultFileHashCache hashCache = DefaultFileHashCache.createDefaultFileHashCache(new ProjectFilesystem(tmpDir.getRoot()));
FileHashLoader hashLoader = new StackedFileHashCache(ImmutableList.of(hashCache));
RuleKey key1 = new DefaultRuleKeyFactory(0, hashLoader, resolver, ruleFinder).build(symlinkTreeBuildRule);
RuleKey key2 = new DefaultRuleKeyFactory(0, hashLoader, resolver, ruleFinder).build(modifiedSymlinkTreeBuildRule);
assertNotEquals(key1, key2);
}
use of com.facebook.buck.rules.keys.DefaultRuleKeyFactory in project buck by facebook.
the class HeaderVerificationTest method getRuleKey.
private RuleKey getRuleKey(HeaderVerification headerVerification) {
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()));
SourcePathResolver resolver = new SourcePathResolver(ruleFinder);
FileHashCache fileHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(new FakeProjectFilesystem())));
DefaultRuleKeyFactory factory = new DefaultRuleKeyFactory(0, fileHashCache, resolver, ruleFinder);
RuleKeyBuilder<RuleKey> builder = new UncachedRuleKeyBuilder(ruleFinder, resolver, fileHashCache, factory);
builder.setReflectively("headerVerification", headerVerification);
return builder.build();
}
use of com.facebook.buck.rules.keys.DefaultRuleKeyFactory in project buck by facebook.
the class PrebuiltCxxLibraryTest method testGetNativeLinkWithDep.
@Test
public void testGetNativeLinkWithDep() throws Exception {
ProjectFilesystem filesystem = new FakeProjectFilesystem();
CxxPlatform platform = CxxLibraryBuilder.createDefaultPlatform();
GenruleBuilder genSrcBuilder = GenruleBuilder.newGenruleBuilder(BuildTargetFactory.newInstance("//:gen_libx")).setOut("gen_libx").setCmd("something");
BuildTarget target = BuildTargetFactory.newInstance("//:x");
PrebuiltCxxLibraryBuilder builder = new PrebuiltCxxLibraryBuilder(target).setLibName("x").setLibDir("$(location //:gen_libx)");
TargetGraph targetGraph = TargetGraphFactory.newInstance(genSrcBuilder.build(), builder.build());
BuildRuleResolver resolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
BuildRule genSrc = genSrcBuilder.build(resolver, filesystem, targetGraph);
filesystem.writeContentsToPath("class Test {}", pathResolver.getAbsolutePath(genSrc.getSourcePathToOutput()).resolve("libx.a"));
PrebuiltCxxLibrary lib = (PrebuiltCxxLibrary) builder.build(resolver, filesystem, targetGraph);
lib.getNativeLinkableInput(platform, Linker.LinkableDepType.STATIC);
FileHashCache originalHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
DefaultRuleKeyFactory factory = new DefaultRuleKeyFactory(0, originalHashCache, pathResolver, ruleFinder);
RuleKey ruleKey = factory.build(lib);
assertNotNull(ruleKey);
}
use of com.facebook.buck.rules.keys.DefaultRuleKeyFactory in project buck by facebook.
the class DefaultJavaLibraryTest method testRuleKeyIsOrderInsensitiveForSourcesAndResources.
@Test
public void testRuleKeyIsOrderInsensitiveForSourcesAndResources() throws Exception {
// Note that these filenames were deliberately chosen to have identical hashes to maximize
// the chance of order-sensitivity when being inserted into a HashMap. Just using
// {foo,bar}.{java,txt} resulted in a passing test even for the old broken code.
ProjectFilesystem filesystem = new AllExistingProjectFilesystem() {
@Override
public boolean isDirectory(Path path, LinkOption... linkOptionsk) {
return false;
}
};
BuildRuleResolver resolver1 = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder1 = new SourcePathRuleFinder(resolver1);
SourcePathResolver pathResolver1 = new SourcePathResolver(ruleFinder1);
DefaultJavaLibrary rule1 = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//lib:lib")).addSrc(Paths.get("agifhbkjdec.java")).addSrc(Paths.get("bdeafhkgcji.java")).addSrc(Paths.get("bdehgaifjkc.java")).addSrc(Paths.get("cfiabkjehgd.java")).addResource(new FakeSourcePath("becgkaifhjd.txt")).addResource(new FakeSourcePath("bkhajdifcge.txt")).addResource(new FakeSourcePath("cabfghjekid.txt")).addResource(new FakeSourcePath("chkdbafijge.txt")).build(resolver1, filesystem);
BuildRuleResolver resolver2 = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder2 = new SourcePathRuleFinder(resolver2);
SourcePathResolver pathResolver2 = new SourcePathResolver(ruleFinder2);
DefaultJavaLibrary rule2 = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//lib:lib")).addSrc(Paths.get("cfiabkjehgd.java")).addSrc(Paths.get("bdehgaifjkc.java")).addSrc(Paths.get("bdeafhkgcji.java")).addSrc(Paths.get("agifhbkjdec.java")).addResource(new FakeSourcePath("chkdbafijge.txt")).addResource(new FakeSourcePath("cabfghjekid.txt")).addResource(new FakeSourcePath("bkhajdifcge.txt")).addResource(new FakeSourcePath("becgkaifhjd.txt")).build(resolver2, filesystem);
ImmutableMap.Builder<String, String> fileHashes = ImmutableMap.builder();
for (String filename : ImmutableList.of("agifhbkjdec.java", "bdeafhkgcji.java", "bdehgaifjkc.java", "cfiabkjehgd.java", "becgkaifhjd.txt", "bkhajdifcge.txt", "cabfghjekid.txt", "chkdbafijge.txt")) {
fileHashes.put(filename, Hashing.sha1().hashString(filename, Charsets.UTF_8).toString());
}
DefaultRuleKeyFactory ruleKeyFactory = new DefaultRuleKeyFactory(0, FakeFileHashCache.createFromStrings(fileHashes.build()), pathResolver1, ruleFinder1);
DefaultRuleKeyFactory ruleKeyFactory2 = new DefaultRuleKeyFactory(0, FakeFileHashCache.createFromStrings(fileHashes.build()), pathResolver2, ruleFinder2);
RuleKey key1 = ruleKeyFactory.build(rule1);
RuleKey key2 = ruleKeyFactory2.build(rule2);
assertEquals(key1, key2);
}
use of com.facebook.buck.rules.keys.DefaultRuleKeyFactory in project buck by facebook.
the class JavaLibrarySymbolsFinderTest method onlyNonGeneratedSrcsShouldAffectRuleKey.
@Test
@SuppressWarnings("PMD.PrematureDeclaration")
public void onlyNonGeneratedSrcsShouldAffectRuleKey() throws IOException {
TestDataHelper.createProjectWorkspaceForScenario(this, "java_library_symbols_finder", tmp).setUp();
final ProjectFilesystem projectFilesystem = new ProjectFilesystem(tmp.getRoot());
Function<String, SourcePath> convert = src -> new PathSourcePath(projectFilesystem, Paths.get(src));
SourcePath example1 = convert.apply("Example1.java");
SourcePath example2 = convert.apply("Example2.java");
final BuildTarget fakeBuildTarget = BuildTargetFactory.newInstance("//foo:GenEx.java");
SourcePath generated = new DefaultBuildTargetSourcePath(fakeBuildTarget);
final boolean shouldRecordRequiredSymbols = true;
JavaLibrarySymbolsFinder example1Finder = new JavaLibrarySymbolsFinder(ImmutableSortedSet.of(example1), javaFileParser, shouldRecordRequiredSymbols);
JavaLibrarySymbolsFinder example2Finder = new JavaLibrarySymbolsFinder(ImmutableSortedSet.of(example2), javaFileParser, shouldRecordRequiredSymbols);
JavaLibrarySymbolsFinder example1AndGeneratedSrcFinder = new JavaLibrarySymbolsFinder(ImmutableSortedSet.of(example1, generated), javaFileParser, shouldRecordRequiredSymbols);
// Mock out calls to a SourcePathResolver so we can create a legitimate
// DefaultRuleKeyFactory.
final SourcePathRuleFinder ruleFinder = createMock(SourcePathRuleFinder.class);
final SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
expect(ruleFinder.getRule(anyObject(SourcePath.class))).andAnswer(() -> {
SourcePath input = (SourcePath) EasyMock.getCurrentArguments()[0];
if (input instanceof ExplicitBuildTargetSourcePath) {
return Optional.of(new FakeBuildRule(fakeBuildTarget, pathResolver));
} else {
return Optional.empty();
}
}).anyTimes();
// Calculates the RuleKey for a JavaSymbolsRule with the specified JavaLibrarySymbolsFinder.
final FileHashCache fileHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(projectFilesystem)));
final DefaultRuleKeyFactory ruleKeyFactory = new DefaultRuleKeyFactory(0, fileHashCache, pathResolver, ruleFinder);
Function<JavaLibrarySymbolsFinder, RuleKey> createRuleKey = finder -> {
JavaSymbolsRule javaSymbolsRule = new JavaSymbolsRule(BuildTargetFactory.newInstance("//foo:rule"), finder, ImmutableSortedSet.of(), ObjectMappers.newDefaultInstance(), projectFilesystem);
return ruleKeyFactory.build(javaSymbolsRule);
};
RuleKey key1 = createRuleKey.apply(example1Finder);
RuleKey key2 = createRuleKey.apply(example2Finder);
RuleKey key3 = createRuleKey.apply(example1AndGeneratedSrcFinder);
assertNotNull(key1);
assertNotNull(key2);
assertNotNull(key3);
assertNotEquals("Two instances of a JavaSymbolsRule with different srcs should change the RuleKey.", key1, key2);
assertEquals("Introducing an extra generated .java file to the srcs should not change the RuleKey.", key1, key3);
}
Aggregations