Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Ulf Adams | 8917925 | 2015-04-23 18:48:39 +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.analysis; |
| 15 | |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 16 | import static com.google.common.truth.Truth.assertThat; |
Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 17 | import static com.google.devtools.build.lib.packages.Attribute.attr; |
michajlo | 660d17f | 2020-03-27 09:01:57 -0700 | [diff] [blame] | 18 | import static org.junit.Assert.assertThrows; |
Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 19 | |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
Rumou Duan | 6f8393f | 2016-11-30 16:03:10 +0000 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
gregce | 614dc50 | 2017-12-20 17:24:46 -0800 | [diff] [blame] | 22 | import com.google.devtools.build.lib.analysis.config.ConfigAwareAspectBuilder; |
jcater | a666f00 | 2020-04-14 14:55:20 -0700 | [diff] [blame] | 23 | import com.google.devtools.build.lib.analysis.config.Fragment; |
gregce | c7b2121 | 2017-12-21 12:40:20 -0800 | [diff] [blame] | 24 | import com.google.devtools.build.lib.analysis.config.HostTransition; |
gregce | 7fa23ea | 2018-01-18 12:46:04 -0800 | [diff] [blame] | 25 | import com.google.devtools.build.lib.analysis.config.transitions.NoTransition; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.cmdline.Label; |
Dmitry Lomov | b5174c7 | 2017-01-10 10:57:17 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.packages.AdvertisedProviderSet; |
Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.packages.AspectDefinition; |
Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 29 | import com.google.devtools.build.lib.packages.AspectParameters; |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.packages.Attribute; |
janakr | a56a6ad | 2018-02-02 15:52:22 -0800 | [diff] [blame] | 31 | import com.google.devtools.build.lib.packages.Attribute.LabelLateBoundDefault; |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 32 | import com.google.devtools.build.lib.packages.Attribute.LateBoundDefault; |
Lukacs Berki | ffa73ad | 2015-09-18 11:40:12 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.packages.BuildType; |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 34 | import com.google.devtools.build.lib.packages.ConfigurationFragmentPolicy.MissingFragmentPolicy; |
| 35 | import com.google.devtools.build.lib.packages.NativeAspectClass; |
messa | 1bca1bd | 2021-05-14 00:48:19 -0700 | [diff] [blame] | 36 | import com.google.devtools.build.lib.packages.StarlarkProvider; |
| 37 | import com.google.devtools.build.lib.packages.StarlarkProviderIdentifier; |
janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 38 | import com.google.devtools.build.lib.skyframe.ConfiguredTargetAndData; |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 39 | import com.google.devtools.build.lib.util.FileTypeSet; |
adonovan | b017468 | 2020-05-18 16:01:53 -0700 | [diff] [blame] | 40 | import net.starlark.java.annot.StarlarkBuiltin; |
adonovan | 450c7ad | 2020-09-14 13:00:21 -0700 | [diff] [blame] | 41 | import net.starlark.java.eval.StarlarkValue; |
Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 42 | import org.junit.Test; |
| 43 | import org.junit.runner.RunWith; |
| 44 | import org.junit.runners.JUnit4; |
| 45 | |
| 46 | /** |
| 47 | * Tests for aspect definitions. |
| 48 | */ |
| 49 | @RunWith(JUnit4.class) |
| 50 | public class AspectDefinitionTest { |
Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 51 | |
ilist | 85e89a6 | 2020-04-24 08:52:25 -0700 | [diff] [blame] | 52 | private static final class P1 implements TransitiveInfoProvider {} |
| 53 | |
| 54 | private static final class P2 implements TransitiveInfoProvider {} |
| 55 | |
| 56 | private static final class P3 implements TransitiveInfoProvider {} |
| 57 | |
| 58 | private static final class P4 implements TransitiveInfoProvider {} |
| 59 | |
messa | 1bca1bd | 2021-05-14 00:48:19 -0700 | [diff] [blame] | 60 | private static final Label FAKE_LABEL = Label.parseAbsoluteUnchecked("//fake/label.bzl"); |
| 61 | |
| 62 | private static final StarlarkProviderIdentifier STARLARK_P1 = |
| 63 | StarlarkProviderIdentifier.forKey(new StarlarkProvider.Key(FAKE_LABEL, "STARLARK_P1")); |
| 64 | |
| 65 | private static final StarlarkProviderIdentifier STARLARK_P2 = |
| 66 | StarlarkProviderIdentifier.forKey(new StarlarkProvider.Key(FAKE_LABEL, "STARLARK_P2")); |
| 67 | |
| 68 | private static final StarlarkProviderIdentifier STARLARK_P3 = |
| 69 | StarlarkProviderIdentifier.forKey(new StarlarkProvider.Key(FAKE_LABEL, "STARLARK_P3")); |
| 70 | |
| 71 | private static final StarlarkProviderIdentifier STARLARK_P4 = |
| 72 | StarlarkProviderIdentifier.forKey(new StarlarkProvider.Key(FAKE_LABEL, "STARLARK_P4")); |
| 73 | |
Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 74 | /** |
| 75 | * A dummy aspect factory. Is there to demonstrate how to define aspects and so that we can test |
| 76 | * {@code attributeAspect}. |
| 77 | */ |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 78 | public static final class TestAspectClass extends NativeAspectClass |
| 79 | implements ConfiguredAspectFactory { |
| 80 | private AspectDefinition definition; |
Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 81 | |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 82 | public void setAspectDefinition(AspectDefinition definition) { |
Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 83 | this.definition = definition; |
| 84 | } |
| 85 | |
| 86 | @Override |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 87 | public ConfiguredAspect create( |
dannark | e75cfb3 | 2019-01-10 14:36:24 -0800 | [diff] [blame] | 88 | ConfiguredTargetAndData ctadBase, |
| 89 | RuleContext context, |
| 90 | AspectParameters parameters, |
| 91 | String toolsRepository) { |
Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 92 | throw new IllegalStateException(); |
| 93 | } |
| 94 | |
| 95 | @Override |
Dmitry Lomov | 6231d08 | 2015-11-02 17:17:20 +0000 | [diff] [blame] | 96 | public AspectDefinition getDefinition(AspectParameters aspectParameters) { |
Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 97 | return definition; |
| 98 | } |
| 99 | } |
| 100 | |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 101 | public static final TestAspectClass TEST_ASPECT_CLASS = new TestAspectClass(); |
| 102 | |
Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 103 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 104 | public void testAspectWithImplicitOrLateboundAttribute_addsToAttributeMap() throws Exception { |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 105 | Attribute implicit = attr("$runtime", BuildType.LABEL) |
| 106 | .value(Label.parseAbsoluteUnchecked("//run:time")) |
Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 107 | .build(); |
janakr | a56a6ad | 2018-02-02 15:52:22 -0800 | [diff] [blame] | 108 | LabelLateBoundDefault<Void> latebound = |
| 109 | LateBoundDefault.fromConstantForTesting(Label.parseAbsoluteUnchecked("//run:away")); |
Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 110 | AspectDefinition simple = new AspectDefinition.Builder(TEST_ASPECT_CLASS) |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 111 | .add(implicit) |
| 112 | .add(attr(":latebound", BuildType.LABEL).value(latebound)) |
| 113 | .build(); |
| 114 | assertThat(simple.getAttributes()).containsEntry("$runtime", implicit); |
| 115 | assertThat(simple.getAttributes()).containsKey(":latebound"); |
| 116 | assertThat(simple.getAttributes().get(":latebound").getLateBoundDefault()) |
| 117 | .isEqualTo(latebound); |
Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 121 | public void testAspectWithDuplicateAttribute_failsToAdd() throws Exception { |
jcater | 42edea6 | 2019-05-01 08:46:18 -0700 | [diff] [blame] | 122 | assertThrows( |
| 123 | IllegalArgumentException.class, |
| 124 | () -> |
| 125 | new AspectDefinition.Builder(TEST_ASPECT_CLASS) |
| 126 | .add( |
| 127 | attr("$runtime", BuildType.LABEL) |
| 128 | .value(Label.parseAbsoluteUnchecked("//run:time"))) |
| 129 | .add( |
| 130 | attr("$runtime", BuildType.LABEL) |
| 131 | .value(Label.parseAbsoluteUnchecked("//oops")))); |
Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 132 | } |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 133 | |
| 134 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 135 | public void testAspectWithUserVisibleAttribute_failsToAdd() throws Exception { |
jcater | 42edea6 | 2019-05-01 08:46:18 -0700 | [diff] [blame] | 136 | assertThrows( |
| 137 | IllegalArgumentException.class, |
| 138 | () -> |
| 139 | new AspectDefinition.Builder(TEST_ASPECT_CLASS) |
| 140 | .add( |
| 141 | attr("invalid", BuildType.LABEL) |
| 142 | .value(Label.parseAbsoluteUnchecked("//run:time")) |
| 143 | .allowedFileTypes(FileTypeSet.NO_FILE)) |
| 144 | .build()); |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 148 | public void testAttributeAspect_wrapsAndAddsToMap() throws Exception { |
Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 149 | AspectDefinition withAspects = new AspectDefinition.Builder(TEST_ASPECT_CLASS) |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 150 | .propagateAlongAttribute("srcs") |
| 151 | .propagateAlongAttribute("deps") |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 152 | .build(); |
Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 153 | |
lberki | 3bd2839 | 2019-02-05 01:01:02 -0800 | [diff] [blame] | 154 | assertThat(withAspects.propagateAlong("srcs")).isTrue(); |
| 155 | assertThat(withAspects.propagateAlong("deps")).isTrue(); |
Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 159 | public void testAttributeAspect_allAttributes() throws Exception { |
Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 160 | AspectDefinition withAspects = new AspectDefinition.Builder(TEST_ASPECT_CLASS) |
Dmitry Lomov | 0d38064 | 2017-01-20 14:39:55 +0000 | [diff] [blame] | 161 | .propagateAlongAllAttributes() |
Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 162 | .build(); |
| 163 | |
lberki | 3bd2839 | 2019-02-05 01:01:02 -0800 | [diff] [blame] | 164 | assertThat(withAspects.propagateAlong("srcs")).isTrue(); |
| 165 | assertThat(withAspects.propagateAlong("deps")).isTrue(); |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Test |
messa | 1bca1bd | 2021-05-14 00:48:19 -0700 | [diff] [blame] | 169 | public void testRequireBuiltinProviders_addsToSetOfRequiredProvidersAndNames() throws Exception { |
ilist | 85e89a6 | 2020-04-24 08:52:25 -0700 | [diff] [blame] | 170 | AspectDefinition requiresProviders = |
| 171 | new AspectDefinition.Builder(TEST_ASPECT_CLASS) |
| 172 | .requireProviders(P1.class, P2.class) |
| 173 | .build(); |
Dmitry Lomov | b5174c7 | 2017-01-10 10:57:17 +0000 | [diff] [blame] | 174 | AdvertisedProviderSet expectedOkSet = |
| 175 | AdvertisedProviderSet.builder() |
adonovan | dd00046 | 2020-11-06 12:57:38 -0800 | [diff] [blame] | 176 | .addBuiltin(P1.class) |
| 177 | .addBuiltin(P2.class) |
| 178 | .addBuiltin(P3.class) |
Dmitry Lomov | b5174c7 | 2017-01-10 10:57:17 +0000 | [diff] [blame] | 179 | .build(); |
| 180 | assertThat(requiresProviders.getRequiredProviders().isSatisfiedBy(expectedOkSet)) |
| 181 | .isTrue(); |
| 182 | |
| 183 | AdvertisedProviderSet expectedFailSet = |
adonovan | dd00046 | 2020-11-06 12:57:38 -0800 | [diff] [blame] | 184 | AdvertisedProviderSet.builder().addBuiltin(P1.class).build(); |
Dmitry Lomov | b5174c7 | 2017-01-10 10:57:17 +0000 | [diff] [blame] | 185 | assertThat(requiresProviders.getRequiredProviders().isSatisfiedBy(expectedFailSet)) |
| 186 | .isFalse(); |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 187 | |
| 188 | assertThat(requiresProviders.getRequiredProviders().isSatisfiedBy(AdvertisedProviderSet.ANY)) |
| 189 | .isTrue(); |
| 190 | assertThat(requiresProviders.getRequiredProviders().isSatisfiedBy(AdvertisedProviderSet.EMPTY)) |
| 191 | .isFalse(); |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 192 | } |
| 193 | |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 194 | @Test |
messa | 1bca1bd | 2021-05-14 00:48:19 -0700 | [diff] [blame] | 195 | public void testRequireBuiltinProviders_addsTwoSetsOfRequiredProvidersAndNames() |
| 196 | throws Exception { |
ilist | 85e89a6 | 2020-04-24 08:52:25 -0700 | [diff] [blame] | 197 | AspectDefinition requiresProviders = |
| 198 | new AspectDefinition.Builder(TEST_ASPECT_CLASS) |
| 199 | .requireProviderSets( |
| 200 | ImmutableList.of(ImmutableSet.of(P1.class, P2.class), ImmutableSet.of(P3.class))) |
| 201 | .build(); |
Dmitry Lomov | b5174c7 | 2017-01-10 10:57:17 +0000 | [diff] [blame] | 202 | |
| 203 | AdvertisedProviderSet expectedOkSet1 = |
adonovan | dd00046 | 2020-11-06 12:57:38 -0800 | [diff] [blame] | 204 | AdvertisedProviderSet.builder().addBuiltin(P1.class).addBuiltin(P2.class).build(); |
Dmitry Lomov | b5174c7 | 2017-01-10 10:57:17 +0000 | [diff] [blame] | 205 | |
| 206 | AdvertisedProviderSet expectedOkSet2 = |
adonovan | dd00046 | 2020-11-06 12:57:38 -0800 | [diff] [blame] | 207 | AdvertisedProviderSet.builder().addBuiltin(P3.class).build(); |
Dmitry Lomov | b5174c7 | 2017-01-10 10:57:17 +0000 | [diff] [blame] | 208 | |
| 209 | AdvertisedProviderSet expectedFailSet = |
adonovan | dd00046 | 2020-11-06 12:57:38 -0800 | [diff] [blame] | 210 | AdvertisedProviderSet.builder().addBuiltin(P4.class).build(); |
Dmitry Lomov | b5174c7 | 2017-01-10 10:57:17 +0000 | [diff] [blame] | 211 | |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 212 | assertThat(requiresProviders.getRequiredProviders().isSatisfiedBy(AdvertisedProviderSet.ANY)) |
| 213 | .isTrue(); |
Dmitry Lomov | b5174c7 | 2017-01-10 10:57:17 +0000 | [diff] [blame] | 214 | assertThat(requiresProviders.getRequiredProviders().isSatisfiedBy(expectedOkSet1)).isTrue(); |
| 215 | assertThat(requiresProviders.getRequiredProviders().isSatisfiedBy(expectedOkSet2)).isTrue(); |
| 216 | assertThat(requiresProviders.getRequiredProviders().isSatisfiedBy(expectedFailSet)).isFalse(); |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 217 | assertThat(requiresProviders.getRequiredProviders().isSatisfiedBy(AdvertisedProviderSet.EMPTY)) |
| 218 | .isFalse(); |
| 219 | |
| 220 | } |
| 221 | |
| 222 | @Test |
messa | 1bca1bd | 2021-05-14 00:48:19 -0700 | [diff] [blame] | 223 | public void testRequireStarlarkProviders_addsFlatSetOfRequiredProviders() throws Exception { |
| 224 | AspectDefinition requiresProviders = |
| 225 | new AspectDefinition.Builder(TEST_ASPECT_CLASS) |
| 226 | .requireStarlarkProviders(STARLARK_P1, STARLARK_P2) |
| 227 | .build(); |
| 228 | |
| 229 | AdvertisedProviderSet expectedOkSet = |
| 230 | AdvertisedProviderSet.builder() |
| 231 | .addStarlark(STARLARK_P1) |
| 232 | .addStarlark(STARLARK_P2) |
| 233 | .addStarlark(STARLARK_P3) |
| 234 | .build(); |
| 235 | assertThat(requiresProviders.getRequiredProviders().isSatisfiedBy(expectedOkSet)).isTrue(); |
| 236 | |
| 237 | AdvertisedProviderSet expectedFailSet = |
| 238 | AdvertisedProviderSet.builder().addStarlark(STARLARK_P1).build(); |
| 239 | assertThat(requiresProviders.getRequiredProviders().isSatisfiedBy(expectedFailSet)).isFalse(); |
| 240 | |
| 241 | assertThat(requiresProviders.getRequiredProviders().isSatisfiedBy(AdvertisedProviderSet.ANY)) |
| 242 | .isTrue(); |
| 243 | assertThat(requiresProviders.getRequiredProviders().isSatisfiedBy(AdvertisedProviderSet.EMPTY)) |
| 244 | .isFalse(); |
| 245 | } |
| 246 | |
| 247 | @Test |
| 248 | public void testRequireStarlarkProviders_addsTwoSetsOfRequiredProviders() throws Exception { |
| 249 | AspectDefinition requiresProviders = |
| 250 | new AspectDefinition.Builder(TEST_ASPECT_CLASS) |
| 251 | .requireStarlarkProviderSets( |
| 252 | ImmutableList.of( |
| 253 | ImmutableSet.of(STARLARK_P1, STARLARK_P2), ImmutableSet.of(STARLARK_P3))) |
| 254 | .build(); |
| 255 | |
| 256 | AdvertisedProviderSet expectedOkSet1 = |
| 257 | AdvertisedProviderSet.builder().addStarlark(STARLARK_P1).addStarlark(STARLARK_P2).build(); |
| 258 | assertThat(requiresProviders.getRequiredProviders().isSatisfiedBy(expectedOkSet1)).isTrue(); |
| 259 | |
| 260 | AdvertisedProviderSet expectedOkSet2 = |
| 261 | AdvertisedProviderSet.builder().addStarlark(STARLARK_P3).build(); |
| 262 | assertThat(requiresProviders.getRequiredProviders().isSatisfiedBy(expectedOkSet2)).isTrue(); |
| 263 | |
| 264 | AdvertisedProviderSet expectedFailSet = |
| 265 | AdvertisedProviderSet.builder().addStarlark(STARLARK_P4).build(); |
| 266 | assertThat(requiresProviders.getRequiredProviders().isSatisfiedBy(expectedFailSet)).isFalse(); |
| 267 | |
| 268 | assertThat(requiresProviders.getRequiredProviders().isSatisfiedBy(AdvertisedProviderSet.ANY)) |
| 269 | .isTrue(); |
| 270 | assertThat(requiresProviders.getRequiredProviders().isSatisfiedBy(AdvertisedProviderSet.EMPTY)) |
| 271 | .isFalse(); |
| 272 | } |
| 273 | |
| 274 | @Test |
| 275 | public void testRequireProviders_defaultAcceptsEverything() { |
| 276 | AspectDefinition noRequiredProviders = new AspectDefinition.Builder(TEST_ASPECT_CLASS).build(); |
| 277 | |
| 278 | AdvertisedProviderSet expectedOkSet = |
| 279 | AdvertisedProviderSet.builder().addBuiltin(P4.class).addStarlark(STARLARK_P4).build(); |
| 280 | assertThat(noRequiredProviders.getRequiredProviders().isSatisfiedBy(expectedOkSet)).isTrue(); |
| 281 | |
| 282 | assertThat(noRequiredProviders.getRequiredProviders().isSatisfiedBy(AdvertisedProviderSet.ANY)) |
| 283 | .isTrue(); |
| 284 | assertThat( |
| 285 | noRequiredProviders.getRequiredProviders().isSatisfiedBy(AdvertisedProviderSet.EMPTY)) |
| 286 | .isTrue(); |
| 287 | } |
| 288 | |
| 289 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 290 | public void testRequireAspectClass_defaultAcceptsNothing() { |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 291 | AspectDefinition noAspects = new AspectDefinition.Builder(TEST_ASPECT_CLASS) |
| 292 | .build(); |
| 293 | |
| 294 | AdvertisedProviderSet expectedFailSet = |
adonovan | dd00046 | 2020-11-06 12:57:38 -0800 | [diff] [blame] | 295 | AdvertisedProviderSet.builder().addBuiltin(P4.class).build(); |
Dmitry Lomov | f868b3e | 2017-01-17 10:25:28 +0000 | [diff] [blame] | 296 | |
| 297 | assertThat(noAspects.getRequiredProvidersForAspects().isSatisfiedBy(AdvertisedProviderSet.ANY)) |
| 298 | .isFalse(); |
| 299 | assertThat(noAspects.getRequiredProvidersForAspects() |
| 300 | .isSatisfiedBy(AdvertisedProviderSet.EMPTY)) |
| 301 | .isFalse(); |
| 302 | |
| 303 | assertThat(noAspects.getRequiredProvidersForAspects().isSatisfiedBy(expectedFailSet)) |
| 304 | .isFalse(); |
Rumou Duan | 6f8393f | 2016-11-30 16:03:10 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 307 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 308 | public void testNoConfigurationFragmentPolicySetup_hasNonNullPolicy() throws Exception { |
Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 309 | AspectDefinition noPolicy = new AspectDefinition.Builder(TEST_ASPECT_CLASS) |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 310 | .build(); |
Michael Staib | af27046 | 2016-05-18 16:22:07 +0000 | [diff] [blame] | 311 | assertThat(noPolicy.getConfigurationFragmentPolicy()).isNotNull(); |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 315 | public void testRequiresConfigurationFragments_propagatedToConfigurationFragmentPolicy() |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 316 | throws Exception { |
Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 317 | AspectDefinition requiresFragments = new AspectDefinition.Builder(TEST_ASPECT_CLASS) |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 318 | .requiresConfigurationFragments(Integer.class, String.class) |
| 319 | .build(); |
| 320 | assertThat(requiresFragments.getConfigurationFragmentPolicy()).isNotNull(); |
| 321 | assertThat( |
| 322 | requiresFragments.getConfigurationFragmentPolicy().getRequiredConfigurationFragments()) |
| 323 | .containsExactly(Integer.class, String.class); |
| 324 | } |
| 325 | |
jcater | a666f00 | 2020-04-14 14:55:20 -0700 | [diff] [blame] | 326 | private static class FooFragment extends Fragment {} |
| 327 | |
| 328 | private static class BarFragment extends Fragment {} |
gregce | 614dc50 | 2017-12-20 17:24:46 -0800 | [diff] [blame] | 329 | |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 330 | @Test |
twigg | 8e9f9e1 | 2020-09-17 09:07:58 -0700 | [diff] [blame] | 331 | public void testMissingFragmentPolicy_propagatedToConfigurationFragmentPolicy() throws Exception { |
| 332 | AspectDefinition missingFragments = |
| 333 | new AspectDefinition.Builder(TEST_ASPECT_CLASS) |
| 334 | .setMissingFragmentPolicy(FooFragment.class, MissingFragmentPolicy.IGNORE) |
| 335 | .build(); |
| 336 | assertThat(missingFragments.getConfigurationFragmentPolicy()).isNotNull(); |
| 337 | assertThat( |
| 338 | missingFragments |
| 339 | .getConfigurationFragmentPolicy() |
| 340 | .getMissingFragmentPolicy(FooFragment.class)) |
| 341 | .isEqualTo(MissingFragmentPolicy.IGNORE); |
| 342 | } |
| 343 | |
| 344 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 345 | public void testRequiresHostConfigurationFragments_propagatedToConfigurationFragmentPolicy() |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 346 | throws Exception { |
gregce | 614dc50 | 2017-12-20 17:24:46 -0800 | [diff] [blame] | 347 | AspectDefinition requiresFragments = |
| 348 | ConfigAwareAspectBuilder.of(new AspectDefinition.Builder(TEST_ASPECT_CLASS)) |
| 349 | .requiresHostConfigurationFragments(FooFragment.class, BarFragment.class) |
| 350 | .originalBuilder() |
| 351 | .build(); |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 352 | assertThat(requiresFragments.getConfigurationFragmentPolicy()).isNotNull(); |
| 353 | assertThat( |
| 354 | requiresFragments.getConfigurationFragmentPolicy().getRequiredConfigurationFragments()) |
gregce | 614dc50 | 2017-12-20 17:24:46 -0800 | [diff] [blame] | 355 | .containsExactly(FooFragment.class, BarFragment.class); |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 359 | public void testRequiresConfigurationFragmentNames_propagatedToConfigurationFragmentPolicy() |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 360 | throws Exception { |
gregce | 220a1c8 | 2020-05-01 10:19:52 -0700 | [diff] [blame] | 361 | AspectDefinition requiresFragments = |
| 362 | new AspectDefinition.Builder(TEST_ASPECT_CLASS) |
| 363 | .requiresConfigurationFragmentsByStarlarkBuiltinName(ImmutableList.of("test_fragment")) |
| 364 | .build(); |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 365 | assertThat(requiresFragments.getConfigurationFragmentPolicy()).isNotNull(); |
| 366 | assertThat( |
| 367 | requiresFragments.getConfigurationFragmentPolicy() |
gregce | 7fa23ea | 2018-01-18 12:46:04 -0800 | [diff] [blame] | 368 | .isLegalConfigurationFragment(TestFragment.class, NoTransition.INSTANCE)) |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 369 | .isTrue(); |
| 370 | } |
| 371 | |
| 372 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 373 | public void testRequiresHostConfigurationFragmentNames_propagatedToConfigurationFragmentPolicy() |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 374 | throws Exception { |
gregce | 614dc50 | 2017-12-20 17:24:46 -0800 | [diff] [blame] | 375 | AspectDefinition requiresFragments = |
| 376 | ConfigAwareAspectBuilder.of(new AspectDefinition.Builder(TEST_ASPECT_CLASS)) |
gregce | 220a1c8 | 2020-05-01 10:19:52 -0700 | [diff] [blame] | 377 | .requiresHostConfigurationFragmentsByStarlarkBuiltinName( |
gregce | 614dc50 | 2017-12-20 17:24:46 -0800 | [diff] [blame] | 378 | ImmutableList.of("test_fragment")) |
| 379 | .originalBuilder() |
| 380 | .build(); |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 381 | assertThat(requiresFragments.getConfigurationFragmentPolicy()).isNotNull(); |
| 382 | assertThat( |
| 383 | requiresFragments.getConfigurationFragmentPolicy() |
gregce | c7b2121 | 2017-12-21 12:40:20 -0800 | [diff] [blame] | 384 | .isLegalConfigurationFragment(TestFragment.class, HostTransition.INSTANCE)) |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 385 | .isTrue(); |
| 386 | } |
| 387 | |
| 388 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 389 | public void testEmptyStarlarkConfigurationFragmentPolicySetup_hasNonNullPolicy() |
gregce | b100b1d | 2020-05-20 10:22:17 -0700 | [diff] [blame] | 390 | throws Exception { |
gregce | 614dc50 | 2017-12-20 17:24:46 -0800 | [diff] [blame] | 391 | AspectDefinition noPolicy = |
| 392 | ConfigAwareAspectBuilder.of(new AspectDefinition.Builder(TEST_ASPECT_CLASS)) |
gregce | 220a1c8 | 2020-05-01 10:19:52 -0700 | [diff] [blame] | 393 | .requiresHostConfigurationFragmentsByStarlarkBuiltinName(ImmutableList.<String>of()) |
gregce | 614dc50 | 2017-12-20 17:24:46 -0800 | [diff] [blame] | 394 | .originalBuilder() |
gregce | 220a1c8 | 2020-05-01 10:19:52 -0700 | [diff] [blame] | 395 | .requiresConfigurationFragmentsByStarlarkBuiltinName(ImmutableList.<String>of()) |
gregce | 614dc50 | 2017-12-20 17:24:46 -0800 | [diff] [blame] | 396 | .build(); |
Michael Staib | af27046 | 2016-05-18 16:22:07 +0000 | [diff] [blame] | 397 | assertThat(noPolicy.getConfigurationFragmentPolicy()).isNotNull(); |
Michael Staib | cd48cd5 | 2016-01-15 20:11:11 +0000 | [diff] [blame] | 398 | } |
| 399 | |
gregce | 220a1c8 | 2020-05-01 10:19:52 -0700 | [diff] [blame] | 400 | @StarlarkBuiltin(name = "test_fragment", doc = "test fragment") |
Googler | 34f7058 | 2019-11-25 12:27:34 -0800 | [diff] [blame] | 401 | private static final class TestFragment implements StarlarkValue {} |
Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 402 | } |