Esempio n. 1
0
  @Test
  public void setInputBuildTargetSourcePath() {
    BuildRuleResolver resolver =
        new BuildRuleResolver(TargetGraph.EMPTY, new BuildTargetNodeToBuildRuleTransformer());
    SourcePathResolver pathResolver = new SourcePathResolver(resolver);
    FakeBuildRule fake1 = new FakeBuildRule("//:fake1", pathResolver);
    FakeBuildRule fake2 = new FakeBuildRule("//:fake2", pathResolver);
    resolver.addToIndex(fake1);
    resolver.addToIndex(fake2);

    // Verify that two BuildTargetSourcePaths with the same rule and path are equal.
    assertEquals(
        createEmptyRuleKey(pathResolver)
            .setReflectively(
                "key", new BuildTargetSourcePath(fake1.getBuildTarget(), Paths.get("location")))
            .build(),
        createEmptyRuleKey(pathResolver)
            .setReflectively(
                "key", new BuildTargetSourcePath(fake1.getBuildTarget(), Paths.get("location")))
            .build());

    // Verify that just changing the path of the build rule changes the rule key.
    assertNotEquals(
        createEmptyRuleKey(pathResolver)
            .setReflectively(
                "key", new BuildTargetSourcePath(fake1.getBuildTarget(), Paths.get("location")))
            .build(),
        createEmptyRuleKey(pathResolver)
            .setReflectively(
                "key", new BuildTargetSourcePath(fake1.getBuildTarget(), Paths.get("different")))
            .build());

    // Verify that just changing the build rule rule key changes the calculated rule key.
    assertNotEquals(
        createEmptyRuleKey(pathResolver)
            .setReflectively(
                "key", new BuildTargetSourcePath(fake1.getBuildTarget(), Paths.get("location")))
            .build(),
        createEmptyRuleKey(pathResolver)
            .setReflectively(
                "key", new BuildTargetSourcePath(fake2.getBuildTarget(), Paths.get("location")))
            .build());

    // Verify that just changing the key changes the calculated rule key.
    assertNotEquals(
        createEmptyRuleKey(pathResolver)
            .setReflectively(
                "key", new BuildTargetSourcePath(fake1.getBuildTarget(), Paths.get("location")))
            .build(),
        createEmptyRuleKey(pathResolver)
            .setReflectively(
                "different-key",
                new BuildTargetSourcePath(fake1.getBuildTarget(), Paths.get("location")))
            .build());
  }
  @Test
  public void upperBoundGenericTypesCauseValuesToBeSetToTheUpperBound()
      throws ConstructorArgMarshalException, NoSuchBuildTargetException {

    class Dto {
      public List<? extends SourcePath> yup;
    }

    ProjectFilesystem projectFilesystem = new FakeProjectFilesystem();
    SourcePathResolver pathResolver = new SourcePathResolver(new BuildRuleResolver());
    BuildRule rule =
        new FakeBuildRule(BuildTargetFactory.newInstance("//will:happen"), pathResolver);
    ruleResolver.addToIndex(rule);
    Dto dto = new Dto();
    marshaller.populate(
        createCellRoots(filesystem),
        filesystem,
        buildRuleFactoryParams(),
        dto,
        ImmutableSet.<BuildTarget>builder(),
        ImmutableSet.<BuildTargetPattern>builder(),
        ImmutableMap.<String, Object>of(
            "yup", ImmutableList.of(rule.getBuildTarget().getFullyQualifiedName())));

    BuildTargetSourcePath path = new BuildTargetSourcePath(rule.getBuildTarget());
    assertEquals(ImmutableList.of(path), dto.yup);
  }
  @Test
  public void shouldPopulateSourcePaths()
      throws ConstructorArgMarshalException, NoSuchBuildTargetException {
    class Dto {
      public SourcePath filePath;
      public SourcePath targetPath;
    }

    ProjectFilesystem projectFilesystem = new FakeProjectFilesystem();
    BuildTarget target = BuildTargetFactory.newInstance("//example/path:peas");
    FakeBuildRule rule = new FakeBuildRule(target, new SourcePathResolver(new BuildRuleResolver()));
    ruleResolver.addToIndex(rule);
    Dto dto = new Dto();
    marshaller.populate(
        createCellRoots(filesystem),
        filesystem,
        buildRuleFactoryParams(),
        dto,
        ImmutableSet.<BuildTarget>builder(),
        ImmutableSet.<BuildTargetPattern>builder(),
        ImmutableMap.<String, Object>of(
            "filePath", "cheese.txt",
            "targetPath", ":peas"));

    assertEquals(
        new PathSourcePath(projectFilesystem, Paths.get("example/path/cheese.txt")), dto.filePath);
    assertEquals(new BuildTargetSourcePath(rule.getBuildTarget()), dto.targetPath);
  }
  @Test
  public void shouldResolveCollectionOfSourcePaths()
      throws ConstructorArgMarshalException, NoSuchBuildTargetException {
    class Dto {
      public ImmutableSortedSet<SourcePath> srcs;
    }

    BuildRuleResolver resolver = new BuildRuleResolver();
    BuildTarget target = BuildTargetFactory.newInstance("//example/path:manifest");
    BuildRule rule = new FakeBuildRule(target, new SourcePathResolver(resolver));
    resolver.addToIndex(rule);

    Dto dto = new Dto();
    marshaller.populate(
        createCellRoots(filesystem),
        filesystem,
        buildRuleFactoryParams(),
        dto,
        ImmutableSet.<BuildTarget>builder(),
        ImmutableSet.<BuildTargetPattern>builder(),
        ImmutableMap.<String, Object>of(
            "srcs", ImmutableList.of("main.py", "lib/__init__.py", "lib/manifest.py")));

    ImmutableSet<String> observedValues =
        FluentIterable.from(dto.srcs)
            .transform(
                new Function<SourcePath, String>() {
                  @Nullable
                  @Override
                  public String apply(SourcePath input) {
                    return ((PathSourcePath) input).getRelativePath().toString();
                  }
                })
            .toSet();
    assertEquals(
        ImmutableSet.of(
            Paths.get("example/path/main.py").toString(),
            Paths.get("example/path/lib/__init__.py").toString(),
            Paths.get("example/path/lib/manifest.py").toString()),
        observedValues);
  }
  @Override
  public <T> BuildRule transform(
      TargetGraph targetGraph, BuildRuleResolver ruleResolver, TargetNode<T> targetNode)
      throws NoSuchBuildTargetException {
    BuildRuleFactoryParams ruleFactoryParams = targetNode.getRuleFactoryParams();
    Description<T> description = targetNode.getDescription();

    T arg = targetNode.getConstructorArg();

    // The params used for the Buildable only contain the declared parameters. However, the deps of
    // the rule include not only those, but also any that were picked up through the deps declared
    // via a SourcePath.
    BuildRuleParams params =
        new BuildRuleParams(
            targetNode.getBuildTarget(),
            Suppliers.ofInstance(ruleResolver.requireAllRules(targetNode.getDeclaredDeps())),
            Suppliers.ofInstance(ruleResolver.requireAllRules(targetNode.getExtraDeps())),
            ruleFactoryParams.getProjectFilesystem(),
            targetNode.getCellNames());
    return description.createBuildRule(targetGraph, params, ruleResolver, arg);
  }
  @Test
  public void canPopulateSimpleConstructorArgFromBuildFactoryParams()
      throws ConstructorArgMarshalException, NoSuchBuildTargetException {

    class Dto {
      public String required;
      public Optional<String> notRequired;

      public int num;
      // Turns out there no optional number params
      // public Optional<Long> optionalLong;

      public boolean needed;
      public Optional<Boolean> notNeeded;

      public SourcePath aSrcPath;
      public Optional<SourcePath> notASrcPath;

      public Path aPath;
      public Optional<Path> notAPath;
    }

    ProjectFilesystem projectFilesystem = new FakeProjectFilesystem();
    FakeBuildRule expectedRule =
        new FakeBuildRule(
            BuildTargetFactory.newInstance("//example/path:path"),
            new SourcePathResolver(new BuildRuleResolver()));
    ruleResolver.addToIndex(expectedRule);

    ImmutableMap<String, Object> args =
        ImmutableMap.<String, Object>builder()
            .put("required", "cheese")
            .put("notRequired", "cake")
            // Long because that's what comes from python.
            .put("num", 42L)
            // Skipping optional Long.
            .put("needed", true)
            // Skipping optional boolean.
            .put("aSrcPath", ":path")
            .put("aPath", "./File.java")
            .put("notAPath", "./NotFile.java")
            .build();
    Dto dto = new Dto();
    marshaller.populate(
        createCellRoots(filesystem),
        filesystem,
        buildRuleFactoryParams(),
        dto,
        ImmutableSet.<BuildTarget>builder(),
        ImmutableSet.<BuildTargetPattern>builder(),
        args);

    assertEquals("cheese", dto.required);
    assertEquals("cake", dto.notRequired.get());
    assertEquals(42, dto.num);
    assertTrue(dto.needed);
    assertEquals(Optional.<Boolean>absent(), dto.notNeeded);
    BuildTargetSourcePath expected = new BuildTargetSourcePath(expectedRule.getBuildTarget());
    assertEquals(expected, dto.aSrcPath);
    assertEquals(Paths.get("example/path/NotFile.java"), dto.notAPath.get());
  }