Search in sources :

Example 1 with HasTests

use of com.facebook.buck.model.HasTests in project buck by facebook.

the class TargetsCommand method getDependentNodes.

/**
   * @param graph A graph used to resolve dependencies between targets.
   * @param nodes A set of nodes.
   * @param detectTestChanges If true, tests are considered to be dependencies of the targets they
   *                          are testing.
   * @return A set of all nodes that transitively depend on {@code nodes}
   * (a superset of {@code nodes}).
   */
private static ImmutableSet<TargetNode<?, ?>> getDependentNodes(final TargetGraph graph, ImmutableSet<TargetNode<?, ?>> nodes, boolean detectTestChanges) {
    ImmutableMultimap.Builder<TargetNode<?, ?>, TargetNode<?, ?>> extraEdgesBuilder = ImmutableMultimap.builder();
    if (detectTestChanges) {
        for (TargetNode<?, ?> node : graph.getNodes()) {
            if (node.getConstructorArg() instanceof HasTests) {
                ImmutableSortedSet<BuildTarget> tests = ((HasTests) node.getConstructorArg()).getTests();
                for (BuildTarget testTarget : tests) {
                    Optional<TargetNode<?, ?>> testNode = graph.getOptional(testTarget);
                    if (!testNode.isPresent()) {
                        throw new HumanReadableException("'%s' (test of '%s') is not in the target graph.", testTarget, node);
                    }
                    extraEdgesBuilder.put(testNode.get(), node);
                }
            }
        }
    }
    final ImmutableMultimap<TargetNode<?, ?>, TargetNode<?, ?>> extraEdges = extraEdgesBuilder.build();
    final ImmutableSet.Builder<TargetNode<?, ?>> builder = ImmutableSet.builder();
    AbstractBreadthFirstTraversal<TargetNode<?, ?>> traversal = new AbstractBreadthFirstTraversal<TargetNode<?, ?>>(nodes) {

        @Override
        public ImmutableSet<TargetNode<?, ?>> visit(TargetNode<?, ?> targetNode) {
            builder.add(targetNode);
            return FluentIterable.from(graph.getIncomingNodesFor(targetNode)).append(extraEdges.get(targetNode)).toSet();
        }
    };
    traversal.start();
    return builder.build();
}
Also used : TargetNode(com.facebook.buck.rules.TargetNode) ImmutableSet(com.google.common.collect.ImmutableSet) BuildTarget(com.facebook.buck.model.BuildTarget) HumanReadableException(com.facebook.buck.util.HumanReadableException) HasTests(com.facebook.buck.model.HasTests) AbstractBreadthFirstTraversal(com.facebook.buck.graph.AbstractBreadthFirstTraversal) ImmutableMultimap(com.google.common.collect.ImmutableMultimap)

Example 2 with HasTests

use of com.facebook.buck.model.HasTests in project buck by facebook.

the class WorkspaceAndProjectGenerator method getOrderedTestNodes.

/**
   * Find tests to run.
   *
   * @param targetGraph input target graph
   * @param includeProjectTests whether to include tests of nodes in the project
   * @param orderedTargetNodes target nodes for which to fetch tests for
   * @param extraTestBundleTargets extra tests to include
   *
   * @return test targets that should be run.
   */
private ImmutableSet<TargetNode<AppleTestDescription.Arg, ?>> getOrderedTestNodes(Optional<BuildTarget> mainTarget, TargetGraph targetGraph, boolean includeProjectTests, boolean includeDependenciesTests, ImmutableSet<TargetNode<?, ?>> orderedTargetNodes, ImmutableSet<TargetNode<AppleTestDescription.Arg, ?>> extraTestBundleTargets) {
    LOG.debug("Getting ordered test target nodes for %s", orderedTargetNodes);
    ImmutableSet.Builder<TargetNode<AppleTestDescription.Arg, ?>> testsBuilder = ImmutableSet.builder();
    if (includeProjectTests) {
        Optional<TargetNode<?, ?>> mainTargetNode = Optional.empty();
        if (mainTarget.isPresent()) {
            mainTargetNode = targetGraph.getOptional(mainTarget.get());
        }
        for (TargetNode<?, ?> node : orderedTargetNodes) {
            if (includeDependenciesTests || (mainTargetNode.isPresent() && node.equals(mainTargetNode.get()))) {
                if (!(node.getConstructorArg() instanceof HasTests)) {
                    continue;
                }
                for (BuildTarget explicitTestTarget : ((HasTests) node.getConstructorArg()).getTests()) {
                    if (!explicitTestTarget.matchesUnflavoredTargets(focusModules)) {
                        continue;
                    }
                    Optional<TargetNode<?, ?>> explicitTestNode = targetGraph.getOptional(explicitTestTarget);
                    if (explicitTestNode.isPresent()) {
                        Optional<TargetNode<AppleTestDescription.Arg, ?>> castedNode = explicitTestNode.get().castArg(AppleTestDescription.Arg.class);
                        if (castedNode.isPresent()) {
                            testsBuilder.add(castedNode.get());
                        } else {
                            LOG.debug("Test target specified in '%s' is not a apple_test;" + " not including in project: '%s'", node.getBuildTarget(), explicitTestTarget);
                        }
                    } else {
                        throw new HumanReadableException("Test target specified in '%s' is not in the target graph: '%s'", node.getBuildTarget(), explicitTestTarget);
                    }
                }
            }
        }
    }
    for (TargetNode<AppleTestDescription.Arg, ?> extraTestTarget : extraTestBundleTargets) {
        testsBuilder.add(extraTestTarget);
    }
    return testsBuilder.build();
}
Also used : TargetNode(com.facebook.buck.rules.TargetNode) ImmutableSet(com.google.common.collect.ImmutableSet) AppleTestDescription(com.facebook.buck.apple.AppleTestDescription) BuildTarget(com.facebook.buck.model.BuildTarget) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) HumanReadableException(com.facebook.buck.util.HumanReadableException) HasTests(com.facebook.buck.model.HasTests)

Aggregations

BuildTarget (com.facebook.buck.model.BuildTarget)2 HasTests (com.facebook.buck.model.HasTests)2 TargetNode (com.facebook.buck.rules.TargetNode)2 HumanReadableException (com.facebook.buck.util.HumanReadableException)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 AppleTestDescription (com.facebook.buck.apple.AppleTestDescription)1 AbstractBreadthFirstTraversal (com.facebook.buck.graph.AbstractBreadthFirstTraversal)1 UnflavoredBuildTarget (com.facebook.buck.model.UnflavoredBuildTarget)1 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1