8000 Move analysis/LateBoundAttributeTest from google private to public. · coderabbit-test/bazel@8dfaa4f · GitHub
[go: up one dir, main page]

Skip to content

Commit 8dfaa4f

Browse files
aiutocopybara-github
authored andcommitted
Move analysis/LateBoundAttributeTest from google private to public.
RELNOTES: None PiperOrigin-RevId: 696989936 Change-Id: Ifbbd0c59fecf15efc72a4f00908b5ae3805940cf
1 parent d99595f commit 8dfaa4f

File tree

2 files changed

+250
-0
lines changed

2 files changed

+250
-0
lines changed

src/test/java/com/google/devtools/build/lib/analysis/BUILD

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ java_library(
3939
"ConfiguredTargetTransitivePackagesTest.java",
4040
"GeneratorConflictTest.java",
4141
"JDKJavaLauncherRunfilesSupportTest.java",
42+
"LateBoundAttributeTest.java",
4243
"PackageGroupBuildViewTest.java",
4344
"RuleConfiguredTargetTest.java",
4445
"RunfilesRepoMappingManifestTest.java",
@@ -435,6 +436,31 @@ java_test(
435436
],
436437
)
437438

439+
java_test(
440+
name = "LateBoundAttributeTest",
441+
srcs = ["LateBoundAttributeTest.java"],
442+
runtime_deps = [
443+
"//src/test/java/com/google/devtools/build/lib/analysis/mock",
444+
"//src/test/java/com/google/devtools/build/lib/view/util",
445+
],
446+
deps = [
447+
"//src/main/java/com/google/devtools/build/lib/analysis:analysis_cluster",
448+
"//src/main/java/com/google/devtools/build/lib/analysis:config/build_configuration",
449+
"//src/main/java/com/google/devtools/build/lib/cmdline",
450+
"//src/main/java/com/google/devtools/build/lib/collect/nestedset",
451+
"//src/main/java/com/google/devtools/build/lib/packages",
452+
"//src/main/java/com/google/devtools/build/lib/skyframe:transitive_target_key",
453+
"//src/main/java/com/google/devtools/build/lib/skyframe:transitive_target_value",
454+
"//src/main/java/com/google/devtools/build/skyframe",
455+
"//src/main/java/com/google/devtools/build/skyframe:skyframe-objects",
456+
"//src/test/java/com/google/devtools/build/lib/analysis/util",
457+
"//third_party:guava",
458+
"//third_party:junit4",
459+
"//third_party:mockito",
460+
"//third_party:truth",
461+
],
462+
)
463+
438464
test_suite(
439465
name = "AllAnalysisTests",
440466
tests = [
@@ -446,6 +472,7 @@ test_suite(
446472
":ConfiguredTargetTransitivePackagesTest",
447473
":GeneratorConflictTest",
448474
":JDKJavaLauncherRunfilesSupportTest",
475+
":LateBoundAttributeTest",
449476
":PackageGroupBuildViewTest",
450477
":RuleConfiguredTargetTest",
451478
":SourceManifestActionTest",
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
// Copyright 2024 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.devtools.build.lib.analysis;
16+
17+
import static com.google.common.truth.Truth.assertThat;
18+
19+
import com.google.common.collect.ImmutableSet;
20+
import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue;
21+
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
22+
import com.google.devtools.build.lib.cmdline.Label;
23+
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
24+
import com.google.devtools.build.lib.packages.Attribute;
25+
import com.google.devtools.build.lib.packages.Attribute.LateBoundDefault;
26+
import com.google.devtools.build.lib.packages.AttributeMap;
27+
import com.google.devtools.build.lib.packages.BuildType;
28+
import com.google.devtools.build.lib.packages.RuleClass;
29+
import com.google.devtools.build.lib.skyframe.TransitiveTargetKey;
30+
import com.google.devtools.build.lib.skyframe.TransitiveTargetValue;
31+
import com.google.devtools.build.skyframe.EvaluationContext;
32+
import com.google.devtools.build.skyframe.EvaluationResult;
33+
import com.google.devtools.build.skyframe.SkyKey;
34+
import com.google.devtools.build.skyframe.SkyValue;
35+
import java.util.Arrays;
36+
import java.util.List;
37+
import org.junit.Rule;
38+
import org.junit.Test;
39+
import org.junit.runner.RunWith;
40+
import org.junit.runners.JUnit4;
41+
import org.mockito.Mock;
42+
import org.mockito.junit.MockitoJUnit;
43+
import org.mockito.junit.MockitoRule;
44+
45+
/**
46+
* Tests that late bound label declarations obey the invariant that the computed label is in the
47+
* transitive closure of the default label.
48+
*/
49+
@RunWith(JUnit4.class)
50+
public class LateBoundAttributeTest extends BuildViewTestCase {
51+
52+
/**
53+
* These attributes are only an exception because we can't easily test them: their default value
54+
* as determined by the {@link DependencyResolutionHelpers} depends on the rule, not just on the
55+
* rule class and configuration. The problem with the test below is that we don't instantiate
56+
* rules and the {@link #attributes} collection is just an empty mock.
57+
*/
58+
private static final ImmutableSet<String> ATTRIBUTE_EXCEPTIONS =
59+
ImmutableSet.of(":computed_cc_rpc_libs");
60+
61+
@Rule public final MockitoRule mocks = MockitoJUnit.rule();
62+
63+
@Mock private AttributeMap attributes;
64+
65+
@Test
66+
public void testInvariant() throws Exception {
67+
// This configuration makes java_proto_library and java_lite_proto_library's toolchain
68+
// attributes to have defaultValue == actualValue, which makes the test below skip them.
69+
// Otherwise, the test chokes bec F438 ause it tries to traverse the non-existent
70+
// tools/proto/toolchains/BUILD file.
71+
useConfiguration(
72+
"--proto_toolchain_for_java=//tools/proto/toolchains:java",
73+
"--proto_toolchain_for_javalite=//tools/proto/toolchains:javalite");
74+
75+
new LabelChecker(getTargetConfiguration())
76+
.checkRuleClasses(ruleClassProvider.getRuleClassMap().values());
77+
78+
new LabelChecker(getExecConfiguration())
79+
.checkRuleClasses(ruleClassProvider.getRuleClassMap().values());
80+
}
81+
82+
private class LabelChecker {
83+
private final BuildConfigurationValue configuration;
84+
private boolean failed;
85+
86+
LabelChecker(BuildConfigurationValue configuration) {
87+
this.configuration = configuration;
88+
}
89+
90+
void checkRuleClasses(Iterable<RuleClass> ruleClasses) throws Exception {
91+
for (RuleClass ruleClass : ruleClasses) {
92+
checkRuleClass(ruleClass);
93+
}
94+
// If this fails you need to check your rule class declarations.
95+
assertThat(failed).isFalse();
96+
}
97+
98+
private void checkRuleClass(RuleClass ruleClass) throws Exception {
99+
if (ruleClass.getName().startsWith("$")) {
100+
// Ignore abstract rule classes.
101+
return;
102+
}
103+
104+
for (Attribute attribute : ruleClass.getAttributes()) {
105+
checkAttribute(ruleClass, attribute);
106+
}
107+
}
108+
109+
private void checkAttribute(RuleClass ruleClass, Attribute attribute) throws Exception {
110+
String attributeName = attribute.getName();
111+
if (!Attribute.isAnalysisDependent(attributeName)) {
112+
return;
113+
}
114+
115+
if (ATTRIBUTE_EXCEPTIONS.contains(attributeName)) {
116+
return;
117+
}
118+
119+
if (attribute.getType() == BuildType.LABEL) {
120+
Label label;
121+
label =
122+
BuildType.LABEL.cast(
123+
DependencyResolutionHelpers.resolveLateBoundDefault(
124+
null, attributes, attribute, configuration));
125+
if (label != null) {
126+
checkLabel(ruleClass, attribute, label);
127+
}
128+
} else if (attribute.getType() == BuildType.LABEL_LIST) {
129+
List<Label> labels;
130+
labels =
131+
BuildType.LABEL_LIST.cast(
132+
DependencyResolutionHelpers.resolveLateBoundDefault(
133+
null, attributes, attribute, configuration));
134+
for (Label label : labels) {
135+
checkLabelList(ruleClass, attribute, label);
136+
}
137+
} else {
138+
throw new AssertionError("Unknown attribute: '" + attributeName + "'");
139+
}
140+
}
141+
142+
/**
143+
* We check that the label set by the {@link DependencyResolutionHelpers} with the default
144+
* configuration is in the transitive closure of the default value set in the rule class.
145+
*
146+
* <p>Branches created using the result of {@code "blaze query deps(//target)"} only work if all
147+
* labels loaded by blaze during the loading phase are also returned by this query. The check
148+
* here is a bit stricter than that, and disallows omitting the label if another attribute
149+
* already sets the same label.
150+
*/
151+
void checkLabel(RuleClass ruleClass, Attribute attribute, Label label) throws Exception {
152+
Label defaultValue;
153+
if (attribute.getDefaultValueUnchecked() instanceof LateBoundDefault<?, ?>) {
154+
defaultValue =
155+
BuildType.LABEL.cast(
156+
((LateBoundDefault<?, ?>) attribute.getDefaultValueUnchecked()).getDefault(null));
157+
} else {
158+
defaultValue = (Label) attribute.getDefaultValueUnchecked();
159+
}
160+
if ((defaultValue == null) || !existsPath(defaultValue, label)) {
161+
System.err.println("in " + ruleClass.getName() + " attribute " + attribute.getName() + ":");
162+
System.err.println(" " + label + " is not in the transitive closure of " + defaultValue);
163+
failed = true;
164+
}
165+
}
166+
167+
/**
168+
* Similar to {@link #checkLabel} except for we check that the label is reachable by *any*
169+
* value in the default value (doesn't need to be reachable by all values in the default).
170+
*/
171+
@SuppressWarnings("unchecked")
172+
void checkLabelList(RuleClass ruleClass, Attribute attribute, Label label) throws Exception {
173+
List<Label> defaultValues;
174+
if (attribute.getDefaultValueUnchecked() instanceof LateBoundDefault<?, ?>) {
175+
defaultValues =
176+
BuildType.LABEL_LIST.cast(
177+
((LateBoundDefault<?, ?>) attribute.getDefaultValueUnchecked()).getDefault(null));
178+
} else {
179+
defaultValues = (List<Label>) attribute.getDefaultValueUnchecked();
180+
}
181+
failed = true;
182+
if (defaultValues == null) {
183+
System.err.println("in " + ruleClass.getName() + " attribute " + attribute.getName() + ":");
184+
System.err.println(" no available default for this attribute");
185+
} else {
186+
for (Label defaultLabel : defaultValues) {
187+
if (existsPath(defaultLabel, label)) {
188+
failed = false;
189+
}
190+
}
191+
// label was not reachable from any label in the defaultValue
192+
if (failed) {
193+
System.out.println(
194+
"in " + ruleClass.getName() + " attribute " + attribute.getName() + ":");
195+
System.out.println(
196+
" " + label + " is not in the transitive closure of "
197+
+ Arrays.toString(defaultValues.toArray()));
198+
}
199+
}
200+
}
201+
202+
/**
203+
* Returns whether a path exists from the first given label to the second.
204+
*/
205+
private boolean existsPath(Label from, Label to) throws Exception {
206+
return from.equals(to) || visitTransitively(from).toList().contains(to);
207+
}
208+
209+
private NestedSet<Label> visitTransitively(Label label) throws InterruptedException {
210+
SkyKey key = TransitiveTargetKey.of(label);
211+
EvaluationContext evaluationContext =
212+
EvaluationContext.newBuilder().setParallelism(5).setEventHandler(reporter).build();
213+
EvaluationResult<SkyValue> result =
214+
getSkyframeExecutor().prepareAndGet(ImmutableSet.of(key), evaluationContext);
215+
TransitiveTargetValue value = (TransitiveTargetValue) result.get(key);
216+
boolean hasTransitiveError = (value == null) || value.encounteredLoadingError();
217+
if (result.hasError() || hasTransitiveError) {
218+
throw new RuntimeException(result.getError().getException());
219+
}
220+
return value.getTransitiveTargets();
221+
}
222+
}
223+
}

0 commit comments

Comments
 (0)
0