blob: 6bce873cb1c4b68324dc2f32326cbadecfd899a7 [file] [log] [blame]
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +00001// Copyright 2015 The Bazel Authors. All rights reserved.
Ulf Adams83763ee2015-05-04 15:36:12 +00002//
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.
14package com.google.devtools.build.lib.packages;
15
16import static com.google.common.truth.Truth.assertThat;
Ulf Adams83763ee2015-05-04 15:36:12 +000017import static com.google.devtools.build.lib.packages.Attribute.attr;
Lukacs Berkiffa73ad2015-09-18 11:40:12 +000018import static com.google.devtools.build.lib.packages.BuildType.LABEL;
19import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST;
Googlerc5fcc862019-09-06 16:17:47 -070020import static com.google.devtools.build.lib.packages.Type.INTEGER;
21import static com.google.devtools.build.lib.packages.Type.STRING;
22import static com.google.devtools.build.lib.packages.Type.STRING_LIST;
michajlo660d17f2020-03-27 09:01:57 -070023import static org.junit.Assert.assertThrows;
Ulf Adams83763ee2015-05-04 15:36:12 +000024
25import com.google.common.base.Predicates;
messaf8c34082021-06-04 02:51:04 -070026import com.google.common.collect.ImmutableList;
dannark90e2b4b2018-06-27 13:35:04 -070027import com.google.common.collect.ImmutableMap;
messaf8c34082021-06-04 02:51:04 -070028import com.google.common.collect.ImmutableSet;
Chris Parsons2e62c0d2016-04-19 22:13:59 +000029import com.google.devtools.build.lib.analysis.config.BuildOptions;
gregcea048f0f2020-06-11 15:10:01 -070030import com.google.devtools.build.lib.analysis.config.BuildOptionsView;
gregce676a9572017-12-21 11:33:32 -080031import com.google.devtools.build.lib.analysis.config.HostTransition;
jcater98a09b62019-04-02 13:06:19 -070032import com.google.devtools.build.lib.analysis.config.TransitionFactories;
John Cater5adcd3e2019-03-28 10:14:32 -070033import com.google.devtools.build.lib.analysis.config.transitions.ConfigurationTransition;
gregce6bc35ed2017-12-22 11:51:39 -080034import com.google.devtools.build.lib.analysis.config.transitions.SplitTransition;
John Cater0a9e1ed2019-03-27 11:02:01 -070035import com.google.devtools.build.lib.analysis.config.transitions.TransitionFactory;
Carmi Grushko215fa842016-03-31 18:14:39 +000036import com.google.devtools.build.lib.analysis.util.TestAspects;
Lukacs Berki6e91eb92015-09-21 09:12:37 +000037import com.google.devtools.build.lib.cmdline.Label;
gregcef0a40ac2020-03-31 14:11:30 -070038import com.google.devtools.build.lib.events.EventHandler;
Googler72f3a102017-12-01 16:28:28 -080039import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassNamePredicate;
John Cater0a9e1ed2019-03-27 11:02:01 -070040import com.google.devtools.build.lib.testutil.FakeAttributeMapper;
Ulf Adams83763ee2015-05-04 15:36:12 +000041import com.google.devtools.build.lib.util.FileType;
42import com.google.devtools.build.lib.util.FileTypeSet;
lberkiaea56b32017-05-30 12:35:33 +020043import java.util.Arrays;
44import java.util.Collections;
45import java.util.List;
Googler19226b72020-02-06 12:58:43 -080046import java.util.Map;
adonovan3ed7ed52020-09-30 12:03:28 -070047import net.starlark.java.eval.StarlarkInt;
Ulf Adams83763ee2015-05-04 15:36:12 +000048import org.junit.Test;
49import org.junit.runner.RunWith;
50import org.junit.runners.JUnit4;
51
lberkiaea56b32017-05-30 12:35:33 +020052/** Tests of Attribute code. */
Ulf Adams83763ee2015-05-04 15:36:12 +000053@RunWith(JUnit4.class)
54public class AttributeTest {
55
56 private void assertDefaultValue(Object expected, Attribute attr) {
lberkiaea56b32017-05-30 12:35:33 +020057 assertThat(attr.getDefaultValue(null)).isEqualTo(expected);
Ulf Adams83763ee2015-05-04 15:36:12 +000058 }
59
60 private void assertType(Type<?> expectedType, Attribute attr) {
lberkiaea56b32017-05-30 12:35:33 +020061 assertThat(attr.getType()).isEqualTo(expectedType);
Ulf Adams83763ee2015-05-04 15:36:12 +000062 }
63
64 @Test
65 public void testBasics() throws Exception {
adonovan3ed7ed52020-09-30 12:03:28 -070066 Attribute attr = attr("foo", Type.INTEGER).mandatory().value(StarlarkInt.of(3)).build();
lberkiaea56b32017-05-30 12:35:33 +020067 assertThat(attr.getName()).isEqualTo("foo");
adonovan3ed7ed52020-09-30 12:03:28 -070068 assertThat(attr.getDefaultValue(null)).isEqualTo(StarlarkInt.of(3));
lberkiaea56b32017-05-30 12:35:33 +020069 assertThat(attr.getType()).isEqualTo(Type.INTEGER);
70 assertThat(attr.isMandatory()).isTrue();
71 assertThat(attr.isDocumented()).isTrue();
Ulf Adams83763ee2015-05-04 15:36:12 +000072 attr = attr("$foo", Type.INTEGER).build();
lberkiaea56b32017-05-30 12:35:33 +020073 assertThat(attr.isDocumented()).isFalse();
Ulf Adams83763ee2015-05-04 15:36:12 +000074 }
75
76 @Test
77 public void testNonEmptyReqiresListType() throws Exception {
jcaterb9226772019-04-29 12:04:52 -070078 NullPointerException e =
79 assertThrows(
80 NullPointerException.class,
adonovan3ed7ed52020-09-30 12:03:28 -070081 () -> attr("foo", Type.INTEGER).nonEmpty().value(StarlarkInt.of(3)).build());
jcaterb9226772019-04-29 12:04:52 -070082 assertThat(e).hasMessageThat().isEqualTo("attribute 'foo' must be a list");
Ulf Adams83763ee2015-05-04 15:36:12 +000083 }
84
85 @Test
86 public void testNonEmpty() throws Exception {
Lukacs Berkiffa73ad2015-09-18 11:40:12 +000087 Attribute attr = attr("foo", BuildType.LABEL_LIST).nonEmpty().legacyAllowAnyFileType().build();
lberkiaea56b32017-05-30 12:35:33 +020088 assertThat(attr.getName()).isEqualTo("foo");
89 assertThat(attr.getType()).isEqualTo(BuildType.LABEL_LIST);
90 assertThat(attr.isNonEmpty()).isTrue();
Ulf Adams83763ee2015-05-04 15:36:12 +000091 }
92
93 @Test
94 public void testSingleArtifactReqiresLabelType() throws Exception {
jcaterb9226772019-04-29 12:04:52 -070095 IllegalStateException e =
96 assertThrows(
97 IllegalStateException.class,
adonovan3ed7ed52020-09-30 12:03:28 -070098 () -> attr("foo", Type.INTEGER).singleArtifact().value(StarlarkInt.of(3)).build());
jcaterb9226772019-04-29 12:04:52 -070099 assertThat(e).hasMessageThat().isEqualTo("attribute 'foo' must be a label-valued type");
Ulf Adams83763ee2015-05-04 15:36:12 +0000100 }
101
102 @Test
103 public void testDoublePropertySet() {
jcaterffb65c82019-03-29 07:52:16 -0700104 Attribute.Builder<String> builder =
105 attr("x", STRING)
106 .mandatory()
107 .cfg(HostTransition.createFactory())
108 .undocumented("")
109 .value("y");
jcaterb9226772019-04-29 12:04:52 -0700110 assertThrows(IllegalStateException.class, () -> builder.mandatory());
111 assertThrows(IllegalStateException.class, () -> builder.cfg(HostTransition.createFactory()));
112 assertThrows(IllegalStateException.class, () -> builder.undocumented(""));
113 assertThrows(IllegalStateException.class, () -> builder.value("z"));
Ulf Adams83763ee2015-05-04 15:36:12 +0000114
jcaterb9226772019-04-29 12:04:52 -0700115 Attribute.Builder<String> builder2 = attr("$x", STRING);
116 assertThrows(IllegalStateException.class, () -> builder2.undocumented(""));
Ulf Adams83763ee2015-05-04 15:36:12 +0000117 }
118
119 /**
120 * Tests the "convenience factories" (string, label, etc) for default
121 * values.
122 */
123 @Test
124 public void testConvenienceFactoriesDefaultValues() throws Exception {
adonovan3ed7ed52020-09-30 12:03:28 -0700125 assertDefaultValue(StarlarkInt.of(0), attr("x", INTEGER).build());
126 assertDefaultValue(StarlarkInt.of(42), attr("x", INTEGER).value(StarlarkInt.of(42)).build());
Ulf Adams83763ee2015-05-04 15:36:12 +0000127
128 assertDefaultValue("",
129 attr("x", STRING).build());
130 assertDefaultValue("foo",
131 attr("x", STRING).value("foo").build());
132
dannark90e2b4b2018-06-27 13:35:04 -0700133 Label label = Label.parseAbsolute("//foo:bar", ImmutableMap.of());
Ulf Adams83763ee2015-05-04 15:36:12 +0000134 assertDefaultValue(null,
135 attr("x", LABEL).legacyAllowAnyFileType().build());
136 assertDefaultValue(label,
137 attr("x", LABEL).legacyAllowAnyFileType().value(label).build());
138
139 List<String> slist = Arrays.asList("foo", "bar");
140 assertDefaultValue(Collections.emptyList(),
141 attr("x", STRING_LIST).build());
142 assertDefaultValue(slist,
143 attr("x", STRING_LIST).value(slist).build());
144
dannark90e2b4b2018-06-27 13:35:04 -0700145 List<Label> llist =
146 Arrays.asList(
147 Label.parseAbsolute("//foo:bar", ImmutableMap.of()),
148 Label.parseAbsolute("//foo:wiz", ImmutableMap.of()));
Ulf Adams83763ee2015-05-04 15:36:12 +0000149 assertDefaultValue(Collections.emptyList(),
150 attr("x", LABEL_LIST).legacyAllowAnyFileType().build());
151 assertDefaultValue(llist,
152 attr("x", LABEL_LIST).legacyAllowAnyFileType().value(llist).build());
153 }
154
155 /**
156 * Tests the "convenience factories" (string, label, etc) for types.
157 */
158 @Test
159 public void testConvenienceFactoriesTypes() throws Exception {
160 assertType(INTEGER,
161 attr("x", INTEGER).build());
adonovan3ed7ed52020-09-30 12:03:28 -0700162 assertType(INTEGER, attr("x", INTEGER).value(StarlarkInt.of(42)).build());
Ulf Adams83763ee2015-05-04 15:36:12 +0000163
164 assertType(STRING,
165 attr("x", STRING).build());
166 assertType(STRING,
167 attr("x", STRING).value("foo").build());
168
dannark90e2b4b2018-06-27 13:35:04 -0700169 Label label = Label.parseAbsolute("//foo:bar", ImmutableMap.of());
Ulf Adams83763ee2015-05-04 15:36:12 +0000170 assertType(LABEL,
171 attr("x", LABEL).legacyAllowAnyFileType().build());
172 assertType(LABEL,
173 attr("x", LABEL).legacyAllowAnyFileType().value(label).build());
174
175 List<String> slist = Arrays.asList("foo", "bar");
176 assertType(STRING_LIST,
177 attr("x", STRING_LIST).build());
178 assertType(STRING_LIST,
179 attr("x", STRING_LIST).value(slist).build());
180
dannark90e2b4b2018-06-27 13:35:04 -0700181 List<Label> llist =
182 Arrays.asList(
183 Label.parseAbsolute("//foo:bar", ImmutableMap.of()),
184 Label.parseAbsolute("//foo:wiz", ImmutableMap.of()));
Ulf Adams83763ee2015-05-04 15:36:12 +0000185 assertType(LABEL_LIST,
186 attr("x", LABEL_LIST).legacyAllowAnyFileType().build());
187 assertType(LABEL_LIST,
188 attr("x", LABEL_LIST).legacyAllowAnyFileType().value(llist).build());
189 }
190
191 @Test
192 public void testCloneBuilder() {
193 FileTypeSet txtFiles = FileTypeSet.of(FileType.of("txt"));
Googler72f3a102017-12-01 16:28:28 -0800194 RuleClassNamePredicate ruleClasses = RuleClassNamePredicate.only("mock_rule");
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +0000195
Carmi Grushko215fa842016-03-31 18:14:39 +0000196 Attribute parentAttr =
197 attr("x", LABEL_LIST)
198 .allowedFileTypes(txtFiles)
199 .mandatory()
Luis Fernando Pino Duquee82713d2016-04-26 16:22:38 +0000200 .aspect(TestAspects.SIMPLE_ASPECT)
Carmi Grushko215fa842016-03-31 18:14:39 +0000201 .build();
Han-Wen Nienhuys3428dc92015-10-21 15:03:34 +0000202
Carmi Grushko215fa842016-03-31 18:14:39 +0000203 {
204 Attribute childAttr1 = parentAttr.cloneBuilder().build();
lberkiaea56b32017-05-30 12:35:33 +0200205 assertThat(childAttr1.getName()).isEqualTo("x");
206 assertThat(childAttr1.getAllowedFileTypesPredicate()).isEqualTo(txtFiles);
207 assertThat(childAttr1.getAllowedRuleClassesPredicate()).isEqualTo(Predicates.alwaysTrue());
208 assertThat(childAttr1.isMandatory()).isTrue();
209 assertThat(childAttr1.isNonEmpty()).isFalse();
cushon8c6b7ab2018-04-27 01:25:50 -0700210 assertThat(childAttr1.getAspects(/* rule= */ null)).hasSize(1);
Carmi Grushko215fa842016-03-31 18:14:39 +0000211 }
Ulf Adams83763ee2015-05-04 15:36:12 +0000212
Carmi Grushko215fa842016-03-31 18:14:39 +0000213 {
214 Attribute childAttr2 =
215 parentAttr
216 .cloneBuilder()
217 .nonEmpty()
218 .allowedRuleClasses(ruleClasses)
Luis Fernando Pino Duquee82713d2016-04-26 16:22:38 +0000219 .aspect(TestAspects.ERROR_ASPECT)
Carmi Grushko215fa842016-03-31 18:14:39 +0000220 .build();
lberkiaea56b32017-05-30 12:35:33 +0200221 assertThat(childAttr2.getName()).isEqualTo("x");
222 assertThat(childAttr2.getAllowedFileTypesPredicate()).isEqualTo(txtFiles);
Googler72f3a102017-12-01 16:28:28 -0800223 assertThat(childAttr2.getAllowedRuleClassesPredicate())
224 .isEqualTo(ruleClasses.asPredicateOfRuleClass());
lberkiaea56b32017-05-30 12:35:33 +0200225 assertThat(childAttr2.isMandatory()).isTrue();
226 assertThat(childAttr2.isNonEmpty()).isTrue();
cushon8c6b7ab2018-04-27 01:25:50 -0700227 assertThat(childAttr2.getAspects(/* rule= */ null)).hasSize(2);
Carmi Grushko215fa842016-03-31 18:14:39 +0000228 }
Ulf Adams83763ee2015-05-04 15:36:12 +0000229
lberkiaea56b32017-05-30 12:35:33 +0200230 // Check if the parent attribute is unchanged
231 assertThat(parentAttr.isNonEmpty()).isFalse();
232 assertThat(parentAttr.getAllowedRuleClassesPredicate()).isEqualTo(Predicates.alwaysTrue());
Ulf Adams83763ee2015-05-04 15:36:12 +0000233 }
234
235 /**
236 * Tests that configurability settings are properly received.
237 */
238 @Test
239 public void testConfigurability() {
lberkiaea56b32017-05-30 12:35:33 +0200240 assertThat(
241 attr("foo_configurable", BuildType.LABEL_LIST)
242 .legacyAllowAnyFileType()
243 .build()
244 .isConfigurable())
245 .isTrue();
246 assertThat(
247 attr("foo_nonconfigurable", BuildType.LABEL_LIST)
248 .legacyAllowAnyFileType()
249 .nonconfigurable("test")
250 .build()
251 .isConfigurable())
252 .isFalse();
Ulf Adams83763ee2015-05-04 15:36:12 +0000253 }
Chris Parsons2e62c0d2016-04-19 22:13:59 +0000254
255 @Test
256 public void testSplitTransition() throws Exception {
257 TestSplitTransition splitTransition = new TestSplitTransition();
jcater98a09b62019-04-02 13:06:19 -0700258 Attribute attr =
259 attr("foo", LABEL).cfg(TransitionFactories.of(splitTransition)).allowedFileTypes().build();
jcatere8f5a982019-04-02 11:12:19 -0700260 assertThat(attr.getTransitionFactory().isSplit()).isTrue();
John Cater5adcd3e2019-03-28 10:14:32 -0700261 ConfigurationTransition transition =
John Cater2c0dece2019-04-02 09:18:18 -0700262 attr.getTransitionFactory()
jcater23589462019-05-20 08:51:24 -0700263 .create(
264 AttributeTransitionData.builder().attributes(FakeAttributeMapper.empty()).build());
John Cater5adcd3e2019-03-28 10:14:32 -0700265 assertThat(transition).isEqualTo(splitTransition);
Chris Parsons2e62c0d2016-04-19 22:13:59 +0000266 }
267
268 @Test
269 public void testSplitTransitionProvider() throws Exception {
270 TestSplitTransitionProvider splitTransitionProvider = new TestSplitTransitionProvider();
271 Attribute attr =
272 attr("foo", LABEL).cfg(splitTransitionProvider).allowedFileTypes().build();
jcatere8f5a982019-04-02 11:12:19 -0700273 assertThat(attr.getTransitionFactory().isSplit()).isTrue();
John Cater5adcd3e2019-03-28 10:14:32 -0700274 ConfigurationTransition transition =
John Cater2c0dece2019-04-02 09:18:18 -0700275 attr.getTransitionFactory()
jcater23589462019-05-20 08:51:24 -0700276 .create(
277 AttributeTransitionData.builder().attributes(FakeAttributeMapper.empty()).build());
John Cater5adcd3e2019-03-28 10:14:32 -0700278 assertThat(transition).isInstanceOf(TestSplitTransition.class);
Chris Parsons2e62c0d2016-04-19 22:13:59 +0000279 }
280
281 @Test
282 public void testHostTransition() throws Exception {
jcaterffb65c82019-03-29 07:52:16 -0700283 Attribute attr =
284 attr("foo", LABEL).cfg(HostTransition.createFactory()).allowedFileTypes().build();
jcaterb44167f2019-04-02 12:06:26 -0700285 assertThat(attr.getTransitionFactory().isHost()).isTrue();
jcatere8f5a982019-04-02 11:12:19 -0700286 assertThat(attr.getTransitionFactory().isSplit()).isFalse();
Chris Parsons2e62c0d2016-04-19 22:13:59 +0000287 }
288
gregce6bc35ed2017-12-22 11:51:39 -0800289 private static class TestSplitTransition implements SplitTransition {
Chris Parsons2e62c0d2016-04-19 22:13:59 +0000290 @Override
gregcea048f0f2020-06-11 15:10:01 -0700291 public Map<String, BuildOptions> split(
292 BuildOptionsView buildOptions, EventHandler eventHandler) {
293 return ImmutableMap.of(
294 "test0", buildOptions.clone().underlying(), "test1", buildOptions.clone().underlying());
Chris Parsons2e62c0d2016-04-19 22:13:59 +0000295 }
296 }
297
John Cater0a9e1ed2019-03-27 11:02:01 -0700298 private static class TestSplitTransitionProvider
John Cater2c0dece2019-04-02 09:18:18 -0700299 implements TransitionFactory<AttributeTransitionData> {
Chris Parsons2e62c0d2016-04-19 22:13:59 +0000300 @Override
John Cater2c0dece2019-04-02 09:18:18 -0700301 public SplitTransition create(AttributeTransitionData data) {
Chris Parsons2e62c0d2016-04-19 22:13:59 +0000302 return new TestSplitTransition();
303 }
cparsons21e25182019-01-15 16:00:26 -0800304
305 @Override
John Cater04288282021-07-28 10:36:20 -0700306 public TransitionType transitionType() {
307 return TransitionType.ATTRIBUTE;
308 }
309
310 @Override
John Cater0a9e1ed2019-03-27 11:02:01 -0700311 public boolean isSplit() {
312 return true;
313 }
Chris Parsons2e62c0d2016-04-19 22:13:59 +0000314 }
gregceda4c9592017-07-27 22:09:34 +0200315
316 @Test
317 public void allowedRuleClassesAndAllowedRuleClassesWithWarningsCannotOverlap() throws Exception {
jcaterb9226772019-04-29 12:04:52 -0700318 IllegalStateException e =
319 assertThrows(
320 IllegalStateException.class,
321 () ->
322 attr("x", LABEL_LIST)
323 .allowedRuleClasses("foo", "bar", "baz")
324 .allowedRuleClassesWithWarning("bar")
325 .allowedFileTypes()
326 .build());
327 assertThat(e).hasMessageThat().contains("may not contain the same rule classes");
gregceda4c9592017-07-27 22:09:34 +0200328 }
messaf8c34082021-06-04 02:51:04 -0700329
330 private static final Label FAKE_LABEL = Label.parseAbsoluteUnchecked("//fake/label.bzl");
331
332 private static final StarlarkProviderIdentifier STARLARK_P1 =
333 StarlarkProviderIdentifier.forKey(new StarlarkProvider.Key(FAKE_LABEL, "STARLARK_P1"));
334
335 private static final StarlarkProviderIdentifier STARLARK_P2 =
336 StarlarkProviderIdentifier.forKey(new StarlarkProvider.Key(FAKE_LABEL, "STARLARK_P2"));
337
338 private static final StarlarkProviderIdentifier STARLARK_P3 =
339 StarlarkProviderIdentifier.forKey(new StarlarkProvider.Key(FAKE_LABEL, "STARLARK_P3"));
340
341 private static final StarlarkProviderIdentifier STARLARK_P4 =
342 StarlarkProviderIdentifier.forKey(new StarlarkProvider.Key(FAKE_LABEL, "STARLARK_P4"));
343
344 @Test
345 public void testAttrRequiredAspects_inheritAttrAspects() throws Exception {
346 ImmutableList<String> inheritedAttributeAspects1 = ImmutableList.of("attr1", "attr2");
347 ImmutableList<String> inheritedAttributeAspects2 = ImmutableList.of("attr3", "attr2");
348
messa14694d1c2021-07-06 05:58:31 -0700349 Attribute.Builder<Label> attrBuilder = attr("x", LABEL).allowedFileTypes();
350 attrBuilder
351 .getAspectsListBuilder()
352 .addAspect(
353 TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT,
354 "base_aspect_1",
355 /** inheritedRequiredProviders= */
356 ImmutableList.of(),
357 inheritedAttributeAspects1);
358 attrBuilder
359 .getAspectsListBuilder()
360 .addAspect(
361 TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT,
362 "base_aspect_2",
363 /** inheritedRequiredProviders= */
364 ImmutableList.of(),
365 inheritedAttributeAspects2);
366 Attribute attr = attrBuilder.build();
messaf8c34082021-06-04 02:51:04 -0700367
368 ImmutableList<Aspect> aspects = attr.getAspects(null);
369 assertThat(aspects).hasSize(1);
370 AspectDescriptor aspectDescriptor = aspects.get(0).getDescriptor();
371 assertThat(aspectDescriptor.getInheritedAttributeAspects())
372 .containsExactly("attr1", "attr2", "attr3");
373 }
374
375 @Test
376 public void testAttrRequiredAspects_inheritRequiredProviders() throws Exception {
377 ImmutableList<ImmutableSet<StarlarkProviderIdentifier>> inheritedRequiredProviders1 =
378 ImmutableList.of(ImmutableSet.of(STARLARK_P1), ImmutableSet.of(STARLARK_P2, STARLARK_P3));
379 ImmutableList<ImmutableSet<StarlarkProviderIdentifier>> inheritedRequiredProviders2 =
380 ImmutableList.of(ImmutableSet.of(STARLARK_P4), ImmutableSet.of(STARLARK_P2, STARLARK_P3));
381
messa14694d1c2021-07-06 05:58:31 -0700382 Attribute.Builder<Label> attrBuilder = attr("x", LABEL).allowedFileTypes();
383 attrBuilder
384 .getAspectsListBuilder()
385 .addAspect(
386 TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT,
387 "base_aspect_1",
388 inheritedRequiredProviders1,
389 /** inheritedAttributeAspects= */
390 ImmutableList.of());
391 attrBuilder
392 .getAspectsListBuilder()
393 .addAspect(
394 TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT,
395 "base_aspect_2",
396 inheritedRequiredProviders2,
397 /** inheritedAttributeAspects= */
398 ImmutableList.of());
399 Attribute attr = attrBuilder.build();
messaf8c34082021-06-04 02:51:04 -0700400
401 ImmutableList<Aspect> aspects = attr.getAspects(null);
402 assertThat(aspects).hasSize(1);
403
404 RequiredProviders actualInheritedRequiredProviders =
405 aspects.get(0).getDescriptor().getInheritedRequiredProviders();
406 AdvertisedProviderSet expectedOkSet1 =
407 AdvertisedProviderSet.builder().addStarlark(STARLARK_P1).build();
408 assertThat(actualInheritedRequiredProviders.isSatisfiedBy(expectedOkSet1)).isTrue();
409
410 AdvertisedProviderSet expectedOkSet2 =
411 AdvertisedProviderSet.builder().addStarlark(STARLARK_P4).build();
412 assertThat(actualInheritedRequiredProviders.isSatisfiedBy(expectedOkSet2)).isTrue();
413
414 AdvertisedProviderSet expectedOkSet3 =
415 AdvertisedProviderSet.builder().addStarlark(STARLARK_P2).addStarlark(STARLARK_P3).build();
416 assertThat(actualInheritedRequiredProviders.isSatisfiedBy(expectedOkSet3)).isTrue();
417
418 assertThat(actualInheritedRequiredProviders.isSatisfiedBy(AdvertisedProviderSet.ANY)).isTrue();
419 assertThat(actualInheritedRequiredProviders.isSatisfiedBy(AdvertisedProviderSet.EMPTY))
420 .isFalse();
421 }
422
423 @Test
424 public void testAttrRequiredAspects_aspectAlreadyExists_inheritAttrAspects() throws Exception {
425 ImmutableList<String> inheritedAttributeAspects = ImmutableList.of("attr1", "attr2");
426
messa14694d1c2021-07-06 05:58:31 -0700427 Attribute.Builder<Label> attrBuilder =
428 attr("x", LABEL).aspect(TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT).allowedFileTypes();
429 attrBuilder
430 .getAspectsListBuilder()
431 .addAspect(
432 TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT,
433 "base_aspect",
434 /** inheritedRequiredProviders = */
435 ImmutableList.of(),
436 inheritedAttributeAspects);
437 Attribute attr = attrBuilder.build();
messaf8c34082021-06-04 02:51:04 -0700438
439 ImmutableList<Aspect> aspects = attr.getAspects(null);
440 assertThat(aspects).hasSize(1);
441 AspectDescriptor aspectDescriptor = aspects.get(0).getDescriptor();
442 assertThat(aspectDescriptor.getInheritedAttributeAspects()).containsExactly("attr1", "attr2");
443 }
444
445 @Test
446 public void testAttrRequiredAspects_aspectAlreadyExists_inheritRequiredProviders()
447 throws Exception {
448 ImmutableList<ImmutableSet<StarlarkProviderIdentifier>> inheritedRequiredProviders =
449 ImmutableList.of(ImmutableSet.of(STARLARK_P1), ImmutableSet.of(STARLARK_P2, STARLARK_P3));
450
messa14694d1c2021-07-06 05:58:31 -0700451 Attribute.Builder<Label> attrBuilder =
452 attr("x", LABEL).aspect(TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT).allowedFileTypes();
453 attrBuilder
454 .getAspectsListBuilder()
455 .addAspect(
456 TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT,
457 "base_aspect",
458 inheritedRequiredProviders,
459 /** inheritedAttributeAspects= */
460 ImmutableList.of());
461 Attribute attr = attrBuilder.build();
messaf8c34082021-06-04 02:51:04 -0700462
463 ImmutableList<Aspect> aspects = attr.getAspects(null);
464 assertThat(aspects).hasSize(1);
465
466 RequiredProviders actualInheritedRequiredProviders =
467 aspects.get(0).getDescriptor().getInheritedRequiredProviders();
468 AdvertisedProviderSet expectedOkSet1 =
469 AdvertisedProviderSet.builder().addStarlark(STARLARK_P1).build();
470 assertThat(actualInheritedRequiredProviders.isSatisfiedBy(expectedOkSet1)).isTrue();
471
472 AdvertisedProviderSet expectedOkSet2 =
473 AdvertisedProviderSet.builder().addStarlark(STARLARK_P4).build();
474 assertThat(actualInheritedRequiredProviders.isSatisfiedBy(expectedOkSet2)).isFalse();
475
476 AdvertisedProviderSet expectedOkSet3 =
477 AdvertisedProviderSet.builder().addStarlark(STARLARK_P2).addStarlark(STARLARK_P3).build();
478 assertThat(actualInheritedRequiredProviders.isSatisfiedBy(expectedOkSet3)).isTrue();
479
480 assertThat(actualInheritedRequiredProviders.isSatisfiedBy(AdvertisedProviderSet.ANY)).isTrue();
481 assertThat(actualInheritedRequiredProviders.isSatisfiedBy(AdvertisedProviderSet.EMPTY))
482 .isFalse();
483 }
484
485 @Test
486 public void testAttrRequiredAspects_inheritAllAttrAspects() throws Exception {
487 ImmutableList<String> inheritedAttributeAspects1 = ImmutableList.of("attr1", "attr2");
488 ImmutableList<String> inheritedAttributeAspects2 = ImmutableList.of("*");
489
messa14694d1c2021-07-06 05:58:31 -0700490 Attribute.Builder<Label> attrBuilder =
491 attr("x", LABEL).aspect(TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT).allowedFileTypes();
492 attrBuilder
493 .getAspectsListBuilder()
494 .addAspect(
495 TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT,
496 "base_aspect_1",
497 /** inheritedRequiredProviders = */
498 ImmutableList.of(),
499 inheritedAttributeAspects1);
500 attrBuilder
501 .getAspectsListBuilder()
502 .addAspect(
503 TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT,
504 "base_aspect_2",
505 /** inheritedRequiredProviders = */
506 ImmutableList.of(),
507 inheritedAttributeAspects2);
508 Attribute attr = attrBuilder.build();
messaf8c34082021-06-04 02:51:04 -0700509
510 ImmutableList<Aspect> aspects = attr.getAspects(null);
511 assertThat(aspects).hasSize(1);
512 AspectDescriptor aspectDescriptor = aspects.get(0).getDescriptor();
513 assertThat(aspectDescriptor.getInheritedAttributeAspects()).isNull();
514 }
515
516 @Test
517 public void testAttrRequiredAspects_inheritAllRequiredProviders() throws Exception {
518 ImmutableList<ImmutableSet<StarlarkProviderIdentifier>> inheritedRequiredProviders1 =
519 ImmutableList.of();
520 ImmutableList<ImmutableSet<StarlarkProviderIdentifier>> inheritedRequiredProviders2 =
521 ImmutableList.of(ImmutableSet.of(STARLARK_P4), ImmutableSet.of(STARLARK_P2, STARLARK_P3));
522
messa14694d1c2021-07-06 05:58:31 -0700523 Attribute.Builder<Label> attrBuilder =
524 attr("x", LABEL).aspect(TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT).allowedFileTypes();
525 attrBuilder
526 .getAspectsListBuilder()
527 .addAspect(
528 TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT,
529 "base_aspect_1",
530 inheritedRequiredProviders1,
531 /** inheritedAttributeAspects= */
532 ImmutableList.of());
533 attrBuilder
534 .getAspectsListBuilder()
535 .addAspect(
536 TestAspects.SIMPLE_STARLARK_NATIVE_ASPECT,
537 "base_aspect_2",
538 inheritedRequiredProviders2,
539 /** inheritedAttributeAspects= */
540 ImmutableList.of());
541 Attribute attr = attrBuilder.build();
messaf8c34082021-06-04 02:51:04 -0700542
543 ImmutableList<Aspect> aspects = attr.getAspects(null);
544 assertThat(aspects).hasSize(1);
545 AspectDescriptor aspectDescriptor = aspects.get(0).getDescriptor();
546 assertThat(aspectDescriptor.getInheritedRequiredProviders())
547 .isEqualTo(RequiredProviders.acceptAnyBuilder().build());
548 }
549
550 @Test
551 public void testAttrRequiredAspects_defaultInheritedRequiredProvidersAndAttrAspects()
552 throws Exception {
553 Attribute attr = attr("x", LABEL).aspect(TestAspects.SIMPLE_ASPECT).allowedFileTypes().build();
554
555 ImmutableList<Aspect> aspects = attr.getAspects(null);
556 assertThat(aspects).hasSize(1);
557 AspectDescriptor aspectDescriptor = aspects.get(0).getDescriptor();
558 assertThat(aspectDescriptor.getInheritedAttributeAspects()).isEmpty();
messaed7ddfa2021-06-09 05:45:34 -0700559 assertThat(aspectDescriptor.getInheritedRequiredProviders()).isNull();
messaf8c34082021-06-04 02:51:04 -0700560 }
Ulf Adams83763ee2015-05-04 15:36:12 +0000561}