aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/analysis/DependencyResolverTest.java
blob: dca9b87fddc3dcc7f18d5a3a72e5664f9275020d (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright 2015 The Bazel Authors. 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.analysis;

import static com.google.common.truth.Truth.assertThat;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ListMultimap;
import com.google.common.testing.EqualsTester;
import com.google.common.testing.NullPointerTester;
import com.google.devtools.build.lib.analysis.DependencyResolver.Dependency;
import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
import com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider;
import com.google.devtools.build.lib.analysis.util.AnalysisTestCase;
import com.google.devtools.build.lib.analysis.util.TestAspects;
import com.google.devtools.build.lib.analysis.util.TestAspects.AspectRequiringRule;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.packages.AspectDefinition;
import com.google.devtools.build.lib.packages.AspectFactory;
import com.google.devtools.build.lib.packages.AspectParameters;
import com.google.devtools.build.lib.packages.Attribute;
import com.google.devtools.build.lib.packages.NoSuchThingException;
import com.google.devtools.build.lib.packages.Target;
import com.google.devtools.build.lib.testutil.TestRuleClassProvider;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import javax.annotation.Nullable;

/**
 * Tests for {@link DependencyResolver}.
 *
 * <p>These use custom rules so that all usual and unusual cases related to aspect processing can
 * be tested.
 *
 * <p>It would be nicer is we didn't have a Skyframe executor, if we didn't have that, we'd need a
 * way to create a configuration, a package manager and a whole lot of other things, so it's just
 * easier this way.
 */
@RunWith(JUnit4.class)
public class DependencyResolverTest extends AnalysisTestCase {
  private DependencyResolver dependencyResolver;

  @Override
  @Before
  public void setUp() throws Exception {
    super.setUp();

    dependencyResolver = new DependencyResolver() {
      @Override
      protected void invalidVisibilityReferenceHook(TargetAndConfiguration node, Label label) {
        throw new IllegalStateException();
      }

      @Override
      protected void invalidPackageGroupReferenceHook(TargetAndConfiguration node, Label label) {
        throw new IllegalStateException();
      }

      @Nullable
      @Override
      protected Target getTarget(Label label) throws NoSuchThingException {
        try {
          return packageManager.getTarget(reporter, label);
        } catch (InterruptedException e) {
          throw new IllegalStateException(e);
        }
      }
    };
  }

  @Override
  @After
  public void tearDown() throws Exception {
    super.tearDown();
  }

  private void pkg(String name, String... contents) throws Exception {
    scratch.file("" + name + "/BUILD", contents);
  }

  @SafeVarargs
  private final void setRules(RuleDefinition... rules) throws Exception {
    ConfiguredRuleClassProvider.Builder builder =
        new ConfiguredRuleClassProvider.Builder();
    TestRuleClassProvider.addStandardRules(builder);
    for (RuleDefinition rule : rules) {
      builder.addRuleDefinition(rule);
    }

    useRuleClassProvider(builder.build());
    update();
  }

  private ListMultimap<Attribute, Dependency> dependentNodeMap(
      String targetName, Class<? extends ConfiguredAspectFactory> aspect) throws Exception {
    AspectDefinition aspectDefinition = aspect == null
        ? null
        : AspectFactory.Util.create(aspect).getDefinition();
    Target target = packageManager.getTarget(reporter, Label.parseAbsolute(targetName));
    return dependencyResolver.dependentNodeMap(
        new TargetAndConfiguration(target, getTargetConfiguration()),
        getHostConfiguration(),
        aspectDefinition,
        AspectParameters.EMPTY,
        ImmutableSet.<ConfigMatchingProvider>of());
  }

  @SafeVarargs
  private final void assertDep(
      ListMultimap<Attribute, Dependency> dependentNodeMap,
      String attrName,
      String dep,
      AspectWithParameters... aspects) {
    Attribute attr = null;
    for (Attribute candidate : dependentNodeMap.keySet()) {
      if (candidate.getName().equals(attrName)) {
        attr = candidate;
        break;
      }
    }

    assertNotNull("Attribute '" + attrName + "' not found", attr);
    Dependency dependency = null;
    for (Dependency candidate : dependentNodeMap.get(attr)) {
      if (candidate.getLabel().toString().equals(dep)) {
        dependency = candidate;
        break;
      }
    }

    assertNotNull("Dependency '" + dep + "' on attribute '" + attrName + "' not found", dependency);
    assertThat(dependency.getAspects()).containsExactly((Object[]) aspects);
  }

  @Test
  public void hasAspectsRequiredByRule() throws Exception {
    setRules(new AspectRequiringRule(), new TestAspects.BaseRule());
    pkg("a",
        "aspect(name='a', foo=[':b'])",
        "aspect(name='b', foo=[])");
    ListMultimap<Attribute, Dependency> map = dependentNodeMap("//a:a", null);
    assertDep(map, "foo", "//a:b", new AspectWithParameters(TestAspects.SimpleAspect.class));
  }

  @Test
  public void hasAspectsRequiredByAspect() throws Exception {
    setRules(new TestAspects.BaseRule(), new TestAspects.SimpleRule());
    pkg("a",
        "simple(name='a', foo=[':b'])",
        "simple(name='b', foo=[])");
    ListMultimap<Attribute, Dependency> map =
        dependentNodeMap("//a:a", TestAspects.AttributeAspect.class);
    assertDep(map, "foo", "//a:b", new AspectWithParameters(TestAspects.AttributeAspect.class));
  }

  @Test
  public void hasAspectDependencies() throws Exception {
    setRules(new TestAspects.BaseRule());
    pkg("a", "base(name='a')");
    pkg("extra", "base(name='extra')");
    ListMultimap<Attribute, Dependency> map =
        dependentNodeMap("//a:a", TestAspects.ExtraAttributeAspect.class);
    assertDep(map, "$dep", "//extra:extra");
  }

  @Test
  public void constructorsForDependencyPassNullableTester() throws Exception {
    update();

    new NullPointerTester()
        .setDefault(Label.class, Label.parseAbsolute("//a"))
        .setDefault(BuildConfiguration.class, getTargetConfiguration())
        .setDefault(ImmutableSet.class, ImmutableSet.of())
        .testAllPublicConstructors(Dependency.class);
  }

  @Test
  public void equalsOnDependencyPassesEqualsTester() throws Exception {
    update();

    Label a = Label.parseAbsolute("//a");
    Label aExplicit = Label.parseAbsolute("//a:a");
    Label b = Label.parseAbsolute("//b");

    BuildConfiguration host = getHostConfiguration();
    BuildConfiguration target = getTargetConfiguration();

    ImmutableSet<AspectWithParameters> twoAspects = ImmutableSet.of(
        new AspectWithParameters(TestAspects.SimpleAspect.class), 
        new AspectWithParameters(TestAspects.AttributeAspect.class));
    ImmutableSet<AspectWithParameters> inverseAspects = ImmutableSet.of(
        new AspectWithParameters(TestAspects.AttributeAspect.class), 
        new AspectWithParameters(TestAspects.SimpleAspect.class));
    ImmutableSet<AspectWithParameters> differentAspects = ImmutableSet.of(
        new AspectWithParameters(TestAspects.AttributeAspect.class), 
        new AspectWithParameters(TestAspects.ErrorAspect.class));

    new EqualsTester()
        .addEqualityGroup(
            // base set: //a, host configuration, normal aspect set
            new Dependency(a, host, twoAspects),
            new Dependency(aExplicit, host, twoAspects),
            new Dependency(a, host, inverseAspects),
            new Dependency(aExplicit, host, inverseAspects))
        .addEqualityGroup(
            // base set but with label //b
            new Dependency(b, host, twoAspects),
            new Dependency(b, host, inverseAspects))
        .addEqualityGroup(
            // base set but with target configuration
            new Dependency(a, target, twoAspects),
            new Dependency(aExplicit, target, twoAspects),
            new Dependency(a, target, inverseAspects),
            new Dependency(aExplicit, target, inverseAspects))
        .addEqualityGroup(
            // base set but with null configuration
            new Dependency(a, (BuildConfiguration) null, twoAspects),
            new Dependency(aExplicit, (BuildConfiguration) null, twoAspects),
            new Dependency(a, (BuildConfiguration) null, inverseAspects),
            new Dependency(aExplicit, (BuildConfiguration) null, inverseAspects))
        .addEqualityGroup(
            // base set but with different aspects
            new Dependency(a, host, differentAspects),
            new Dependency(aExplicit, host, differentAspects))
        .addEqualityGroup(
            // base set but with label //b and target configuration
            new Dependency(b, target, twoAspects),
            new Dependency(b, target, inverseAspects))
        .addEqualityGroup(
            // base set but with label //b and null configuration
            new Dependency(b, (BuildConfiguration) null, twoAspects),
            new Dependency(b, (BuildConfiguration) null, inverseAspects))
        .addEqualityGroup(
            // base set but with label //b and different aspects
            new Dependency(b, host, differentAspects))
        .addEqualityGroup(
            // base set but with target configuration and different aspects
            new Dependency(a, target, differentAspects),
            new Dependency(aExplicit, target, differentAspects))
        .addEqualityGroup(
            // base set but with null configuration and different aspects
            new Dependency(a, (BuildConfiguration) null, differentAspects),
            new Dependency(aExplicit, (BuildConfiguration) null, differentAspects))
        .addEqualityGroup(
            // inverse of base set: //b, target configuration, different aspects
            new Dependency(b, target, differentAspects))
        .addEqualityGroup(
            // inverse of base set: //b, null configuration, different aspects
            new Dependency(b, (BuildConfiguration) null, differentAspects))
        .testEquals();
  }
}