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; |
Florian Weikert | fd735f3 | 2015-11-27 17:32:23 +0000 | [diff] [blame] | 17 | import static org.junit.Assert.assertNotNull; |
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; |
Greg Estren | 373e3e2 | 2016-08-09 22:36:51 +0000 | [diff] [blame] | 20 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
| 21 | import com.google.devtools.build.lib.analysis.config.BuildOptions; |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider; |
Florian Weikert | cca703a | 2015-12-07 09:56:38 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.lib.analysis.util.AnalysisTestCase; |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.analysis.util.TestAspects; |
| 25 | import com.google.devtools.build.lib.analysis.util.TestAspects.AspectRequiringRule; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.cmdline.Label; |
Ulf Adams | 2ac2096 | 2016-02-01 13:04:54 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
Dmitry Lomov | b487ac6 | 2015-11-09 13:09:12 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.packages.Aspect; |
Dmitry Lomov | 1575652 | 2016-12-16 16:52:37 +0000 | [diff] [blame] | 29 | import com.google.devtools.build.lib.packages.AspectDescriptor; |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.packages.Attribute; |
Dmitry Lomov | 8ff0645 | 2015-10-21 19:16:30 +0000 | [diff] [blame] | 31 | import com.google.devtools.build.lib.packages.NativeAspectClass; |
Ulf Adams | 2ac2096 | 2016-02-01 13:04:54 +0000 | [diff] [blame] | 32 | import com.google.devtools.build.lib.packages.NoSuchPackageException; |
| 33 | import com.google.devtools.build.lib.packages.NoSuchTargetException; |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 34 | import com.google.devtools.build.lib.packages.NoSuchThingException; |
| 35 | import com.google.devtools.build.lib.packages.Target; |
Greg Estren | 9e26f0f | 2016-09-29 01:01:57 +0000 | [diff] [blame] | 36 | import com.google.devtools.build.lib.testutil.Suite; |
| 37 | import com.google.devtools.build.lib.testutil.TestSpec; |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 38 | import com.google.devtools.build.lib.util.OrderedSetMultimap; |
Greg Estren | 373e3e2 | 2016-08-09 22:36:51 +0000 | [diff] [blame] | 39 | import java.util.List; |
| 40 | import java.util.Set; |
| 41 | import javax.annotation.Nullable; |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 42 | import org.junit.Before; |
| 43 | import org.junit.Test; |
| 44 | import org.junit.runner.RunWith; |
| 45 | import org.junit.runners.JUnit4; |
| 46 | |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 47 | /** |
| 48 | * Tests for {@link DependencyResolver}. |
| 49 | * |
| 50 | * <p>These use custom rules so that all usual and unusual cases related to aspect processing can |
| 51 | * be tested. |
| 52 | * |
| 53 | * <p>It would be nicer is we didn't have a Skyframe executor, if we didn't have that, we'd need a |
| 54 | * way to create a configuration, a package manager and a whole lot of other things, so it's just |
| 55 | * easier this way. |
| 56 | */ |
| 57 | @RunWith(JUnit4.class) |
Florian Weikert | cca703a | 2015-12-07 09:56:38 +0000 | [diff] [blame] | 58 | public class DependencyResolverTest extends AnalysisTestCase { |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 59 | private DependencyResolver dependencyResolver; |
| 60 | |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 61 | @Before |
Florian Weikert | fd735f3 | 2015-11-27 17:32:23 +0000 | [diff] [blame] | 62 | public final void createResolver() throws Exception { |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 63 | dependencyResolver = new DependencyResolver() { |
Dmitry Lomov | d83af9e | 2017-02-23 15:44:23 +0000 | [diff] [blame] | 64 | |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 65 | @Override |
| 66 | protected void invalidVisibilityReferenceHook(TargetAndConfiguration node, Label label) { |
| 67 | throw new IllegalStateException(); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | protected void invalidPackageGroupReferenceHook(TargetAndConfiguration node, Label label) { |
| 72 | throw new IllegalStateException(); |
| 73 | } |
| 74 | |
Ulf Adams | 8490173 | 2016-01-28 15:05:16 +0000 | [diff] [blame] | 75 | @Override |
| 76 | protected void missingEdgeHook(Target from, Label to, NoSuchThingException e) { |
| 77 | throw new IllegalStateException(e); |
| 78 | } |
| 79 | |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 80 | @Nullable |
| 81 | @Override |
Ulf Adams | 2ac2096 | 2016-02-01 13:04:54 +0000 | [diff] [blame] | 82 | protected Target getTarget(Target from, Label label, NestedSetBuilder<Label> rootCauses) { |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 83 | try { |
| 84 | return packageManager.getTarget(reporter, label); |
Ulf Adams | 2ac2096 | 2016-02-01 13:04:54 +0000 | [diff] [blame] | 85 | } catch (NoSuchPackageException | NoSuchTargetException | InterruptedException e) { |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 86 | throw new IllegalStateException(e); |
| 87 | } |
| 88 | } |
Greg Estren | 373e3e2 | 2016-08-09 22:36:51 +0000 | [diff] [blame] | 89 | |
| 90 | @Nullable |
| 91 | @Override |
| 92 | protected List<BuildConfiguration> getConfigurations( |
| 93 | Set<Class<? extends BuildConfiguration.Fragment>> fragments, |
| 94 | Iterable<BuildOptions> buildOptions) { |
| 95 | throw new UnsupportedOperationException( |
| 96 | "this functionality is covered by analysis-phase integration tests"); |
| 97 | } |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 98 | }; |
| 99 | } |
| 100 | |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 101 | private void pkg(String name, String... contents) throws Exception { |
Ulf Adams | 01f1b46 | 2015-04-27 09:52:33 +0000 | [diff] [blame] | 102 | scratch.file("" + name + "/BUILD", contents); |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 105 | private OrderedSetMultimap<Attribute, Dependency> dependentNodeMap( |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 106 | String targetName, NativeAspectClass aspect) throws Exception { |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 107 | Target target = packageManager.getTarget(reporter, Label.parseAbsolute(targetName)); |
| 108 | return dependencyResolver.dependentNodeMap( |
| 109 | new TargetAndConfiguration(target, getTargetConfiguration()), |
Greg Estren | 0004943 | 2015-08-25 16:43:47 +0000 | [diff] [blame] | 110 | getHostConfiguration(), |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 111 | aspect != null ? Aspect.forNative(aspect) : null, |
Lukacs Berki | 7894c18 | 2016-05-10 12:07:01 +0000 | [diff] [blame] | 112 | ImmutableMap.<Label, ConfigMatchingProvider>of()); |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @SafeVarargs |
Greg Estren | 9e26f0f | 2016-09-29 01:01:57 +0000 | [diff] [blame] | 116 | private final Dependency assertDep( |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 117 | OrderedSetMultimap<Attribute, Dependency> dependentNodeMap, |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 118 | String attrName, |
| 119 | String dep, |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 120 | AspectDescriptor... aspects) { |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 121 | Attribute attr = null; |
| 122 | for (Attribute candidate : dependentNodeMap.keySet()) { |
| 123 | if (candidate.getName().equals(attrName)) { |
| 124 | attr = candidate; |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | assertNotNull("Attribute '" + attrName + "' not found", attr); |
| 130 | Dependency dependency = null; |
| 131 | for (Dependency candidate : dependentNodeMap.get(attr)) { |
| 132 | if (candidate.getLabel().toString().equals(dep)) { |
| 133 | dependency = candidate; |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | assertNotNull("Dependency '" + dep + "' on attribute '" + attrName + "' not found", dependency); |
Dmitry Lomov | e851fe2 | 2017-02-14 23:11:23 +0000 | [diff] [blame] | 139 | assertThat(dependency.getAspects().getAllAspects()).containsExactly((Object[]) aspects); |
Greg Estren | 9e26f0f | 2016-09-29 01:01:57 +0000 | [diff] [blame] | 140 | return dependency; |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Test |
| 144 | public void hasAspectsRequiredByRule() throws Exception { |
Dmitry Lomov | d126074 | 2016-05-03 18:37:11 +0000 | [diff] [blame] | 145 | setRulesAvailableInTests(new AspectRequiringRule(), new TestAspects.BaseRule()); |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 146 | pkg("a", |
| 147 | "aspect(name='a', foo=[':b'])", |
| 148 | "aspect(name='b', foo=[])"); |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 149 | OrderedSetMultimap<Attribute, Dependency> map = dependentNodeMap("//a:a", null); |
Dmitry Lomov | 8ff0645 | 2015-10-21 19:16:30 +0000 | [diff] [blame] | 150 | assertDep( |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 151 | map, "foo", "//a:b", |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 152 | new AspectDescriptor(TestAspects.SIMPLE_ASPECT)); |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Test |
| 156 | public void hasAspectsRequiredByAspect() throws Exception { |
Dmitry Lomov | d126074 | 2016-05-03 18:37:11 +0000 | [diff] [blame] | 157 | setRulesAvailableInTests(new TestAspects.BaseRule(), new TestAspects.SimpleRule()); |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 158 | pkg("a", |
| 159 | "simple(name='a', foo=[':b'])", |
| 160 | "simple(name='b', foo=[])"); |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 161 | OrderedSetMultimap<Attribute, Dependency> map = |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 162 | dependentNodeMap("//a:a", TestAspects.ATTRIBUTE_ASPECT); |
Dmitry Lomov | 8ff0645 | 2015-10-21 19:16:30 +0000 | [diff] [blame] | 163 | assertDep( |
Dmitry Lomov | e804017 | 2016-04-06 14:53:43 +0000 | [diff] [blame] | 164 | map, "foo", "//a:b", |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 165 | new AspectDescriptor(TestAspects.ATTRIBUTE_ASPECT)); |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Test |
Dmitry Lomov | bb5901b | 2016-09-27 08:49:26 +0000 | [diff] [blame] | 169 | public void hasAllAttributesAspect() throws Exception { |
| 170 | setRulesAvailableInTests(new TestAspects.BaseRule(), new TestAspects.SimpleRule()); |
| 171 | pkg("a", |
| 172 | "simple(name='a', foo=[':b'])", |
| 173 | "simple(name='b', foo=[])"); |
| 174 | OrderedSetMultimap<Attribute, Dependency> map = |
| 175 | dependentNodeMap("//a:a", TestAspects.ALL_ATTRIBUTES_ASPECT); |
| 176 | assertDep( |
| 177 | map, "foo", "//a:b", |
| 178 | new AspectDescriptor(TestAspects.ALL_ATTRIBUTES_ASPECT)); |
| 179 | } |
| 180 | |
| 181 | @Test |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 182 | public void hasAspectDependencies() throws Exception { |
Dmitry Lomov | d126074 | 2016-05-03 18:37:11 +0000 | [diff] [blame] | 183 | setRulesAvailableInTests(new TestAspects.BaseRule()); |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 184 | pkg("a", "base(name='a')"); |
| 185 | pkg("extra", "base(name='extra')"); |
Greg Estren | d535325 | 2016-08-11 22:13:31 +0000 | [diff] [blame] | 186 | OrderedSetMultimap<Attribute, Dependency> map = |
Luis Fernando Pino Duque | e82713d | 2016-04-26 16:22:38 +0000 | [diff] [blame] | 187 | dependentNodeMap("//a:a", TestAspects.EXTRA_ATTRIBUTE_ASPECT); |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 188 | assertDep(map, "$dep", "//extra:extra"); |
| 189 | } |
Greg Estren | 9e26f0f | 2016-09-29 01:01:57 +0000 | [diff] [blame] | 190 | |
| 191 | /** |
| 192 | * Null configurations should be static whether we're building with static or dynamic |
| 193 | * configurations. This is because the dynamic config logic that translates transitions into |
| 194 | * final configurations can be trivially skipped in those cases. |
| 195 | */ |
| 196 | @Test |
| 197 | public void nullConfigurationsAlwaysStatic() throws Exception { |
| 198 | pkg("a", |
| 199 | "genrule(name = 'gen', srcs = ['gen.in'], cmd = '', outs = ['gen.out'])"); |
| 200 | update(); |
| 201 | Dependency dep = assertDep(dependentNodeMap("//a:gen", null), "srcs", "//a:gen.in"); |
| 202 | assertThat(dep.hasStaticConfiguration()).isTrue(); |
| 203 | assertThat(dep.getConfiguration()).isNull(); |
| 204 | } |
| 205 | |
| 206 | /** Runs the same test with trimmed dynamic configurations. */ |
| 207 | @TestSpec(size = Suite.SMALL_TESTS) |
| 208 | @RunWith(JUnit4.class) |
| 209 | public static class WithDynamicConfigurations extends DependencyResolverTest { |
| 210 | @Override |
| 211 | protected FlagBuilder defaultFlags() { |
| 212 | return super.defaultFlags().with(Flag.DYNAMIC_CONFIGURATIONS); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /** Runs the same test with untrimmed dynamic configurations. */ |
| 217 | @TestSpec(size = Suite.SMALL_TESTS) |
| 218 | @RunWith(JUnit4.class) |
| 219 | public static class WithDynamicConfigurationsNoTrim extends DependencyResolverTest { |
| 220 | @Override |
| 221 | protected FlagBuilder defaultFlags() { |
| 222 | return super.defaultFlags().with(Flag.DYNAMIC_CONFIGURATIONS_NOTRIM); |
| 223 | } |
| 224 | } |
Ulf Adams | 3b2ac8e | 2015-04-23 19:09:50 +0000 | [diff] [blame] | 225 | } |