Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +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 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
Janak Ramakrishnan | 112840b | 2016-12-29 21:49:56 +0000 | [diff] [blame] | 17 | import static com.google.devtools.build.skyframe.WalkableGraphUtils.exists; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 18 | |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 19 | import com.google.common.base.Preconditions; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableMap; |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 22 | import com.google.common.eventbus.EventBus; |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 23 | import com.google.devtools.build.lib.actions.ActionKeyContext; |
| 24 | import com.google.devtools.build.lib.analysis.BlazeDirectories; |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 25 | import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider; |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 26 | import com.google.devtools.build.lib.analysis.ServerDirectories; |
| 27 | import com.google.devtools.build.lib.analysis.util.AnalysisMock; |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 28 | import com.google.devtools.build.lib.analysis.util.DefaultBuildOptionsForTesting; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 29 | import com.google.devtools.build.lib.cmdline.Label; |
| 30 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 31 | import com.google.devtools.build.lib.cmdline.RepositoryName; |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 32 | import com.google.devtools.build.lib.events.Reporter; |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 33 | import com.google.devtools.build.lib.packages.SkylarkSemanticsOptions; |
| 34 | import com.google.devtools.build.lib.pkgcache.PackageCacheOptions; |
| 35 | import com.google.devtools.build.lib.pkgcache.PathPackageLocator; |
| 36 | import com.google.devtools.build.lib.rules.repository.RepositoryDelegatorFunction; |
| 37 | import com.google.devtools.build.lib.testutil.FoundationTestCase; |
| 38 | import com.google.devtools.build.lib.testutil.TestConstants; |
| 39 | import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor; |
Eric Fellheimer | e040fe9 | 2015-11-09 23:54:46 +0000 | [diff] [blame] | 40 | import com.google.devtools.build.lib.vfs.PathFragment; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 41 | import com.google.devtools.build.lib.vfs.Root; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 42 | import com.google.devtools.build.skyframe.EvaluationResult; |
| 43 | import com.google.devtools.build.skyframe.SkyKey; |
| 44 | import com.google.devtools.build.skyframe.SkyValue; |
| 45 | import com.google.devtools.build.skyframe.WalkableGraph; |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 46 | import com.google.devtools.common.options.Options; |
Janak Ramakrishnan | 112840b | 2016-12-29 21:49:56 +0000 | [diff] [blame] | 47 | import java.io.IOException; |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 48 | import java.util.UUID; |
| 49 | import org.junit.Before; |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 50 | import org.junit.Test; |
| 51 | import org.junit.runner.RunWith; |
| 52 | import org.junit.runners.JUnit4; |
| 53 | |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 54 | /** Tests for {@link PrepareDepsOfPatternsFunction}. */ |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 55 | @RunWith(JUnit4.class) |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 56 | public class PrepareDepsOfPatternsFunctionSmartNegationTest extends FoundationTestCase { |
| 57 | private SkyframeExecutor skyframeExecutor; |
| 58 | private static final String ADDITIONAL_BLACKLISTED_PACKAGE_PREFIXES_FILE_PATH_STRING = |
| 59 | "config/blacklist.txt"; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 60 | |
| 61 | private static SkyKey getKeyForLabel(Label label) { |
| 62 | // Note that these tests used to look for TargetMarker SkyKeys before TargetMarker was |
| 63 | // inlined in TransitiveTraversalFunction. Because TargetMarker is now inlined, it doesn't |
| 64 | // appear in the graph. Instead, these tests now look for TransitiveTraversal keys. |
| 65 | return TransitiveTraversalValue.key(label); |
| 66 | } |
| 67 | |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 68 | @Before |
| 69 | public void setUp() throws Exception { |
| 70 | BlazeDirectories directories = |
| 71 | new BlazeDirectories( |
Klaus Aehlig | c2499c4 | 2018-02-27 05:47:21 -0800 | [diff] [blame] | 72 | new ServerDirectories( |
| 73 | getScratch().dir("/install"), |
| 74 | getScratch().dir("/output"), |
| 75 | getScratch().dir("/user_root")), |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 76 | rootDirectory, |
cushon | 849df36 | 2018-05-14 01:51:45 -0700 | [diff] [blame] | 77 | /* defaultSystemJavabase= */ null, |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 78 | AnalysisMock.get().getProductName()); |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 79 | ConfiguredRuleClassProvider ruleClassProvider = AnalysisMock.get().createRuleClassProvider(); |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 80 | skyframeExecutor = |
| 81 | SequencedSkyframeExecutor.create( |
| 82 | AnalysisMock.get() |
| 83 | .getPackageFactoryBuilderForTesting(directories) |
nharmata | e57e9a3 | 2018-04-02 15:10:24 -0700 | [diff] [blame] | 84 | .build(ruleClassProvider), |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 85 | fileSystem, |
| 86 | directories, |
| 87 | new ActionKeyContext(), |
| 88 | /*workspaceStatusActionFactory=*/ null, |
| 89 | AnalysisMock.get().createRuleClassProvider().getBuildInfoFactories(), |
| 90 | ImmutableList.of(), |
| 91 | AnalysisMock.get().getSkyFunctions(directories), |
| 92 | ImmutableList.of(), |
| 93 | BazelSkyframeExecutorConstants.HARDCODED_BLACKLISTED_PACKAGE_PREFIXES, |
| 94 | PathFragment.create(ADDITIONAL_BLACKLISTED_PACKAGE_PREFIXES_FILE_PATH_STRING), |
| 95 | BazelSkyframeExecutorConstants.CROSS_REPOSITORY_LABEL_VIOLATION_STRATEGY, |
| 96 | BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY, |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 97 | BazelSkyframeExecutorConstants.ACTION_ON_IO_EXCEPTION_READING_BUILD_FILE, |
| 98 | DefaultBuildOptionsForTesting.getDefaultBuildOptionsForTest(ruleClassProvider)); |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 99 | TestConstants.processSkyframeExecutorForTesting(skyframeExecutor); |
| 100 | skyframeExecutor.preparePackageLoading( |
| 101 | new PathPackageLocator( |
| 102 | outputBase, |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 103 | ImmutableList.of(Root.fromPath(rootDirectory)), |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 104 | BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY), |
| 105 | Options.getDefaults(PackageCacheOptions.class), |
| 106 | Options.getDefaults(SkylarkSemanticsOptions.class), |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 107 | UUID.randomUUID(), |
| 108 | ImmutableMap.<String, String>of(), |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 109 | new TimestampGranularityMonitor(null)); |
juliexxia | 651797f | 2018-08-01 11:45:13 -0700 | [diff] [blame] | 110 | skyframeExecutor.setActionEnv(ImmutableMap.<String, String>of()); |
Klaus Aehlig | 93fe20c | 2018-06-14 05:48:46 -0700 | [diff] [blame] | 111 | skyframeExecutor.injectExtraPrecomputedValues( |
| 112 | ImmutableList.of( |
| 113 | PrecomputedValue.injected( |
| 114 | RepositoryDelegatorFunction.REPOSITORY_OVERRIDES, |
| 115 | ImmutableMap.<RepositoryName, PathFragment>of()), |
| 116 | PrecomputedValue.injected( |
| 117 | RepositoryDelegatorFunction.DEPENDENCY_FOR_UNCONDITIONAL_FETCHING, |
| 118 | RepositoryDelegatorFunction.DONT_FETCH_UNCONDITIONALLY))); |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 119 | scratch.file(ADDITIONAL_BLACKLISTED_PACKAGE_PREFIXES_FILE_PATH_STRING); |
| 120 | } |
| 121 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 122 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 123 | public void testRecursiveEvaluationFailsOnBadBuildFile() throws Exception { |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 124 | // Given a well-formed package "@//foo" and a malformed package "@//foo/foo", |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 125 | createFooAndFooFoo(); |
| 126 | |
| 127 | // Given a target pattern sequence consisting of a recursive pattern for "//foo/...", |
| 128 | ImmutableList<String> patternSequence = ImmutableList.of("//foo/..."); |
| 129 | |
| 130 | // When PrepareDepsOfPatternsFunction completes evaluation (with no error because it was |
| 131 | // recovered from), |
| 132 | WalkableGraph walkableGraph = |
| 133 | getGraphFromPatternsEvaluation( |
| 134 | patternSequence, /*successExpected=*/ true, /*keepGoing=*/ true); |
| 135 | |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 136 | // Then the graph contains package values for "@//foo" and "@//foo/foo", |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 137 | assertThat(exists(PackageValue.key(PackageIdentifier.parse("@//foo")), walkableGraph)).isTrue(); |
| 138 | assertThat(exists(PackageValue.key(PackageIdentifier.parse("@//foo/foo")), walkableGraph)) |
| 139 | .isTrue(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 140 | |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 141 | // But the graph does not contain a value for the target "@//foo/foo:foofoo". |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 142 | assertThat(exists(getKeyForLabel(Label.create("@//foo/foo", "foofoo")), walkableGraph)) |
| 143 | .isFalse(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 146 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 147 | public void testNegativePatternBlocksPatternEvaluation() throws Exception { |
| 148 | // Given a well-formed package "//foo" and a malformed package "//foo/foo", |
| 149 | createFooAndFooFoo(); |
| 150 | |
| 151 | // Given a target pattern sequence consisting of a recursive pattern for "//foo/..." followed |
| 152 | // by a negative pattern for the malformed package, |
| 153 | ImmutableList<String> patternSequence = ImmutableList.of("//foo/...", "-//foo/foo/..."); |
| 154 | |
Eric Fellheimer | e040fe9 | 2015-11-09 23:54:46 +0000 | [diff] [blame] | 155 | assertSkipsFoo(patternSequence); |
| 156 | } |
| 157 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 158 | @Test |
Eric Fellheimer | e040fe9 | 2015-11-09 23:54:46 +0000 | [diff] [blame] | 159 | public void testBlacklistPatternBlocksPatternEvaluation() throws Exception { |
| 160 | // Given a well-formed package "//foo" and a malformed package "//foo/foo", |
| 161 | createFooAndFooFoo(); |
| 162 | |
| 163 | // Given a target pattern sequence consisting of a recursive pattern for "//foo/...", |
| 164 | ImmutableList<String> patternSequence = ImmutableList.of("//foo/..."); |
| 165 | |
| 166 | // and a blacklist for the malformed package, |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 167 | scratch.overwriteFile(ADDITIONAL_BLACKLISTED_PACKAGE_PREFIXES_FILE_PATH_STRING, "foo/foo"); |
Eric Fellheimer | e040fe9 | 2015-11-09 23:54:46 +0000 | [diff] [blame] | 168 | |
| 169 | assertSkipsFoo(patternSequence); |
| 170 | } |
| 171 | |
| 172 | private void assertSkipsFoo(ImmutableList<String> patternSequence) throws Exception { |
| 173 | |
| 174 | |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 175 | // When PrepareDepsOfPatternsFunction completes evaluation (successfully), |
| 176 | WalkableGraph walkableGraph = |
| 177 | getGraphFromPatternsEvaluation( |
| 178 | patternSequence, /*successExpected=*/ true, /*keepGoing=*/ true); |
| 179 | |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 180 | // Then the graph contains a package value for "@//foo", |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 181 | assertThat(exists(PackageValue.key(PackageIdentifier.parse("@//foo")), walkableGraph)).isTrue(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 182 | |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 183 | // But no package value for "@//foo/foo", |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 184 | assertThat(exists(PackageValue.key(PackageIdentifier.parse("@//foo/foo")), walkableGraph)) |
| 185 | .isFalse(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 186 | |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 187 | // And the graph does not contain a value for the target "@//foo/foo:foofoo". |
| 188 | Label label = Label.create("@//foo/foo", "foofoo"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 189 | assertThat(exists(getKeyForLabel(label), walkableGraph)).isFalse(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 192 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 193 | public void testNegativeNonTBDPatternsAreSkippedWithWarnings() throws Exception { |
| 194 | // Given a target pattern sequence with a negative non-TBD pattern, |
| 195 | ImmutableList<String> patternSequence = ImmutableList.of("-//foo/bar"); |
| 196 | |
| 197 | // When PrepareDepsOfPatternsFunction completes evaluation, |
| 198 | getGraphFromPatternsEvaluation(patternSequence, /*successExpected=*/ true, /*keepGoing=*/ true); |
| 199 | |
| 200 | // Then a event is published that says that negative non-TBD patterns are skipped. |
| 201 | assertContainsEvent( |
| 202 | "Skipping '-//foo/bar': Negative target patterns of types other than \"targets below " |
| 203 | + "directory\" are not permitted."); |
| 204 | } |
| 205 | |
| 206 | // Helpers: |
| 207 | |
| 208 | private WalkableGraph getGraphFromPatternsEvaluation( |
| 209 | ImmutableList<String> patternSequence, boolean successExpected, boolean keepGoing) |
| 210 | throws InterruptedException { |
| 211 | SkyKey independentTarget = PrepareDepsOfPatternsValue.key(patternSequence, ""); |
| 212 | ImmutableList<SkyKey> singletonTargetPattern = ImmutableList.of(independentTarget); |
| 213 | |
| 214 | // When PrepareDepsOfPatternsFunction completes evaluation, |
| 215 | EvaluationResult<SkyValue> evaluationResult = |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 216 | skyframeExecutor |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 217 | .getDriverForTesting() |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 218 | .evaluate( |
| 219 | singletonTargetPattern, |
| 220 | keepGoing, |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 221 | /*numThreads=*/ 100, |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 222 | new Reporter(new EventBus(), eventCollector)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 223 | // The evaluation has no errors if success was expected. |
| 224 | assertThat(evaluationResult.hasError()).isNotEqualTo(successExpected); |
| 225 | return Preconditions.checkNotNull(evaluationResult.getWalkableGraph()); |
| 226 | } |
| 227 | |
| 228 | private void createFooAndFooFoo() throws IOException { |
| 229 | scratch.file( |
| 230 | "foo/BUILD", "genrule(name = 'foo',", " outs = ['out.txt'],", " cmd = 'touch $@')"); |
| 231 | scratch.file( |
| 232 | "foo/foo/BUILD", "genrule(name = 'foofoo',", " This isn't even remotely grammatical.)"); |
| 233 | } |
| 234 | } |