Search in sources :

Example 81 with TargetNode

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;
}
Also used : TargetNode(com.facebook.buck.rules.TargetNode) BuildTarget(com.facebook.buck.model.BuildTarget)

Example 82 with TargetNode

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());
}
Also used : FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) TargetNode(com.facebook.buck.rules.TargetNode) BuildTarget(com.facebook.buck.model.BuildTarget) TargetGraph(com.facebook.buck.rules.TargetGraph) Test(org.junit.Test)

Example 83 with TargetNode

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());
}
Also used : Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) TargetNode(com.facebook.buck.rules.TargetNode) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) BuildTarget(com.facebook.buck.model.BuildTarget) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) TargetGraph(com.facebook.buck.rules.TargetGraph) Test(org.junit.Test)

Example 84 with TargetNode

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());
}
Also used : TargetNode(com.facebook.buck.rules.TargetNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.Test)

Example 85 with TargetNode

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());
}
Also used : Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) TargetNode(com.facebook.buck.rules.TargetNode) BuildTarget(com.facebook.buck.model.BuildTarget) JavaLibraryDescription(com.facebook.buck.jvm.java.JavaLibraryDescription) JavaTestDescription(com.facebook.buck.jvm.java.JavaTestDescription) TargetGraph(com.facebook.buck.rules.TargetGraph) Test(org.junit.Test)

Aggregations

TargetNode (com.facebook.buck.rules.TargetNode)88 BuildTarget (com.facebook.buck.model.BuildTarget)73 TargetGraph (com.facebook.buck.rules.TargetGraph)43 Test (org.junit.Test)40 Path (java.nio.file.Path)36 ImmutableSet (com.google.common.collect.ImmutableSet)33 FakeSourcePath (com.facebook.buck.rules.FakeSourcePath)23 PathSourcePath (com.facebook.buck.rules.PathSourcePath)22 SourcePath (com.facebook.buck.rules.SourcePath)22 ImmutableMap (com.google.common.collect.ImmutableMap)22 Optional (java.util.Optional)20 Map (java.util.Map)18 HumanReadableException (com.facebook.buck.util.HumanReadableException)17 ImmutableList (com.google.common.collect.ImmutableList)17 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)13 UnflavoredBuildTarget (com.facebook.buck.model.UnflavoredBuildTarget)13 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)13 Cell (com.facebook.buck.rules.Cell)13 IOException (java.io.IOException)13 DefaultBuildTargetSourcePath (com.facebook.buck.rules.DefaultBuildTargetSourcePath)12