Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +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; |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertWithMessage; |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 18 | |
Lukacs Berki | 7894c18 | 2016-05-10 12:07:01 +0000 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
lberki | 3c2c821 | 2020-10-21 04:00:04 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Iterables; |
| 21 | import com.google.devtools.build.lib.analysis.AspectCollection.AspectDeps; |
Florian Weikert | cca703a | 2015-12-07 09:56:38 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.analysis.util.AnalysisTestCase; |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.lib.analysis.util.TestAspects; |
ulfjack | 904a8d6 | 2018-05-29 05:17:35 -0700 | [diff] [blame] | 24 | import com.google.devtools.build.lib.causes.Cause; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.cmdline.Label; |
Ulf Adams | 2ac2096 | 2016-02-01 13:04:54 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.packages.Aspect; |
Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.packages.AspectDescriptor; |
Dmitry Lomov | 8ff0645 | 2015-10-21 19:16:30 +0000 | [diff] [blame] | 29 | import com.google.devtools.build.lib.packages.NativeAspectClass; |
Ulf Adams | 2ac2096 | 2016-02-01 13:04:54 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.packages.NoSuchPackageException; |
| 31 | import com.google.devtools.build.lib.packages.NoSuchTargetException; |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 32 | import com.google.devtools.build.lib.packages.Target; |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.util.OrderedSetMultimap; |
janakr | d0a3c5e | 2018-08-09 15:59:24 -0700 | [diff] [blame] | 34 | import java.util.Map; |
| 35 | import java.util.function.Function; |
| 36 | import java.util.stream.Collectors; |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 37 | import org.junit.Before; |
| 38 | import org.junit.Test; |
| 39 | import org.junit.runner.RunWith; |
| 40 | import org.junit.runners.JUnit4; |
| 41 | |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 42 | /** |
| 43 | * Tests for {@link DependencyResolver}. |
| 44 | * |
jhorvitz | e68a7d0 | 2021-04-26 11:49:28 -0700 | [diff] [blame] | 45 | * <p>These use custom rules so that all usual and unusual cases related to aspect processing can be |
| 46 | * tested. |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 47 | * |
| 48 | * <p>It would be nicer is we didn't have a Skyframe executor, if we didn't have that, we'd need a |
| 49 | * way to create a configuration, a package manager and a whole lot of other things, so it's just |
| 50 | * easier this way. |
| 51 | */ |
| 52 | @RunWith(JUnit4.class) |
Florian Weikert | cca703a | 2015-12-07 09:56:38 +0000 | [diff] [blame] | 53 | public class DependencyResolverTest extends AnalysisTestCase { |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 54 | private DependencyResolver dependencyResolver; |
| 55 | |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 56 | @Before |
jhorvitz | e68a7d0 | 2021-04-26 11:49:28 -0700 | [diff] [blame] | 57 | public final void createResolver() { |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 58 | dependencyResolver = |
| 59 | new DependencyResolver() { |
janakr | 9fcef03 | 2018-01-23 13:38:24 -0800 | [diff] [blame] | 60 | @Override |
janakr | d0a3c5e | 2018-08-09 15:59:24 -0700 | [diff] [blame] | 61 | protected Map<Label, Target> getTargets( |
lberki | 4cd4447 | 2019-02-06 08:36:34 -0800 | [diff] [blame] | 62 | OrderedSetMultimap<DependencyKind, Label> labelMap, |
adgar | 412e03c | 2020-05-07 08:20:55 -0700 | [diff] [blame] | 63 | TargetAndConfiguration fromNode, |
lberki | 4cd4447 | 2019-02-06 08:36:34 -0800 | [diff] [blame] | 64 | NestedSetBuilder<Cause> rootCauses) { |
| 65 | return labelMap.values().stream() |
janakr | d0a3c5e | 2018-08-09 15:59:24 -0700 | [diff] [blame] | 66 | .distinct() |
| 67 | .collect( |
| 68 | Collectors.toMap( |
| 69 | Function.identity(), |
| 70 | label -> { |
| 71 | try { |
| 72 | return packageManager.getTarget(reporter, label); |
| 73 | } catch (NoSuchPackageException |
| 74 | | NoSuchTargetException |
| 75 | | InterruptedException e) { |
| 76 | throw new IllegalStateException(e); |
| 77 | } |
| 78 | })); |
janakr | 9fcef03 | 2018-01-23 13:38:24 -0800 | [diff] [blame] | 79 | } |
janakr | 9fcef03 | 2018-01-23 13:38:24 -0800 | [diff] [blame] | 80 | }; |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 83 | private void pkg(String name, String... contents) throws Exception { |
jhorvitz | e68a7d0 | 2021-04-26 11:49:28 -0700 | [diff] [blame] | 84 | scratch.file(name + "/BUILD", contents); |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 85 | } |
| 86 | |
John Cater | 2a54659 | 2020-05-19 04:48:52 -0700 | [diff] [blame] | 87 | private OrderedSetMultimap<DependencyKind, DependencyKey> dependentNodeMap( |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 88 | String targetName, NativeAspectClass aspect) throws Exception { |
dannark | 90e2b4b | 2018-06-27 13:35:04 -0700 | [diff] [blame] | 89 | Target target = |
| 90 | packageManager.getTarget(reporter, Label.parseAbsolute(targetName, ImmutableMap.of())); |
lberki | 102256f | 2019-02-08 01:34:23 -0800 | [diff] [blame] | 91 | |
jhorvitz | e68a7d0 | 2021-04-26 11:49:28 -0700 | [diff] [blame] | 92 | return dependencyResolver.dependentNodeMap( |
| 93 | new TargetAndConfiguration(target, getTargetConfiguration()), |
| 94 | getHostConfiguration(), |
| 95 | aspect != null ? Aspect.forNative(aspect) : null, |
| 96 | ImmutableMap.of(), |
| 97 | /*toolchainContexts=*/ null, |
| 98 | /*useToolchainTransition=*/ false, |
| 99 | /*trimmingTransitionFactory=*/ null); |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 100 | } |
| 101 | |
jhorvitz | e68a7d0 | 2021-04-26 11:49:28 -0700 | [diff] [blame] | 102 | private static void assertDep( |
John Cater | 2a54659 | 2020-05-19 04:48:52 -0700 | [diff] [blame] | 103 | OrderedSetMultimap<DependencyKind, DependencyKey> dependentNodeMap, |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 104 | String attrName, |
| 105 | String dep, |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 106 | AspectDescriptor... aspects) { |
lberki | 102256f | 2019-02-08 01:34:23 -0800 | [diff] [blame] | 107 | DependencyKind kind = null; |
| 108 | for (DependencyKind candidate : dependentNodeMap.keySet()) { |
| 109 | if (candidate.getAttribute() != null && candidate.getAttribute().getName().equals(attrName)) { |
| 110 | kind = candidate; |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 111 | break; |
| 112 | } |
| 113 | } |
| 114 | |
lberki | 102256f | 2019-02-08 01:34:23 -0800 | [diff] [blame] | 115 | assertWithMessage("Attribute '" + attrName + "' not found").that(kind).isNotNull(); |
John Cater | 2a54659 | 2020-05-19 04:48:52 -0700 | [diff] [blame] | 116 | DependencyKey dependency = null; |
| 117 | for (DependencyKey depCandidate : dependentNodeMap.get(kind)) { |
lberki | 102256f | 2019-02-08 01:34:23 -0800 | [diff] [blame] | 118 | if (depCandidate.getLabel().toString().equals(dep)) { |
| 119 | dependency = depCandidate; |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 120 | break; |
| 121 | } |
| 122 | } |
| 123 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 124 | assertWithMessage("Dependency '" + dep + "' on attribute '" + attrName + "' not found") |
| 125 | .that(dependency) |
| 126 | .isNotNull(); |
lberki | 3c2c821 | 2020-10-21 04:00:04 -0700 | [diff] [blame] | 127 | assertThat(Iterables.transform(dependency.getAspects().getUsedAspects(), AspectDeps::getAspect)) |
| 128 | .containsExactlyElementsIn(aspects); |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Test |
| 132 | public void hasAspectsRequiredByRule() throws Exception { |
gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 133 | setRulesAvailableInTests(TestAspects.ASPECT_REQUIRING_RULE, TestAspects.BASE_RULE); |
jhorvitz | e68a7d0 | 2021-04-26 11:49:28 -0700 | [diff] [blame] | 134 | pkg("a", "aspect(name='a', foo=[':b'])", "aspect(name='b', foo=[])"); |
John Cater | 2a54659 | 2020-05-19 04:48:52 -0700 | [diff] [blame] | 135 | OrderedSetMultimap<DependencyKind, DependencyKey> map = dependentNodeMap("//a:a", null); |
jhorvitz | e68a7d0 | 2021-04-26 11:49:28 -0700 | [diff] [blame] | 136 | assertDep(map, "foo", "//a:b", new AspectDescriptor(TestAspects.SIMPLE_ASPECT)); |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Test |
| 140 | public void hasAspectsRequiredByAspect() throws Exception { |
gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 141 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.SIMPLE_RULE); |
jhorvitz | e68a7d0 | 2021-04-26 11:49:28 -0700 | [diff] [blame] | 142 | pkg("a", "simple(name='a', foo=[':b'])", "simple(name='b', foo=[])"); |
John Cater | 2a54659 | 2020-05-19 04:48:52 -0700 | [diff] [blame] | 143 | OrderedSetMultimap<DependencyKind, DependencyKey> map = |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 144 | dependentNodeMap("//a:a", TestAspects.ATTRIBUTE_ASPECT); |
jhorvitz | e68a7d0 | 2021-04-26 11:49:28 -0700 | [diff] [blame] | 145 | assertDep(map, "foo", "//a:b", new AspectDescriptor(TestAspects.ATTRIBUTE_ASPECT)); |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Test |
Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 149 | public void hasAllAttributesAspect() throws Exception { |
gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 150 | setRulesAvailableInTests(TestAspects.BASE_RULE, TestAspects.SIMPLE_RULE); |
jhorvitz | e68a7d0 | 2021-04-26 11:49:28 -0700 | [diff] [blame] | 151 | pkg("a", "simple(name='a', foo=[':b'])", "simple(name='b', foo=[])"); |
John Cater | 2a54659 | 2020-05-19 04:48:52 -0700 | [diff] [blame] | 152 | OrderedSetMultimap<DependencyKind, DependencyKey> map = |
Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 153 | dependentNodeMap("//a:a", TestAspects.ALL_ATTRIBUTES_ASPECT); |
jhorvitz | e68a7d0 | 2021-04-26 11:49:28 -0700 | [diff] [blame] | 154 | assertDep(map, "foo", "//a:b", new AspectDescriptor(TestAspects.ALL_ATTRIBUTES_ASPECT)); |
Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Test |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 158 | public void hasAspectDependencies() throws Exception { |
gregce | b87a41f3 | 2017-11-29 07:46:25 -0800 | [diff] [blame] | 159 | setRulesAvailableInTests(TestAspects.BASE_RULE); |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 160 | pkg("a", "base(name='a')"); |
| 161 | pkg("extra", "base(name='extra')"); |
John Cater | 2a54659 | 2020-05-19 04:48:52 -0700 | [diff] [blame] | 162 | OrderedSetMultimap<DependencyKind, DependencyKey> map = |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 163 | dependentNodeMap("//a:a", TestAspects.EXTRA_ATTRIBUTE_ASPECT); |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 164 | assertDep(map, "$dep", "//extra:extra"); |
| 165 | } |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 166 | } |