| 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 | |
| Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 16 | import static com.google.common.truth.Truth.assertThat; |
| Carmi Grushko | eaaa9d0d | 2015-11-17 01:54:45 +0000 | [diff] [blame] | 17 | import static com.google.devtools.build.lib.analysis.BaseRuleClasses.ACTION_LISTENER; |
| gregce | 593f7f9 | 2017-09-19 02:02:21 +0200 | [diff] [blame] | 18 | import static com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode.TARGET; |
| Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 19 | import static com.google.devtools.build.lib.packages.Attribute.attr; |
| 20 | import static com.google.devtools.build.lib.packages.BuildType.LABEL; |
| 21 | import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST; |
| Florian Weikert | fd735f3 | 2015-11-27 17:32:23 +0000 | [diff] [blame] | 22 | import static org.junit.Assert.fail; |
| Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 23 | |
| Greg Estren | c8a127c | 2017-02-13 20:21:33 +0000 | [diff] [blame] | 24 | import com.google.common.collect.ImmutableList; |
| dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 25 | import com.google.common.collect.Iterables; |
| 26 | import com.google.common.eventbus.EventBus; |
| Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.actions.Artifact; |
| tomlu | 39a0a38 | 2018-06-22 09:43:23 -0700 | [diff] [blame] | 28 | import com.google.devtools.build.lib.actions.MutableActionGraph.ActionConflictException; |
| Carmi Grushko | eaaa9d0d | 2015-11-17 01:54:45 +0000 | [diff] [blame] | 29 | import com.google.devtools.build.lib.actions.util.ActionsTestUtil.NullAction; |
| gregce | 676a957 | 2017-12-21 11:33:32 -0800 | [diff] [blame] | 30 | import com.google.devtools.build.lib.analysis.config.HostTransition; |
| Florian Weikert | cca703a | 2015-12-07 09:56:38 +0000 | [diff] [blame] | 31 | import com.google.devtools.build.lib.analysis.util.AnalysisTestCase; |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 32 | import com.google.devtools.build.lib.analysis.util.MockRule; |
| Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.analysis.util.TestAspects; |
| dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 34 | import com.google.devtools.build.lib.analysis.util.TestAspects.AspectApplyingToFiles; |
| Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 35 | import com.google.devtools.build.lib.analysis.util.TestAspects.AspectInfo; |
| Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 36 | import com.google.devtools.build.lib.analysis.util.TestAspects.DummyRuleFactory; |
| 37 | import com.google.devtools.build.lib.analysis.util.TestAspects.RuleInfo; |
| Carmi Grushko | eaaa9d0d | 2015-11-17 01:54:45 +0000 | [diff] [blame] | 38 | import com.google.devtools.build.lib.cmdline.Label; |
| 39 | import com.google.devtools.build.lib.collect.nestedset.NestedSet; |
| Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 40 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
| 41 | import com.google.devtools.build.lib.collect.nestedset.Order; |
| Michael Staib | 2707a88 | 2016-09-16 21:06:40 +0000 | [diff] [blame] | 42 | import com.google.devtools.build.lib.events.OutputFilter.RegexOutputFilter; |
| Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 43 | import com.google.devtools.build.lib.packages.AspectDefinition; |
| 44 | import com.google.devtools.build.lib.packages.AspectParameters; |
| mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 45 | import com.google.devtools.build.lib.packages.Attribute.LateBoundDefault; |
| Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 46 | import com.google.devtools.build.lib.packages.NativeAspectClass; |
| dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 47 | import com.google.devtools.build.lib.skyframe.AspectValue; |
| janakr | 9c10140 | 2018-03-10 06:48:59 -0800 | [diff] [blame] | 48 | import com.google.devtools.build.lib.skyframe.ConfiguredTargetAndData; |
| janakr | 7a6e002 | 2017-10-12 22:53:47 +0200 | [diff] [blame] | 49 | import com.google.devtools.build.lib.testutil.TestConstants; |
| Lukacs Berki | 75250e6 | 2016-04-12 13:48:24 +0000 | [diff] [blame] | 50 | import com.google.devtools.build.lib.vfs.ModifiedFileSet; |
| tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 51 | import com.google.devtools.build.lib.vfs.Root; |
| Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 52 | import java.util.ArrayList; |
| 53 | import java.util.List; |
| Michael Staib | 2707a88 | 2016-09-16 21:06:40 +0000 | [diff] [blame] | 54 | import java.util.regex.Pattern; |
| Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 55 | import org.junit.Test; |
| 56 | import org.junit.runner.RunWith; |
| 57 | import org.junit.runners.JUnit4; |
| 58 | |
| 59 | /** |
| 60 | * Tests for aspect creation and merging with configured targets. |
| 61 | * |
| 62 | * <p>Uses the complete analysis machinery and depends on custom rules so that behaviors related to |
| 63 | * aspects can be tested even if they aren't used by regular rules. |
| 64 | */ |
| 65 | @RunWith(JUnit4.class) |
| Florian Weikert | cca703a | 2015-12-07 09:56:38 +0000 | [diff] [blame] | 66 | public class AspectTest extends AnalysisTestCase { |
| Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 67 | |
| Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 68 | private void pkg(String name, String... contents) throws Exception { |
| Ulf Adams | 01f1b46 | 2015-04-27 09:52:33 +0000 | [diff] [blame] | 69 | scratch.file("" + name + "/BUILD", contents); |
| Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Test |
| Lukacs Berki | ea988b6 | 2016-08-30 12:26:18 +0000 | [diff] [blame] | 73 | public void testAspectAppliedToAliasWithSelect() throws Exception { |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 74 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.ASPECT_REQUIRING_RULE); |
| Lukacs Berki | ea988b6 | 2016-08-30 12:26:18 +0000 | [diff] [blame] | 75 | pkg("a", |
| 76 | "aspect(name='a', foo=[':b'])", |
| 77 | "alias(name='b', actual=select({'//conditions:default': ':c'}))", |
| 78 | "base(name='c')"); |
| 79 | ConfiguredTarget a = getConfiguredTarget("//a:a"); |
| 80 | assertThat(a.getProvider(RuleInfo.class).getData()) |
| 81 | .containsExactly("aspect //a:c", "rule //a:a"); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void testAspectAppliedToChainedAliases() throws Exception { |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 86 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.ASPECT_REQUIRING_RULE); |
| Lukacs Berki | ea988b6 | 2016-08-30 12:26:18 +0000 | [diff] [blame] | 87 | pkg("a", |
| 88 | "aspect(name='a', foo=[':b'])", |
| 89 | "alias(name='b', actual=':c')", |
| 90 | "alias(name='c', actual=':d')", |
| 91 | "alias(name='d', actual=':e')", |
| 92 | "base(name='e')"); |
| 93 | |
| 94 | ConfiguredTarget a = getConfiguredTarget("//a:a"); |
| 95 | assertThat(a.getProvider(RuleInfo.class).getData()) |
| 96 | .containsExactly("aspect //a:e", "rule //a:a"); |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void testAspectAppliedToChainedAliasesAndSelect() throws Exception { |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 101 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.ASPECT_REQUIRING_RULE); |
| Lukacs Berki | ea988b6 | 2016-08-30 12:26:18 +0000 | [diff] [blame] | 102 | pkg("a", |
| 103 | "aspect(name='a', foo=[':b'])", |
| 104 | "alias(name='b', actual=select({'//conditions:default': ':c'}))", |
| 105 | "alias(name='c', actual=select({'//conditions:default': ':d'}))", |
| 106 | "base(name='d')"); |
| 107 | ConfiguredTarget a = getConfiguredTarget("//a:a"); |
| 108 | assertThat(a.getProvider(RuleInfo.class).getData()) |
| 109 | .containsExactly("aspect //a:d", "rule //a:a"); |
| 110 | } |
| 111 | |
| 112 | @Test |
| Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 113 | public void providersOfAspectAreMergedIntoDependency() throws Exception { |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 114 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.ASPECT_REQUIRING_RULE); |
| Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 115 | pkg("a", |
| 116 | "aspect(name='a', foo=[':b'])", |
| 117 | "aspect(name='b', foo=[])"); |
| 118 | |
| 119 | ConfiguredTarget a = getConfiguredTarget("//a:a"); |
| Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 120 | assertThat(a.getProvider(RuleInfo.class).getData()) |
| Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 121 | .containsExactly("aspect //a:b", "rule //a:a"); |
| 122 | } |
| 123 | |
| 124 | @Test |
| 125 | public void aspectIsNotCreatedIfAdvertisedProviderIsNotPresent() throws Exception { |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 126 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.LIAR_RULE, |
| 127 | TestAspects.ASPECT_REQUIRING_PROVIDER_RULE); |
| Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 128 | |
| 129 | pkg("a", |
| 130 | "aspect_requiring_provider(name='a', foo=[':b'])", |
| 131 | "liar(name='b', foo=[])"); |
| 132 | |
| 133 | ConfiguredTarget a = getConfiguredTarget("//a:a"); |
| Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 134 | assertThat(a.getProvider(RuleInfo.class).getData()).containsExactly("rule //a:a"); |
| Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| Lukacs Berki | 549bfce | 2016-04-22 15:29:12 +0000 | [diff] [blame] | 137 | @Test |
| Dmitry Lomov | 6cd9897 | 2017-03-01 15:44:00 +0000 | [diff] [blame] | 138 | public void aspectIsNotCreatedIfAdvertisedProviderIsNotPresentWithAlias() throws Exception { |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 139 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.LIAR_RULE, |
| 140 | TestAspects.ASPECT_REQUIRING_PROVIDER_RULE); |
| Dmitry Lomov | 6cd9897 | 2017-03-01 15:44:00 +0000 | [diff] [blame] | 141 | |
| 142 | pkg("a", |
| 143 | "aspect_requiring_provider(name='a', foo=[':b'])", |
| 144 | "alias(name = 'b_alias', actual = ':b')", |
| 145 | "liar(name='b', foo=[])"); |
| 146 | |
| 147 | ConfiguredTarget a = getConfiguredTarget("//a:a"); |
| 148 | assertThat(a.getProvider(RuleInfo.class).getData()).containsExactly("rule //a:a"); |
| 149 | } |
| 150 | |
| 151 | @Test |
| 152 | public void aspectIsNotPropagatedThroughLiars() throws Exception { |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 153 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.LIAR_RULE, |
| 154 | TestAspects.HONEST_RULE, TestAspects.ASPECT_REQUIRING_PROVIDER_RULE); |
| Dmitry Lomov | 6cd9897 | 2017-03-01 15:44:00 +0000 | [diff] [blame] | 155 | |
| 156 | pkg("a", |
| 157 | "aspect_requiring_provider(name='a', foo=[':b_alias'])", |
| 158 | "alias(name = 'b_alias', actual = ':b')", |
| 159 | "liar(name='b', foo=[':c'])", |
| 160 | "honest(name = 'c', foo = [])" |
| 161 | ); |
| 162 | |
| 163 | ConfiguredTarget a = getConfiguredTarget("//a:a"); |
| 164 | assertThat(a.getProvider(RuleInfo.class).getData()).containsExactly("rule //a:a"); |
| 165 | } |
| 166 | |
| 167 | @Test |
| 168 | public void aspectPropagatedThroughAliasRule() throws Exception { |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 169 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.HONEST_RULE, |
| 170 | TestAspects.ASPECT_REQUIRING_PROVIDER_RULE); |
| Dmitry Lomov | 6cd9897 | 2017-03-01 15:44:00 +0000 | [diff] [blame] | 171 | |
| 172 | pkg("a", |
| 173 | "aspect_requiring_provider(name='a', foo=[':b_alias'])", |
| 174 | "alias(name = 'b_alias', actual = ':b')", |
| 175 | "honest(name='b', foo=[])"); |
| 176 | |
| 177 | ConfiguredTarget a = getConfiguredTarget("//a:a"); |
| 178 | assertThat(a.getProvider(RuleInfo.class).getData()).containsExactly( |
| 179 | "rule //a:a", "aspect //a:b"); |
| 180 | } |
| 181 | |
| 182 | @Test |
| 183 | public void aspectPropagatedThroughAliasRuleAndHonestRules() throws Exception { |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 184 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.HONEST_RULE, |
| 185 | TestAspects.ASPECT_REQUIRING_PROVIDER_RULE); |
| Dmitry Lomov | 6cd9897 | 2017-03-01 15:44:00 +0000 | [diff] [blame] | 186 | |
| 187 | pkg("a", |
| 188 | "aspect_requiring_provider(name='a', foo=[':b'])", |
| 189 | "alias(name = 'b_alias', actual = ':b')", |
| 190 | "honest(name='b', foo=[':c'])", |
| 191 | "honest(name='c', foo=[])" |
| 192 | ); |
| 193 | |
| 194 | ConfiguredTarget a = getConfiguredTarget("//a:a"); |
| 195 | assertThat(a.getProvider(RuleInfo.class).getData()).containsExactly( |
| 196 | "rule //a:a", "aspect //a:b", "aspect //a:c"); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | @Test |
| Lukacs Berki | 75250e6 | 2016-04-12 13:48:24 +0000 | [diff] [blame] | 204 | public void aspectCreationWorksThroughBind() throws Exception { |
| janakr | 7a6e002 | 2017-10-12 22:53:47 +0200 | [diff] [blame] | 205 | if (getInternalTestExecutionMode() != TestConstants.InternalTestExecutionMode.NORMAL) { |
| 206 | // TODO(b/67651960): fix or justify disabling. |
| 207 | return; |
| 208 | } |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 209 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.HONEST_RULE, |
| 210 | TestAspects.ASPECT_REQUIRING_PROVIDER_RULE); |
| Lukacs Berki | 75250e6 | 2016-04-12 13:48:24 +0000 | [diff] [blame] | 211 | pkg("a", |
| 212 | "aspect_requiring_provider(name='a', foo=['//external:b'])", |
| 213 | "honest(name='b', foo=[])"); |
| 214 | |
| 215 | scratch.overwriteFile("WORKSPACE", |
| Greg Estren | c8a127c | 2017-02-13 20:21:33 +0000 | [diff] [blame] | 216 | new ImmutableList.Builder<String>() |
| 217 | .addAll(analysisMock.getWorkspaceContents(mockToolsConfig)) |
| 218 | .add("bind(name='b', actual='//a:b')") |
| 219 | .build()); |
| Lukacs Berki | 75250e6 | 2016-04-12 13:48:24 +0000 | [diff] [blame] | 220 | |
| tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 221 | skyframeExecutor.invalidateFilesUnderPathForTesting( |
| 222 | reporter, ModifiedFileSet.EVERYTHING_MODIFIED, Root.fromPath(rootDirectory)); |
| Lukacs Berki | 75250e6 | 2016-04-12 13:48:24 +0000 | [diff] [blame] | 223 | |
| 224 | ConfiguredTarget a = getConfiguredTarget("//a:a"); |
| 225 | assertThat(a.getProvider(RuleInfo.class).getData()) |
| 226 | .containsExactly("rule //a:a", "aspect //a:b"); |
| 227 | } |
| 228 | |
| 229 | |
| Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 230 | @Test |
| 231 | public void aspectCreatedIfAdvertisedProviderIsPresent() throws Exception { |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 232 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.HONEST_RULE, |
| 233 | TestAspects.ASPECT_REQUIRING_PROVIDER_RULE); |
| Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 234 | |
| 235 | pkg("a", |
| 236 | "aspect_requiring_provider(name='a', foo=[':b'])", |
| 237 | "honest(name='b', foo=[])"); |
| 238 | |
| 239 | ConfiguredTarget a = getConfiguredTarget("//a:a"); |
| Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 240 | assertThat(a.getProvider(RuleInfo.class).getData()) |
| Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 241 | .containsExactly("rule //a:a", "aspect //a:b"); |
| 242 | } |
| 243 | |
| 244 | @Test |
| Rumou Duan | 6f8393f | 2016-11-30 16:03:10 +0000 | [diff] [blame] | 245 | public void aspectCreatedIfAtLeastOneSetOfAdvertisedProvidersArePresent() throws Exception { |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 246 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.HONEST_RULE, |
| 247 | TestAspects.HONEST_RULE_2, TestAspects.ASPECT_REQUIRING_PROVIDER_SETS_RULE); |
| Rumou Duan | 6f8393f | 2016-11-30 16:03:10 +0000 | [diff] [blame] | 248 | |
| 249 | pkg("a", |
| 250 | "aspect_requiring_provider_sets(name='a', foo=[':b', ':c'])", |
| 251 | "honest(name='b', foo=[])", |
| 252 | "honest2(name='c', foo=[])"); |
| 253 | |
| 254 | ConfiguredTarget a = getConfiguredTarget("//a:a"); |
| 255 | assertThat(a.getProvider(RuleInfo.class).getData()) |
| 256 | .containsExactly("rule //a:a", "aspect //a:b", "aspect //a:c"); |
| 257 | } |
| 258 | |
| 259 | @Test |
| Dmitry Lomov | 6231d08 | 2015-11-02 17:17:20 +0000 | [diff] [blame] | 260 | public void aspectWithParametrizedDefinition() throws Exception { |
| Dmitry Lomov | d126074 | 2016-05-03 18:37:11 +0000 | [diff] [blame] | 261 | setRulesAvailableInTests( |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 262 | TestAspects.BASE_RULE, |
| 263 | TestAspects.HONEST_RULE, |
| 264 | TestAspects.PARAMETERIZED_DEFINITION_ASPECT_RULE); |
| Dmitry Lomov | 6231d08 | 2015-11-02 17:17:20 +0000 | [diff] [blame] | 265 | |
| 266 | pkg( |
| 267 | "a", |
| 268 | "honest(name='q', foo=[])", |
| 269 | "parametrized_definition_aspect(name='a', foo=[':b'], baz='//a:q')", |
| 270 | "honest(name='c', foo=[])", |
| 271 | "honest(name='b', foo=[':c'])"); |
| 272 | |
| 273 | ConfiguredTarget a = getConfiguredTarget("//a:a"); |
| 274 | assertThat(a.getProvider(TestAspects.RuleInfo.class).getData()) |
| 275 | .containsExactly( |
| 276 | "rule //a:a", |
| 277 | "aspect //a:b data //a:q $dep:[ //a:q]", |
| 278 | "aspect //a:c data //a:q $dep:[ //a:q]"); |
| 279 | } |
| 280 | |
| 281 | @Test |
| Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 282 | public void aspectInError() throws Exception { |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 283 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.ERROR_ASPECT_RULE, |
| 284 | TestAspects.SIMPLE_RULE); |
| Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 285 | |
| 286 | pkg("a", |
| 287 | "simple(name='a', foo=[':b'])", |
| 288 | "error_aspect(name='b', foo=[':c'])", |
| 289 | "simple(name='c')"); |
| 290 | |
| 291 | reporter.removeHandler(failFastHandler); |
| 292 | // getConfiguredTarget() uses a separate code path that does not hit |
| 293 | // SkyframeBuildView#configureTargets |
| 294 | try { |
| 295 | update("//a:a"); |
| 296 | fail(); |
| 297 | } catch (ViewCreationFailedException e) { |
| 298 | // expected |
| 299 | } |
| 300 | assertContainsEvent("Aspect error"); |
| 301 | } |
| Marian Lobur | 2099da0 | 2015-05-12 14:42:01 +0000 | [diff] [blame] | 302 | |
| 303 | @Test |
| Marian Lobur | fc567b3 | 2015-09-14 08:44:25 +0000 | [diff] [blame] | 304 | public void transitiveAspectInError() throws Exception { |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 305 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.ERROR_ASPECT_RULE, |
| 306 | TestAspects.SIMPLE_RULE); |
| Marian Lobur | fc567b3 | 2015-09-14 08:44:25 +0000 | [diff] [blame] | 307 | |
| 308 | pkg("a", |
| 309 | "error_aspect(name='a', foo=[':b'])", |
| 310 | "error_aspect(name='b', bar=[':c'])", |
| 311 | "error_aspect(name='c', bar=[':d'])", |
| 312 | "error_aspect(name='d')"); |
| 313 | |
| 314 | reporter.removeHandler(failFastHandler); |
| 315 | // getConfiguredTarget() uses a separate code path that does not hit |
| 316 | // SkyframeBuildView#configureTargets |
| 317 | try { |
| 318 | update("//a:a"); |
| 319 | fail(); |
| 320 | } catch (ViewCreationFailedException e) { |
| 321 | // expected |
| 322 | } |
| 323 | assertContainsEvent("Aspect error"); |
| 324 | } |
| 325 | |
| 326 | @Test |
| Michael Staib | 2707a88 | 2016-09-16 21:06:40 +0000 | [diff] [blame] | 327 | public void aspectDependenciesDontShowDeprecationWarnings() throws Exception { |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 328 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.EXTRA_ATTRIBUTE_ASPECT_RULE); |
| Michael Staib | 2707a88 | 2016-09-16 21:06:40 +0000 | [diff] [blame] | 329 | |
| 330 | pkg("extra", "base(name='extra', deprecation='bad aspect')"); |
| 331 | |
| 332 | pkg("a", |
| 333 | "rule_with_extra_deps_aspect(name='a', foo=[':b'])", |
| 334 | "base(name='b')"); |
| 335 | |
| 336 | getConfiguredTarget("//a:a"); |
| 337 | assertContainsEventWithFrequency("bad aspect", 0); |
| 338 | } |
| 339 | |
| 340 | @Test |
| 341 | public void ruleDependencyDeprecationWarningsAbsentDuringAspectEvaluations() throws Exception { |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 342 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.ASPECT_REQUIRING_RULE); |
| Michael Staib | 2707a88 | 2016-09-16 21:06:40 +0000 | [diff] [blame] | 343 | |
| 344 | pkg("a", "aspect(name='a', foo=['//b:b'])"); |
| 345 | pkg("b", "aspect(name='b', bar=['//d:d'])"); |
| 346 | pkg("d", "base(name='d', deprecation='bad rule')"); |
| 347 | |
| 348 | getConfiguredTarget("//a:a"); |
| 349 | assertContainsEventWithFrequency("bad rule", 1); |
| 350 | } |
| 351 | |
| 352 | @Test |
| 353 | public void aspectWarningsFilteredByOutputFiltersForAssociatedRules() throws Exception { |
| janakr | 7a6e002 | 2017-10-12 22:53:47 +0200 | [diff] [blame] | 354 | if (getInternalTestExecutionMode() != TestConstants.InternalTestExecutionMode.NORMAL) { |
| 355 | // TODO(b/67651960): fix or justify disabling. |
| 356 | return; |
| 357 | } |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 358 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.WARNING_ASPECT_RULE); |
| Michael Staib | 2707a88 | 2016-09-16 21:06:40 +0000 | [diff] [blame] | 359 | pkg("a", "warning_aspect(name='a', foo=['//b:b', '//c:c'])"); |
| 360 | pkg("b", "base(name='b')"); |
| 361 | pkg("c", "base(name='c')"); |
| 362 | |
| 363 | reporter.setOutputFilter(RegexOutputFilter.forPattern(Pattern.compile("^//b:"))); |
| 364 | |
| 365 | getConfiguredTarget("//a:a"); |
| 366 | assertContainsEventWithFrequency("Aspect warning on //b:b", 1); |
| 367 | assertContainsEventWithFrequency("Aspect warning on //c:c", 0); |
| 368 | } |
| 369 | |
| 370 | @Test |
| Marian Lobur | 2099da0 | 2015-05-12 14:42:01 +0000 | [diff] [blame] | 371 | public void sameTargetInDifferentAttributes() throws Exception { |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 372 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.ASPECT_REQUIRING_RULE, |
| 373 | TestAspects.SIMPLE_RULE); |
| Marian Lobur | 2099da0 | 2015-05-12 14:42:01 +0000 | [diff] [blame] | 374 | pkg("a", |
| 375 | "aspect(name='a', foo=[':b'], bar=[':b'])", |
| 376 | "aspect(name='b', foo=[])"); |
| 377 | |
| 378 | ConfiguredTarget a = getConfiguredTarget("//a:a"); |
| Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 379 | assertThat(a.getProvider(RuleInfo.class).getData()) |
| Marian Lobur | 2099da0 | 2015-05-12 14:42:01 +0000 | [diff] [blame] | 380 | .containsExactly("aspect //a:b", "rule //a:a"); |
| 381 | } |
| Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 382 | |
| 383 | @Test |
| kaipi | 64e952c | 2017-05-22 17:12:08 +0200 | [diff] [blame] | 384 | public void sameTargetInDifferentAttributesWithDifferentAspects() throws Exception { |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 385 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.MULTI_ASPECT_RULE, |
| 386 | TestAspects.SIMPLE_RULE); |
| kaipi | 64e952c | 2017-05-22 17:12:08 +0200 | [diff] [blame] | 387 | pkg("a", |
| 388 | "multi_aspect(name='a', foo=':b', bar=':b')", |
| 389 | "simple(name='b')"); |
| 390 | |
| 391 | ConfiguredTarget a = getConfiguredTarget("//a:a"); |
| 392 | assertThat(a.getProvider(RuleInfo.class).getData()).containsExactly("foo", "bar"); |
| 393 | } |
| 394 | |
| 395 | @Test |
| Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 396 | public void informationFromBaseRulePassedToAspect() throws Exception { |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 397 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.HONEST_RULE, |
| 398 | TestAspects.ASPECT_REQUIRING_PROVIDER_RULE); |
| Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 399 | pkg("a", |
| 400 | "aspect_requiring_provider(name='a', foo=[':b'], baz='hello')", |
| 401 | "honest(name='b', foo=[])"); |
| 402 | |
| 403 | ConfiguredTarget a = getConfiguredTarget("//a:a"); |
| Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 404 | assertThat(a.getProvider(RuleInfo.class).getData()) |
| Marian Lobur | 702cad7 | 2015-09-02 09:53:58 +0000 | [diff] [blame] | 405 | .containsExactly("rule //a:a", "aspect //a:b data hello"); |
| 406 | } |
| Marian Lobur | 86bd4fd | 2015-09-16 10:01:38 +0000 | [diff] [blame] | 407 | |
| Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 408 | /** |
| 409 | * Rule definitions to be used in emptyAspectAttributesAreAvailableInRuleContext(). |
| 410 | */ |
| 411 | public static class EmptyAspectAttributesAreAvailableInRuleContext { |
| janakr | cd72f4b | 2018-02-13 16:25:36 -0800 | [diff] [blame] | 412 | public static final MockRule TEST_RULE = |
| 413 | () -> |
| 414 | MockRule.ancestor(TestAspects.BASE_RULE.getClass()) |
| 415 | .factory(DummyRuleFactory.class) |
| 416 | .define( |
| 417 | "testrule", |
| 418 | (builder, env) -> |
| 419 | builder.add( |
| 420 | attr("foo", LABEL_LIST) |
| 421 | .legacyAllowAnyFileType() |
| 422 | .aspect(AspectWithEmptyLateBoundAttribute.INSTANCE))); |
| Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 423 | |
| Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 424 | public static class AspectWithEmptyLateBoundAttribute extends NativeAspectClass |
| 425 | implements ConfiguredAspectFactory { |
| janakr | cd72f4b | 2018-02-13 16:25:36 -0800 | [diff] [blame] | 426 | static final AspectWithEmptyLateBoundAttribute INSTANCE = |
| 427 | new AspectWithEmptyLateBoundAttribute(); |
| 428 | |
| 429 | private AspectWithEmptyLateBoundAttribute() {} |
| 430 | |
| Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 431 | @Override |
| 432 | public AspectDefinition getDefinition(AspectParameters params) { |
| Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 433 | return new AspectDefinition.Builder(this) |
| mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 434 | .add(attr(":late", LABEL).value(LateBoundDefault.alwaysNull())) |
| 435 | .build(); |
| Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | @Override |
| Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 439 | public ConfiguredAspect create( |
| dannark | e75cfb3 | 2019-01-10 14:36:24 -0800 | [diff] [blame] | 440 | ConfiguredTargetAndData ctadBase, |
| 441 | RuleContext ruleContext, |
| 442 | AspectParameters parameters, |
| 443 | String toolsRepository) |
| tomlu | 39a0a38 | 2018-06-22 09:43:23 -0700 | [diff] [blame] | 444 | throws InterruptedException, ActionConflictException { |
| Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 445 | Object lateBoundPrereq = ruleContext.getPrerequisite(":late", TARGET); |
| Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 446 | return new ConfiguredAspect.Builder(this, parameters, ruleContext) |
| Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 447 | .addProvider( |
| Carmi Grushko | 261f5bb | 2015-12-09 19:38:38 +0000 | [diff] [blame] | 448 | AspectInfo.class, |
| Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 449 | new AspectInfo( |
| 450 | NestedSetBuilder.create( |
| 451 | Order.STABLE_ORDER, lateBoundPrereq != null ? "non-empty" : "empty"))) |
| Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 452 | .build(); |
| 453 | } |
| 454 | } |
| Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /** |
| mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 458 | * An Aspect has a late-bound attribute with no value (that is, a LateBoundDefault whose |
| 459 | * getDefault() returns `null`). Test that this attribute is available in the RuleContext which is |
| 460 | * provided to the Aspect's `create()` method. |
| Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 461 | */ |
| 462 | @Test |
| 463 | public void emptyAspectAttributesAreAvailableInRuleContext() throws Exception { |
| janakr | cd72f4b | 2018-02-13 16:25:36 -0800 | [diff] [blame] | 464 | setRulesAndAspectsAvailableInTests( |
| 465 | ImmutableList.of( |
| 466 | TestAspects.SIMPLE_ASPECT, |
| 467 | EmptyAspectAttributesAreAvailableInRuleContext.AspectWithEmptyLateBoundAttribute |
| 468 | .INSTANCE), |
| 469 | ImmutableList.of( |
| 470 | TestAspects.BASE_RULE, EmptyAspectAttributesAreAvailableInRuleContext.TEST_RULE)); |
| Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 471 | pkg("a", |
| 472 | "testrule(name='a', foo=[':b'])", |
| 473 | "testrule(name='b')"); |
| 474 | ConfiguredTarget a = getConfiguredTarget("//a:a"); |
| 475 | assertThat(a.getProvider(RuleInfo.class).getData()).contains("empty"); |
| 476 | } |
| 477 | |
| Carmi Grushko | eaaa9d0d | 2015-11-17 01:54:45 +0000 | [diff] [blame] | 478 | /** |
| 479 | * Rule definitions to be used in extraActionsAreEmitted(). |
| 480 | */ |
| 481 | public static class ExtraActionsAreEmitted { |
| janakr | cd72f4b | 2018-02-13 16:25:36 -0800 | [diff] [blame] | 482 | public static final MockRule TEST_RULE = |
| 483 | () -> |
| 484 | MockRule.ancestor(TestAspects.BASE_RULE.getClass()) |
| 485 | .factory(DummyRuleFactory.class) |
| 486 | .define( |
| 487 | "testrule", |
| 488 | (builder, env) -> |
| 489 | builder |
| 490 | .add( |
| 491 | attr("foo", LABEL_LIST) |
| 492 | .legacyAllowAnyFileType() |
| 493 | .aspect(AspectThatRegistersAction.INSTANCE)) |
| 494 | .add( |
| 495 | attr(":action_listener", LABEL_LIST) |
| 496 | .cfg(HostTransition.INSTANCE) |
| 497 | .value(ACTION_LISTENER))); |
| Carmi Grushko | eaaa9d0d | 2015-11-17 01:54:45 +0000 | [diff] [blame] | 498 | |
| Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 499 | public static class AspectThatRegistersAction extends NativeAspectClass |
| 500 | implements ConfiguredAspectFactory { |
| janakr | cd72f4b | 2018-02-13 16:25:36 -0800 | [diff] [blame] | 501 | |
| 502 | static final AspectThatRegistersAction INSTANCE = new AspectThatRegistersAction(); |
| 503 | |
| 504 | private AspectThatRegistersAction() {} |
| 505 | |
| Carmi Grushko | eaaa9d0d | 2015-11-17 01:54:45 +0000 | [diff] [blame] | 506 | @Override |
| 507 | public AspectDefinition getDefinition(AspectParameters params) { |
| Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 508 | return new AspectDefinition.Builder(this).build(); |
| Carmi Grushko | eaaa9d0d | 2015-11-17 01:54:45 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | @Override |
| 512 | public ConfiguredAspect create( |
| dannark | e75cfb3 | 2019-01-10 14:36:24 -0800 | [diff] [blame] | 513 | ConfiguredTargetAndData ctadBase, |
| 514 | RuleContext ruleContext, |
| 515 | AspectParameters parameters, |
| 516 | String toolsRepository) |
| tomlu | 39a0a38 | 2018-06-22 09:43:23 -0700 | [diff] [blame] | 517 | throws InterruptedException, ActionConflictException { |
| Carmi Grushko | eaaa9d0d | 2015-11-17 01:54:45 +0000 | [diff] [blame] | 518 | ruleContext.registerAction(new NullAction(ruleContext.createOutputArtifact())); |
| Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 519 | return new ConfiguredAspect.Builder(this, parameters, ruleContext).build(); |
| Carmi Grushko | eaaa9d0d | 2015-11-17 01:54:45 +0000 | [diff] [blame] | 520 | } |
| 521 | } |
| Carmi Grushko | eaaa9d0d | 2015-11-17 01:54:45 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /** |
| 525 | * Test that actions registered in an Aspect are reported as extra-actions on the attached rule. |
| 526 | * AspectThatRegistersAction registers a NullAction, whose mnemonic is "Null". We have an |
| 527 | * action_listener that targets that mnemonic, which makes sure the Aspect machinery will expose |
| 528 | * an ExtraActionArtifactsProvider. |
| 529 | * The rule //a:a doesn't have an aspect, so the only action we get is the one on //a:b |
| 530 | * (which does have an aspect). |
| 531 | */ |
| 532 | @Test |
| 533 | public void extraActionsAreEmitted() throws Exception { |
| janakr | cd72f4b | 2018-02-13 16:25:36 -0800 | [diff] [blame] | 534 | setRulesAndAspectsAvailableInTests( |
| 535 | ImmutableList.of( |
| 536 | TestAspects.SIMPLE_ASPECT, ExtraActionsAreEmitted.AspectThatRegistersAction.INSTANCE), |
| 537 | ImmutableList.of(TestAspects.BASE_RULE, ExtraActionsAreEmitted.TEST_RULE)); |
| Carmi Grushko | eaaa9d0d | 2015-11-17 01:54:45 +0000 | [diff] [blame] | 538 | useConfiguration("--experimental_action_listener=//extra_actions:listener"); |
| 539 | scratch.file( |
| 540 | "extra_actions/BUILD", |
| 541 | "extra_action(name='xa', cmd='echo dont-care')", |
| 542 | "action_listener(name='listener', mnemonics=['Null'], extra_actions=[':xa'])"); |
| 543 | pkg("a", |
| 544 | "testrule(name='a', foo=[':b'])", |
| 545 | "testrule(name='b')"); |
| 546 | update(); |
| 547 | |
| 548 | ConfiguredTarget a = getConfiguredTarget("//a:a"); |
| Carmi Grushko | babd485 | 2016-11-18 17:58:09 +0000 | [diff] [blame] | 549 | NestedSet<Artifact> extraActionArtifacts = |
| 550 | a.getProvider(ExtraActionArtifactsProvider.class).getTransitiveExtraActionArtifacts(); |
| 551 | for (Artifact artifact : extraActionArtifacts) { |
| 552 | assertThat(artifact.getOwnerLabel()).isEqualTo(Label.create("@//a", "b")); |
| 553 | } |
| Carmi Grushko | eaaa9d0d | 2015-11-17 01:54:45 +0000 | [diff] [blame] | 554 | } |
| Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 555 | |
| 556 | @Test |
| 557 | public void aspectPropagatesToAllAttributes() throws Exception { |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 558 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.SIMPLE_RULE, |
| 559 | TestAspects.ALL_ATTRIBUTES_ASPECT_RULE); |
| Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 560 | pkg("a", |
| 561 | "simple(name='a', foo=[':b'], foo1=':c', txt='some text')", |
| 562 | "simple(name='b', foo=[], txt='some text')", |
| 563 | "simple(name='c', foo=[], txt='more text')", |
| 564 | "all_attributes_aspect(name='x', foo=[':a'])"); |
| 565 | |
| 566 | ConfiguredTarget a = getConfiguredTarget("//a:x"); |
| 567 | assertThat(a.getProvider(RuleInfo.class).getData()) |
| 568 | .containsExactly("aspect //a:a", "aspect //a:b", "aspect //a:c", "rule //a:x"); |
| 569 | } |
| 570 | |
| Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 571 | /** |
| 572 | * Tests that when --experimental_extra_action_top_level_only, Blaze reports extra-actions for |
| 573 | * actions registered by Aspects injected by a top-level rule. Because we can't know whether an |
| 574 | * aspect was injected by a top-level target or one of its children, we approximate it by only |
| 575 | * reporting extra-actions from Aspects that the top-level target could have injected. |
| 576 | * |
| 577 | * <p>Here, injector1() and injector2() inject aspects into their children. null_rule() just |
| 578 | * passes the aspects to its children. The test makes sure that actions registered by aspect1 |
| 579 | * (injected by injector1()) are reported to the extra-action mechanism. Actions registered by |
| 580 | * aspect2 (from injector2) are not reported, because the target under test (//x:a) doesn't inject |
| 581 | * aspect2. |
| 582 | */ |
| 583 | @Test |
| 584 | public void extraActionsAreEmitted_topLevel() throws Exception { |
| 585 | useConfiguration( |
| 586 | "--experimental_action_listener=//pkg1:listener", |
| 587 | "--experimental_extra_action_top_level_only"); |
| 588 | |
| 589 | scratch.file( |
| 590 | "x/BUILD", |
| 591 | "load(':extension.bzl', 'injector1', 'injector2', 'null_rule')", |
| 592 | "injector1(name='a', deps=[':b'])", |
| 593 | "null_rule(name='b', deps=[':c'])", |
| 594 | "null_rule(name='c', deps=[':d'])", |
| 595 | "injector2(name = 'd', extra_deps=[':e'])", |
| 596 | "null_rule(name = 'e')"); |
| 597 | |
| 598 | scratch.file( |
| 599 | "x/extension.bzl", |
| 600 | "def _aspect_impl(target, ctx):", |
| dslomov | ae0b774 | 2017-06-30 10:07:48 +0200 | [diff] [blame] | 601 | " ctx.actions.do_nothing(mnemonic='Mnemonic')", |
| Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 602 | " return struct()", |
| 603 | "aspect1 = aspect(_aspect_impl, attr_aspects=['deps'])", |
| 604 | "aspect2 = aspect(_aspect_impl, attr_aspects=['extra_deps'])", |
| 605 | "def _rule_impl(ctx):", |
| 606 | " return struct()", |
| 607 | "injector1 = rule(_rule_impl, attrs = { 'deps' : attr.label_list(aspects = [aspect1]) })", |
| 608 | "null_rule = rule(_rule_impl, attrs = { 'deps' : attr.label_list() })", |
| 609 | "injector2 = rule(", |
| 610 | " _rule_impl, attrs = { 'extra_deps' : attr.label_list(aspects = [aspect2]) })"); |
| 611 | |
| 612 | scratch.file( |
| 613 | "pkg1/BUILD", |
| 614 | "extra_action(name='xa', cmd='echo dont-care')", |
| 615 | "action_listener(name='listener', mnemonics=['Mnemonic'], extra_actions=[':xa'])"); |
| 616 | |
| 617 | // Sanity check: //x:d injects an aspect which produces some extra-action. |
| 618 | { |
| ulfjack | c23bdac | 2018-06-13 03:06:16 -0700 | [diff] [blame] | 619 | AnalysisResult analysisResult = update("//x:d"); |
| Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 620 | |
| 621 | // Get owners of all extra-action artifacts. |
| 622 | List<Label> extraArtifactOwners = new ArrayList<>(); |
| janakr | ccb7456 | 2018-07-30 10:56:57 -0700 | [diff] [blame] | 623 | for (Artifact artifact : analysisResult.getTopLevelArtifactsToOwnerLabels().getArtifacts()) { |
| Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 624 | if (artifact.getRootRelativePathString().endsWith(".xa")) { |
| 625 | extraArtifactOwners.add(artifact.getOwnerLabel()); |
| 626 | } |
| 627 | } |
| 628 | assertThat(extraArtifactOwners).containsExactly(Label.create("@//x", "e")); |
| 629 | } |
| 630 | |
| 631 | // Actual test: //x:a reports actions registered by the aspect it injects. |
| 632 | { |
| ulfjack | c23bdac | 2018-06-13 03:06:16 -0700 | [diff] [blame] | 633 | AnalysisResult analysisResult = update("//x:a"); |
| Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 634 | |
| 635 | // Get owners of all extra-action artifacts. |
| 636 | List<Label> extraArtifactOwners = new ArrayList<>(); |
| janakr | ccb7456 | 2018-07-30 10:56:57 -0700 | [diff] [blame] | 637 | for (Artifact artifact : analysisResult.getTopLevelArtifactsToOwnerLabels().getArtifacts()) { |
| Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 638 | if (artifact.getRootRelativePathString().endsWith(".xa")) { |
| 639 | extraArtifactOwners.add(artifact.getOwnerLabel()); |
| 640 | } |
| 641 | } |
| 642 | assertThat(extraArtifactOwners) |
| 643 | .containsExactly( |
| 644 | Label.create("@//x", "b"), Label.create("@//x", "c"), Label.create("@//x", "d")); |
| 645 | } |
| 646 | } |
| 647 | |
| Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 648 | @Test |
| Carmi Grushko | 905a29d | 2016-12-09 20:42:29 +0000 | [diff] [blame] | 649 | public void extraActionsFromDifferentAspectsDontConflict() throws Exception { |
| 650 | useConfiguration( |
| 651 | "--experimental_action_listener=//pkg1:listener", |
| 652 | "--experimental_extra_action_top_level_only"); |
| 653 | |
| 654 | scratch.file( |
| 655 | "x/BUILD", |
| 656 | "load(':extension.bzl', 'injector1', 'injector2', 'null_rule')", |
| Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 657 | "injector2(name='i2_a', deps = [':i1_a'])", |
| Carmi Grushko | 905a29d | 2016-12-09 20:42:29 +0000 | [diff] [blame] | 658 | "injector1(name='i1_a', deps=[':n'], param = 'a')", |
| 659 | "injector1(name='i1_b', deps=[':n'], param = 'b')", |
| 660 | "injector2(name='i2', deps=[':n'])", |
| 661 | "null_rule(name = 'n')"); |
| 662 | |
| 663 | scratch.file( |
| 664 | "x/extension.bzl", |
| 665 | "def _aspect_impl(target, ctx):", |
| dslomov | ae0b774 | 2017-06-30 10:07:48 +0200 | [diff] [blame] | 666 | " ctx.actions.do_nothing(mnemonic='Mnemonic')", |
| Carmi Grushko | 905a29d | 2016-12-09 20:42:29 +0000 | [diff] [blame] | 667 | " return struct()", |
| 668 | "aspect1 = aspect(_aspect_impl, attr_aspects=['deps'], attrs =", |
| 669 | " {'param': attr.string(values = ['a', 'b'])})", |
| 670 | "aspect2 = aspect(_aspect_impl, attr_aspects=['deps'])", |
| 671 | "def _rule_impl(ctx):", |
| 672 | " return struct()", |
| 673 | "injector1 = rule(_rule_impl, attrs =", |
| 674 | " { 'deps' : attr.label_list(aspects = [aspect1]), 'param' : attr.string() })", |
| 675 | "injector2 = rule(_rule_impl, attrs = { 'deps' : attr.label_list(aspects = [aspect2]) })", |
| 676 | "null_rule = rule(_rule_impl, attrs = { 'deps' : attr.label_list() })" |
| 677 | ); |
| 678 | |
| 679 | scratch.file( |
| 680 | "pkg1/BUILD", |
| 681 | "extra_action(name='xa', cmd='echo dont-care')", |
| 682 | "action_listener(name='listener', mnemonics=['Mnemonic'], extra_actions=[':xa'])"); |
| 683 | |
| Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 684 | update("//x:i1_a", "//x:i1_b", "//x:i2", "//x:i2_a"); |
| Carmi Grushko | 905a29d | 2016-12-09 20:42:29 +0000 | [diff] [blame] | 685 | |
| 686 | // Implicitly check that update() didn't throw an exception because of two actions producing |
| 687 | // the same outputs. |
| 688 | } |
| 689 | |
| 690 | @Test |
| Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 691 | public void aspectPropagatesToAllAttributesImplicit() throws Exception { |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 692 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.SIMPLE_RULE, |
| 693 | TestAspects.IMPLICIT_DEP_RULE, TestAspects.ALL_ATTRIBUTES_ASPECT_RULE); |
| Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 694 | scratch.file( |
| 695 | "extra/BUILD", |
| 696 | "simple(name ='extra')" |
| 697 | ); |
| 698 | pkg("a", |
| 699 | "simple(name='a', foo=[':b'], foo1=':c', txt='some text')", |
| 700 | "simple(name='b', foo=[], txt='some text')", |
| 701 | "implicit_dep(name='c')", |
| 702 | "all_attributes_aspect(name='x', foo=[':a'])"); |
| 703 | update(); |
| 704 | |
| 705 | ConfiguredTarget a = getConfiguredTarget("//a:x"); |
| 706 | assertThat(a.getProvider(RuleInfo.class).getData()) |
| 707 | .containsExactly( |
| 708 | "aspect //a:a", |
| 709 | "aspect //a:b", |
| 710 | "aspect //a:c", |
| 711 | "aspect //extra:extra", |
| 712 | "rule //a:x"); |
| 713 | } |
| 714 | |
| 715 | |
| 716 | @Test |
| 717 | public void aspectPropagatesToAllAttributesLateBound() throws Exception { |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 718 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.SIMPLE_RULE, |
| 719 | TestAspects.LATE_BOUND_DEP_RULE, TestAspects.ALL_ATTRIBUTES_ASPECT_RULE); |
| Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 720 | |
| 721 | scratch.file( |
| 722 | "extra/BUILD", |
| 723 | "simple(name ='extra')" |
| 724 | ); |
| 725 | pkg("a", |
| 726 | "simple(name='a', foo=[':b'], foo1=':c', txt='some text')", |
| 727 | "simple(name='b', foo=[], txt='some text')", |
| 728 | "late_bound_dep(name='c')", |
| 729 | "all_attributes_aspect(name='x', foo=[':a'])"); |
| 730 | useConfiguration("--plugin=//extra:extra"); |
| 731 | update(); |
| 732 | |
| 733 | ConfiguredTarget a = getConfiguredTarget("//a:x"); |
| 734 | assertThat(a.getProvider(RuleInfo.class).getData()) |
| 735 | .containsExactly( |
| 736 | "aspect //a:a", |
| 737 | "aspect //a:b", |
| 738 | "aspect //a:c", |
| 739 | "aspect //extra:extra", |
| 740 | "rule //a:x"); |
| 741 | } |
| 742 | |
| Googler | 909910c | 2016-11-04 15:50:51 +0000 | [diff] [blame] | 743 | /** |
| 744 | * Ensures an aspect with attr = '*' doesn't try to propagate to its own implicit attributes. |
| 745 | * Doing so leads to a dependency cycle. |
| 746 | */ |
| 747 | @Test |
| 748 | public void aspectWithAllAttributesDoesNotPropagateToOwnImplicitAttributes() throws Exception { |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 749 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.SIMPLE_RULE, |
| 750 | TestAspects.ALL_ATTRIBUTES_WITH_TOOL_ASPECT_RULE); |
| Googler | 909910c | 2016-11-04 15:50:51 +0000 | [diff] [blame] | 751 | pkg( |
| 752 | "a", |
| 753 | "simple(name='tool')", |
| 754 | "simple(name='a')", |
| 755 | "all_attributes_with_tool_aspect(name='x', foo=[':a'])"); |
| 756 | |
| 757 | ConfiguredTarget a = getConfiguredTarget("//a:x"); |
| 758 | assertThat(a.getProvider(RuleInfo.class).getData()) |
| 759 | .containsExactly("aspect //a:a", "rule //a:x"); |
| 760 | } |
| 761 | |
| 762 | /** |
| 763 | * Makes sure the aspect *will* propagate to its implicit attributes if there is a "regular" |
| 764 | * dependency path to it (i.e. not through its own implicit attributes). |
| 765 | */ |
| 766 | @Test |
| 767 | public void aspectWithAllAttributesPropagatesToItsToolIfThereIsPath() throws Exception { |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 768 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.SIMPLE_RULE, |
| 769 | TestAspects.ALL_ATTRIBUTES_WITH_TOOL_ASPECT_RULE); |
| Googler | 909910c | 2016-11-04 15:50:51 +0000 | [diff] [blame] | 770 | pkg( |
| 771 | "a", |
| 772 | "simple(name='tool')", |
| 773 | "simple(name='a', foo=[':b'], foo1=':c', txt='some text')", |
| 774 | "simple(name='b', foo=[], txt='some text')", |
| 775 | "simple(name='c', foo=[':tool'], txt='more text')", |
| 776 | "all_attributes_with_tool_aspect(name='x', foo=[':a'])"); |
| 777 | |
| 778 | ConfiguredTarget a = getConfiguredTarget("//a:x"); |
| 779 | assertThat(a.getProvider(RuleInfo.class).getData()) |
| 780 | .containsExactly( |
| 781 | "aspect //a:a", "aspect //a:b", "aspect //a:c", "aspect //a:tool", "rule //a:x"); |
| 782 | } |
| Dmitry Lomov | 65fde00 | 2017-02-07 17:24:04 +0000 | [diff] [blame] | 783 | |
| 784 | @Test |
| 785 | public void aspectTruthInAdvertisement() throws Exception { |
| 786 | reporter.removeHandler(failFastHandler); // expect errors |
| gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 787 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.SIMPLE_RULE, |
| 788 | TestAspects.FALSE_ADVERTISEMENT_ASPECT_RULE); |
| Dmitry Lomov | 65fde00 | 2017-02-07 17:24:04 +0000 | [diff] [blame] | 789 | pkg( |
| 790 | "a", |
| 791 | "simple(name = 's')", |
| 792 | "false_advertisement_aspect(name = 'x', deps = [':s'])" |
| 793 | ); |
| 794 | try { |
| 795 | update("//a:x"); |
| 796 | } catch (ViewCreationFailedException e) { |
| 797 | // expected. |
| 798 | } |
| 799 | assertContainsEvent( |
| 800 | "Aspect 'FalseAdvertisementAspect', applied to '//a:s'," |
| 801 | + " does not provide advertised provider 'RequiredProvider'"); |
| 802 | assertContainsEvent( |
| 803 | "Aspect 'FalseAdvertisementAspect', applied to '//a:s'," |
| 804 | + " does not provide advertised provider 'advertised_provider'"); |
| 805 | } |
| dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 806 | |
| 807 | @Test |
| dslomov | ce59d4d | 2017-08-04 17:32:47 +0200 | [diff] [blame] | 808 | public void aspectApplyingToFiles() throws Exception { |
| dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 809 | AspectApplyingToFiles aspectApplyingToFiles = new AspectApplyingToFiles(); |
| 810 | setRulesAndAspectsAvailableInTests( |
| 811 | ImmutableList.<NativeAspectClass>of(aspectApplyingToFiles), |
| 812 | ImmutableList.<RuleDefinition>of()); |
| 813 | pkg( |
| 814 | "a", |
| 815 | "java_binary(name = 'x', main_class = 'x.FooBar', srcs = ['x.java'])" |
| 816 | ); |
| 817 | AnalysisResult analysisResult = update(new EventBus(), defaultFlags(), |
| 818 | ImmutableList.of(aspectApplyingToFiles.getName()), |
| 819 | "//a:x_deploy.jar"); |
| 820 | AspectValue aspect = Iterables.getOnlyElement(analysisResult.getAspects()); |
| 821 | AspectApplyingToFiles.Provider provider = |
| 822 | aspect.getConfiguredAspect().getProvider(AspectApplyingToFiles.Provider.class); |
| 823 | assertThat(provider.getLabel()) |
| 824 | .isEqualTo(Label.parseAbsoluteUnchecked("//a:x_deploy.jar")); |
| 825 | } |
| dslomov | ce59d4d | 2017-08-04 17:32:47 +0200 | [diff] [blame] | 826 | |
| 827 | @Test |
| 828 | public void aspectApplyingToSourceFilesIgnored() throws Exception { |
| 829 | AspectApplyingToFiles aspectApplyingToFiles = new AspectApplyingToFiles(); |
| 830 | setRulesAndAspectsAvailableInTests( |
| 831 | ImmutableList.<NativeAspectClass>of(aspectApplyingToFiles), |
| 832 | ImmutableList.<RuleDefinition>of()); |
| 833 | pkg( |
| 834 | "a", |
| 835 | "java_binary(name = 'x', main_class = 'x.FooBar', srcs = ['x.java'])" |
| 836 | ); |
| 837 | scratch.file("a/x.java", ""); |
| 838 | AnalysisResult analysisResult = update(new EventBus(), defaultFlags(), |
| 839 | ImmutableList.of(aspectApplyingToFiles.getName()), |
| 840 | "//a:x.java"); |
| 841 | AspectValue aspect = Iterables.getOnlyElement(analysisResult.getAspects()); |
| 842 | assertThat(aspect.getConfiguredAspect().getProvider(AspectApplyingToFiles.Provider.class)) |
| 843 | .isNull(); |
| 844 | } |
| 845 | |
| Googler | 9204499 | 2018-03-14 14:26:30 -0700 | [diff] [blame] | 846 | @Test |
| 847 | public void duplicateAspectsDeduped() throws Exception { |
| 848 | AspectApplyingToFiles aspectApplyingToFiles = new AspectApplyingToFiles(); |
| 849 | setRulesAndAspectsAvailableInTests( |
| 850 | ImmutableList.<NativeAspectClass>of(aspectApplyingToFiles), |
| 851 | ImmutableList.<RuleDefinition>of()); |
| 852 | pkg("a", "java_binary(name = 'x', main_class = 'x.FooBar', srcs = ['x.java'])"); |
| 853 | AnalysisResult analysisResult = |
| 854 | update( |
| 855 | new EventBus(), |
| 856 | defaultFlags(), |
| 857 | ImmutableList.of(aspectApplyingToFiles.getName(), aspectApplyingToFiles.getName()), |
| 858 | "//a:x_deploy.jar"); |
| 859 | AspectValue aspect = Iterables.getOnlyElement(analysisResult.getAspects()); |
| 860 | AspectApplyingToFiles.Provider provider = |
| 861 | aspect.getConfiguredAspect().getProvider(AspectApplyingToFiles.Provider.class); |
| 862 | assertThat(provider.getLabel()).isEqualTo(Label.parseAbsoluteUnchecked("//a:x_deploy.jar")); |
| 863 | } |
| Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 864 | } |