Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
| 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.skyframe; |
| 15 | |
ulfjack | aa2ff99 | 2018-06-07 07:05:37 -0700 | [diff] [blame] | 16 | import com.google.common.collect.ImmutableSet; |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 17 | import com.google.devtools.build.lib.cmdline.Label; |
| 18 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
| 19 | import com.google.devtools.build.lib.cmdline.ResolvedTargets; |
| 20 | import com.google.devtools.build.lib.events.Event; |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.packages.BuildFileNotFoundException; |
| 22 | import com.google.devtools.build.lib.packages.BuildType; |
| 23 | import com.google.devtools.build.lib.packages.NoSuchTargetException; |
| 24 | import com.google.devtools.build.lib.packages.NonconfigurableAttributeMapper; |
| 25 | import com.google.devtools.build.lib.packages.Package; |
| 26 | import com.google.devtools.build.lib.packages.Rule; |
| 27 | import com.google.devtools.build.lib.packages.Target; |
| 28 | import com.google.devtools.build.lib.packages.TargetUtils; |
| 29 | import com.google.devtools.build.lib.packages.TestTargetUtils; |
ulfjack | 72dfe94 | 2017-07-21 10:11:02 +0200 | [diff] [blame] | 30 | import com.google.devtools.build.lib.skyframe.TestsInSuiteValue.TestsInSuiteKey; |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 31 | import com.google.devtools.build.skyframe.SkyFunction; |
| 32 | import com.google.devtools.build.skyframe.SkyKey; |
| 33 | import com.google.devtools.build.skyframe.SkyValue; |
| 34 | import com.google.devtools.build.skyframe.ValueOrException; |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 35 | import java.util.ArrayList; |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 36 | import java.util.HashMap; |
ulfjack | aa2ff99 | 2018-06-07 07:05:37 -0700 | [diff] [blame] | 37 | import java.util.HashSet; |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 38 | import java.util.LinkedHashSet; |
| 39 | import java.util.List; |
| 40 | import java.util.Map; |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 41 | import java.util.Set; |
cpeyser | 1045300 | 2018-05-16 08:31:01 -0700 | [diff] [blame] | 42 | import java.util.stream.Collectors; |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 43 | import javax.annotation.Nullable; |
| 44 | |
| 45 | /** |
| 46 | * TestsInSuiteFunction takes a single test_suite target and expands all of the tests it contains, |
| 47 | * possibly recursively. |
| 48 | */ |
| 49 | // TODO(ulfjack): What about test_suite rules that include each other. |
| 50 | final class TestsInSuiteFunction implements SkyFunction { |
| 51 | @Override |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 52 | public SkyValue compute(SkyKey key, Environment env) throws InterruptedException { |
ulfjack | 72dfe94 | 2017-07-21 10:11:02 +0200 | [diff] [blame] | 53 | TestsInSuiteKey expansion = (TestsInSuiteKey) key.argument(); |
cpeyser | 807bdbc | 2018-05-03 12:41:33 -0700 | [diff] [blame] | 54 | SkyKey packageKey = PackageValue.key(expansion.getTestSuiteLabel().getPackageIdentifier()); |
| 55 | PackageValue pkg = (PackageValue) env.getValue(packageKey); |
| 56 | if (env.valuesMissing()) { |
| 57 | return null; |
| 58 | } |
| 59 | Rule testSuite = pkg.getPackage().getRule(expansion.getTestSuiteLabel().getName()); |
cpeyser | 1045300 | 2018-05-16 08:31:01 -0700 | [diff] [blame] | 60 | ResolvedTargets<Label> result = computeTestsInSuite(env, testSuite, expansion.isStrict()); |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 61 | if (env.valuesMissing()) { |
| 62 | return null; |
| 63 | } |
| 64 | return new TestsInSuiteValue(result); |
| 65 | } |
| 66 | |
cpeyser | 1045300 | 2018-05-16 08:31:01 -0700 | [diff] [blame] | 67 | private static Set<Label> toLabels(Set<Target> targets) { |
| 68 | return targets.stream().map(Target::getLabel).collect(Collectors.toSet()); |
| 69 | } |
| 70 | |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 71 | /** |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 72 | * Populates 'result' with all the tests associated with the specified 'testSuite'. Throws an |
| 73 | * exception if any target is missing. |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 74 | * |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 75 | * <p>CAUTION! Keep this logic consistent with {@code TestSuite}! |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 76 | */ |
cpeyser | 1045300 | 2018-05-16 08:31:01 -0700 | [diff] [blame] | 77 | private static ResolvedTargets<Label> computeTestsInSuite( |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 78 | Environment env, Rule testSuite, boolean strict) throws InterruptedException { |
ulfjack | aa2ff99 | 2018-06-07 07:05:37 -0700 | [diff] [blame] | 79 | Set<Target> result = new HashSet<>(); |
| 80 | boolean hasError = false; |
| 81 | |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 82 | List<Target> testsAndSuites = new ArrayList<>(); |
| 83 | // Note that testsAndSuites can contain input file targets; the test_suite rule does not |
| 84 | // restrict the set of targets that can appear in tests or suites. |
ulfjack | aa2ff99 | 2018-06-07 07:05:37 -0700 | [diff] [blame] | 85 | hasError |= getPrerequisites(env, testSuite, "tests", testsAndSuites); |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 86 | |
| 87 | // 1. Add all tests |
| 88 | for (Target test : testsAndSuites) { |
| 89 | if (TargetUtils.isTestRule(test)) { |
ulfjack | aa2ff99 | 2018-06-07 07:05:37 -0700 | [diff] [blame] | 90 | result.add(test); |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 91 | } else if (strict && !TargetUtils.isTestSuiteRule(test)) { |
| 92 | // If strict mode is enabled, then give an error for any non-test, non-test-suite targets. |
| 93 | // TODO(ulfjack): We need to throw to end the process if we happen to be in --nokeep_going, |
| 94 | // but we can't know whether or not we are at this point. |
| 95 | env.getListener().handle(Event.error(testSuite.getLocation(), |
| 96 | "in test_suite rule '" + testSuite.getLabel() |
| 97 | + "': expecting a test or a test_suite rule but '" + test.getLabel() |
| 98 | + "' is not one.")); |
ulfjack | aa2ff99 | 2018-06-07 07:05:37 -0700 | [diff] [blame] | 99 | hasError = true; |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | // 2. Add implicit dependencies on tests in same package, if any. |
| 104 | List<Target> implicitTests = new ArrayList<>(); |
ulfjack | aa2ff99 | 2018-06-07 07:05:37 -0700 | [diff] [blame] | 105 | hasError |= getPrerequisites(env, testSuite, "$implicit_tests", implicitTests); |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 106 | for (Target target : implicitTests) { |
| 107 | // The Package construction of $implicit_tests ensures that this check never fails, but we |
| 108 | // add it here anyway for compatibility with future code. |
| 109 | if (TargetUtils.isTestRule(target)) { |
ulfjack | aa2ff99 | 2018-06-07 07:05:37 -0700 | [diff] [blame] | 110 | result.add(target); |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | // 3. Filter based on tags, size, env. |
ulfjack | aa2ff99 | 2018-06-07 07:05:37 -0700 | [diff] [blame] | 115 | TestTargetUtils.filterTests(testSuite, result); |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 116 | |
cpeyser | 1045300 | 2018-05-16 08:31:01 -0700 | [diff] [blame] | 117 | // 4. Expand all suites recursively, collecting labels. |
cpeyser | 1045300 | 2018-05-16 08:31:01 -0700 | [diff] [blame] | 118 | ResolvedTargets.Builder<Label> labelsBuilder = ResolvedTargets.builder(); |
ulfjack | aa2ff99 | 2018-06-07 07:05:37 -0700 | [diff] [blame] | 119 | // Don't set filtered targets; they would be removed from the containing test suite. |
| 120 | labelsBuilder.merge(new ResolvedTargets<>(toLabels(result), ImmutableSet.of(), hasError)); |
cpeyser | 1045300 | 2018-05-16 08:31:01 -0700 | [diff] [blame] | 121 | |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 122 | for (Target suite : testsAndSuites) { |
| 123 | if (TargetUtils.isTestSuiteRule(suite)) { |
| 124 | TestsInSuiteValue value = |
| 125 | (TestsInSuiteValue) env.getValue(TestsInSuiteValue.key(suite, strict)); |
Ulf Adams | ddb7c27 | 2016-02-09 18:01:37 +0000 | [diff] [blame] | 126 | if (value == null) { |
| 127 | continue; |
| 128 | } |
cpeyser | 1045300 | 2018-05-16 08:31:01 -0700 | [diff] [blame] | 129 | labelsBuilder.merge(value.getLabels()); |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
cpeyser | 1045300 | 2018-05-16 08:31:01 -0700 | [diff] [blame] | 133 | return labelsBuilder.build(); |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Adds the set of targets found in the attribute named {@code attrName}, which must be of label |
| 138 | * list type, of the {@code test_suite} rule named {@code testSuite}. Returns true if the method |
| 139 | * found a problem during the lookup process; the actual error message is reported to the |
| 140 | * environment. |
| 141 | */ |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 142 | private static boolean getPrerequisites( |
| 143 | Environment env, Rule testSuite, String attrName, List<Target> targets) |
| 144 | throws InterruptedException { |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 145 | List<Label> labels = |
| 146 | NonconfigurableAttributeMapper.of(testSuite).get(attrName, BuildType.LABEL_LIST); |
| 147 | Set<PackageIdentifier> pkgIdentifiers = new LinkedHashSet<>(); |
| 148 | for (Label label : labels) { |
| 149 | pkgIdentifiers.add(label.getPackageIdentifier()); |
| 150 | } |
| 151 | Map<SkyKey, ValueOrException<BuildFileNotFoundException>> packages = env.getValuesOrThrow( |
| 152 | PackageValue.keys(pkgIdentifiers), BuildFileNotFoundException.class); |
| 153 | if (env.valuesMissing()) { |
| 154 | return false; |
| 155 | } |
| 156 | boolean hasError = false; |
| 157 | Map<PackageIdentifier, Package> packageMap = new HashMap<>(); |
jcater | cecb3a8 | 2018-05-01 14:37:48 -0700 | [diff] [blame] | 158 | for (Map.Entry<SkyKey, ValueOrException<BuildFileNotFoundException>> entry : |
| 159 | packages.entrySet()) { |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 160 | try { |
| 161 | packageMap.put( |
| 162 | (PackageIdentifier) entry.getKey().argument(), |
| 163 | ((PackageValue) entry.getValue().get()).getPackage()); |
| 164 | } catch (BuildFileNotFoundException e) { |
| 165 | env.getListener().handle(Event.error(e.getMessage())); |
| 166 | hasError = true; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | for (Label label : labels) { |
| 171 | Package pkg = packageMap.get(label.getPackageIdentifier()); |
| 172 | if (pkg == null) { |
| 173 | continue; |
| 174 | } |
| 175 | try { |
| 176 | targets.add(pkg.getTarget(label.getName())); |
| 177 | } catch (NoSuchTargetException e) { |
| 178 | env.getListener().handle(Event.error(e.getMessage())); |
| 179 | hasError = true; |
| 180 | } |
| 181 | } |
| 182 | return hasError; |
| 183 | } |
| 184 | |
Ulf Adams | d6fce44 | 2015-10-14 10:08:25 +0000 | [diff] [blame] | 185 | @Nullable |
| 186 | @Override |
| 187 | public String extractTag(SkyKey skyKey) { |
| 188 | return null; |
| 189 | } |
| 190 | } |