use of com.facebook.buck.rules.TargetNode in project buck by facebook.
the class TargetsCommandTest method buildTargetNodes.
private Iterable<TargetNode<?, ?>> buildTargetNodes(ProjectFilesystem filesystem, String buildTarget) {
SortedSet<TargetNode<?, ?>> buildRules = Sets.newTreeSet();
BuildTarget target = BuildTargetFactory.newInstance(filesystem, buildTarget);
TargetNode<?, ?> node = JavaLibraryBuilder.createBuilder(target).build();
buildRules.add(node);
return buildRules;
}
use of com.facebook.buck.rules.TargetNode in project buck by facebook.
the class TargetsCommandTest method testGetMatchingAppleTestBuildTarget.
@Test
public void testGetMatchingAppleTestBuildTarget() throws CmdLineException, IOException {
BuildTarget libraryTarget = BuildTargetFactory.newInstance("//foo:lib");
TargetNode<?, ?> libraryNode = AppleLibraryBuilder.createBuilder(libraryTarget).setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("foo/foo.m")))).build();
BuildTarget testTarget = BuildTargetFactory.newInstance("//foo:xctest");
TargetNode<?, ?> testNode = AppleTestBuilder.createBuilder(testTarget).setSrcs(ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("foo/testfoo.m")))).setDeps(ImmutableSortedSet.of(libraryTarget)).build();
ImmutableSet<TargetNode<?, ?>> nodes = ImmutableSet.of(libraryNode, testNode);
TargetGraph targetGraph = TargetGraphFactory.newInstance(nodes);
// No target depends on the referenced file.
SortedMap<String, TargetNode<?, ?>> matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.of(ImmutableSet.of(Paths.get("foo/bar.m"))), Optional.empty(), Optional.empty(), false, "BUCK");
assertTrue(matchingBuildRules.isEmpty());
// Both AppleLibrary nodes, AppleBundle, and AppleTest match the referenced file.
matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.of(ImmutableSet.of(Paths.get("foo/foo.m"))), Optional.empty(), Optional.empty(), false, "BUCK");
assertEquals(ImmutableSet.of("//foo:lib", "//foo:xctest"), matchingBuildRules.keySet());
// The test AppleLibrary, AppleBundle and AppleTest match the referenced file.
matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.of(ImmutableSet.of(Paths.get("foo/testfoo.m"))), Optional.empty(), Optional.empty(), false, "BUCK");
assertEquals(ImmutableSet.of("//foo:xctest"), matchingBuildRules.keySet());
}
use of com.facebook.buck.rules.TargetNode in project buck by facebook.
the class TargetsCommandTest method testPathsUnderDirectories.
@Test
public void testPathsUnderDirectories() throws CmdLineException, IOException {
ProjectFilesystem projectFilesystem = new FakeProjectFilesystem();
Path resDir = Paths.get("some/resources/dir");
BuildTarget androidResourceTarget = BuildTargetFactory.newInstance("//:res");
TargetNode<?, ?> androidResourceNode = AndroidResourceBuilder.createBuilder(androidResourceTarget).setRes(resDir).build();
Path genSrc = resDir.resolve("foo.txt");
BuildTarget genTarget = BuildTargetFactory.newInstance("//:gen");
TargetNode<?, ?> genNode = GenruleBuilder.newGenruleBuilder(genTarget).setSrcs(ImmutableList.of(new PathSourcePath(projectFilesystem, genSrc))).build();
TargetGraph targetGraph = TargetGraphFactory.newInstance(androidResourceNode, genNode);
SortedMap<String, TargetNode<?, ?>> matchingBuildRules;
// Specifying a resource under the resource directory causes a match.
matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.of(ImmutableSet.of(resDir.resolve("some_resource.txt"))), Optional.empty(), Optional.empty(), false, "BUCK");
assertEquals(ImmutableSet.of(androidResourceTarget.toString()), matchingBuildRules.keySet());
// Specifying a resource with the same string-like common prefix, but not under the above
// resource dir, should not trigger a match.
matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.of(ImmutableSet.of(Paths.get(resDir.toString() + "_extra").resolve("some_resource.txt"))), Optional.empty(), Optional.empty(), false, "BUCK");
assertTrue(matchingBuildRules.isEmpty());
// Specifying a resource with the same string-like common prefix, but not under the above
// resource dir, should not trigger a match.
matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.of(ImmutableSet.of(genSrc)), Optional.empty(), Optional.empty(), false, "BUCK");
assertEquals(ImmutableSet.of(androidResourceTarget.toString(), genTarget.toString()), matchingBuildRules.keySet());
}
use of com.facebook.buck.rules.TargetNode in project buck by facebook.
the class TargetsCommandTest method testJsonOutputForBuildTarget.
@Test
public void testJsonOutputForBuildTarget() throws IOException, BuildFileParseException, InterruptedException {
// run `buck targets` on the build file and parse the observed JSON.
Iterable<TargetNode<?, ?>> nodes = buildTargetNodes(filesystem, "//:test-library");
targetsCommand.printJsonForTargets(params, executor, nodes, ImmutableMap.of(), ImmutableSet.of());
String observedOutput = console.getTextWrittenToStdOut();
JsonNode observed = objectMapper.readTree(objectMapper.getFactory().createParser(observedOutput));
// parse the expected JSON.
String expectedJson = workspace.getFileContents("TargetsCommandTestBuckJson1.js");
JsonNode expected = objectMapper.readTree(objectMapper.getFactory().createParser(expectedJson).enable(Feature.ALLOW_COMMENTS));
assertEquals("Output from targets command should match expected JSON.", expected, observed);
assertEquals("Nothing should be printed to stderr.", "", console.getTextWrittenToStdErr());
}
use of com.facebook.buck.rules.TargetNode in project buck by facebook.
the class TargetsCommandTest method testGetMatchingBuildTargets.
@Test
public void testGetMatchingBuildTargets() throws CmdLineException, IOException {
BuildTarget prebuiltJarTarget = BuildTargetFactory.newInstance("//empty:empty");
TargetNode<?, ?> prebuiltJarNode = PrebuiltJarBuilder.createBuilder(prebuiltJarTarget).setBinaryJar(Paths.get("spoof")).build();
BuildTarget javaLibraryTarget = BuildTargetFactory.newInstance("//javasrc:java-library");
TargetNode<?, ?> javaLibraryNode = JavaLibraryBuilder.createBuilder(javaLibraryTarget).addSrc(Paths.get("javasrc/JavaLibrary.java")).addDep(prebuiltJarTarget).build();
BuildTarget javaTestTarget = BuildTargetFactory.newInstance("//javatest:test-java-library");
TargetNode<?, ?> javaTestNode = JavaTestBuilder.createBuilder(javaTestTarget).addSrc(Paths.get("javatest/TestJavaLibrary.java")).addDep(javaLibraryTarget).build();
ImmutableSet<TargetNode<?, ?>> nodes = ImmutableSet.of(prebuiltJarNode, javaLibraryNode, javaTestNode);
TargetGraph targetGraph = TargetGraphFactory.newInstance(nodes);
ImmutableSet<Path> referencedFiles;
// No target depends on the referenced file.
referencedFiles = ImmutableSet.of(Paths.get("excludesrc/CannotFind.java"));
SortedMap<String, TargetNode<?, ?>> matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.of(referencedFiles), Optional.empty(), Optional.empty(), false, "BUCK");
assertTrue(matchingBuildRules.isEmpty());
// Only test-android-library target depends on the referenced file.
referencedFiles = ImmutableSet.of(Paths.get("javatest/TestJavaLibrary.java"));
matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.of(referencedFiles), Optional.empty(), Optional.empty(), false, "BUCK");
assertEquals(ImmutableSet.of("//javatest:test-java-library"), matchingBuildRules.keySet());
// The test-android-library target indirectly depends on the referenced file,
// while test-java-library target directly depends on the referenced file.
referencedFiles = ImmutableSet.of(Paths.get("javasrc/JavaLibrary.java"));
matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.of(referencedFiles), Optional.empty(), Optional.empty(), false, "BUCK");
assertEquals(ImmutableSet.of("//javatest:test-java-library", "//javasrc:java-library"), matchingBuildRules.keySet());
// Verify that BUCK files show up as referenced files.
referencedFiles = ImmutableSet.of(Paths.get("javasrc/BUCK"));
matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.of(referencedFiles), Optional.empty(), Optional.empty(), false, "BUCK");
assertEquals(ImmutableSet.of("//javatest:test-java-library", "//javasrc:java-library"), matchingBuildRules.keySet());
// Output target only need to depend on one referenced file.
referencedFiles = ImmutableSet.of(Paths.get("javatest/TestJavaLibrary.java"), Paths.get("othersrc/CannotFind.java"));
matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.of(referencedFiles), Optional.empty(), Optional.empty(), false, "BUCK");
assertEquals(ImmutableSet.of("//javatest:test-java-library"), matchingBuildRules.keySet());
// If no referenced file, means this filter is disabled, we can find all targets.
matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.empty(), Optional.empty(), Optional.empty(), false, "BUCK");
assertEquals(ImmutableSet.of("//javatest:test-java-library", "//javasrc:java-library", "//empty:empty"), matchingBuildRules.keySet());
// Specify java_test, java_library as type filters.
matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.empty(), Optional.empty(), Optional.of(ImmutableSet.of(JavaTestDescription.class, JavaLibraryDescription.class)), false, "BUCK");
assertEquals(ImmutableSet.of("//javatest:test-java-library", "//javasrc:java-library"), matchingBuildRules.keySet());
// Specify java_test, java_library, and a rule name as type filters.
matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.empty(), Optional.of(ImmutableSet.of(BuildTargetFactory.newInstance("//javasrc:java-library"))), Optional.of(ImmutableSet.of(JavaTestDescription.class, JavaLibraryDescription.class)), false, "BUCK");
assertEquals(ImmutableSet.of("//javasrc:java-library"), matchingBuildRules.keySet());
// Only filter by BuildTarget
matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.empty(), Optional.of(ImmutableSet.of(BuildTargetFactory.newInstance("//javasrc:java-library"))), Optional.empty(), false, "BUCK");
assertEquals(ImmutableSet.of("//javasrc:java-library"), matchingBuildRules.keySet());
// Filter by BuildTarget and Referenced Files
matchingBuildRules = targetsCommand.getMatchingNodes(targetGraph, Optional.of(ImmutableSet.of(Paths.get("javatest/TestJavaLibrary.java"))), Optional.of(ImmutableSet.of(BuildTargetFactory.newInstance("//javasrc:java-library"))), Optional.empty(), false, "BUCK");
assertEquals(ImmutableSet.<String>of(), matchingBuildRules.keySet());
}
Aggregations