aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/skyframe/util/SkyframeExecutorTestUtils.java
blob: 109f937703e09f04f7c8f194e7711dbdbab7ac43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2015 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.skyframe.util;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.events.EventHandler;
import com.google.devtools.build.lib.packages.NoSuchTargetException;
import com.google.devtools.build.lib.packages.Target;
import com.google.devtools.build.lib.skyframe.ConfiguredTargetValue;
import com.google.devtools.build.lib.skyframe.PackageValue;
import com.google.devtools.build.lib.skyframe.SkyFunctions;
import com.google.devtools.build.lib.skyframe.SkyframeExecutor;
import com.google.devtools.build.lib.skyframe.TargetMarkerValue;
import com.google.devtools.build.skyframe.ErrorInfo;
import com.google.devtools.build.skyframe.EvaluationResult;
import com.google.devtools.build.skyframe.MemoizingEvaluator;
import com.google.devtools.build.skyframe.SkyKey;
import com.google.devtools.build.skyframe.SkyValue;

import java.util.Collection;
import java.util.List;

import javax.annotation.Nullable;

/**
 * Helper functions for manually dealing with a {@link SkyframeExecutor}'s graph in tests.
 */
public class SkyframeExecutorTestUtils {

  private SkyframeExecutorTestUtils() {
  }

  /**
   * Returns an existing value, or {@code null} if the given key is not currently in the graph.
   */
  @Nullable
  public static SkyValue getExistingValue(SkyframeExecutor skyframeExecutor, SkyKey key) {
    return skyframeExecutor.getEvaluatorForTesting().getExistingValueForTesting(key);
  }

  /**
   * Returns an existing error info, or {@code null} if the given key is not currently in the
   * graph.
   */
  @Nullable
  public static ErrorInfo getExistingError(SkyframeExecutor skyframeExecutor, SkyKey key) {
    return skyframeExecutor.getEvaluatorForTesting().getExistingErrorForTesting(key);
  }

  /**
   * Calls {@link MemoizingEvaluator#evaluate} on the given {@link SkyframeExecutor}'s
   * graph.
   */
  public static <T extends SkyValue> EvaluationResult<T> evaluate(
      SkyframeExecutor skyframeExecutor, SkyKey key, boolean keepGoing,
      EventHandler errorEventListener) throws InterruptedException {
    return skyframeExecutor.getDriverForTesting().evaluate(ImmutableList.of(key), keepGoing,
        SkyframeExecutor.DEFAULT_THREAD_COUNT, errorEventListener);
  }

  /**
   * Returns an existing configured target value, or {@code null} if there is not an appropriate
   * configured target value key in the graph.
   *
   * This helper is provided so legacy tests don't need to know about details of skyframe keys.
   */
  @Nullable
  public static ConfiguredTargetValue getExistingConfiguredTargetValue(
      SkyframeExecutor skyframeExecutor, Label label, BuildConfiguration config) {
    SkyKey key = ConfiguredTargetValue.key(label, config);
    return (ConfiguredTargetValue) getExistingValue(skyframeExecutor, key);
  }

  /**
   * Returns the configured target for an existing configured target value, or {@code null} if there
   * is not an appropriate configured target value key in the graph.
   *
   * This helper is provided so legacy tests don't need to know about details of skyframe keys.
   */
  @Nullable
  public static ConfiguredTarget getExistingConfiguredTarget(
      SkyframeExecutor skyframeExecutor,
      Label label, BuildConfiguration config) {
    ConfiguredTargetValue value = getExistingConfiguredTargetValue(skyframeExecutor, label, config);
    if (value == null) {
      return null;
    }
    return value.getConfiguredTarget();
  }

  /**
   * Returns all configured targets currently in the graph.
   */
  public static Iterable<ConfiguredTarget> getAllExistingConfiguredTargets(
      SkyframeExecutor skyframeExecutor) {
    Collection<SkyValue> values =
        Maps.filterKeys(skyframeExecutor.getEvaluatorForTesting().getValues(),
            SkyFunctions.isSkyFunction(SkyFunctions.CONFIGURED_TARGET)).values();
    List<ConfiguredTarget> cts = Lists.newArrayList();
    for (SkyValue value : values) {
      if (value != null) {
        cts.add(((ConfiguredTargetValue) value).getConfiguredTarget());
      }
    }
    return cts;
  }

  /**
   * Returns the target for an existing target value, or {@code null} if there is not an appropriate
   * target value key in the graph.
   *
   * This helper is provided so legacy tests don't need to know about details of skyframe keys.
   */
  @Nullable
  public static Target getExistingTarget(SkyframeExecutor skyframeExecutor,
      Label label) {
    PackageValue value = (PackageValue) getExistingValue(skyframeExecutor,
        PackageValue.key(label.getPackageIdentifier()));
    if (value == null) {
      return null;
    }
    try {
      return value.getPackage().getTarget(label.getName());
    } catch (NoSuchTargetException e) {
      return null;
    }
  }

  /**
   * Returns the error info for an existing target value, or {@code null} if there is not an
   * appropriate target value key in the graph.
   *
   * This helper is provided so legacy tests don't need to know about details of skyframe keys.
   */
  @Nullable
  public static ErrorInfo getExistingFailedTarget(SkyframeExecutor skyframeExecutor, Label label) {
    SkyKey key = TargetMarkerValue.key(label);
    return getExistingError(skyframeExecutor, key);
  }
}