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 | |
| 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; |
Carmi Grushko | eaaa9d0d | 2015-11-17 01:54:45 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.actions.util.ActionsTestUtil.NullAction; |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 29 | import com.google.devtools.build.lib.analysis.BuildView.AnalysisResult; |
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; |
mjhalupka | b9a0baf | 2018-01-31 14:04:50 -0800 | [diff] [blame] | 48 | import com.google.devtools.build.lib.skyframe.ConfiguredTargetAndTarget; |
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 { |
gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 412 | public static final MockRule TEST_RULE = () -> |
| 413 | MockRule.ancestor(TestAspects.BASE_RULE.getClass()).factory(DummyRuleFactory.class).define( |
| 414 | "testrule", |
| 415 | (builder, env) -> |
| 416 | builder |
| 417 | .add(attr("foo", LABEL_LIST).legacyAllowAnyFileType() |
| 418 | .aspect(new AspectWithEmptyLateBoundAttribute()))); |
Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 419 | |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 420 | public static class AspectWithEmptyLateBoundAttribute extends NativeAspectClass |
| 421 | implements ConfiguredAspectFactory { |
Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 422 | @Override |
| 423 | public AspectDefinition getDefinition(AspectParameters params) { |
Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 424 | return new AspectDefinition.Builder(this) |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 425 | .add(attr(":late", LABEL).value(LateBoundDefault.alwaysNull())) |
| 426 | .build(); |
Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | @Override |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 430 | public ConfiguredAspect create( |
mjhalupka | b9a0baf | 2018-01-31 14:04:50 -0800 | [diff] [blame] | 431 | ConfiguredTargetAndTarget ctatBase, RuleContext ruleContext, AspectParameters parameters) |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 432 | throws InterruptedException { |
Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 433 | Object lateBoundPrereq = ruleContext.getPrerequisite(":late", TARGET); |
Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 434 | return new ConfiguredAspect.Builder(this, parameters, ruleContext) |
Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 435 | .addProvider( |
Carmi Grushko | 261f5bb | 2015-12-09 19:38:38 +0000 | [diff] [blame] | 436 | AspectInfo.class, |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 437 | new AspectInfo( |
| 438 | NestedSetBuilder.create( |
| 439 | Order.STABLE_ORDER, lateBoundPrereq != null ? "non-empty" : "empty"))) |
Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 440 | .build(); |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | /** |
mstaib | 807a9b2 | 2017-09-19 17:06:32 +0200 | [diff] [blame] | 446 | * An Aspect has a late-bound attribute with no value (that is, a LateBoundDefault whose |
| 447 | * getDefault() returns `null`). Test that this attribute is available in the RuleContext which is |
| 448 | * provided to the Aspect's `create()` method. |
Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 449 | */ |
| 450 | @Test |
| 451 | public void emptyAspectAttributesAreAvailableInRuleContext() throws Exception { |
gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 452 | setRulesAvailableInTests(TestAspects.BASE_RULE, |
| 453 | EmptyAspectAttributesAreAvailableInRuleContext.TEST_RULE); |
Carmi Grushko | 06f65f7 | 2015-11-02 22:42:24 +0000 | [diff] [blame] | 454 | pkg("a", |
| 455 | "testrule(name='a', foo=[':b'])", |
| 456 | "testrule(name='b')"); |
| 457 | ConfiguredTarget a = getConfiguredTarget("//a:a"); |
| 458 | assertThat(a.getProvider(RuleInfo.class).getData()).contains("empty"); |
| 459 | } |
| 460 | |
Carmi Grushko | eaaa9d0d | 2015-11-17 01:54:45 +0000 | [diff] [blame] | 461 | /** |
| 462 | * Rule definitions to be used in extraActionsAreEmitted(). |
| 463 | */ |
| 464 | public static class ExtraActionsAreEmitted { |
gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 465 | public static final MockRule TEST_RULE = () -> |
| 466 | MockRule.ancestor(TestAspects.BASE_RULE.getClass()).factory(DummyRuleFactory.class).define( |
| 467 | "testrule", |
| 468 | (builder, env) -> |
| 469 | builder |
| 470 | .add(attr("foo", LABEL_LIST).legacyAllowAnyFileType() |
| 471 | .aspect(new AspectThatRegistersAction())) |
gregce | 676a957 | 2017-12-21 11:33:32 -0800 | [diff] [blame] | 472 | .add(attr(":action_listener", LABEL_LIST) |
| 473 | .cfg(HostTransition.INSTANCE) |
| 474 | .value(ACTION_LISTENER))); |
Carmi Grushko | eaaa9d0d | 2015-11-17 01:54:45 +0000 | [diff] [blame] | 475 | |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 476 | public static class AspectThatRegistersAction extends NativeAspectClass |
| 477 | implements ConfiguredAspectFactory { |
Carmi Grushko | eaaa9d0d | 2015-11-17 01:54:45 +0000 | [diff] [blame] | 478 | @Override |
| 479 | public AspectDefinition getDefinition(AspectParameters params) { |
Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 480 | return new AspectDefinition.Builder(this).build(); |
Carmi Grushko | eaaa9d0d | 2015-11-17 01:54:45 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Carmi Grushko | eaaa9d0d | 2015-11-17 01:54:45 +0000 | [diff] [blame] | 483 | @Override |
| 484 | public ConfiguredAspect create( |
mjhalupka | b9a0baf | 2018-01-31 14:04:50 -0800 | [diff] [blame] | 485 | ConfiguredTargetAndTarget ctatBase, RuleContext ruleContext, AspectParameters parameters) |
| 486 | throws InterruptedException { |
Carmi Grushko | eaaa9d0d | 2015-11-17 01:54:45 +0000 | [diff] [blame] | 487 | ruleContext.registerAction(new NullAction(ruleContext.createOutputArtifact())); |
Dmitry Lomov | dce0170 | 2016-11-28 15:51:32 +0000 | [diff] [blame] | 488 | return new ConfiguredAspect.Builder(this, parameters, ruleContext).build(); |
Carmi Grushko | eaaa9d0d | 2015-11-17 01:54:45 +0000 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * Test that actions registered in an Aspect are reported as extra-actions on the attached rule. |
| 495 | * AspectThatRegistersAction registers a NullAction, whose mnemonic is "Null". We have an |
| 496 | * action_listener that targets that mnemonic, which makes sure the Aspect machinery will expose |
| 497 | * an ExtraActionArtifactsProvider. |
| 498 | * The rule //a:a doesn't have an aspect, so the only action we get is the one on //a:b |
| 499 | * (which does have an aspect). |
| 500 | */ |
| 501 | @Test |
| 502 | public void extraActionsAreEmitted() throws Exception { |
gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 503 | setRulesAvailableInTests(TestAspects.BASE_RULE, ExtraActionsAreEmitted.TEST_RULE); |
Carmi Grushko | eaaa9d0d | 2015-11-17 01:54:45 +0000 | [diff] [blame] | 504 | useConfiguration("--experimental_action_listener=//extra_actions:listener"); |
| 505 | scratch.file( |
| 506 | "extra_actions/BUILD", |
| 507 | "extra_action(name='xa', cmd='echo dont-care')", |
| 508 | "action_listener(name='listener', mnemonics=['Null'], extra_actions=[':xa'])"); |
| 509 | pkg("a", |
| 510 | "testrule(name='a', foo=[':b'])", |
| 511 | "testrule(name='b')"); |
| 512 | update(); |
| 513 | |
| 514 | ConfiguredTarget a = getConfiguredTarget("//a:a"); |
Carmi Grushko | babd485 | 2016-11-18 17:58:09 +0000 | [diff] [blame] | 515 | NestedSet<Artifact> extraActionArtifacts = |
| 516 | a.getProvider(ExtraActionArtifactsProvider.class).getTransitiveExtraActionArtifacts(); |
| 517 | for (Artifact artifact : extraActionArtifacts) { |
| 518 | assertThat(artifact.getOwnerLabel()).isEqualTo(Label.create("@//a", "b")); |
| 519 | } |
Carmi Grushko | eaaa9d0d | 2015-11-17 01:54:45 +0000 | [diff] [blame] | 520 | } |
Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 521 | |
| 522 | @Test |
| 523 | public void aspectPropagatesToAllAttributes() throws Exception { |
gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 524 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.SIMPLE_RULE, |
| 525 | TestAspects.ALL_ATTRIBUTES_ASPECT_RULE); |
Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 526 | pkg("a", |
| 527 | "simple(name='a', foo=[':b'], foo1=':c', txt='some text')", |
| 528 | "simple(name='b', foo=[], txt='some text')", |
| 529 | "simple(name='c', foo=[], txt='more text')", |
| 530 | "all_attributes_aspect(name='x', foo=[':a'])"); |
| 531 | |
| 532 | ConfiguredTarget a = getConfiguredTarget("//a:x"); |
| 533 | assertThat(a.getProvider(RuleInfo.class).getData()) |
| 534 | .containsExactly("aspect //a:a", "aspect //a:b", "aspect //a:c", "rule //a:x"); |
| 535 | } |
| 536 | |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 537 | /** |
| 538 | * Tests that when --experimental_extra_action_top_level_only, Blaze reports extra-actions for |
| 539 | * actions registered by Aspects injected by a top-level rule. Because we can't know whether an |
| 540 | * aspect was injected by a top-level target or one of its children, we approximate it by only |
| 541 | * reporting extra-actions from Aspects that the top-level target could have injected. |
| 542 | * |
| 543 | * <p>Here, injector1() and injector2() inject aspects into their children. null_rule() just |
| 544 | * passes the aspects to its children. The test makes sure that actions registered by aspect1 |
| 545 | * (injected by injector1()) are reported to the extra-action mechanism. Actions registered by |
| 546 | * aspect2 (from injector2) are not reported, because the target under test (//x:a) doesn't inject |
| 547 | * aspect2. |
| 548 | */ |
| 549 | @Test |
| 550 | public void extraActionsAreEmitted_topLevel() throws Exception { |
| 551 | useConfiguration( |
| 552 | "--experimental_action_listener=//pkg1:listener", |
| 553 | "--experimental_extra_action_top_level_only"); |
| 554 | |
| 555 | scratch.file( |
| 556 | "x/BUILD", |
| 557 | "load(':extension.bzl', 'injector1', 'injector2', 'null_rule')", |
| 558 | "injector1(name='a', deps=[':b'])", |
| 559 | "null_rule(name='b', deps=[':c'])", |
| 560 | "null_rule(name='c', deps=[':d'])", |
| 561 | "injector2(name = 'd', extra_deps=[':e'])", |
| 562 | "null_rule(name = 'e')"); |
| 563 | |
| 564 | scratch.file( |
| 565 | "x/extension.bzl", |
| 566 | "def _aspect_impl(target, ctx):", |
dslomov | ae0b774 | 2017-06-30 10:07:48 +0200 | [diff] [blame] | 567 | " ctx.actions.do_nothing(mnemonic='Mnemonic')", |
Carmi Grushko | df9e5e1 | 2016-11-08 23:07:57 +0000 | [diff] [blame] | 568 | " return struct()", |
| 569 | "aspect1 = aspect(_aspect_impl, attr_aspects=['deps'])", |
| 570 | "aspect2 = aspect(_aspect_impl, attr_aspects=['extra_deps'])", |
| 571 | "def _rule_impl(ctx):", |
| 572 | " return struct()", |
| 573 | "injector1 = rule(_rule_impl, attrs = { 'deps' : attr.label_list(aspects = [aspect1]) })", |
| 574 | "null_rule = rule(_rule_impl, attrs = { 'deps' : attr.label_list() })", |
| 575 | "injector2 = rule(", |
| 576 | " _rule_impl, attrs = { 'extra_deps' : attr.label_list(aspects = [aspect2]) })"); |
| 577 | |
| 578 | scratch.file( |
| 579 | "pkg1/BUILD", |
| 580 | "extra_action(name='xa', cmd='echo dont-care')", |
| 581 | "action_listener(name='listener', mnemonics=['Mnemonic'], extra_actions=[':xa'])"); |
| 582 | |
| 583 | // Sanity check: //x:d injects an aspect which produces some extra-action. |
| 584 | { |
| 585 | BuildView.AnalysisResult analysisResult = update("//x:d"); |
| 586 | |
| 587 | // Get owners of all extra-action artifacts. |
| 588 | List<Label> extraArtifactOwners = new ArrayList<>(); |
| 589 | for (Artifact artifact : analysisResult.getAdditionalArtifactsToBuild()) { |
| 590 | if (artifact.getRootRelativePathString().endsWith(".xa")) { |
| 591 | extraArtifactOwners.add(artifact.getOwnerLabel()); |
| 592 | } |
| 593 | } |
| 594 | assertThat(extraArtifactOwners).containsExactly(Label.create("@//x", "e")); |
| 595 | } |
| 596 | |
| 597 | // Actual test: //x:a reports actions registered by the aspect it injects. |
| 598 | { |
| 599 | BuildView.AnalysisResult analysisResult = update("//x:a"); |
| 600 | |
| 601 | // Get owners of all extra-action artifacts. |
| 602 | List<Label> extraArtifactOwners = new ArrayList<>(); |
| 603 | for (Artifact artifact : analysisResult.getAdditionalArtifactsToBuild()) { |
| 604 | if (artifact.getRootRelativePathString().endsWith(".xa")) { |
| 605 | extraArtifactOwners.add(artifact.getOwnerLabel()); |
| 606 | } |
| 607 | } |
| 608 | assertThat(extraArtifactOwners) |
| 609 | .containsExactly( |
| 610 | Label.create("@//x", "b"), Label.create("@//x", "c"), Label.create("@//x", "d")); |
| 611 | } |
| 612 | } |
| 613 | |
Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 614 | @Test |
Carmi Grushko | 905a29d | 2016-12-09 20:42:29 +0000 | [diff] [blame] | 615 | public void extraActionsFromDifferentAspectsDontConflict() throws Exception { |
| 616 | useConfiguration( |
| 617 | "--experimental_action_listener=//pkg1:listener", |
| 618 | "--experimental_extra_action_top_level_only"); |
| 619 | |
| 620 | scratch.file( |
| 621 | "x/BUILD", |
| 622 | "load(':extension.bzl', 'injector1', 'injector2', 'null_rule')", |
Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 623 | "injector2(name='i2_a', deps = [':i1_a'])", |
Carmi Grushko | 905a29d | 2016-12-09 20:42:29 +0000 | [diff] [blame] | 624 | "injector1(name='i1_a', deps=[':n'], param = 'a')", |
| 625 | "injector1(name='i1_b', deps=[':n'], param = 'b')", |
| 626 | "injector2(name='i2', deps=[':n'])", |
| 627 | "null_rule(name = 'n')"); |
| 628 | |
| 629 | scratch.file( |
| 630 | "x/extension.bzl", |
| 631 | "def _aspect_impl(target, ctx):", |
dslomov | ae0b774 | 2017-06-30 10:07:48 +0200 | [diff] [blame] | 632 | " ctx.actions.do_nothing(mnemonic='Mnemonic')", |
Carmi Grushko | 905a29d | 2016-12-09 20:42:29 +0000 | [diff] [blame] | 633 | " return struct()", |
| 634 | "aspect1 = aspect(_aspect_impl, attr_aspects=['deps'], attrs =", |
| 635 | " {'param': attr.string(values = ['a', 'b'])})", |
| 636 | "aspect2 = aspect(_aspect_impl, attr_aspects=['deps'])", |
| 637 | "def _rule_impl(ctx):", |
| 638 | " return struct()", |
| 639 | "injector1 = rule(_rule_impl, attrs =", |
| 640 | " { 'deps' : attr.label_list(aspects = [aspect1]), 'param' : attr.string() })", |
| 641 | "injector2 = rule(_rule_impl, attrs = { 'deps' : attr.label_list(aspects = [aspect2]) })", |
| 642 | "null_rule = rule(_rule_impl, attrs = { 'deps' : attr.label_list() })" |
| 643 | ); |
| 644 | |
| 645 | scratch.file( |
| 646 | "pkg1/BUILD", |
| 647 | "extra_action(name='xa', cmd='echo dont-care')", |
| 648 | "action_listener(name='listener', mnemonics=['Mnemonic'], extra_actions=[':xa'])"); |
| 649 | |
Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 650 | update("//x:i1_a", "//x:i1_b", "//x:i2", "//x:i2_a"); |
Carmi Grushko | 905a29d | 2016-12-09 20:42:29 +0000 | [diff] [blame] | 651 | |
| 652 | // Implicitly check that update() didn't throw an exception because of two actions producing |
| 653 | // the same outputs. |
| 654 | } |
| 655 | |
| 656 | @Test |
Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 657 | public void aspectPropagatesToAllAttributesImplicit() throws Exception { |
gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 658 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.SIMPLE_RULE, |
| 659 | TestAspects.IMPLICIT_DEP_RULE, TestAspects.ALL_ATTRIBUTES_ASPECT_RULE); |
Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 660 | scratch.file( |
| 661 | "extra/BUILD", |
| 662 | "simple(name ='extra')" |
| 663 | ); |
| 664 | pkg("a", |
| 665 | "simple(name='a', foo=[':b'], foo1=':c', txt='some text')", |
| 666 | "simple(name='b', foo=[], txt='some text')", |
| 667 | "implicit_dep(name='c')", |
| 668 | "all_attributes_aspect(name='x', foo=[':a'])"); |
| 669 | update(); |
| 670 | |
| 671 | ConfiguredTarget a = getConfiguredTarget("//a:x"); |
| 672 | assertThat(a.getProvider(RuleInfo.class).getData()) |
| 673 | .containsExactly( |
| 674 | "aspect //a:a", |
| 675 | "aspect //a:b", |
| 676 | "aspect //a:c", |
| 677 | "aspect //extra:extra", |
| 678 | "rule //a:x"); |
| 679 | } |
| 680 | |
| 681 | |
| 682 | @Test |
| 683 | public void aspectPropagatesToAllAttributesLateBound() throws Exception { |
gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 684 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.SIMPLE_RULE, |
| 685 | TestAspects.LATE_BOUND_DEP_RULE, TestAspects.ALL_ATTRIBUTES_ASPECT_RULE); |
Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 686 | |
| 687 | scratch.file( |
| 688 | "extra/BUILD", |
| 689 | "simple(name ='extra')" |
| 690 | ); |
| 691 | pkg("a", |
| 692 | "simple(name='a', foo=[':b'], foo1=':c', txt='some text')", |
| 693 | "simple(name='b', foo=[], txt='some text')", |
| 694 | "late_bound_dep(name='c')", |
| 695 | "all_attributes_aspect(name='x', foo=[':a'])"); |
| 696 | useConfiguration("--plugin=//extra:extra"); |
| 697 | update(); |
| 698 | |
| 699 | ConfiguredTarget a = getConfiguredTarget("//a:x"); |
| 700 | assertThat(a.getProvider(RuleInfo.class).getData()) |
| 701 | .containsExactly( |
| 702 | "aspect //a:a", |
| 703 | "aspect //a:b", |
| 704 | "aspect //a:c", |
| 705 | "aspect //extra:extra", |
| 706 | "rule //a:x"); |
| 707 | } |
| 708 | |
Googler | 909910c | 2016-11-04 15:50:51 +0000 | [diff] [blame] | 709 | /** |
| 710 | * Ensures an aspect with attr = '*' doesn't try to propagate to its own implicit attributes. |
| 711 | * Doing so leads to a dependency cycle. |
| 712 | */ |
| 713 | @Test |
| 714 | public void aspectWithAllAttributesDoesNotPropagateToOwnImplicitAttributes() throws Exception { |
gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 715 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.SIMPLE_RULE, |
| 716 | TestAspects.ALL_ATTRIBUTES_WITH_TOOL_ASPECT_RULE); |
Googler | 909910c | 2016-11-04 15:50:51 +0000 | [diff] [blame] | 717 | pkg( |
| 718 | "a", |
| 719 | "simple(name='tool')", |
| 720 | "simple(name='a')", |
| 721 | "all_attributes_with_tool_aspect(name='x', foo=[':a'])"); |
| 722 | |
| 723 | ConfiguredTarget a = getConfiguredTarget("//a:x"); |
| 724 | assertThat(a.getProvider(RuleInfo.class).getData()) |
| 725 | .containsExactly("aspect //a:a", "rule //a:x"); |
| 726 | } |
| 727 | |
| 728 | /** |
| 729 | * Makes sure the aspect *will* propagate to its implicit attributes if there is a "regular" |
| 730 | * dependency path to it (i.e. not through its own implicit attributes). |
| 731 | */ |
| 732 | @Test |
| 733 | public void aspectWithAllAttributesPropagatesToItsToolIfThereIsPath() throws Exception { |
gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 734 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.SIMPLE_RULE, |
| 735 | TestAspects.ALL_ATTRIBUTES_WITH_TOOL_ASPECT_RULE); |
Googler | 909910c | 2016-11-04 15:50:51 +0000 | [diff] [blame] | 736 | pkg( |
| 737 | "a", |
| 738 | "simple(name='tool')", |
| 739 | "simple(name='a', foo=[':b'], foo1=':c', txt='some text')", |
| 740 | "simple(name='b', foo=[], txt='some text')", |
| 741 | "simple(name='c', foo=[':tool'], txt='more text')", |
| 742 | "all_attributes_with_tool_aspect(name='x', foo=[':a'])"); |
| 743 | |
| 744 | ConfiguredTarget a = getConfiguredTarget("//a:x"); |
| 745 | assertThat(a.getProvider(RuleInfo.class).getData()) |
| 746 | .containsExactly( |
| 747 | "aspect //a:a", "aspect //a:b", "aspect //a:c", "aspect //a:tool", "rule //a:x"); |
| 748 | } |
Dmitry Lomov | 65fde00 | 2017-02-07 17:24:04 +0000 | [diff] [blame] | 749 | |
| 750 | @Test |
| 751 | public void aspectTruthInAdvertisement() throws Exception { |
| 752 | reporter.removeHandler(failFastHandler); // expect errors |
gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 753 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.SIMPLE_RULE, |
| 754 | TestAspects.FALSE_ADVERTISEMENT_ASPECT_RULE); |
Dmitry Lomov | 65fde00 | 2017-02-07 17:24:04 +0000 | [diff] [blame] | 755 | pkg( |
| 756 | "a", |
| 757 | "simple(name = 's')", |
| 758 | "false_advertisement_aspect(name = 'x', deps = [':s'])" |
| 759 | ); |
| 760 | try { |
| 761 | update("//a:x"); |
| 762 | } catch (ViewCreationFailedException e) { |
| 763 | // expected. |
| 764 | } |
| 765 | assertContainsEvent( |
| 766 | "Aspect 'FalseAdvertisementAspect', applied to '//a:s'," |
| 767 | + " does not provide advertised provider 'RequiredProvider'"); |
| 768 | assertContainsEvent( |
| 769 | "Aspect 'FalseAdvertisementAspect', applied to '//a:s'," |
| 770 | + " does not provide advertised provider 'advertised_provider'"); |
| 771 | } |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 772 | |
| 773 | @Test |
dslomov | ce59d4d | 2017-08-04 17:32:47 +0200 | [diff] [blame] | 774 | public void aspectApplyingToFiles() throws Exception { |
dslomov | 99ea6b4 | 2017-04-25 17:46:17 +0200 | [diff] [blame] | 775 | AspectApplyingToFiles aspectApplyingToFiles = new AspectApplyingToFiles(); |
| 776 | setRulesAndAspectsAvailableInTests( |
| 777 | ImmutableList.<NativeAspectClass>of(aspectApplyingToFiles), |
| 778 | ImmutableList.<RuleDefinition>of()); |
| 779 | pkg( |
| 780 | "a", |
| 781 | "java_binary(name = 'x', main_class = 'x.FooBar', srcs = ['x.java'])" |
| 782 | ); |
| 783 | AnalysisResult analysisResult = update(new EventBus(), defaultFlags(), |
| 784 | ImmutableList.of(aspectApplyingToFiles.getName()), |
| 785 | "//a:x_deploy.jar"); |
| 786 | AspectValue aspect = Iterables.getOnlyElement(analysisResult.getAspects()); |
| 787 | AspectApplyingToFiles.Provider provider = |
| 788 | aspect.getConfiguredAspect().getProvider(AspectApplyingToFiles.Provider.class); |
| 789 | assertThat(provider.getLabel()) |
| 790 | .isEqualTo(Label.parseAbsoluteUnchecked("//a:x_deploy.jar")); |
| 791 | } |
dslomov | ce59d4d | 2017-08-04 17:32:47 +0200 | [diff] [blame] | 792 | |
| 793 | @Test |
| 794 | public void aspectApplyingToSourceFilesIgnored() throws Exception { |
| 795 | AspectApplyingToFiles aspectApplyingToFiles = new AspectApplyingToFiles(); |
| 796 | setRulesAndAspectsAvailableInTests( |
| 797 | ImmutableList.<NativeAspectClass>of(aspectApplyingToFiles), |
| 798 | ImmutableList.<RuleDefinition>of()); |
| 799 | pkg( |
| 800 | "a", |
| 801 | "java_binary(name = 'x', main_class = 'x.FooBar', srcs = ['x.java'])" |
| 802 | ); |
| 803 | scratch.file("a/x.java", ""); |
| 804 | AnalysisResult analysisResult = update(new EventBus(), defaultFlags(), |
| 805 | ImmutableList.of(aspectApplyingToFiles.getName()), |
| 806 | "//a:x.java"); |
| 807 | AspectValue aspect = Iterables.getOnlyElement(analysisResult.getAspects()); |
| 808 | assertThat(aspect.getConfiguredAspect().getProvider(AspectApplyingToFiles.Provider.class)) |
| 809 | .isNull(); |
| 810 | } |
| 811 | |
Ulf Adams | 8917925 | 2015-04-23 18:48:39 +0000 | [diff] [blame] | 812 | } |