Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 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 | package com.google.devtools.build.lib.packages; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 17 | import static com.google.devtools.build.lib.packages.Attribute.attr; |
Lukacs Berki | ffa73ad | 2015-09-18 11:40:12 +0000 | [diff] [blame] | 18 | import static com.google.devtools.build.lib.packages.BuildType.LABEL; |
| 19 | import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST; |
Googler | c5fcc86 | 2019-09-06 16:17:47 -0700 | [diff] [blame] | 20 | import static com.google.devtools.build.lib.packages.Type.INTEGER; |
| 21 | import static com.google.devtools.build.lib.packages.Type.STRING; |
| 22 | import static com.google.devtools.build.lib.packages.Type.STRING_LIST; |
michajlo | 660d17f | 2020-03-27 09:01:57 -0700 | [diff] [blame] | 23 | import static org.junit.Assert.assertThrows; |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 24 | |
| 25 | import com.google.common.base.Predicates; |
dannark | 90e2b4b | 2018-06-27 13:35:04 -0700 | [diff] [blame] | 26 | import com.google.common.collect.ImmutableMap; |
Chris Parsons | 2e62c0d | 2016-04-19 22:13:59 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.analysis.config.BuildOptions; |
gregce | a048f0f | 2020-06-11 15:10:01 -0700 | [diff] [blame] | 28 | import com.google.devtools.build.lib.analysis.config.BuildOptionsView; |
gregce | 676a957 | 2017-12-21 11:33:32 -0800 | [diff] [blame] | 29 | import com.google.devtools.build.lib.analysis.config.HostTransition; |
jcater | 98a09b6 | 2019-04-02 13:06:19 -0700 | [diff] [blame] | 30 | import com.google.devtools.build.lib.analysis.config.TransitionFactories; |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 31 | import com.google.devtools.build.lib.analysis.config.transitions.ConfigurationTransition; |
gregce | 6bc35ed | 2017-12-22 11:51:39 -0800 | [diff] [blame] | 32 | import com.google.devtools.build.lib.analysis.config.transitions.SplitTransition; |
John Cater | 0a9e1ed | 2019-03-27 11:02:01 -0700 | [diff] [blame] | 33 | import com.google.devtools.build.lib.analysis.config.transitions.TransitionFactory; |
Carmi Grushko | 215fa84 | 2016-03-31 18:14:39 +0000 | [diff] [blame] | 34 | import com.google.devtools.build.lib.analysis.util.TestAspects; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 35 | import com.google.devtools.build.lib.cmdline.Label; |
gregce | f0a40ac | 2020-03-31 14:11:30 -0700 | [diff] [blame] | 36 | import com.google.devtools.build.lib.events.EventHandler; |
Googler | 72f3a10 | 2017-12-01 16:28:28 -0800 | [diff] [blame] | 37 | import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassNamePredicate; |
John Cater | 0a9e1ed | 2019-03-27 11:02:01 -0700 | [diff] [blame] | 38 | import com.google.devtools.build.lib.testutil.FakeAttributeMapper; |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 39 | import com.google.devtools.build.lib.util.FileType; |
| 40 | import com.google.devtools.build.lib.util.FileTypeSet; |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 41 | import java.util.Arrays; |
| 42 | import java.util.Collections; |
| 43 | import java.util.List; |
Googler | 19226b7 | 2020-02-06 12:58:43 -0800 | [diff] [blame] | 44 | import java.util.Map; |
adonovan | 3ed7ed5 | 2020-09-30 12:03:28 -0700 | [diff] [blame] | 45 | import net.starlark.java.eval.StarlarkInt; |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 46 | import org.junit.Test; |
| 47 | import org.junit.runner.RunWith; |
| 48 | import org.junit.runners.JUnit4; |
| 49 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 50 | /** Tests of Attribute code. */ |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 51 | @RunWith(JUnit4.class) |
| 52 | public class AttributeTest { |
| 53 | |
| 54 | private void assertDefaultValue(Object expected, Attribute attr) { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 55 | assertThat(attr.getDefaultValue(null)).isEqualTo(expected); |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | private void assertType(Type<?> expectedType, Attribute attr) { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 59 | assertThat(attr.getType()).isEqualTo(expectedType); |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void testBasics() throws Exception { |
adonovan | 3ed7ed5 | 2020-09-30 12:03:28 -0700 | [diff] [blame] | 64 | Attribute attr = attr("foo", Type.INTEGER).mandatory().value(StarlarkInt.of(3)).build(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 65 | assertThat(attr.getName()).isEqualTo("foo"); |
adonovan | 3ed7ed5 | 2020-09-30 12:03:28 -0700 | [diff] [blame] | 66 | assertThat(attr.getDefaultValue(null)).isEqualTo(StarlarkInt.of(3)); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 67 | assertThat(attr.getType()).isEqualTo(Type.INTEGER); |
| 68 | assertThat(attr.isMandatory()).isTrue(); |
| 69 | assertThat(attr.isDocumented()).isTrue(); |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 70 | attr = attr("$foo", Type.INTEGER).build(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 71 | assertThat(attr.isDocumented()).isFalse(); |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Test |
| 75 | public void testNonEmptyReqiresListType() throws Exception { |
jcater | b922677 | 2019-04-29 12:04:52 -0700 | [diff] [blame] | 76 | NullPointerException e = |
| 77 | assertThrows( |
| 78 | NullPointerException.class, |
adonovan | 3ed7ed5 | 2020-09-30 12:03:28 -0700 | [diff] [blame] | 79 | () -> attr("foo", Type.INTEGER).nonEmpty().value(StarlarkInt.of(3)).build()); |
jcater | b922677 | 2019-04-29 12:04:52 -0700 | [diff] [blame] | 80 | assertThat(e).hasMessageThat().isEqualTo("attribute 'foo' must be a list"); |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Test |
| 84 | public void testNonEmpty() throws Exception { |
Lukacs Berki | ffa73ad | 2015-09-18 11:40:12 +0000 | [diff] [blame] | 85 | Attribute attr = attr("foo", BuildType.LABEL_LIST).nonEmpty().legacyAllowAnyFileType().build(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 86 | assertThat(attr.getName()).isEqualTo("foo"); |
| 87 | assertThat(attr.getType()).isEqualTo(BuildType.LABEL_LIST); |
| 88 | assertThat(attr.isNonEmpty()).isTrue(); |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Test |
| 92 | public void testSingleArtifactReqiresLabelType() throws Exception { |
jcater | b922677 | 2019-04-29 12:04:52 -0700 | [diff] [blame] | 93 | IllegalStateException e = |
| 94 | assertThrows( |
| 95 | IllegalStateException.class, |
adonovan | 3ed7ed5 | 2020-09-30 12:03:28 -0700 | [diff] [blame] | 96 | () -> attr("foo", Type.INTEGER).singleArtifact().value(StarlarkInt.of(3)).build()); |
jcater | b922677 | 2019-04-29 12:04:52 -0700 | [diff] [blame] | 97 | assertThat(e).hasMessageThat().isEqualTo("attribute 'foo' must be a label-valued type"); |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void testDoublePropertySet() { |
jcater | ffb65c8 | 2019-03-29 07:52:16 -0700 | [diff] [blame] | 102 | Attribute.Builder<String> builder = |
| 103 | attr("x", STRING) |
| 104 | .mandatory() |
| 105 | .cfg(HostTransition.createFactory()) |
| 106 | .undocumented("") |
| 107 | .value("y"); |
jcater | b922677 | 2019-04-29 12:04:52 -0700 | [diff] [blame] | 108 | assertThrows(IllegalStateException.class, () -> builder.mandatory()); |
| 109 | assertThrows(IllegalStateException.class, () -> builder.cfg(HostTransition.createFactory())); |
| 110 | assertThrows(IllegalStateException.class, () -> builder.undocumented("")); |
| 111 | assertThrows(IllegalStateException.class, () -> builder.value("z")); |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 112 | |
jcater | b922677 | 2019-04-29 12:04:52 -0700 | [diff] [blame] | 113 | Attribute.Builder<String> builder2 = attr("$x", STRING); |
| 114 | assertThrows(IllegalStateException.class, () -> builder2.undocumented("")); |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Tests the "convenience factories" (string, label, etc) for default |
| 119 | * values. |
| 120 | */ |
| 121 | @Test |
| 122 | public void testConvenienceFactoriesDefaultValues() throws Exception { |
adonovan | 3ed7ed5 | 2020-09-30 12:03:28 -0700 | [diff] [blame] | 123 | assertDefaultValue(StarlarkInt.of(0), attr("x", INTEGER).build()); |
| 124 | assertDefaultValue(StarlarkInt.of(42), attr("x", INTEGER).value(StarlarkInt.of(42)).build()); |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 125 | |
| 126 | assertDefaultValue("", |
| 127 | attr("x", STRING).build()); |
| 128 | assertDefaultValue("foo", |
| 129 | attr("x", STRING).value("foo").build()); |
| 130 | |
dannark | 90e2b4b | 2018-06-27 13:35:04 -0700 | [diff] [blame] | 131 | Label label = Label.parseAbsolute("//foo:bar", ImmutableMap.of()); |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 132 | assertDefaultValue(null, |
| 133 | attr("x", LABEL).legacyAllowAnyFileType().build()); |
| 134 | assertDefaultValue(label, |
| 135 | attr("x", LABEL).legacyAllowAnyFileType().value(label).build()); |
| 136 | |
| 137 | List<String> slist = Arrays.asList("foo", "bar"); |
| 138 | assertDefaultValue(Collections.emptyList(), |
| 139 | attr("x", STRING_LIST).build()); |
| 140 | assertDefaultValue(slist, |
| 141 | attr("x", STRING_LIST).value(slist).build()); |
| 142 | |
dannark | 90e2b4b | 2018-06-27 13:35:04 -0700 | [diff] [blame] | 143 | List<Label> llist = |
| 144 | Arrays.asList( |
| 145 | Label.parseAbsolute("//foo:bar", ImmutableMap.of()), |
| 146 | Label.parseAbsolute("//foo:wiz", ImmutableMap.of())); |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 147 | assertDefaultValue(Collections.emptyList(), |
| 148 | attr("x", LABEL_LIST).legacyAllowAnyFileType().build()); |
| 149 | assertDefaultValue(llist, |
| 150 | attr("x", LABEL_LIST).legacyAllowAnyFileType().value(llist).build()); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Tests the "convenience factories" (string, label, etc) for types. |
| 155 | */ |
| 156 | @Test |
| 157 | public void testConvenienceFactoriesTypes() throws Exception { |
| 158 | assertType(INTEGER, |
| 159 | attr("x", INTEGER).build()); |
adonovan | 3ed7ed5 | 2020-09-30 12:03:28 -0700 | [diff] [blame] | 160 | assertType(INTEGER, attr("x", INTEGER).value(StarlarkInt.of(42)).build()); |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 161 | |
| 162 | assertType(STRING, |
| 163 | attr("x", STRING).build()); |
| 164 | assertType(STRING, |
| 165 | attr("x", STRING).value("foo").build()); |
| 166 | |
dannark | 90e2b4b | 2018-06-27 13:35:04 -0700 | [diff] [blame] | 167 | Label label = Label.parseAbsolute("//foo:bar", ImmutableMap.of()); |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 168 | assertType(LABEL, |
| 169 | attr("x", LABEL).legacyAllowAnyFileType().build()); |
| 170 | assertType(LABEL, |
| 171 | attr("x", LABEL).legacyAllowAnyFileType().value(label).build()); |
| 172 | |
| 173 | List<String> slist = Arrays.asList("foo", "bar"); |
| 174 | assertType(STRING_LIST, |
| 175 | attr("x", STRING_LIST).build()); |
| 176 | assertType(STRING_LIST, |
| 177 | attr("x", STRING_LIST).value(slist).build()); |
| 178 | |
dannark | 90e2b4b | 2018-06-27 13:35:04 -0700 | [diff] [blame] | 179 | List<Label> llist = |
| 180 | Arrays.asList( |
| 181 | Label.parseAbsolute("//foo:bar", ImmutableMap.of()), |
| 182 | Label.parseAbsolute("//foo:wiz", ImmutableMap.of())); |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 183 | assertType(LABEL_LIST, |
| 184 | attr("x", LABEL_LIST).legacyAllowAnyFileType().build()); |
| 185 | assertType(LABEL_LIST, |
| 186 | attr("x", LABEL_LIST).legacyAllowAnyFileType().value(llist).build()); |
| 187 | } |
| 188 | |
| 189 | @Test |
| 190 | public void testCloneBuilder() { |
| 191 | FileTypeSet txtFiles = FileTypeSet.of(FileType.of("txt")); |
Googler | 72f3a10 | 2017-12-01 16:28:28 -0800 | [diff] [blame] | 192 | RuleClassNamePredicate ruleClasses = RuleClassNamePredicate.only("mock_rule"); |
Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 193 | |
Carmi Grushko | 215fa84 | 2016-03-31 18:14:39 +0000 | [diff] [blame] | 194 | Attribute parentAttr = |
| 195 | attr("x", LABEL_LIST) |
| 196 | .allowedFileTypes(txtFiles) |
| 197 | .mandatory() |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 198 | .aspect(TestAspects.SIMPLE_ASPECT) |
Carmi Grushko | 215fa84 | 2016-03-31 18:14:39 +0000 | [diff] [blame] | 199 | .build(); |
Han-Wen Nienhuys | 3428dc9 | 2015-10-21 15:03:34 +0000 | [diff] [blame] | 200 | |
Carmi Grushko | 215fa84 | 2016-03-31 18:14:39 +0000 | [diff] [blame] | 201 | { |
| 202 | Attribute childAttr1 = parentAttr.cloneBuilder().build(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 203 | assertThat(childAttr1.getName()).isEqualTo("x"); |
| 204 | assertThat(childAttr1.getAllowedFileTypesPredicate()).isEqualTo(txtFiles); |
| 205 | assertThat(childAttr1.getAllowedRuleClassesPredicate()).isEqualTo(Predicates.alwaysTrue()); |
| 206 | assertThat(childAttr1.isMandatory()).isTrue(); |
| 207 | assertThat(childAttr1.isNonEmpty()).isFalse(); |
cushon | 8c6b7ab | 2018-04-27 01:25:50 -0700 | [diff] [blame] | 208 | assertThat(childAttr1.getAspects(/* rule= */ null)).hasSize(1); |
Carmi Grushko | 215fa84 | 2016-03-31 18:14:39 +0000 | [diff] [blame] | 209 | } |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 210 | |
Carmi Grushko | 215fa84 | 2016-03-31 18:14:39 +0000 | [diff] [blame] | 211 | { |
| 212 | Attribute childAttr2 = |
| 213 | parentAttr |
| 214 | .cloneBuilder() |
| 215 | .nonEmpty() |
| 216 | .allowedRuleClasses(ruleClasses) |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 217 | .aspect(TestAspects.ERROR_ASPECT) |
Carmi Grushko | 215fa84 | 2016-03-31 18:14:39 +0000 | [diff] [blame] | 218 | .build(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 219 | assertThat(childAttr2.getName()).isEqualTo("x"); |
| 220 | assertThat(childAttr2.getAllowedFileTypesPredicate()).isEqualTo(txtFiles); |
Googler | 72f3a10 | 2017-12-01 16:28:28 -0800 | [diff] [blame] | 221 | assertThat(childAttr2.getAllowedRuleClassesPredicate()) |
| 222 | .isEqualTo(ruleClasses.asPredicateOfRuleClass()); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 223 | assertThat(childAttr2.isMandatory()).isTrue(); |
| 224 | assertThat(childAttr2.isNonEmpty()).isTrue(); |
cushon | 8c6b7ab | 2018-04-27 01:25:50 -0700 | [diff] [blame] | 225 | assertThat(childAttr2.getAspects(/* rule= */ null)).hasSize(2); |
Carmi Grushko | 215fa84 | 2016-03-31 18:14:39 +0000 | [diff] [blame] | 226 | } |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 227 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 228 | // Check if the parent attribute is unchanged |
| 229 | assertThat(parentAttr.isNonEmpty()).isFalse(); |
| 230 | assertThat(parentAttr.getAllowedRuleClassesPredicate()).isEqualTo(Predicates.alwaysTrue()); |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Tests that configurability settings are properly received. |
| 235 | */ |
| 236 | @Test |
| 237 | public void testConfigurability() { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 238 | assertThat( |
| 239 | attr("foo_configurable", BuildType.LABEL_LIST) |
| 240 | .legacyAllowAnyFileType() |
| 241 | .build() |
| 242 | .isConfigurable()) |
| 243 | .isTrue(); |
| 244 | assertThat( |
| 245 | attr("foo_nonconfigurable", BuildType.LABEL_LIST) |
| 246 | .legacyAllowAnyFileType() |
| 247 | .nonconfigurable("test") |
| 248 | .build() |
| 249 | .isConfigurable()) |
| 250 | .isFalse(); |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 251 | } |
Chris Parsons | 2e62c0d | 2016-04-19 22:13:59 +0000 | [diff] [blame] | 252 | |
| 253 | @Test |
| 254 | public void testSplitTransition() throws Exception { |
| 255 | TestSplitTransition splitTransition = new TestSplitTransition(); |
jcater | 98a09b6 | 2019-04-02 13:06:19 -0700 | [diff] [blame] | 256 | Attribute attr = |
| 257 | attr("foo", LABEL).cfg(TransitionFactories.of(splitTransition)).allowedFileTypes().build(); |
jcater | e8f5a98 | 2019-04-02 11:12:19 -0700 | [diff] [blame] | 258 | assertThat(attr.getTransitionFactory().isSplit()).isTrue(); |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 259 | ConfigurationTransition transition = |
John Cater | 2c0dece | 2019-04-02 09:18:18 -0700 | [diff] [blame] | 260 | attr.getTransitionFactory() |
jcater | 2358946 | 2019-05-20 08:51:24 -0700 | [diff] [blame] | 261 | .create( |
| 262 | AttributeTransitionData.builder().attributes(FakeAttributeMapper.empty()).build()); |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 263 | assertThat(transition).isEqualTo(splitTransition); |
Chris Parsons | 2e62c0d | 2016-04-19 22:13:59 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | @Test |
| 267 | public void testSplitTransitionProvider() throws Exception { |
| 268 | TestSplitTransitionProvider splitTransitionProvider = new TestSplitTransitionProvider(); |
| 269 | Attribute attr = |
| 270 | attr("foo", LABEL).cfg(splitTransitionProvider).allowedFileTypes().build(); |
jcater | e8f5a98 | 2019-04-02 11:12:19 -0700 | [diff] [blame] | 271 | assertThat(attr.getTransitionFactory().isSplit()).isTrue(); |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 272 | ConfigurationTransition transition = |
John Cater | 2c0dece | 2019-04-02 09:18:18 -0700 | [diff] [blame] | 273 | attr.getTransitionFactory() |
jcater | 2358946 | 2019-05-20 08:51:24 -0700 | [diff] [blame] | 274 | .create( |
| 275 | AttributeTransitionData.builder().attributes(FakeAttributeMapper.empty()).build()); |
John Cater | 5adcd3e | 2019-03-28 10:14:32 -0700 | [diff] [blame] | 276 | assertThat(transition).isInstanceOf(TestSplitTransition.class); |
Chris Parsons | 2e62c0d | 2016-04-19 22:13:59 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | @Test |
| 280 | public void testHostTransition() throws Exception { |
jcater | ffb65c8 | 2019-03-29 07:52:16 -0700 | [diff] [blame] | 281 | Attribute attr = |
| 282 | attr("foo", LABEL).cfg(HostTransition.createFactory()).allowedFileTypes().build(); |
jcater | b44167f | 2019-04-02 12:06:26 -0700 | [diff] [blame] | 283 | assertThat(attr.getTransitionFactory().isHost()).isTrue(); |
jcater | e8f5a98 | 2019-04-02 11:12:19 -0700 | [diff] [blame] | 284 | assertThat(attr.getTransitionFactory().isSplit()).isFalse(); |
Chris Parsons | 2e62c0d | 2016-04-19 22:13:59 +0000 | [diff] [blame] | 285 | } |
| 286 | |
gregce | 6bc35ed | 2017-12-22 11:51:39 -0800 | [diff] [blame] | 287 | private static class TestSplitTransition implements SplitTransition { |
Chris Parsons | 2e62c0d | 2016-04-19 22:13:59 +0000 | [diff] [blame] | 288 | @Override |
gregce | a048f0f | 2020-06-11 15:10:01 -0700 | [diff] [blame] | 289 | public Map<String, BuildOptions> split( |
| 290 | BuildOptionsView buildOptions, EventHandler eventHandler) { |
| 291 | return ImmutableMap.of( |
| 292 | "test0", buildOptions.clone().underlying(), "test1", buildOptions.clone().underlying()); |
Chris Parsons | 2e62c0d | 2016-04-19 22:13:59 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
John Cater | 0a9e1ed | 2019-03-27 11:02:01 -0700 | [diff] [blame] | 296 | private static class TestSplitTransitionProvider |
John Cater | 2c0dece | 2019-04-02 09:18:18 -0700 | [diff] [blame] | 297 | implements TransitionFactory<AttributeTransitionData> { |
Chris Parsons | 2e62c0d | 2016-04-19 22:13:59 +0000 | [diff] [blame] | 298 | @Override |
John Cater | 2c0dece | 2019-04-02 09:18:18 -0700 | [diff] [blame] | 299 | public SplitTransition create(AttributeTransitionData data) { |
Chris Parsons | 2e62c0d | 2016-04-19 22:13:59 +0000 | [diff] [blame] | 300 | return new TestSplitTransition(); |
| 301 | } |
cparsons | 21e2518 | 2019-01-15 16:00:26 -0800 | [diff] [blame] | 302 | |
| 303 | @Override |
John Cater | 0a9e1ed | 2019-03-27 11:02:01 -0700 | [diff] [blame] | 304 | public boolean isSplit() { |
| 305 | return true; |
| 306 | } |
Chris Parsons | 2e62c0d | 2016-04-19 22:13:59 +0000 | [diff] [blame] | 307 | } |
gregce | da4c959 | 2017-07-27 22:09:34 +0200 | [diff] [blame] | 308 | |
| 309 | @Test |
| 310 | public void allowedRuleClassesAndAllowedRuleClassesWithWarningsCannotOverlap() throws Exception { |
jcater | b922677 | 2019-04-29 12:04:52 -0700 | [diff] [blame] | 311 | IllegalStateException e = |
| 312 | assertThrows( |
| 313 | IllegalStateException.class, |
| 314 | () -> |
| 315 | attr("x", LABEL_LIST) |
| 316 | .allowedRuleClasses("foo", "bar", "baz") |
| 317 | .allowedRuleClassesWithWarning("bar") |
| 318 | .allowedFileTypes() |
| 319 | .build()); |
| 320 | assertThat(e).hasMessageThat().contains("may not contain the same rule classes"); |
gregce | da4c959 | 2017-07-27 22:09:34 +0200 | [diff] [blame] | 321 | } |
Ulf Adams | 83763ee | 2015-05-04 15:36:12 +0000 | [diff] [blame] | 322 | } |