Beispiel #1
0
  @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("//:res");
    TargetNode<?> genNode =
        GenruleBuilder.newGenruleBuilder(genTarget)
            .setSrcs(ImmutableList.<SourcePath>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.<ImmutableSet<BuildTarget>>absent(),
            Optional.<ImmutableSet<BuildRuleType>>absent(),
            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.<ImmutableSet<BuildTarget>>absent(),
            Optional.<ImmutableSet<BuildRuleType>>absent(),
            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.<ImmutableSet<BuildTarget>>absent(),
            Optional.<ImmutableSet<BuildRuleType>>absent(),
            false,
            "BUCK");
    assertEquals(
        ImmutableSet.of(androidResourceTarget.toString(), genTarget.toString()),
        matchingBuildRules.keySet());
  }
Beispiel #2
0
  @Test
  public void testGetMatchingAppleTestBuildTarget() throws CmdLineException, IOException {
    BuildTarget libraryTarget = BuildTargetFactory.newInstance("//foo:lib");
    TargetNode<?> libraryNode =
        AppleLibraryBuilder.createBuilder(libraryTarget)
            .setSrcs(
                Optional.of(
                    ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("foo/foo.m")))))
            .build();

    BuildTarget testTarget = BuildTargetFactory.newInstance("//foo:xctest");
    TargetNode<?> testNode =
        AppleTestBuilder.createBuilder(testTarget)
            .setExtension(Either.<AppleBundleExtension, String>ofLeft(AppleBundleExtension.XCTEST))
            .setSrcs(
                Optional.of(
                    ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("foo/testfoo.m")))))
            .setDeps(Optional.of(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.<ImmutableSet<BuildTarget>>absent(),
            Optional.<ImmutableSet<BuildRuleType>>absent(),
            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.<ImmutableSet<BuildTarget>>absent(),
            Optional.<ImmutableSet<BuildRuleType>>absent(),
            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.<ImmutableSet<BuildTarget>>absent(),
            Optional.<ImmutableSet<BuildRuleType>>absent(),
            false,
            "BUCK");
    assertEquals(ImmutableSet.of("//foo:xctest"), matchingBuildRules.keySet());
  }
Beispiel #3
0
 @Test
 public void testPrintNullDelimitedTargets() throws UnsupportedEncodingException {
   Iterable<String> targets = ImmutableList.of("//foo:bar", "//foo:baz");
   FakeOutputStream fakeStream = new FakeOutputStream();
   PrintStream printStream = new PrintStream(fakeStream);
   TargetsCommand.printNullDelimitedTargets(targets, printStream);
   printStream.flush();
   assertEquals("//foo:bar\0//foo:baz\0", fakeStream.toString(Charsets.UTF_8.name()));
 }
Beispiel #4
0
  @Test
  public void testJsonOutputForMissingBuildTarget()
      throws BuildFileParseException, IOException, InterruptedException {
    // nonexistent target should not exist.
    SortedMap<String, TargetNode<?>> buildRules = buildTargetNodes(filesystem, "//:nonexistent");
    targetsCommand.printJsonForTargets(
        params, executor, buildRules, ImmutableMap.<String, ShowOptions>of());

    String output = console.getTextWrittenToStdOut();
    assertEquals("[\n]\n", output);
    assertEquals(
        "unable to find rule for target //:nonexistent\n", console.getTextWrittenToStdErr());
  }
Beispiel #5
0
  @Test
  public void testGetMatchingAppleLibraryBuildTarget() throws CmdLineException, IOException {
    BuildTarget libraryTarget = BuildTargetFactory.newInstance("//foo:lib");
    TargetNode<?> libraryNode =
        AppleLibraryBuilder.createBuilder(libraryTarget)
            .setSrcs(
                Optional.of(
                    ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("foo/foo.m")))))
            .build();

    ImmutableSet<TargetNode<?>> nodes = ImmutableSet.<TargetNode<?>>of(libraryNode);

    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.<ImmutableSet<BuildTarget>>absent(),
            Optional.<ImmutableSet<BuildRuleType>>absent(),
            false,
            "BUCK");
    assertTrue(matchingBuildRules.isEmpty());

    // The AppleLibrary matches the referenced file.
    matchingBuildRules =
        targetsCommand.getMatchingNodes(
            targetGraph,
            Optional.of(ImmutableSet.of(Paths.get("foo/foo.m"))),
            Optional.<ImmutableSet<BuildTarget>>absent(),
            Optional.<ImmutableSet<BuildRuleType>>absent(),
            false,
            "BUCK");
    assertEquals(ImmutableSet.of("//foo:lib"), matchingBuildRules.keySet());
  }
Beispiel #6
0
  @Test
  public void testJsonOutputForBuildTarget()
      throws IOException, BuildFileParseException, InterruptedException {
    // run `buck targets` on the build file and parse the observed JSON.
    SortedMap<String, TargetNode<?>> nodes = buildTargetNodes(filesystem, "//:test-library");

    targetsCommand.printJsonForTargets(
        params, executor, nodes, ImmutableMap.<String, ShowOptions>of());
    String observedOutput = console.getTextWrittenToStdOut();
    JsonNode observed =
        objectMapper.readTree(objectMapper.getJsonFactory().createJsonParser(observedOutput));

    // parse the expected JSON.
    String expectedJson = workspace.getFileContents("TargetsCommandTestBuckJson1.js");
    JsonNode expected =
        objectMapper.readTree(
            objectMapper
                .getJsonFactory()
                .createJsonParser(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());
  }
Beispiel #7
0
  @Test
  public void testDetectTestChanges() throws CmdLineException, IOException {
    BuildTarget libraryTarget = BuildTargetFactory.newInstance("//foo:lib");
    BuildTarget libraryTestTarget1 = BuildTargetFactory.newInstance("//foo:xctest1");
    BuildTarget libraryTestTarget2 = BuildTargetFactory.newInstance("//foo:xctest2");
    BuildTarget testLibraryTarget = BuildTargetFactory.newInstance("//testlib:testlib");
    BuildTarget testLibraryTestTarget = BuildTargetFactory.newInstance("//testlib:testlib-xctest");

    TargetNode<?> libraryNode =
        AppleLibraryBuilder.createBuilder(libraryTarget)
            .setSrcs(
                Optional.of(
                    ImmutableSortedSet.of(SourceWithFlags.of(new FakeSourcePath("foo/foo.m")))))
            .setTests(Optional.of(ImmutableSortedSet.of(libraryTestTarget1, libraryTestTarget2)))
            .build();

    TargetNode<?> libraryTestNode1 =
        AppleTestBuilder.createBuilder(libraryTestTarget1)
            .setExtension(Either.<AppleBundleExtension, String>ofLeft(AppleBundleExtension.XCTEST))
            .setSrcs(
                Optional.of(
                    ImmutableSortedSet.of(
                        SourceWithFlags.of(new FakeSourcePath("foo/testfoo1.m")))))
            .setDeps(Optional.of(ImmutableSortedSet.of(libraryTarget)))
            .build();

    TargetNode<?> libraryTestNode2 =
        AppleTestBuilder.createBuilder(libraryTestTarget2)
            .setExtension(Either.<AppleBundleExtension, String>ofLeft(AppleBundleExtension.XCTEST))
            .setSrcs(
                Optional.of(
                    ImmutableSortedSet.of(
                        SourceWithFlags.of(new FakeSourcePath("foo/testfoo2.m")))))
            .setDeps(Optional.of(ImmutableSortedSet.of(testLibraryTarget)))
            .build();

    TargetNode<?> testLibraryNode =
        AppleLibraryBuilder.createBuilder(testLibraryTarget)
            .setSrcs(
                Optional.of(
                    ImmutableSortedSet.of(
                        SourceWithFlags.of(new FakeSourcePath("testlib/testlib.m")))))
            .setTests(Optional.of(ImmutableSortedSet.of(testLibraryTestTarget)))
            .build();

    TargetNode<?> testLibraryTestNode =
        AppleTestBuilder.createBuilder(testLibraryTestTarget)
            .setExtension(Either.<AppleBundleExtension, String>ofLeft(AppleBundleExtension.XCTEST))
            .setSrcs(
                Optional.of(
                    ImmutableSortedSet.of(
                        SourceWithFlags.of(new FakeSourcePath("testlib/testlib-test.m")))))
            .setDeps(Optional.of(ImmutableSortedSet.of(testLibraryTarget)))
            .build();

    ImmutableSet<TargetNode<?>> nodes =
        ImmutableSet.of(
            libraryNode, libraryTestNode1, libraryTestNode2, testLibraryNode, testLibraryTestNode);

    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.<ImmutableSet<BuildTarget>>absent(),
            Optional.<ImmutableSet<BuildRuleType>>absent(),
            true,
            "BUCK");
    assertTrue(matchingBuildRules.isEmpty());

    // Test1, test2 and the library depend on the referenced file.
    matchingBuildRules =
        targetsCommand.getMatchingNodes(
            targetGraph,
            Optional.of(ImmutableSet.of(Paths.get("foo/testfoo1.m"))),
            Optional.<ImmutableSet<BuildTarget>>absent(),
            Optional.<ImmutableSet<BuildRuleType>>absent(),
            true,
            "BUCK");
    assertEquals(ImmutableSet.of("//foo:lib", "//foo:xctest1"), matchingBuildRules.keySet());

    // Test1, test2 and the library depend on the referenced file.
    matchingBuildRules =
        targetsCommand.getMatchingNodes(
            targetGraph,
            Optional.of(ImmutableSet.of(Paths.get("foo/testfoo2.m"))),
            Optional.<ImmutableSet<BuildTarget>>absent(),
            Optional.<ImmutableSet<BuildRuleType>>absent(),
            true,
            "BUCK");
    assertEquals(
        ImmutableSet.of("//foo:lib", "//foo:xctest1", "//foo:xctest2"),
        matchingBuildRules.keySet());

    // Library, test1, test2, test library and its test depend on the referenced file.
    matchingBuildRules =
        targetsCommand.getMatchingNodes(
            targetGraph,
            Optional.of(ImmutableSet.of(Paths.get("testlib/testlib.m"))),
            Optional.<ImmutableSet<BuildTarget>>absent(),
            Optional.<ImmutableSet<BuildRuleType>>absent(),
            true,
            "BUCK");
    assertEquals(
        ImmutableSet.of(
            "//foo:lib",
            "//foo:xctest1",
            "//foo:xctest2",
            "//testlib:testlib",
            "//testlib:testlib-xctest"),
        matchingBuildRules.keySet());

    // Library, test1, test2, test library and its test depend on the referenced file.
    matchingBuildRules =
        targetsCommand.getMatchingNodes(
            targetGraph,
            Optional.of(ImmutableSet.of(Paths.get("testlib/testlib-test.m"))),
            Optional.<ImmutableSet<BuildTarget>>absent(),
            Optional.<ImmutableSet<BuildRuleType>>absent(),
            true,
            "BUCK");
    assertEquals(
        ImmutableSet.of(
            "//foo:lib",
            "//foo:xctest1",
            "//foo:xctest2",
            "//testlib:testlib",
            "//testlib:testlib-xctest"),
        matchingBuildRules.keySet());
  }
Beispiel #8
0
  @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.<ImmutableSet<BuildTarget>>absent(),
            Optional.<ImmutableSet<BuildRuleType>>absent(),
            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.<ImmutableSet<BuildTarget>>absent(),
            Optional.<ImmutableSet<BuildRuleType>>absent(),
            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.<ImmutableSet<BuildTarget>>absent(),
            Optional.<ImmutableSet<BuildRuleType>>absent(),
            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.<ImmutableSet<BuildTarget>>absent(),
            Optional.<ImmutableSet<BuildRuleType>>absent(),
            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.<ImmutableSet<BuildTarget>>absent(),
            Optional.<ImmutableSet<BuildRuleType>>absent(),
            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.<ImmutableSet<Path>>absent(),
            Optional.<ImmutableSet<BuildTarget>>absent(),
            Optional.<ImmutableSet<BuildRuleType>>absent(),
            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.<ImmutableSet<Path>>absent(),
            Optional.<ImmutableSet<BuildTarget>>absent(),
            Optional.of(ImmutableSet.of(JavaTestDescription.TYPE, JavaLibraryDescription.TYPE)),
            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.<ImmutableSet<Path>>absent(),
            Optional.of(ImmutableSet.of(BuildTargetFactory.newInstance("//javasrc:java-library"))),
            Optional.of(ImmutableSet.of(JavaTestDescription.TYPE, JavaLibraryDescription.TYPE)),
            false,
            "BUCK");
    assertEquals(ImmutableSet.of("//javasrc:java-library"), matchingBuildRules.keySet());

    // Only filter by BuildTarget
    matchingBuildRules =
        targetsCommand.getMatchingNodes(
            targetGraph,
            Optional.<ImmutableSet<Path>>absent(),
            Optional.of(ImmutableSet.of(BuildTargetFactory.newInstance("//javasrc:java-library"))),
            Optional.<ImmutableSet<BuildRuleType>>absent(),
            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.<ImmutableSet<BuildRuleType>>absent(),
            false,
            "BUCK");
    assertEquals(ImmutableSet.<String>of(), matchingBuildRules.keySet());
  }