use of com.facebook.buck.rules.TargetGraph in project buck by facebook.
the class DefaultJavaLibraryTest method testExportedDeps.
@Test
public void testExportedDeps() throws Exception {
ProjectFilesystem filesystem = new FakeProjectFilesystem();
BuildTarget nonIncludedTarget = BuildTargetFactory.newInstance("//:not_included");
TargetNode<?, ?> notIncludedNode = JavaLibraryBuilder.createBuilder(nonIncludedTarget).addSrc(Paths.get("java/src/com/not_included/Raz.java")).build();
BuildTarget includedTarget = BuildTargetFactory.newInstance("//:included");
TargetNode<?, ?> includedNode = JavaLibraryBuilder.createBuilder(includedTarget).addSrc(Paths.get("java/src/com/included/Rofl.java")).build();
BuildTarget libraryOneTarget = BuildTargetFactory.newInstance("//:libone");
TargetNode<?, ?> libraryOneNode = JavaLibraryBuilder.createBuilder(libraryOneTarget).addDep(notIncludedNode.getBuildTarget()).addDep(includedNode.getBuildTarget()).addExportedDep(includedNode.getBuildTarget()).addSrc(Paths.get("java/src/com/libone/Bar.java")).build();
BuildTarget libraryTwoTarget = BuildTargetFactory.newInstance("//:libtwo");
TargetNode<?, ?> libraryTwoNode = JavaLibraryBuilder.createBuilder(libraryTwoTarget).addSrc(Paths.get("java/src/com/libtwo/Foo.java")).addDep(libraryOneNode.getBuildTarget()).addExportedDep(libraryOneNode.getBuildTarget()).build();
BuildTarget parentTarget = BuildTargetFactory.newInstance("//:parent");
TargetNode<?, ?> parentNode = JavaLibraryBuilder.createBuilder(parentTarget).addSrc(Paths.get("java/src/com/parent/Meh.java")).addDep(libraryTwoNode.getBuildTarget()).build();
TargetGraph targetGraph = TargetGraphFactory.newInstance(notIncludedNode, includedNode, libraryOneNode, libraryTwoNode, parentNode);
ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(ruleResolver));
BuildRule notIncluded = ruleResolver.requireRule(notIncludedNode.getBuildTarget());
BuildRule included = ruleResolver.requireRule(includedNode.getBuildTarget());
BuildRule libraryOne = ruleResolver.requireRule(libraryOneTarget);
BuildRule libraryTwo = ruleResolver.requireRule(libraryTwoTarget);
BuildRule parent = ruleResolver.requireRule(parentTarget);
Path root = parent.getProjectFilesystem().getRootPath();
assertEquals("A java_library that depends on //:libone should include only libone.jar in its " + "classpath when compiling itself.", ImmutableSet.of(root.resolve(DefaultJavaLibrary.getOutputJarPath(nonIncludedTarget, filesystem))), resolve(getJavaLibrary(notIncluded).getOutputClasspaths(), pathResolver));
assertEquals(ImmutableSet.of(root.resolve(DefaultJavaLibrary.getOutputJarPath(includedTarget, filesystem))), resolve(getJavaLibrary(included).getOutputClasspaths(), pathResolver));
assertEquals(ImmutableSet.of(root.resolve(DefaultJavaLibrary.getOutputJarPath(includedTarget, filesystem)), root.resolve(DefaultJavaLibrary.getOutputJarPath(libraryOneTarget, filesystem)), root.resolve(DefaultJavaLibrary.getOutputJarPath(includedTarget, filesystem))), resolve(getJavaLibrary(libraryOne).getOutputClasspaths(), pathResolver));
assertEquals("//:libtwo exports its deps, so a java_library that depends on //:libtwo should include " + "both libone.jar and libtwo.jar in its classpath when compiling itself.", ImmutableSet.of(root.resolve(DefaultJavaLibrary.getOutputJarPath(libraryOneTarget, filesystem)), root.resolve(DefaultJavaLibrary.getOutputJarPath(includedTarget, filesystem)), root.resolve(DefaultJavaLibrary.getOutputJarPath(libraryOneTarget, filesystem)), root.resolve(DefaultJavaLibrary.getOutputJarPath(libraryTwoTarget, filesystem)), root.resolve(DefaultJavaLibrary.getOutputJarPath(includedTarget, filesystem))), resolve(getJavaLibrary(libraryTwo).getOutputClasspaths(), pathResolver));
assertEquals("A java_binary that depends on //:parent should include libone.jar, libtwo.jar and " + "parent.jar.", ImmutableSet.<Path>builder().add(root.resolve(DefaultJavaLibrary.getOutputJarPath(includedTarget, filesystem)), root.resolve(DefaultJavaLibrary.getOutputJarPath(nonIncludedTarget, filesystem)), root.resolve(DefaultJavaLibrary.getOutputJarPath(libraryOneTarget, filesystem)), root.resolve(DefaultJavaLibrary.getOutputJarPath(libraryTwoTarget, filesystem)), root.resolve(DefaultJavaLibrary.getOutputJarPath(parentTarget, filesystem))).build(), resolve(getJavaLibrary(parent).getTransitiveClasspaths(), pathResolver));
assertThat(getJavaLibrary(parent).getTransitiveClasspathDeps(), equalTo(ImmutableSet.<JavaLibrary>builder().add(getJavaLibrary(included)).add(getJavaLibrary(notIncluded)).add(getJavaLibrary(libraryOne)).add(getJavaLibrary(libraryTwo)).add(getJavaLibrary(parent)).build()));
assertEquals("A java_library that depends on //:parent should include only parent.jar in its " + "-classpath when compiling itself.", ImmutableSet.of(root.resolve(DefaultJavaLibrary.getOutputJarPath(parentTarget, filesystem))), resolve(getJavaLibrary(parent).getOutputClasspaths(), pathResolver));
}
use of com.facebook.buck.rules.TargetGraph in project buck by facebook.
the class DefaultJavaLibraryTest method testInputBasedRuleKeyWithRecursiveExportedDeps.
/**
* Tests that input-based rule keys work properly with a Java library dep exported through
* multiple Java library dependencies.
*/
@Test
public void testInputBasedRuleKeyWithRecursiveExportedDeps() throws Exception {
ProjectFilesystem filesystem = new FakeProjectFilesystem();
// Setup a Java library which builds against another Java library dep exporting another Java
// library dep.
TargetNode<JavaLibraryDescription.Arg, ?> exportedDepNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:edep"), filesystem).addSrc(Paths.get("Source1.java")).build();
TargetNode<?, ?> dep2Node = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:dep2"), filesystem).addExportedDep(exportedDepNode.getBuildTarget()).build();
TargetNode<?, ?> dep1Node = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:dep1"), filesystem).addExportedDep(dep2Node.getBuildTarget()).build();
TargetNode<?, ?> libraryNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:lib"), filesystem).addDep(dep1Node.getBuildTarget()).build();
TargetGraph targetGraph = TargetGraphFactory.newInstance(exportedDepNode, dep2Node, dep1Node, libraryNode);
ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
JavaLibrary exportedDep = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:edep"));
JavaLibrary library = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:lib"));
filesystem.writeContentsToPath("JAR contents", pathResolver.getRelativePath(exportedDep.getSourcePathToOutput()));
writeAbiJar(filesystem, pathResolver.getRelativePath(ruleResolver.requireRule(exportedDep.getAbiJar().get()).getSourcePathToOutput()), "Source1.class", "ABI JAR contents");
FileHashCache originalHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
InputBasedRuleKeyFactory factory = new InputBasedRuleKeyFactory(0, originalHashCache, pathResolver, ruleFinder);
RuleKey originalRuleKey = factory.build(library);
// Now change the exported Java library dependency such that its rule key changes, and change
// its JAR contents, but keep its ABI JAR the same. This should *not* affect the input-based
// rule key of the consuming java library, since it only cares about the contents of the ABI
// JAR.
exportedDepNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:edep"), filesystem).addSrc(Paths.get("Source1.java")).setResourcesRoot(Paths.get("some root that changes the rule key")).build();
targetGraph = TargetGraphFactory.newInstance(exportedDepNode, dep2Node, dep1Node, libraryNode);
ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
ruleFinder = new SourcePathRuleFinder(ruleResolver);
pathResolver = new SourcePathResolver(ruleFinder);
exportedDep = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:edep"));
library = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:lib"));
filesystem.writeContentsToPath("different JAR contents", pathResolver.getRelativePath(exportedDep.getSourcePathToOutput()));
FileHashCache unaffectedHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
factory = new InputBasedRuleKeyFactory(0, unaffectedHashCache, pathResolver, ruleFinder);
RuleKey unaffectedRuleKey = factory.build(library);
assertThat(originalRuleKey, equalTo(unaffectedRuleKey));
// Now actually change the exproted Java library dependency's ABI JAR. This *should* affect
// the input-based rule key of the consuming java library.
ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
ruleFinder = new SourcePathRuleFinder(ruleResolver);
pathResolver = new SourcePathResolver(ruleFinder);
exportedDep = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:edep"));
library = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:lib"));
writeAbiJar(filesystem, pathResolver.getRelativePath(ruleResolver.requireRule(exportedDep.getAbiJar().get()).getSourcePathToOutput()), "Source1.class", "changed ABI JAR contents");
FileHashCache affectedHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
factory = new InputBasedRuleKeyFactory(0, affectedHashCache, pathResolver, ruleFinder);
RuleKey affectedRuleKey = factory.build(library);
assertThat(originalRuleKey, Matchers.not(equalTo(affectedRuleKey)));
}
use of com.facebook.buck.rules.TargetGraph in project buck by facebook.
the class DefaultJavaLibraryTest method testInputBasedRuleKeyWithExportedDeps.
/**
* Tests that input-based rule keys work properly with a Java library dep exported by a
* first-order dep.
*/
@Test
public void testInputBasedRuleKeyWithExportedDeps() throws Exception {
ProjectFilesystem filesystem = new FakeProjectFilesystem();
// Setup a Java library which builds against another Java library dep exporting another Java
// library dep.
TargetNode<JavaLibraryDescription.Arg, ?> exportedDepNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:edep"), filesystem).addSrc(Paths.get("Source1.java")).build();
TargetNode<?, ?> depNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:dep"), filesystem).addExportedDep(exportedDepNode.getBuildTarget()).build();
TargetNode<?, ?> libraryNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:lib"), filesystem).addDep(depNode.getBuildTarget()).build();
TargetGraph targetGraph = TargetGraphFactory.newInstance(exportedDepNode, depNode, libraryNode);
ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
JavaLibrary exportedDep = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:edep"));
JavaLibrary library = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:lib"));
filesystem.writeContentsToPath("JAR contents", pathResolver.getRelativePath(exportedDep.getSourcePathToOutput()));
writeAbiJar(filesystem, pathResolver.getRelativePath(ruleResolver.requireRule(exportedDep.getAbiJar().get()).getSourcePathToOutput()), "Source1.class", "ABI JAR contents");
FileHashCache originalHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
InputBasedRuleKeyFactory factory = new InputBasedRuleKeyFactory(0, originalHashCache, pathResolver, ruleFinder);
RuleKey originalRuleKey = factory.build(library);
// Now change the exported Java library dependency such that its rule key changes, and change
// its JAR contents, but keep its ABI JAR the same. This should *not* affect the input-based
// rule key of the consuming java library, since it only cares about the contents of the ABI
// JAR.
exportedDepNode = JavaLibraryBuilder.createBuilder(BuildTargetFactory.newInstance("//:edep"), filesystem).addSrc(Paths.get("Source1.java")).setResourcesRoot(Paths.get("some root that changes the rule key")).build();
targetGraph = TargetGraphFactory.newInstance(exportedDepNode, depNode, libraryNode);
ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
ruleFinder = new SourcePathRuleFinder(ruleResolver);
pathResolver = new SourcePathResolver(ruleFinder);
exportedDep = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:edep"));
library = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:lib"));
filesystem.writeContentsToPath("different JAR contents", pathResolver.getRelativePath(exportedDep.getSourcePathToOutput()));
FileHashCache unaffectedHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
factory = new InputBasedRuleKeyFactory(0, unaffectedHashCache, pathResolver, ruleFinder);
RuleKey unaffectedRuleKey = factory.build(library);
assertThat(originalRuleKey, equalTo(unaffectedRuleKey));
// Now actually change the exproted Java library dependency's ABI JAR. This *should* affect
// the input-based rule key of the consuming java library.
ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
ruleFinder = new SourcePathRuleFinder(ruleResolver);
pathResolver = new SourcePathResolver(ruleFinder);
exportedDep = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:edep"));
library = (JavaLibrary) ruleResolver.requireRule(BuildTargetFactory.newInstance("//:lib"));
writeAbiJar(filesystem, pathResolver.getRelativePath(ruleResolver.requireRule(exportedDep.getAbiJar().get()).getSourcePathToOutput()), "Source1.class", "changed ABI JAR contents");
FileHashCache affectedHashCache = new StackedFileHashCache(ImmutableList.of(DefaultFileHashCache.createDefaultFileHashCache(filesystem)));
factory = new InputBasedRuleKeyFactory(0, affectedHashCache, pathResolver, ruleFinder);
RuleKey affectedRuleKey = factory.build(library);
assertThat(originalRuleKey, Matchers.not(equalTo(affectedRuleKey)));
}
use of com.facebook.buck.rules.TargetGraph in project buck by facebook.
the class DefaultJavaLibraryTest method testGetClasspathDeps.
@Test
public void testGetClasspathDeps() throws Exception {
BuildTarget libraryOneTarget = BuildTargetFactory.newInstance("//:libone");
TargetNode<?, ?> libraryOne = JavaLibraryBuilder.createBuilder(libraryOneTarget).addSrc(Paths.get("java/src/com/libone/Bar.java")).build();
BuildTarget libraryTwoTarget = BuildTargetFactory.newInstance("//:libtwo");
TargetNode<?, ?> libraryTwo = JavaLibraryBuilder.createBuilder(libraryTwoTarget).addSrc(Paths.get("java/src/com/libtwo/Foo.java")).addDep(libraryOne.getBuildTarget()).build();
BuildTarget parentTarget = BuildTargetFactory.newInstance("//:parent");
TargetNode<?, ?> parent = JavaLibraryBuilder.createBuilder(parentTarget).addSrc(Paths.get("java/src/com/parent/Meh.java")).addDep(libraryTwo.getBuildTarget()).build();
TargetGraph targetGraph = TargetGraphFactory.newInstance(libraryOne, libraryTwo, parent);
ruleResolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
BuildRule libraryOneRule = ruleResolver.requireRule(libraryOneTarget);
BuildRule libraryTwoRule = ruleResolver.requireRule(libraryTwoTarget);
BuildRule parentRule = ruleResolver.requireRule(parentTarget);
assertThat(((HasClasspathEntries) parentRule).getTransitiveClasspathDeps(), equalTo(ImmutableSet.of(getJavaLibrary(libraryOneRule), getJavaLibrary(libraryTwoRule), getJavaLibrary(parentRule))));
}
use of com.facebook.buck.rules.TargetGraph in project buck by facebook.
the class DefaultSuggestBuildRulesTest method suggestTopologicallyDistantDependency.
@Test
public void suggestTopologicallyDistantDependency() throws NoSuchBuildTargetException {
// TODO(grumpyjames): stop duplicating source/symbol names if possible
TargetNode<?, ?> libraryTwoNode = javaLibrary("//:libtwo", "com/facebook/Bar.java");
TargetNode<?, ?> parentNode = javaLibrary("//:parent", "com/facebook/Foo.java", libraryTwoNode);
TargetNode<?, ?> grandparentNode = javaLibrary("//:grandparent", "com/parent/OldManRiver.java", parentNode);
TargetGraph targetGraph = TargetGraphFactory.newInstance(libraryTwoNode, parentNode, grandparentNode);
BuildRuleResolver resolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathResolver pathResolver = new SourcePathResolver(new SourcePathRuleFinder(resolver));
BuildRule libraryTwo = resolver.requireRule(libraryTwoNode.getBuildTarget());
BuildRule parent = resolver.requireRule(parentNode.getBuildTarget());
BuildRule grandparent = resolver.requireRule(grandparentNode.getBuildTarget());
ImmutableMap<Path, String> jarPathToSymbols = ImmutableMap.of(pathResolver.getAbsolutePath(parent.getSourcePathToOutput()), "com.facebook.Foo", pathResolver.getAbsolutePath(libraryTwo.getSourcePathToOutput()), "com.facebook.Bar");
ImmutableSetMultimap<JavaLibrary, Path> transitiveClasspathEntries = fromLibraries(pathResolver, libraryTwo, parent, grandparent);
SuggestBuildRules.JarResolver jarResolver = createJarResolver(jarPathToSymbols);
SuggestBuildRules suggestFn = DefaultSuggestBuildRules.createSuggestBuildFunction(jarResolver, pathResolver, ImmutableSet.of(), transitiveClasspathEntries.keySet(), ImmutableList.of(libraryTwo, parent, grandparent));
final ImmutableSet<String> suggestions = suggestFn.suggest(ImmutableSet.of("com.facebook.Bar"));
assertEquals(ImmutableSet.of("//:libtwo"), suggestions);
}
Aggregations