-
Notifications
You must be signed in to change notification settings - Fork 4k
/
ProtoOutputFormatterCallbackTest.java
236 lines (210 loc) · 9.52 KB
/
ProtoOutputFormatterCallbackTest.java
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
// Copyright 2018 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.query2.cquery;
import static com.google.common.truth.Truth.assertThat;
import static com.google.devtools.build.lib.packages.Attribute.attr;
import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST;
import com.google.common.collect.Iterables;
import com.google.common.eventbus.EventBus;
import com.google.devtools.build.lib.analysis.AnalysisProtosV2;
import com.google.devtools.build.lib.analysis.config.TransitionFactories;
import com.google.devtools.build.lib.analysis.util.MockRule;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.events.NullEventHandler;
import com.google.devtools.build.lib.events.Reporter;
import com.google.devtools.build.lib.query2.PostAnalysisQueryEnvironment;
import com.google.devtools.build.lib.query2.cquery.ProtoOutputFormatterCallback.OutputType;
import com.google.devtools.build.lib.query2.engine.QueryEnvironment.Setting;
import com.google.devtools.build.lib.query2.engine.QueryExpression;
import com.google.devtools.build.lib.query2.engine.QueryParser;
import com.google.devtools.build.lib.query2.proto.proto2api.Build;
import com.google.devtools.build.lib.query2.query.aspectresolvers.AspectResolver.Mode;
import com.google.devtools.build.lib.util.FileTypeSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
/**
* Test for cquery's proto output format.
*
* <p>TODO(blaze-configurability): refactor all cquery output format tests to consolidate duplicate
* infrastructure.
*/
public class ProtoOutputFormatterCallbackTest extends ConfiguredTargetQueryTest {
private CqueryOptions options;
private Reporter reporter;
private final List<Event> events = new ArrayList<>();
@Before
public final void setUpCqueryOptions() {
this.options = new CqueryOptions();
// TODO(bazel-team): reduce the confusion about these two seemingly similar settings.
// options.aspectDeps impacts how proto and similar output formatters output aspect results.
// Setting.INCLUDE_ASPECTS impacts whether or not aspect dependencies are included when
// following target deps. See CommonQueryOptions for further flag details.
options.aspectDeps = Mode.OFF;
helper.setQuerySettings(Setting.INCLUDE_ASPECTS);
options.protoIncludeConfigurations = true;
options.protoIncludeRuleInputsAndOutputs = true;
this.reporter = new Reporter(new EventBus(), events::add);
}
@Test
public void testSelectInAttribute() throws Exception {
MockRule depsRule =
() ->
MockRule.define(
"my_rule",
(builder, env) ->
builder
.add(attr("deps", LABEL_LIST).allowedFileTypes(FileTypeSet.ANY_FILE)));
helper.useRuleClassProvider(setRuleClassProviders(depsRule).build());
writeFile(
"test/BUILD",
"my_rule(name = 'my_rule',",
" deps = select({",
" ':garfield': ['lasagna.java', 'naps.java'],",
" '//conditions:default': ['mondays.java']",
" })",
")",
"config_setting(",
" name = 'garfield',",
" values = {'foo': 'cat'}",
")");
AnalysisProtosV2.ConfiguredTarget myRuleProto =
(Iterables.getOnlyElement(getOutput("//test:my_rule").getResultsList()));
List<Build.Attribute> attributes = myRuleProto.getTarget().getRule().getAttributeList();
for (Build.Attribute attribute : attributes) {
if (!attribute.getName().equals("deps")) {
continue;
}
assertThat(attribute.getStringListValueCount()).isEqualTo(1);
assertThat(attribute.getStringListValue(0)).isEqualTo("//test:mondays.java");
break;
}
getHelper().useConfiguration("--foo=cat");
myRuleProto = Iterables.getOnlyElement(getOutput("//test:my_rule").getResultsList());
attributes = myRuleProto.getTarget().getRule().getAttributeList();
for (Build.Attribute attribute : attributes) {
if (!attribute.getName().equals("deps")) {
continue;
}
assertThat(attribute.getStringListValueCount()).isEqualTo(2);
assertThat(attribute.getStringListValue(0)).isEqualTo("//test:lasagna.java");
assertThat(attribute.getStringListValue(1)).isEqualTo("//test:naps.java");
break;
}
}
@Test
public void testConfigurationHash() throws Exception {
FooPatchTransition attributePatchTransition = new FooPatchTransition("SET BY PATCH");
MockRule ruleWithPatch =
() ->
MockRule.define(
"my_rule",
(builder, env) ->
builder.add(
attr("deps", LABEL_LIST)
.allowedFileTypes(FileTypeSet.ANY_FILE)
.cfg(TransitionFactories.of(attributePatchTransition))));
helper.useRuleClassProvider(setRuleClassProviders(ruleWithPatch, getSimpleRule()).build());
writeFile(
"test/BUILD",
"my_rule(name = 'my_rule',",
" deps = [':patched'],",
")",
"simple_rule(name = 'dep')");
// Assert checksum from proto is proper checksum.
AnalysisProtosV2.ConfiguredTarget myRuleProto =
Iterables.getOnlyElement(getOutput("//test:my_rule").getResultsList());
KeyedConfiguredTarget myRule = Iterables.getOnlyElement(eval("//test:my_rule"));
assertThat(myRuleProto.getConfiguration().getChecksum())
.isEqualTo(myRule.getConfigurationChecksum());
// Assert checksum for two configured targets in proto are not the same.
List<AnalysisProtosV2.ConfiguredTarget> protoDeps =
getOutput("deps(//test:my_rule)").getResultsList();
assertThat(protoDeps).hasSize(2);
Iterator<AnalysisProtosV2.ConfiguredTarget> protoDepsIterator = protoDeps.iterator();
assertThat(protoDepsIterator.next().getConfiguration().getChecksum())
.isNotEqualTo(protoDepsIterator.next().getConfiguration().getChecksum());
}
@Test
public void testAlias() throws Exception {
helper.useRuleClassProvider(setRuleClassProviders(getSimpleRule()).build());
writeFile(
"test/BUILD",
"simple_rule(name = 'my_rule')",
"alias(name = 'my_alias', actual = ':my_rule')");
AnalysisProtosV2.ConfiguredTarget alias =
Iterables.getOnlyElement(getOutput("//test:my_alias").getResultsList());
assertThat(alias.getTarget().getRule().getName()).isEqualTo("//test:my_alias");
assertThat(alias.getTarget().getRule().getRuleInputCount()).isEqualTo(1);
assertThat(alias.getTarget().getRule().getRuleInput(0)).isEqualTo("//test:my_rule");
}
/* See b/209787345 for context. */
@Test
public void testAlias_withSelect() throws Exception {
helper.useRuleClassProvider(setRuleClassProviders(getSimpleRule()).build());
writeFile(
"test/BUILD",
"alias(",
" name = 'my_alias_rule',",
" actual = select({",
" ':config1': ':target1',",
" '//conditions:default': ':target2',",
" }),",
")",
"config_setting(",
" name = 'config1',",
" values = {'foo': 'woof'},",
")",
"simple_rule(name = 'target1')",
"simple_rule(name = 'target2')");
getHelper().useConfiguration("--foo=woof");
helper.setQuerySettings(Setting.NO_IMPLICIT_DEPS);
List<AnalysisProtosV2.ConfiguredTarget> myAliasRuleProto =
getOutput("deps(//test:my_alias_rule)").getResultsList();
List<String> depNames = new ArrayList<>(myAliasRuleProto.size());
myAliasRuleProto.forEach(
configuredTarget -> depNames.add(configuredTarget.getTarget().getRule().getName()));
assertThat(depNames)
// The alias also includes platform info since aliases with select() trigger toolchain
// resolution. We're not interested in those here.
.containsAtLeast("//test:my_alias_rule", "//test:config1", "//test:target1");
}
private MockRule getSimpleRule() {
return () -> MockRule.define("simple_rule");
}
private AnalysisProtosV2.CqueryResult getOutput(String queryExpression) throws Exception {
QueryExpression expression = QueryParser.parse(queryExpression, getDefaultFunctions());
Set<String> targetPatternSet = new LinkedHashSet<>();
expression.collectTargetPatterns(targetPatternSet);
helper.setQuerySettings(Setting.NO_IMPLICIT_DEPS);
PostAnalysisQueryEnvironment<KeyedConfiguredTarget> env =
((ConfiguredTargetQueryHelper) helper).getPostAnalysisQueryEnvironment(targetPatternSet);
ProtoOutputFormatterCallback callback =
new ProtoOutputFormatterCallback(
reporter,
options,
/*out=*/ null,
getHelper().getSkyframeExecutor(),
env.getAccessor(),
options.aspectDeps.createResolver(
getHelper().getPackageManager(), NullEventHandler.INSTANCE),
OutputType.BINARY);
env.evaluateQuery(expression, callback);
return callback.getProtoResult();
}
}