blob: 7982b2d5d609093e592ea878499a0d2ee50a1b10 [file] [log] [blame]
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +00001// 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.
14package com.google.devtools.build.lib.skyframe;
15
lberkiaea56b32017-05-30 12:35:33 +020016import static com.google.common.truth.Truth.assertThat;
Florian Weikert92b22362015-12-03 10:17:18 +000017
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000018import com.google.common.collect.ImmutableList;
John Cater45932962017-09-06 21:57:46 +020019import com.google.common.collect.ImmutableMap;
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000020import com.google.common.collect.ImmutableSet;
21import com.google.common.testing.EqualsTester;
Kristina Chodorow5a2936f2016-04-22 17:02:19 +000022import com.google.devtools.build.lib.analysis.BlazeDirectories;
janakr3b63a4e2017-09-14 09:55:40 +020023import com.google.devtools.build.lib.analysis.ServerDirectories;
John Cater45932962017-09-06 21:57:46 +020024import com.google.devtools.build.lib.analysis.util.AnalysisMock;
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000025import com.google.devtools.build.lib.cmdline.PackageIdentifier;
John Cater45932962017-09-06 21:57:46 +020026import com.google.devtools.build.lib.cmdline.RepositoryName;
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000027import com.google.devtools.build.lib.events.NullEventHandler;
John Cater45932962017-09-06 21:57:46 +020028import com.google.devtools.build.lib.packages.PackageFactory;
29import com.google.devtools.build.lib.packages.PackageFactory.EnvironmentExtension;
30import com.google.devtools.build.lib.packages.RuleClassProvider;
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000031import com.google.devtools.build.lib.pkgcache.PathPackageLocator;
John Cater45932962017-09-06 21:57:46 +020032import com.google.devtools.build.lib.rules.repository.LocalRepositoryFunction;
33import com.google.devtools.build.lib.rules.repository.LocalRepositoryRule;
34import com.google.devtools.build.lib.rules.repository.RepositoryDelegatorFunction;
35import com.google.devtools.build.lib.rules.repository.RepositoryFunction;
36import com.google.devtools.build.lib.rules.repository.RepositoryLoaderFunction;
Nathan Harmatad4f75942016-10-18 08:55:17 +000037import com.google.devtools.build.lib.skyframe.ExternalFilesHelper.ExternalFileAction;
John Cater5e9ce942016-10-12 17:23:30 +000038import com.google.devtools.build.lib.skyframe.PackageLookupFunction.CrossRepositoryLabelViolationStrategy;
brandjondbff8b82017-10-23 19:02:31 +020039import com.google.devtools.build.lib.syntax.SkylarkSemantics;
Florian Weikertcca703a2015-12-07 09:56:38 +000040import com.google.devtools.build.lib.testutil.FoundationTestCase;
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000041import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor;
Eric Fellheimere040fe92015-11-09 23:54:46 +000042import com.google.devtools.build.lib.vfs.PathFragment;
tomluee6a6862018-01-17 14:36:26 -080043import com.google.devtools.build.lib.vfs.Root;
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000044import com.google.devtools.build.skyframe.InMemoryMemoizingEvaluator;
45import com.google.devtools.build.skyframe.MemoizingEvaluator;
46import com.google.devtools.build.skyframe.RecordingDifferencer;
janakr1cde8722017-10-10 03:22:21 +020047import com.google.devtools.build.skyframe.SequencedRecordingDifferencer;
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000048import com.google.devtools.build.skyframe.SequentialBuildDriver;
49import com.google.devtools.build.skyframe.SkyFunction;
50import com.google.devtools.build.skyframe.SkyFunctionName;
51import com.google.devtools.build.skyframe.SkyKey;
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000052import java.util.HashMap;
53import java.util.Map;
54import java.util.UUID;
John Cater45932962017-09-06 21:57:46 +020055import java.util.concurrent.atomic.AtomicBoolean;
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000056import java.util.concurrent.atomic.AtomicReference;
John Cater5e9ce942016-10-12 17:23:30 +000057import org.junit.Before;
58import org.junit.Test;
59import org.junit.runner.RunWith;
60import org.junit.runners.JUnit4;
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000061
62/**
63 * Tests for {@link ContainingPackageLookupFunction}.
64 */
Florian Weikert92b22362015-12-03 10:17:18 +000065@RunWith(JUnit4.class)
Florian Weikertcca703a2015-12-07 09:56:38 +000066public class ContainingPackageLookupFunctionTest extends FoundationTestCase {
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000067
68 private AtomicReference<ImmutableSet<PackageIdentifier>> deletedPackages;
69 private MemoizingEvaluator evaluator;
70 private SequentialBuildDriver driver;
John Cater45932962017-09-06 21:57:46 +020071 private RecordingDifferencer differencer;
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000072
Florian Weikert92b22362015-12-03 10:17:18 +000073 @Before
74 public final void setUp() throws Exception {
John Cater45932962017-09-06 21:57:46 +020075 AnalysisMock analysisMock = AnalysisMock.get();
76
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000077 AtomicReference<PathPackageLocator> pkgLocator =
John Catere0d1d0e2017-11-28 20:47:41 -080078 new AtomicReference<>(
79 new PathPackageLocator(
80 outputBase,
tomluee6a6862018-01-17 14:36:26 -080081 ImmutableList.of(Root.fromPath(rootDirectory)),
John Catere0d1d0e2017-11-28 20:47:41 -080082 BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY));
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000083 deletedPackages = new AtomicReference<>(ImmutableSet.<PackageIdentifier>of());
John Cater45932962017-09-06 21:57:46 +020084 BlazeDirectories directories =
Nathan Harmatad4f75942016-10-18 08:55:17 +000085 new BlazeDirectories(
Klaus Aehligc2499c42018-02-27 05:47:21 -080086 new ServerDirectories(rootDirectory, outputBase, outputBase),
janakr3b63a4e2017-09-14 09:55:40 +020087 rootDirectory,
88 analysisMock.getProductName());
John Cater45932962017-09-06 21:57:46 +020089 ExternalFilesHelper externalFilesHelper =
nharmata3fb7d342018-02-23 11:37:51 -080090 ExternalFilesHelper.createForTesting(
John Cater45932962017-09-06 21:57:46 +020091 pkgLocator,
92 ExternalFileAction.DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_PATHS,
93 directories);
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000094
95 Map<SkyFunctionName, SkyFunction> skyFunctions = new HashMap<>();
John Cater45932962017-09-06 21:57:46 +020096 skyFunctions.put(SkyFunctions.CONTAINING_PACKAGE_LOOKUP, new ContainingPackageLookupFunction());
97
John Cater5e9ce942016-10-12 17:23:30 +000098 skyFunctions.put(
99 SkyFunctions.PACKAGE_LOOKUP,
John Cater0c0735a2016-11-11 01:52:02 +0000100 new PackageLookupFunction(
101 deletedPackages,
102 CrossRepositoryLabelViolationStrategy.ERROR,
John Catere0d1d0e2017-11-28 20:47:41 -0800103 BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY));
John Cater45932962017-09-06 21:57:46 +0200104 skyFunctions.put(
105 SkyFunctions.PACKAGE, new PackageFunction(null, null, null, null, null, null, null));
Eric Fellheimer7ef96d72015-11-12 02:28:44 +0000106 skyFunctions.put(SkyFunctions.BLACKLISTED_PACKAGE_PREFIXES,
nharmatae4eb23f2017-12-05 09:27:45 -0800107 new BlacklistedPackagePrefixesFunction(
108 /*hardcodedBlacklistedPackagePrefixes=*/ ImmutableSet.of(),
109 /*additionalBlacklistedPackagePrefixesFile=*/ PathFragment.EMPTY_FRAGMENT));
Ulf Adamsc73051c62016-03-23 09:18:13 +0000110 skyFunctions.put(SkyFunctions.FILE_STATE, new FileStateFunction(
111 new AtomicReference<TimestampGranularityMonitor>(), externalFilesHelper));
Kristina Chodorowf9fdc8d2015-12-08 12:49:31 +0000112 skyFunctions.put(SkyFunctions.FILE, new FileFunction(pkgLocator));
John Caterb4f461e2016-10-25 16:16:35 +0000113 skyFunctions.put(SkyFunctions.DIRECTORY_LISTING, new DirectoryListingFunction());
114 skyFunctions.put(
115 SkyFunctions.DIRECTORY_LISTING_STATE,
116 new DirectoryListingStateFunction(externalFilesHelper));
John Cater45932962017-09-06 21:57:46 +0200117 RuleClassProvider ruleClassProvider = analysisMock.createRuleClassProvider();
118 skyFunctions.put(SkyFunctions.WORKSPACE_AST, new WorkspaceASTFunction(ruleClassProvider));
119 skyFunctions.put(
120 SkyFunctions.WORKSPACE_FILE,
121 new WorkspaceFileFunction(
122 ruleClassProvider,
123 analysisMock
janakr52d05e82017-09-22 13:27:14 -0400124 .getPackageFactoryBuilderForTesting(directories)
John Cater45932962017-09-06 21:57:46 +0200125 .setEnvironmentExtensions(
126 ImmutableList.<EnvironmentExtension>of(
127 new PackageFactory.EmptyEnvironmentExtension()))
nharmatae57e9a32018-04-02 15:10:24 -0700128 .build(ruleClassProvider),
John Cater45932962017-09-06 21:57:46 +0200129 directories));
130 skyFunctions.put(SkyFunctions.EXTERNAL_PACKAGE, new ExternalPackageFunction());
John Caterb4f461e2016-10-25 16:16:35 +0000131 skyFunctions.put(SkyFunctions.LOCAL_REPOSITORY_LOOKUP, new LocalRepositoryLookupFunction());
John Cater45932962017-09-06 21:57:46 +0200132 skyFunctions.put(
133 SkyFunctions.FILE_SYMLINK_CYCLE_UNIQUENESS, new FileSymlinkCycleUniquenessFunction());
134 ImmutableMap<String, RepositoryFunction> repositoryHandlers =
135 ImmutableMap.of(
136 LocalRepositoryRule.NAME, (RepositoryFunction) new LocalRepositoryFunction());
137 skyFunctions.put(
138 SkyFunctions.REPOSITORY_DIRECTORY,
janakrca6b46d2017-09-22 14:07:15 -0400139 new RepositoryDelegatorFunction(
140 repositoryHandlers, null, new AtomicBoolean(true), ImmutableMap::of, directories));
John Cater45932962017-09-06 21:57:46 +0200141 skyFunctions.put(SkyFunctions.REPOSITORY, new RepositoryLoaderFunction());
142
janakr1cde8722017-10-10 03:22:21 +0200143 differencer = new SequencedRecordingDifferencer();
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000144 evaluator = new InMemoryMemoizingEvaluator(skyFunctions, differencer);
145 driver = new SequentialBuildDriver(evaluator);
146 PrecomputedValue.BUILD_ID.set(differencer, UUID.randomUUID());
brandjondbff8b82017-10-23 19:02:31 +0200147 PrecomputedValue.PATH_PACKAGE_LOCATOR.set(differencer, pkgLocator.get());
148 PrecomputedValue.SKYLARK_SEMANTICS.set(differencer, SkylarkSemantics.DEFAULT_SEMANTICS);
John Cater45932962017-09-06 21:57:46 +0200149 RepositoryDelegatorFunction.REPOSITORY_OVERRIDES.set(
150 differencer, ImmutableMap.<RepositoryName, PathFragment>of());
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000151 }
152
153 private ContainingPackageLookupValue lookupContainingPackage(String packageName)
154 throws InterruptedException {
John Cater45932962017-09-06 21:57:46 +0200155 return lookupContainingPackage(PackageIdentifier.createInMainRepo(packageName));
156 }
157
158 private ContainingPackageLookupValue lookupContainingPackage(PackageIdentifier packageIdentifier)
159 throws InterruptedException {
160 SkyKey key = ContainingPackageLookupValue.key(packageIdentifier);
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000161 return driver
162 .<ContainingPackageLookupValue>evaluate(
163 ImmutableList.of(key),
164 false,
165 SkyframeExecutor.DEFAULT_THREAD_COUNT,
166 NullEventHandler.INSTANCE)
167 .get(key);
168 }
169
Florian Weikert92b22362015-12-03 10:17:18 +0000170 @Test
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000171 public void testNoContainingPackage() throws Exception {
172 ContainingPackageLookupValue value = lookupContainingPackage("a/b");
lberkiaea56b32017-05-30 12:35:33 +0200173 assertThat(value.hasContainingPackage()).isFalse();
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000174 }
175
Florian Weikert92b22362015-12-03 10:17:18 +0000176 @Test
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000177 public void testContainingPackageIsParent() throws Exception {
178 scratch.file("a/BUILD");
179 ContainingPackageLookupValue value = lookupContainingPackage("a/b");
lberkiaea56b32017-05-30 12:35:33 +0200180 assertThat(value.hasContainingPackage()).isTrue();
181 assertThat(value.getContainingPackageName()).isEqualTo(PackageIdentifier.createInMainRepo("a"));
tomluee6a6862018-01-17 14:36:26 -0800182 assertThat(value.getContainingPackageRoot()).isEqualTo(Root.fromPath(rootDirectory));
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000183 }
184
Florian Weikert92b22362015-12-03 10:17:18 +0000185 @Test
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000186 public void testContainingPackageIsSelf() throws Exception {
187 scratch.file("a/b/BUILD");
188 ContainingPackageLookupValue value = lookupContainingPackage("a/b");
lberkiaea56b32017-05-30 12:35:33 +0200189 assertThat(value.hasContainingPackage()).isTrue();
190 assertThat(value.getContainingPackageName())
191 .isEqualTo(PackageIdentifier.createInMainRepo("a/b"));
tomluee6a6862018-01-17 14:36:26 -0800192 assertThat(value.getContainingPackageRoot()).isEqualTo(Root.fromPath(rootDirectory));
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000193 }
194
Florian Weikert92b22362015-12-03 10:17:18 +0000195 @Test
John Cater45932962017-09-06 21:57:46 +0200196 public void testContainingPackageIsExternalRepositoryViaExternalRepository() throws Exception {
197 scratch.overwriteFile(
198 "WORKSPACE",
199 "local_repository(name='a', path='a')");
200 scratch.file("a/WORKSPACE");
201 scratch.file("a/BUILD");
202 scratch.file("a/b/BUILD");
203 ContainingPackageLookupValue value =
204 lookupContainingPackage(
205 PackageIdentifier.create(RepositoryName.create("@a"), PathFragment.create("b")));
206 assertThat(value.hasContainingPackage()).isTrue();
207 assertThat(value.getContainingPackageName())
208 .isEqualTo(PackageIdentifier.create(RepositoryName.create("@a"), PathFragment.create("b")));
209 }
210
211 @Test
212 public void testContainingPackageIsExternalRepositoryViaLocalPath() throws Exception {
213 scratch.overwriteFile(
214 "WORKSPACE",
215 "local_repository(name='a', path='a')");
216 scratch.file("a/WORKSPACE");
217 scratch.file("a/BUILD");
218 scratch.file("a/b/BUILD");
219 ContainingPackageLookupValue value = lookupContainingPackage("a/b");
220 assertThat(value.hasContainingPackage()).isTrue();
221 assertThat(value.getContainingPackageName())
222 .isEqualTo(PackageIdentifier.create(RepositoryName.create("@a"), PathFragment.create("b")));
223 }
224
225 @Test
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000226 public void testEqualsAndHashCodeContract() throws Exception {
227 ContainingPackageLookupValue valueA1 = ContainingPackageLookupValue.NONE;
228 ContainingPackageLookupValue valueA2 = ContainingPackageLookupValue.NONE;
229 ContainingPackageLookupValue valueB1 =
230 ContainingPackageLookupValue.withContainingPackage(
tomluee6a6862018-01-17 14:36:26 -0800231 PackageIdentifier.createInMainRepo("b"), Root.fromPath(rootDirectory));
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000232 ContainingPackageLookupValue valueB2 =
233 ContainingPackageLookupValue.withContainingPackage(
tomluee6a6862018-01-17 14:36:26 -0800234 PackageIdentifier.createInMainRepo("b"), Root.fromPath(rootDirectory));
Brian Silvermand7d6d622016-03-17 09:53:39 +0000235 PackageIdentifier cFrag = PackageIdentifier.createInMainRepo("c");
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000236 ContainingPackageLookupValue valueC1 =
tomluee6a6862018-01-17 14:36:26 -0800237 ContainingPackageLookupValue.withContainingPackage(cFrag, Root.fromPath(rootDirectory));
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000238 ContainingPackageLookupValue valueC2 =
tomluee6a6862018-01-17 14:36:26 -0800239 ContainingPackageLookupValue.withContainingPackage(cFrag, Root.fromPath(rootDirectory));
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000240 ContainingPackageLookupValue valueCOther =
241 ContainingPackageLookupValue.withContainingPackage(
tomluee6a6862018-01-17 14:36:26 -0800242 cFrag, Root.fromPath(rootDirectory.getRelative("other_root")));
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000243 new EqualsTester()
244 .addEqualityGroup(valueA1, valueA2)
245 .addEqualityGroup(valueB1, valueB2)
246 .addEqualityGroup(valueC1, valueC2)
247 .addEqualityGroup(valueCOther)
248 .testEquals();
249 }
250}