blob: e35b333fc43e28f1823cb3779a9012f14db723e6 [file] [log] [blame]
Kristina Chodorow335f0672015-11-16 23:19:13 +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.
14
15package com.google.devtools.build.lib.skyframe;
16
lberkiaea56b32017-05-30 12:35:33 +020017import static com.google.common.truth.Truth.assertThat;
John Caterb4f461e2016-10-25 16:16:35 +000018import static com.google.devtools.build.skyframe.EvaluationResultSubjectFactory.assertThatEvaluationResult;
Florian Weikert92b22362015-12-03 10:17:18 +000019
Klaus Aehlig8eb47482018-09-17 09:14:58 -070020import com.google.common.base.Optional;
Kristina Chodorow335f0672015-11-16 23:19:13 +000021import com.google.common.collect.ImmutableList;
John Caterb4f461e2016-10-25 16:16:35 +000022import com.google.common.collect.ImmutableMap;
Kristina Chodorow335f0672015-11-16 23:19:13 +000023import com.google.common.collect.ImmutableSet;
24import com.google.common.testing.EqualsTester;
shahan602cc852018-06-06 20:09:57 -070025import com.google.devtools.build.lib.actions.FileStateValue;
26import com.google.devtools.build.lib.actions.FileValue;
Kristina Chodorow335f0672015-11-16 23:19:13 +000027import com.google.devtools.build.lib.analysis.BlazeDirectories;
janakr3b63a4e2017-09-14 09:55:40 +020028import com.google.devtools.build.lib.analysis.ServerDirectories;
Ulf Adams015aad92016-07-13 16:49:40 +000029import com.google.devtools.build.lib.analysis.util.AnalysisMock;
Kristina Chodorow335f0672015-11-16 23:19:13 +000030import com.google.devtools.build.lib.cmdline.PackageIdentifier;
kchodorowdfcd5da82017-04-19 18:58:50 +020031import com.google.devtools.build.lib.cmdline.RepositoryName;
Kristina Chodorow335f0672015-11-16 23:19:13 +000032import com.google.devtools.build.lib.events.NullEventHandler;
John Catere0d1d0e2017-11-28 20:47:41 -080033import com.google.devtools.build.lib.packages.BuildFileName;
John Caterb4f461e2016-10-25 16:16:35 +000034import com.google.devtools.build.lib.packages.BuildFileNotFoundException;
Kristina Chodorow335f0672015-11-16 23:19:13 +000035import com.google.devtools.build.lib.packages.RuleClassProvider;
janakr15e15c22019-01-30 11:24:49 -080036import com.google.devtools.build.lib.packages.WorkspaceFileValue;
Kristina Chodorow335f0672015-11-16 23:19:13 +000037import com.google.devtools.build.lib.pkgcache.PathPackageLocator;
John Caterb4f461e2016-10-25 16:16:35 +000038import com.google.devtools.build.lib.rules.repository.LocalRepositoryFunction;
39import com.google.devtools.build.lib.rules.repository.LocalRepositoryRule;
John Caterb4f461e2016-10-25 16:16:35 +000040import com.google.devtools.build.lib.rules.repository.RepositoryDelegatorFunction;
41import com.google.devtools.build.lib.rules.repository.RepositoryFunction;
42import com.google.devtools.build.lib.rules.repository.RepositoryLoaderFunction;
Nathan Harmatad4f75942016-10-18 08:55:17 +000043import com.google.devtools.build.lib.skyframe.ExternalFilesHelper.ExternalFileAction;
John Cater5e9ce942016-10-12 17:23:30 +000044import com.google.devtools.build.lib.skyframe.PackageLookupFunction.CrossRepositoryLabelViolationStrategy;
Kristina Chodorow335f0672015-11-16 23:19:13 +000045import com.google.devtools.build.lib.skyframe.PackageLookupValue.ErrorReason;
John Cater4a12e662017-09-06 17:51:18 +020046import com.google.devtools.build.lib.skyframe.PackageLookupValue.IncorrectRepositoryReferencePackageLookupValue;
laurentlb6659b4c2019-02-18 07:23:36 -080047import com.google.devtools.build.lib.syntax.StarlarkSemantics;
Florian Weikertcca703a2015-12-07 09:56:38 +000048import com.google.devtools.build.lib.testutil.FoundationTestCase;
Kristina Chodorow335f0672015-11-16 23:19:13 +000049import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor;
John Caterb4f461e2016-10-25 16:16:35 +000050import com.google.devtools.build.lib.vfs.FileSystemUtils;
Kristina Chodorow335f0672015-11-16 23:19:13 +000051import com.google.devtools.build.lib.vfs.Path;
52import com.google.devtools.build.lib.vfs.PathFragment;
tomluee6a6862018-01-17 14:36:26 -080053import com.google.devtools.build.lib.vfs.Root;
Kristina Chodorow335f0672015-11-16 23:19:13 +000054import com.google.devtools.build.lib.vfs.RootedPath;
djasperb109a5f2019-02-14 01:05:34 -080055import com.google.devtools.build.lib.vfs.UnixGlob;
Googler10028672018-10-25 12:14:34 -070056import com.google.devtools.build.skyframe.EvaluationContext;
John Caterb4f461e2016-10-25 16:16:35 +000057import com.google.devtools.build.skyframe.EvaluationResult;
Kristina Chodorow335f0672015-11-16 23:19:13 +000058import com.google.devtools.build.skyframe.InMemoryMemoizingEvaluator;
59import com.google.devtools.build.skyframe.MemoizingEvaluator;
60import com.google.devtools.build.skyframe.RecordingDifferencer;
janakr1cde8722017-10-10 03:22:21 +020061import com.google.devtools.build.skyframe.SequencedRecordingDifferencer;
Kristina Chodorow335f0672015-11-16 23:19:13 +000062import com.google.devtools.build.skyframe.SequentialBuildDriver;
63import com.google.devtools.build.skyframe.SkyFunction;
64import com.google.devtools.build.skyframe.SkyFunctionName;
65import com.google.devtools.build.skyframe.SkyKey;
John Cater5b4b7a32017-10-12 16:30:03 +020066import java.util.ArrayList;
Kristina Chodorow335f0672015-11-16 23:19:13 +000067import java.util.HashMap;
John Cater5b4b7a32017-10-12 16:30:03 +020068import java.util.List;
Kristina Chodorow335f0672015-11-16 23:19:13 +000069import java.util.Map;
70import java.util.UUID;
John Caterb4f461e2016-10-25 16:16:35 +000071import java.util.concurrent.atomic.AtomicBoolean;
Kristina Chodorow335f0672015-11-16 23:19:13 +000072import java.util.concurrent.atomic.AtomicReference;
John Cater94695912016-08-03 12:09:39 +000073import org.junit.Before;
74import org.junit.Test;
75import org.junit.runner.RunWith;
76import org.junit.runners.JUnit4;
John Cater5b4b7a32017-10-12 16:30:03 +020077import org.junit.runners.Parameterized;
78import org.junit.runners.Parameterized.Parameters;
Kristina Chodorow335f0672015-11-16 23:19:13 +000079
John Cater5e9ce942016-10-12 17:23:30 +000080/** Tests for {@link PackageLookupFunction}. */
81public abstract class PackageLookupFunctionTest extends FoundationTestCase {
Kristina Chodorow335f0672015-11-16 23:19:13 +000082 private AtomicReference<ImmutableSet<PackageIdentifier>> deletedPackages;
83 private MemoizingEvaluator evaluator;
84 private SequentialBuildDriver driver;
85 private RecordingDifferencer differencer;
John Cater0c0735a2016-11-11 01:52:02 +000086 private Path emptyPackagePath;
nharmataa32ac672020-01-08 12:54:25 -080087 private static final String BLACKLISTED_PACKAGE_PREFIXES_FILE_PATH_STRING =
nharmatae4eb23f2017-12-05 09:27:45 -080088 "config/blacklisted.txt";
Kristina Chodorow335f0672015-11-16 23:19:13 +000089
John Cater5e9ce942016-10-12 17:23:30 +000090 protected abstract CrossRepositoryLabelViolationStrategy crossRepositoryLabelViolationStrategy();
91
Florian Weikert92b22362015-12-03 10:17:18 +000092 @Before
93 public final void setUp() throws Exception {
John Cater0c0735a2016-11-11 01:52:02 +000094 emptyPackagePath = rootDirectory.getRelative("somewhere/else");
Kristina Chodorow335f0672015-11-16 23:19:13 +000095 scratch.file("parentpackage/BUILD");
96
Ulf Adams015aad92016-07-13 16:49:40 +000097 AnalysisMock analysisMock = AnalysisMock.get();
John Catere0d1d0e2017-11-28 20:47:41 -080098 AtomicReference<PathPackageLocator> pkgLocator =
99 new AtomicReference<>(
100 new PathPackageLocator(
101 outputBase,
tomluee6a6862018-01-17 14:36:26 -0800102 ImmutableList.of(Root.fromPath(emptyPackagePath), Root.fromPath(rootDirectory)),
John Catere0d1d0e2017-11-28 20:47:41 -0800103 BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000104 deletedPackages = new AtomicReference<>(ImmutableSet.<PackageIdentifier>of());
Ulf Adams015aad92016-07-13 16:49:40 +0000105 BlazeDirectories directories =
106 new BlazeDirectories(
Klaus Aehligc2499c42018-02-27 05:47:21 -0800107 new ServerDirectories(rootDirectory, outputBase, rootDirectory),
janakr3b63a4e2017-09-14 09:55:40 +0200108 rootDirectory,
cushon849df362018-05-14 01:51:45 -0700109 /* defaultSystemJavabase= */ null,
janakr3b63a4e2017-09-14 09:55:40 +0200110 analysisMock.getProductName());
nharmata3fb7d342018-02-23 11:37:51 -0800111 ExternalFilesHelper externalFilesHelper = ExternalFilesHelper.createForTesting(
Nathan Harmatad4f75942016-10-18 08:55:17 +0000112 pkgLocator, ExternalFileAction.DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_PATHS, directories);
Kristina Chodorow335f0672015-11-16 23:19:13 +0000113
114 Map<SkyFunctionName, SkyFunction> skyFunctions = new HashMap<>();
John Cater5e9ce942016-10-12 17:23:30 +0000115 skyFunctions.put(
116 SkyFunctions.PACKAGE_LOOKUP,
John Cater0c0735a2016-11-11 01:52:02 +0000117 new PackageLookupFunction(
118 deletedPackages,
119 crossRepositoryLabelViolationStrategy(),
ajurkowski2018b872020-04-14 09:59:00 -0700120 BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY,
121 BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000122 skyFunctions.put(
ajurkowski2018b872020-04-14 09:59:00 -0700123 SkyFunctions.PACKAGE, new PackageFunction(null, null, null, null, null, null, null, null));
shahan602cc852018-06-06 20:09:57 -0700124 skyFunctions.put(
125 FileStateValue.FILE_STATE,
126 new FileStateFunction(
djasper7faa0ef2019-03-28 10:00:00 -0700127 new AtomicReference<TimestampGranularityMonitor>(),
Googler166c3a32019-11-05 13:25:10 -0800128 new AtomicReference<>(UnixGlob.DEFAULT_SYSCALLS),
djasper7faa0ef2019-03-28 10:00:00 -0700129 externalFilesHelper));
shahan602cc852018-06-06 20:09:57 -0700130 skyFunctions.put(FileValue.FILE, new FileFunction(pkgLocator));
John Caterb4f461e2016-10-25 16:16:35 +0000131 skyFunctions.put(SkyFunctions.DIRECTORY_LISTING, new DirectoryListingFunction());
132 skyFunctions.put(
133 SkyFunctions.DIRECTORY_LISTING_STATE,
djasperb109a5f2019-02-14 01:05:34 -0800134 new DirectoryListingStateFunction(
135 externalFilesHelper, new AtomicReference<>(UnixGlob.DEFAULT_SYSCALLS)));
nharmataa32ac672020-01-08 12:54:25 -0800136 skyFunctions.put(
137 SkyFunctions.BLACKLISTED_PACKAGE_PREFIXES,
nharmatae4eb23f2017-12-05 09:27:45 -0800138 new BlacklistedPackagePrefixesFunction(
nharmataa32ac672020-01-08 12:54:25 -0800139 PathFragment.create(BLACKLISTED_PACKAGE_PREFIXES_FILE_PATH_STRING)));
Ulf Adams015aad92016-07-13 16:49:40 +0000140 RuleClassProvider ruleClassProvider = analysisMock.createRuleClassProvider();
141 skyFunctions.put(SkyFunctions.WORKSPACE_AST, new WorkspaceASTFunction(ruleClassProvider));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000142 skyFunctions.put(
janakr15e15c22019-01-30 11:24:49 -0800143 WorkspaceFileValue.WORKSPACE_FILE,
Kristina Chodorow335f0672015-11-16 23:19:13 +0000144 new WorkspaceFileFunction(
145 ruleClassProvider,
Ulf Adams015aad92016-07-13 16:49:40 +0000146 analysisMock
janakr52d05e82017-09-22 13:27:14 -0400147 .getPackageFactoryBuilderForTesting(directories)
nharmatafde0bd2f2018-12-21 10:17:56 -0800148 .build(ruleClassProvider, fileSystem),
mjhalupkaf0e48112019-01-14 13:01:56 -0800149 directories,
gregceebd616f2020-04-10 16:47:41 -0700150 /*starlarkImportLookupFunctionForInlining=*/ null));
ajurkowski2018b872020-04-14 09:59:00 -0700151 skyFunctions.put(
152 SkyFunctions.EXTERNAL_PACKAGE,
153 new ExternalPackageFunction(BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER));
154 skyFunctions.put(
155 SkyFunctions.LOCAL_REPOSITORY_LOOKUP,
156 new LocalRepositoryLookupFunction(BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER));
John Caterb4f461e2016-10-25 16:16:35 +0000157 skyFunctions.put(
158 SkyFunctions.FILE_SYMLINK_CYCLE_UNIQUENESS, new FileSymlinkCycleUniquenessFunction());
159
160 ImmutableMap<String, RepositoryFunction> repositoryHandlers =
John Caterb25a0342016-10-26 10:17:40 +0000161 ImmutableMap.of(
162 LocalRepositoryRule.NAME, (RepositoryFunction) new LocalRepositoryFunction());
John Caterb4f461e2016-10-25 16:16:35 +0000163 skyFunctions.put(
164 SkyFunctions.REPOSITORY_DIRECTORY,
janakrca6b46d2017-09-22 14:07:15 -0400165 new RepositoryDelegatorFunction(
Googler22913422019-05-02 12:42:20 -0700166 repositoryHandlers,
167 null,
168 new AtomicBoolean(true),
169 ImmutableMap::of,
170 directories,
ajurkowski2018b872020-04-14 09:59:00 -0700171 ManagedDirectoriesKnowledge.NO_MANAGED_DIRECTORIES,
172 BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER));
John Caterb4f461e2016-10-25 16:16:35 +0000173 skyFunctions.put(SkyFunctions.REPOSITORY, new RepositoryLoaderFunction());
174
janakr1cde8722017-10-10 03:22:21 +0200175 differencer = new SequencedRecordingDifferencer();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000176 evaluator = new InMemoryMemoizingEvaluator(skyFunctions, differencer);
177 driver = new SequentialBuildDriver(evaluator);
178 PrecomputedValue.BUILD_ID.set(differencer, UUID.randomUUID());
brandjondbff8b82017-10-23 19:02:31 +0200179 PrecomputedValue.PATH_PACKAGE_LOCATOR.set(differencer, pkgLocator.get());
laurentlb8c02aff2019-02-18 10:53:34 -0800180 PrecomputedValue.STARLARK_SEMANTICS.set(differencer, StarlarkSemantics.DEFAULT_SEMANTICS);
kchodorowdfcd5da82017-04-19 18:58:50 +0200181 RepositoryDelegatorFunction.REPOSITORY_OVERRIDES.set(
182 differencer, ImmutableMap.<RepositoryName, PathFragment>of());
Klaus Aehlig93fe20c2018-06-14 05:48:46 -0700183 RepositoryDelegatorFunction.DEPENDENCY_FOR_UNCONDITIONAL_FETCHING.set(
184 differencer, RepositoryDelegatorFunction.DONT_FETCH_UNCONDITIONALLY);
Klaus Aehlig8eb47482018-09-17 09:14:58 -0700185 RepositoryDelegatorFunction.RESOLVED_FILE_INSTEAD_OF_WORKSPACE.set(
186 differencer, Optional.<RootedPath>absent());
Kristina Chodorow335f0672015-11-16 23:19:13 +0000187 }
188
John Caterb4f461e2016-10-25 16:16:35 +0000189 protected PackageLookupValue lookupPackage(String packageName) throws InterruptedException {
Brian Silvermand7d6d622016-03-17 09:53:39 +0000190 return lookupPackage(PackageIdentifier.createInMainRepo(packageName));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000191 }
192
John Caterb4f461e2016-10-25 16:16:35 +0000193 protected PackageLookupValue lookupPackage(PackageIdentifier packageId)
Kristina Chodorow335f0672015-11-16 23:19:13 +0000194 throws InterruptedException {
195 SkyKey key = PackageLookupValue.key(packageId);
John Caterb4f461e2016-10-25 16:16:35 +0000196 return lookupPackage(key).get(key);
197 }
198
199 protected EvaluationResult<PackageLookupValue> lookupPackage(SkyKey packageIdentifierSkyKey)
200 throws InterruptedException {
Googler10028672018-10-25 12:14:34 -0700201 EvaluationContext evaluationContext =
202 EvaluationContext.newBuilder()
203 .setKeepGoing(false)
204 .setNumThreads(SkyframeExecutor.DEFAULT_THREAD_COUNT)
205 .setEventHander(NullEventHandler.INSTANCE)
206 .build();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000207 return driver.<PackageLookupValue>evaluate(
Googler10028672018-10-25 12:14:34 -0700208 ImmutableList.of(packageIdentifierSkyKey), evaluationContext);
Kristina Chodorow335f0672015-11-16 23:19:13 +0000209 }
210
Florian Weikert92b22362015-12-03 10:17:18 +0000211 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000212 public void testNoBuildFile() throws Exception {
213 scratch.file("parentpackage/nobuildfile/foo.txt");
214 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/nobuildfile");
lberkiaea56b32017-05-30 12:35:33 +0200215 assertThat(packageLookupValue.packageExists()).isFalse();
216 assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.NO_BUILD_FILE);
217 assertThat(packageLookupValue.getErrorMsg()).isNotNull();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000218 }
219
Florian Weikert92b22362015-12-03 10:17:18 +0000220 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000221 public void testNoBuildFileAndNoParentPackage() throws Exception {
222 scratch.file("noparentpackage/foo.txt");
223 PackageLookupValue packageLookupValue = lookupPackage("noparentpackage");
lberkiaea56b32017-05-30 12:35:33 +0200224 assertThat(packageLookupValue.packageExists()).isFalse();
225 assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.NO_BUILD_FILE);
226 assertThat(packageLookupValue.getErrorMsg()).isNotNull();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000227 }
228
Florian Weikert92b22362015-12-03 10:17:18 +0000229 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000230 public void testDeletedPackage() throws Exception {
231 scratch.file("parentpackage/deletedpackage/BUILD");
232 deletedPackages.set(ImmutableSet.of(
Brian Silvermand7d6d622016-03-17 09:53:39 +0000233 PackageIdentifier.createInMainRepo("parentpackage/deletedpackage")));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000234 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/deletedpackage");
lberkiaea56b32017-05-30 12:35:33 +0200235 assertThat(packageLookupValue.packageExists()).isFalse();
236 assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.DELETED_PACKAGE);
237 assertThat(packageLookupValue.getErrorMsg()).isNotNull();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000238 }
239
Florian Weikert92b22362015-12-03 10:17:18 +0000240 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000241 public void testBlacklistedPackage() throws Exception {
242 scratch.file("blacklisted/subdir/BUILD");
243 scratch.file("blacklisted/BUILD");
nharmataa32ac672020-01-08 12:54:25 -0800244 Path blacklist =
245 scratch.overwriteFile(BLACKLISTED_PACKAGE_PREFIXES_FILE_PATH_STRING, "blacklisted");
Kristina Chodorow335f0672015-11-16 23:19:13 +0000246
247 ImmutableSet<String> pkgs = ImmutableSet.of("blacklisted/subdir", "blacklisted");
248 for (String pkg : pkgs) {
249 PackageLookupValue packageLookupValue = lookupPackage(pkg);
lberkiaea56b32017-05-30 12:35:33 +0200250 assertThat(packageLookupValue.packageExists()).isFalse();
251 assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.DELETED_PACKAGE);
252 assertThat(packageLookupValue.getErrorMsg()).isNotNull();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000253 }
254
nharmataa32ac672020-01-08 12:54:25 -0800255 scratch.overwriteFile(BLACKLISTED_PACKAGE_PREFIXES_FILE_PATH_STRING, "not_blacklisted");
tomluee6a6862018-01-17 14:36:26 -0800256 RootedPath rootedBlacklist =
257 RootedPath.toRootedPath(
258 Root.fromPath(blacklist.getParentDirectory().getParentDirectory()),
259 PathFragment.create("config/blacklisted.txt"));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000260 differencer.invalidate(ImmutableSet.of(FileStateValue.key(rootedBlacklist)));
261 for (String pkg : pkgs) {
262 PackageLookupValue packageLookupValue = lookupPackage(pkg);
lberkiaea56b32017-05-30 12:35:33 +0200263 assertThat(packageLookupValue.packageExists()).isTrue();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000264 }
265 }
266
Florian Weikert92b22362015-12-03 10:17:18 +0000267 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000268 public void testInvalidPackageName() throws Exception {
twerthc4f2d802018-05-15 05:02:31 -0700269 scratch.file("parentpackage/invalidpackagename:42/BUILD");
270 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/invalidpackagename:42");
lberkiaea56b32017-05-30 12:35:33 +0200271 assertThat(packageLookupValue.packageExists()).isFalse();
272 assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.INVALID_PACKAGE_NAME);
273 assertThat(packageLookupValue.getErrorMsg()).isNotNull();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000274 }
275
Florian Weikert92b22362015-12-03 10:17:18 +0000276 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000277 public void testDirectoryNamedBuild() throws Exception {
278 scratch.dir("parentpackage/isdirectory/BUILD");
279 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/isdirectory");
lberkiaea56b32017-05-30 12:35:33 +0200280 assertThat(packageLookupValue.packageExists()).isFalse();
281 assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.NO_BUILD_FILE);
282 assertThat(packageLookupValue.getErrorMsg()).isNotNull();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000283 }
284
Florian Weikert92b22362015-12-03 10:17:18 +0000285 @Test
John Cater0c0735a2016-11-11 01:52:02 +0000286 public void testEverythingIsGood_BUILD() throws Exception {
Kristina Chodorow335f0672015-11-16 23:19:13 +0000287 scratch.file("parentpackage/everythinggood/BUILD");
288 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/everythinggood");
lberkiaea56b32017-05-30 12:35:33 +0200289 assertThat(packageLookupValue.packageExists()).isTrue();
tomluee6a6862018-01-17 14:36:26 -0800290 assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory));
lberkiaea56b32017-05-30 12:35:33 +0200291 assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD);
Kristina Chodorow335f0672015-11-16 23:19:13 +0000292 }
293
Florian Weikert92b22362015-12-03 10:17:18 +0000294 @Test
John Cater0c0735a2016-11-11 01:52:02 +0000295 public void testEverythingIsGood_BUILD_bazel() throws Exception {
296 scratch.file("parentpackage/everythinggood/BUILD.bazel");
297 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/everythinggood");
lberkiaea56b32017-05-30 12:35:33 +0200298 assertThat(packageLookupValue.packageExists()).isTrue();
tomluee6a6862018-01-17 14:36:26 -0800299 assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory));
lberkiaea56b32017-05-30 12:35:33 +0200300 assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD_DOT_BAZEL);
John Cater0c0735a2016-11-11 01:52:02 +0000301 }
302
303 @Test
304 public void testEverythingIsGood_both() throws Exception {
305 scratch.file("parentpackage/everythinggood/BUILD");
306 scratch.file("parentpackage/everythinggood/BUILD.bazel");
307 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/everythinggood");
lberkiaea56b32017-05-30 12:35:33 +0200308 assertThat(packageLookupValue.packageExists()).isTrue();
tomluee6a6862018-01-17 14:36:26 -0800309 assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory));
lberkiaea56b32017-05-30 12:35:33 +0200310 assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD_DOT_BAZEL);
John Cater0c0735a2016-11-11 01:52:02 +0000311 }
312
313 @Test
314 public void testBuildFilesInMultiplePackagePaths() throws Exception {
315 scratch.file(emptyPackagePath.getPathString() + "/foo/BUILD");
316 scratch.file("foo/BUILD.bazel");
317
318 // BUILD file in the first package path should be preferred to BUILD.bazel in the second.
319 PackageLookupValue packageLookupValue = lookupPackage("foo");
lberkiaea56b32017-05-30 12:35:33 +0200320 assertThat(packageLookupValue.packageExists()).isTrue();
tomluee6a6862018-01-17 14:36:26 -0800321 assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(emptyPackagePath));
lberkiaea56b32017-05-30 12:35:33 +0200322 assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD);
John Cater0c0735a2016-11-11 01:52:02 +0000323 }
324
325 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000326 public void testEmptyPackageName() throws Exception {
327 scratch.file("BUILD");
328 PackageLookupValue packageLookupValue = lookupPackage("");
lberkiaea56b32017-05-30 12:35:33 +0200329 assertThat(packageLookupValue.packageExists()).isTrue();
tomluee6a6862018-01-17 14:36:26 -0800330 assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory));
lberkiaea56b32017-05-30 12:35:33 +0200331 assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD);
Kristina Chodorow335f0672015-11-16 23:19:13 +0000332 }
333
Florian Weikert92b22362015-12-03 10:17:18 +0000334 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000335 public void testWorkspaceLookup() throws Exception {
336 scratch.overwriteFile("WORKSPACE");
Brian Silvermand7d6d622016-03-17 09:53:39 +0000337 PackageLookupValue packageLookupValue = lookupPackage(
338 PackageIdentifier.createInMainRepo("external"));
lberkiaea56b32017-05-30 12:35:33 +0200339 assertThat(packageLookupValue.packageExists()).isTrue();
tomluee6a6862018-01-17 14:36:26 -0800340 assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000341 }
John Cater94695912016-08-03 12:09:39 +0000342
Florian Weikert92b22362015-12-03 10:17:18 +0000343 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000344 public void testPackageLookupValueHashCodeAndEqualsContract() throws Exception {
tomluee6a6862018-01-17 14:36:26 -0800345 Root root1 = Root.fromPath(rootDirectory.getRelative("root1"));
346 Root root2 = Root.fromPath(rootDirectory.getRelative("root2"));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000347 // Our (seeming) duplication of parameters here is intentional. Some of the subclasses of
348 // PackageLookupValue are supposed to have reference equality semantics, and some are supposed
349 // to have logical equality semantics.
350 new EqualsTester()
John Cater94695912016-08-03 12:09:39 +0000351 .addEqualityGroup(
352 PackageLookupValue.success(root1, BuildFileName.BUILD),
353 PackageLookupValue.success(root1, BuildFileName.BUILD))
354 .addEqualityGroup(
355 PackageLookupValue.success(root2, BuildFileName.BUILD),
356 PackageLookupValue.success(root2, BuildFileName.BUILD))
Kristina Chodorow335f0672015-11-16 23:19:13 +0000357 .addEqualityGroup(
janakr677ba952019-03-07 09:50:01 -0800358 PackageLookupValue.NO_BUILD_FILE_VALUE, PackageLookupValue.NO_BUILD_FILE_VALUE)
359 .addEqualityGroup(
Kristina Chodorow335f0672015-11-16 23:19:13 +0000360 PackageLookupValue.DELETED_PACKAGE_VALUE, PackageLookupValue.DELETED_PACKAGE_VALUE)
John Cater94695912016-08-03 12:09:39 +0000361 .addEqualityGroup(
362 PackageLookupValue.invalidPackageName("nope1"),
Kristina Chodorow335f0672015-11-16 23:19:13 +0000363 PackageLookupValue.invalidPackageName("nope1"))
John Cater94695912016-08-03 12:09:39 +0000364 .addEqualityGroup(
365 PackageLookupValue.invalidPackageName("nope2"),
366 PackageLookupValue.invalidPackageName("nope2"))
Kristina Chodorow335f0672015-11-16 23:19:13 +0000367 .testEquals();
368 }
John Cater5e9ce942016-10-12 17:23:30 +0000369
John Caterb4f461e2016-10-25 16:16:35 +0000370 protected void createAndCheckInvalidPackageLabel(boolean expectedPackageExists) throws Exception {
371 scratch.overwriteFile("WORKSPACE", "local_repository(name='local', path='local/repo')");
372 scratch.file("local/repo/WORKSPACE");
373 scratch.file("local/repo/BUILD");
374
375 // First, use the correct label.
376 PackageLookupValue packageLookupValue =
377 lookupPackage(PackageIdentifier.create("@local", PathFragment.EMPTY_FRAGMENT));
lberkiaea56b32017-05-30 12:35:33 +0200378 assertThat(packageLookupValue.packageExists()).isTrue();
John Caterb4f461e2016-10-25 16:16:35 +0000379
380 // Then, use the incorrect label.
381 packageLookupValue = lookupPackage(PackageIdentifier.createInMainRepo("local/repo"));
lberkiaea56b32017-05-30 12:35:33 +0200382 assertThat(packageLookupValue.packageExists()).isEqualTo(expectedPackageExists);
John Caterb4f461e2016-10-25 16:16:35 +0000383 }
384
John Cater5e9ce942016-10-12 17:23:30 +0000385 /**
John Caterb4f461e2016-10-25 16:16:35 +0000386 * Runs all tests in the base {@link PackageLookupFunctionTest} class with the {@link
387 * CrossRepositoryLabelViolationStrategy#IGNORE} enum set, and also additional tests specific to
388 * that setting.
John Cater5e9ce942016-10-12 17:23:30 +0000389 */
390 @RunWith(JUnit4.class)
John Caterb4f461e2016-10-25 16:16:35 +0000391 public static class IgnoreLabelViolationsTest extends PackageLookupFunctionTest {
John Cater5e9ce942016-10-12 17:23:30 +0000392 @Override
393 protected CrossRepositoryLabelViolationStrategy crossRepositoryLabelViolationStrategy() {
394 return CrossRepositoryLabelViolationStrategy.IGNORE;
395 }
396
397 // Add any ignore-specific tests here.
John Caterb4f461e2016-10-25 16:16:35 +0000398
399 @Test
400 public void testInvalidPackageLabelIsIgnored() throws Exception {
401 createAndCheckInvalidPackageLabel(true);
402 }
John Cater5e9ce942016-10-12 17:23:30 +0000403 }
404
405 /**
John Caterb4f461e2016-10-25 16:16:35 +0000406 * Runs all tests in the base {@link PackageLookupFunctionTest} class with the {@link
407 * CrossRepositoryLabelViolationStrategy#ERROR} enum set, and also additional tests specific to
408 * that setting.
John Cater5e9ce942016-10-12 17:23:30 +0000409 */
410 @RunWith(JUnit4.class)
John Caterb4f461e2016-10-25 16:16:35 +0000411 public static class ErrorLabelViolationsTest extends PackageLookupFunctionTest {
John Cater5e9ce942016-10-12 17:23:30 +0000412 @Override
413 protected CrossRepositoryLabelViolationStrategy crossRepositoryLabelViolationStrategy() {
414 return CrossRepositoryLabelViolationStrategy.ERROR;
415 }
416
417 // Add any error-specific tests here.
John Caterb4f461e2016-10-25 16:16:35 +0000418
419 @Test
420 public void testInvalidPackageLabelIsError() throws Exception {
421 createAndCheckInvalidPackageLabel(false);
422 }
423
424 @Test
425 public void testSymlinkCycleInWorkspace() throws Exception {
426 scratch.overwriteFile("WORKSPACE", "local_repository(name='local', path='local/repo')");
427 Path localRepoWorkspace = scratch.resolve("local/repo/WORKSPACE");
428 Path localRepoWorkspaceLink = scratch.resolve("local/repo/WORKSPACE.link");
429 FileSystemUtils.createDirectoryAndParents(localRepoWorkspace.getParentDirectory());
430 FileSystemUtils.createDirectoryAndParents(localRepoWorkspaceLink.getParentDirectory());
431 localRepoWorkspace.createSymbolicLink(localRepoWorkspaceLink);
432 localRepoWorkspaceLink.createSymbolicLink(localRepoWorkspace);
433 scratch.file("local/repo/BUILD");
434
435 SkyKey skyKey = PackageLookupValue.key(PackageIdentifier.createInMainRepo("local/repo"));
436 EvaluationResult<PackageLookupValue> result = lookupPackage(skyKey);
437 assertThatEvaluationResult(result)
438 .hasErrorEntryForKeyThat(skyKey)
439 .hasExceptionThat()
440 .isInstanceOf(BuildFileNotFoundException.class);
441 assertThatEvaluationResult(result)
442 .hasErrorEntryForKeyThat(skyKey)
443 .hasExceptionThat()
diamondm4d8d1d62019-03-25 16:05:02 -0700444 .hasMessageThat()
445 .isEqualTo(
John Caterb4f461e2016-10-25 16:16:35 +0000446 "no such package 'local/repo': Unable to determine the local repository for "
447 + "directory /workspace/local/repo");
448 }
John Cater5e9ce942016-10-12 17:23:30 +0000449 }
John Cater5b4b7a32017-10-12 16:30:03 +0200450
451 /** Tests for detection of invalid package identifiers for local repositories. */
452 @RunWith(Parameterized.class)
453 public static class CorrectedLocalRepositoryTest extends PackageLookupFunctionTest {
454
455 /**
456 * Create parameters for this test. The contents are:
457 *
458 * <ol>
459 * <li>description
460 * <li>repository path
461 * <li>package path - under the repository
462 * <li>expected corrected package identifier
463 * </ol>
464 */
465 @Parameters(name = "{0}")
466 public static List<Object[]> parameters() {
467 List<Object[]> params = new ArrayList<>();
468
469 params.add(new String[] {"simpleRepo_emptyPackage", "local", "", "@local//"});
470 params.add(new String[] {"simpleRepo_singlePackage", "local", "package", "@local//package"});
471 params.add(
472 new String[] {
473 "simpleRepo_subPackage", "local", "package/subpackage", "@local//package/subpackage"
474 });
475 params.add(new String[] {"deepRepo_emptyPackage", "local/repo", "", "@local//"});
476 params.add(new String[] {"deepRepo_subPackage", "local/repo", "package", "@local//package"});
477
478 return params;
479 }
480
jcater25fb5b12017-10-13 17:23:44 +0200481 private final String repositoryPath;
482 private final String packagePath;
483 private final String expectedCorrectedPackageIdentifier;
John Cater5b4b7a32017-10-12 16:30:03 +0200484
485 public CorrectedLocalRepositoryTest(
jcater25fb5b12017-10-13 17:23:44 +0200486 String unusedDescription,
John Cater5b4b7a32017-10-12 16:30:03 +0200487 String repositoryPath,
488 String packagePath,
489 String expectedCorrectedPackageIdentifier) {
John Cater5b4b7a32017-10-12 16:30:03 +0200490 this.repositoryPath = repositoryPath;
491 this.packagePath = packagePath;
492 this.expectedCorrectedPackageIdentifier = expectedCorrectedPackageIdentifier;
493 }
494
495 @Override
496 protected CrossRepositoryLabelViolationStrategy crossRepositoryLabelViolationStrategy() {
497 return CrossRepositoryLabelViolationStrategy.ERROR;
498 }
499
500 @Test
501 public void testCorrectPackageDetection_relativePath() throws Exception {
John Cater5b4b7a32017-10-12 16:30:03 +0200502 scratch.overwriteFile(
503 "WORKSPACE", "local_repository(name='local', path='" + repositoryPath + "')");
504 scratch.file(PathFragment.create(repositoryPath).getRelative("WORKSPACE").getPathString());
505 scratch.file(
506 PathFragment.create(repositoryPath)
507 .getRelative(packagePath)
508 .getRelative("BUILD")
509 .getPathString());
510
511 PackageIdentifier packageIdentifier =
512 PackageIdentifier.createInMainRepo(
513 PathFragment.create(repositoryPath).getRelative(packagePath));
514 PackageLookupValue packageLookupValue = lookupPackage(packageIdentifier);
515 assertThat(packageLookupValue.packageExists()).isFalse();
516 assertThat(packageLookupValue)
517 .isInstanceOf(IncorrectRepositoryReferencePackageLookupValue.class);
518
519 IncorrectRepositoryReferencePackageLookupValue incorrectPackageLookupValue =
520 (IncorrectRepositoryReferencePackageLookupValue) packageLookupValue;
521 assertThat(incorrectPackageLookupValue.getInvalidPackageIdentifier())
522 .isEqualTo(packageIdentifier);
523 assertThat(incorrectPackageLookupValue.getCorrectedPackageIdentifier().toString())
524 .isEqualTo(expectedCorrectedPackageIdentifier);
525 }
526
527 @Test
528 public void testCorrectPackageDetection_absolutePath() throws Exception {
John Cater5b4b7a32017-10-12 16:30:03 +0200529 scratch.overwriteFile(
530 "WORKSPACE",
531 "local_repository(name='local', path=__workspace_dir__ + '/" + repositoryPath + "')");
532 scratch.file(PathFragment.create(repositoryPath).getRelative("WORKSPACE").getPathString());
533 scratch.file(
534 PathFragment.create(repositoryPath)
535 .getRelative(packagePath)
536 .getRelative("BUILD")
537 .getPathString());
538
539 PackageIdentifier packageIdentifier =
540 PackageIdentifier.createInMainRepo(
541 PathFragment.create(repositoryPath).getRelative(packagePath));
542 PackageLookupValue packageLookupValue = lookupPackage(packageIdentifier);
543 assertThat(packageLookupValue.packageExists()).isFalse();
544 assertThat(packageLookupValue)
545 .isInstanceOf(IncorrectRepositoryReferencePackageLookupValue.class);
546
547 IncorrectRepositoryReferencePackageLookupValue incorrectPackageLookupValue =
548 (IncorrectRepositoryReferencePackageLookupValue) packageLookupValue;
549 assertThat(incorrectPackageLookupValue.getInvalidPackageIdentifier())
550 .isEqualTo(packageIdentifier);
551 assertThat(incorrectPackageLookupValue.getCorrectedPackageIdentifier().toString())
552 .isEqualTo(expectedCorrectedPackageIdentifier);
553 }
554 }
Kristina Chodorow335f0672015-11-16 23:19:13 +0000555}