blob: 034194fe3c9d8329127d836d7c3c8c1f212db360 [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
Kristina Chodorow335f0672015-11-16 23:19:13 +000020import com.google.common.collect.ImmutableList;
John Caterb4f461e2016-10-25 16:16:35 +000021import com.google.common.collect.ImmutableMap;
Kristina Chodorow335f0672015-11-16 23:19:13 +000022import com.google.common.collect.ImmutableSet;
23import com.google.common.testing.EqualsTester;
shahan602cc852018-06-06 20:09:57 -070024import com.google.devtools.build.lib.actions.FileStateValue;
25import com.google.devtools.build.lib.actions.FileValue;
Kristina Chodorow335f0672015-11-16 23:19:13 +000026import com.google.devtools.build.lib.analysis.BlazeDirectories;
janakr3b63a4e2017-09-14 09:55:40 +020027import com.google.devtools.build.lib.analysis.ServerDirectories;
Ulf Adams015aad92016-07-13 16:49:40 +000028import com.google.devtools.build.lib.analysis.util.AnalysisMock;
Kristina Chodorow335f0672015-11-16 23:19:13 +000029import com.google.devtools.build.lib.cmdline.PackageIdentifier;
kchodorowdfcd5da82017-04-19 18:58:50 +020030import com.google.devtools.build.lib.cmdline.RepositoryName;
Kristina Chodorow335f0672015-11-16 23:19:13 +000031import com.google.devtools.build.lib.events.NullEventHandler;
John Catere0d1d0e2017-11-28 20:47:41 -080032import com.google.devtools.build.lib.packages.BuildFileName;
John Caterb4f461e2016-10-25 16:16:35 +000033import com.google.devtools.build.lib.packages.BuildFileNotFoundException;
Kristina Chodorow335f0672015-11-16 23:19:13 +000034import com.google.devtools.build.lib.packages.RuleClassProvider;
janakr15e15c22019-01-30 11:24:49 -080035import com.google.devtools.build.lib.packages.WorkspaceFileValue;
Kristina Chodorow335f0672015-11-16 23:19:13 +000036import com.google.devtools.build.lib.pkgcache.PathPackageLocator;
John Caterb4f461e2016-10-25 16:16:35 +000037import com.google.devtools.build.lib.rules.repository.LocalRepositoryFunction;
38import com.google.devtools.build.lib.rules.repository.LocalRepositoryRule;
John Caterb4f461e2016-10-25 16:16:35 +000039import com.google.devtools.build.lib.rules.repository.RepositoryDelegatorFunction;
40import com.google.devtools.build.lib.rules.repository.RepositoryFunction;
41import com.google.devtools.build.lib.rules.repository.RepositoryLoaderFunction;
Nathan Harmatad4f75942016-10-18 08:55:17 +000042import com.google.devtools.build.lib.skyframe.ExternalFilesHelper.ExternalFileAction;
John Cater5e9ce942016-10-12 17:23:30 +000043import com.google.devtools.build.lib.skyframe.PackageLookupFunction.CrossRepositoryLabelViolationStrategy;
Kristina Chodorow335f0672015-11-16 23:19:13 +000044import com.google.devtools.build.lib.skyframe.PackageLookupValue.ErrorReason;
John Cater4a12e662017-09-06 17:51:18 +020045import com.google.devtools.build.lib.skyframe.PackageLookupValue.IncorrectRepositoryReferencePackageLookupValue;
laurentlb6659b4c2019-02-18 07:23:36 -080046import com.google.devtools.build.lib.syntax.StarlarkSemantics;
Florian Weikertcca703a2015-12-07 09:56:38 +000047import com.google.devtools.build.lib.testutil.FoundationTestCase;
Kristina Chodorow335f0672015-11-16 23:19:13 +000048import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor;
John Caterb4f461e2016-10-25 16:16:35 +000049import com.google.devtools.build.lib.vfs.FileSystemUtils;
Kristina Chodorow335f0672015-11-16 23:19:13 +000050import com.google.devtools.build.lib.vfs.Path;
51import com.google.devtools.build.lib.vfs.PathFragment;
tomluee6a6862018-01-17 14:36:26 -080052import com.google.devtools.build.lib.vfs.Root;
Kristina Chodorow335f0672015-11-16 23:19:13 +000053import com.google.devtools.build.lib.vfs.RootedPath;
djasperb109a5f2019-02-14 01:05:34 -080054import com.google.devtools.build.lib.vfs.UnixGlob;
Googler10028672018-10-25 12:14:34 -070055import com.google.devtools.build.skyframe.EvaluationContext;
John Caterb4f461e2016-10-25 16:16:35 +000056import com.google.devtools.build.skyframe.EvaluationResult;
Kristina Chodorow335f0672015-11-16 23:19:13 +000057import com.google.devtools.build.skyframe.InMemoryMemoizingEvaluator;
58import com.google.devtools.build.skyframe.MemoizingEvaluator;
59import com.google.devtools.build.skyframe.RecordingDifferencer;
janakr1cde8722017-10-10 03:22:21 +020060import com.google.devtools.build.skyframe.SequencedRecordingDifferencer;
Kristina Chodorow335f0672015-11-16 23:19:13 +000061import com.google.devtools.build.skyframe.SequentialBuildDriver;
62import com.google.devtools.build.skyframe.SkyFunction;
63import com.google.devtools.build.skyframe.SkyFunctionName;
64import com.google.devtools.build.skyframe.SkyKey;
John Cater5b4b7a32017-10-12 16:30:03 +020065import java.util.ArrayList;
Kristina Chodorow335f0672015-11-16 23:19:13 +000066import java.util.HashMap;
John Cater5b4b7a32017-10-12 16:30:03 +020067import java.util.List;
Kristina Chodorow335f0672015-11-16 23:19:13 +000068import java.util.Map;
jhorvitzdd1d8412020-08-01 05:59:14 -070069import java.util.Optional;
Kristina Chodorow335f0672015-11-16 23:19:13 +000070import 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;
kkress1847a012020-06-24 12:30:11 -070087 private static final String IGNORED_PACKAGE_PREFIXES_FILE_PATH_STRING = "config/ignored.txt";
Kristina Chodorow335f0672015-11-16 23:19:13 +000088
John Cater5e9ce942016-10-12 17:23:30 +000089 protected abstract CrossRepositoryLabelViolationStrategy crossRepositoryLabelViolationStrategy();
90
Florian Weikert92b22362015-12-03 10:17:18 +000091 @Before
92 public final void setUp() throws Exception {
John Cater0c0735a2016-11-11 01:52:02 +000093 emptyPackagePath = rootDirectory.getRelative("somewhere/else");
Kristina Chodorow335f0672015-11-16 23:19:13 +000094 scratch.file("parentpackage/BUILD");
95
Ulf Adams015aad92016-07-13 16:49:40 +000096 AnalysisMock analysisMock = AnalysisMock.get();
John Catere0d1d0e2017-11-28 20:47:41 -080097 AtomicReference<PathPackageLocator> pkgLocator =
98 new AtomicReference<>(
99 new PathPackageLocator(
100 outputBase,
tomluee6a6862018-01-17 14:36:26 -0800101 ImmutableList.of(Root.fromPath(emptyPackagePath), Root.fromPath(rootDirectory)),
John Catere0d1d0e2017-11-28 20:47:41 -0800102 BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000103 deletedPackages = new AtomicReference<>(ImmutableSet.<PackageIdentifier>of());
Ulf Adams015aad92016-07-13 16:49:40 +0000104 BlazeDirectories directories =
105 new BlazeDirectories(
Klaus Aehligc2499c42018-02-27 05:47:21 -0800106 new ServerDirectories(rootDirectory, outputBase, rootDirectory),
janakr3b63a4e2017-09-14 09:55:40 +0200107 rootDirectory,
cushon849df362018-05-14 01:51:45 -0700108 /* defaultSystemJavabase= */ null,
janakr3b63a4e2017-09-14 09:55:40 +0200109 analysisMock.getProductName());
nharmata3fb7d342018-02-23 11:37:51 -0800110 ExternalFilesHelper externalFilesHelper = ExternalFilesHelper.createForTesting(
Nathan Harmatad4f75942016-10-18 08:55:17 +0000111 pkgLocator, ExternalFileAction.DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_PATHS, directories);
Kristina Chodorow335f0672015-11-16 23:19:13 +0000112
113 Map<SkyFunctionName, SkyFunction> skyFunctions = new HashMap<>();
John Cater5e9ce942016-10-12 17:23:30 +0000114 skyFunctions.put(
115 SkyFunctions.PACKAGE_LOOKUP,
John Cater0c0735a2016-11-11 01:52:02 +0000116 new PackageLookupFunction(
117 deletedPackages,
118 crossRepositoryLabelViolationStrategy(),
ajurkowski2018b872020-04-14 09:59:00 -0700119 BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY,
120 BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000121 skyFunctions.put(
ajurkowski2018b872020-04-14 09:59:00 -0700122 SkyFunctions.PACKAGE, new PackageFunction(null, null, null, null, null, null, null, null));
shahan602cc852018-06-06 20:09:57 -0700123 skyFunctions.put(
124 FileStateValue.FILE_STATE,
125 new FileStateFunction(
djasper7faa0ef2019-03-28 10:00:00 -0700126 new AtomicReference<TimestampGranularityMonitor>(),
Googler166c3a32019-11-05 13:25:10 -0800127 new AtomicReference<>(UnixGlob.DEFAULT_SYSCALLS),
djasper7faa0ef2019-03-28 10:00:00 -0700128 externalFilesHelper));
shahan602cc852018-06-06 20:09:57 -0700129 skyFunctions.put(FileValue.FILE, new FileFunction(pkgLocator));
John Caterb4f461e2016-10-25 16:16:35 +0000130 skyFunctions.put(SkyFunctions.DIRECTORY_LISTING, new DirectoryListingFunction());
131 skyFunctions.put(
132 SkyFunctions.DIRECTORY_LISTING_STATE,
djasperb109a5f2019-02-14 01:05:34 -0800133 new DirectoryListingStateFunction(
134 externalFilesHelper, new AtomicReference<>(UnixGlob.DEFAULT_SYSCALLS)));
nharmataa32ac672020-01-08 12:54:25 -0800135 skyFunctions.put(
kkress1847a012020-06-24 12:30:11 -0700136 SkyFunctions.IGNORED_PACKAGE_PREFIXES,
137 new IgnoredPackagePrefixesFunction(
138 PathFragment.create(IGNORED_PACKAGE_PREFIXES_FILE_PATH_STRING)));
Ulf Adams015aad92016-07-13 16:49:40 +0000139 RuleClassProvider ruleClassProvider = analysisMock.createRuleClassProvider();
140 skyFunctions.put(SkyFunctions.WORKSPACE_AST, new WorkspaceASTFunction(ruleClassProvider));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000141 skyFunctions.put(
janakr15e15c22019-01-30 11:24:49 -0800142 WorkspaceFileValue.WORKSPACE_FILE,
Kristina Chodorow335f0672015-11-16 23:19:13 +0000143 new WorkspaceFileFunction(
144 ruleClassProvider,
Ulf Adams015aad92016-07-13 16:49:40 +0000145 analysisMock
janakr52d05e82017-09-22 13:27:14 -0400146 .getPackageFactoryBuilderForTesting(directories)
nharmatafde0bd2f2018-12-21 10:17:56 -0800147 .build(ruleClassProvider, fileSystem),
mjhalupkaf0e48112019-01-14 13:01:56 -0800148 directories,
brandjon771a0292020-05-26 12:04:16 -0700149 /*bzlLoadFunctionForInlining=*/ null));
ajurkowski2018b872020-04-14 09:59:00 -0700150 skyFunctions.put(
151 SkyFunctions.EXTERNAL_PACKAGE,
152 new ExternalPackageFunction(BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER));
153 skyFunctions.put(
154 SkyFunctions.LOCAL_REPOSITORY_LOOKUP,
155 new LocalRepositoryLookupFunction(BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER));
John Caterb4f461e2016-10-25 16:16:35 +0000156 skyFunctions.put(
157 SkyFunctions.FILE_SYMLINK_CYCLE_UNIQUENESS, new FileSymlinkCycleUniquenessFunction());
158
159 ImmutableMap<String, RepositoryFunction> repositoryHandlers =
John Caterb25a0342016-10-26 10:17:40 +0000160 ImmutableMap.of(
161 LocalRepositoryRule.NAME, (RepositoryFunction) new LocalRepositoryFunction());
John Caterb4f461e2016-10-25 16:16:35 +0000162 skyFunctions.put(
163 SkyFunctions.REPOSITORY_DIRECTORY,
janakrca6b46d2017-09-22 14:07:15 -0400164 new RepositoryDelegatorFunction(
Googler22913422019-05-02 12:42:20 -0700165 repositoryHandlers,
166 null,
167 new AtomicBoolean(true),
168 ImmutableMap::of,
169 directories,
ajurkowski2018b872020-04-14 09:59:00 -0700170 ManagedDirectoriesKnowledge.NO_MANAGED_DIRECTORIES,
171 BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER));
John Caterb4f461e2016-10-25 16:16:35 +0000172 skyFunctions.put(SkyFunctions.REPOSITORY, new RepositoryLoaderFunction());
173
janakr1cde8722017-10-10 03:22:21 +0200174 differencer = new SequencedRecordingDifferencer();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000175 evaluator = new InMemoryMemoizingEvaluator(skyFunctions, differencer);
176 driver = new SequentialBuildDriver(evaluator);
177 PrecomputedValue.BUILD_ID.set(differencer, UUID.randomUUID());
brandjondbff8b82017-10-23 19:02:31 +0200178 PrecomputedValue.PATH_PACKAGE_LOCATOR.set(differencer, pkgLocator.get());
adonovanb85d0b72020-05-08 11:59:19 -0700179 PrecomputedValue.STARLARK_SEMANTICS.set(differencer, StarlarkSemantics.DEFAULT);
kchodorowdfcd5da82017-04-19 18:58:50 +0200180 RepositoryDelegatorFunction.REPOSITORY_OVERRIDES.set(
181 differencer, ImmutableMap.<RepositoryName, PathFragment>of());
Klaus Aehlig93fe20c2018-06-14 05:48:46 -0700182 RepositoryDelegatorFunction.DEPENDENCY_FOR_UNCONDITIONAL_FETCHING.set(
183 differencer, RepositoryDelegatorFunction.DONT_FETCH_UNCONDITIONALLY);
Klaus Aehlig8eb47482018-09-17 09:14:58 -0700184 RepositoryDelegatorFunction.RESOLVED_FILE_INSTEAD_OF_WORKSPACE.set(
jhorvitzdd1d8412020-08-01 05:59:14 -0700185 differencer, Optional.empty());
Kristina Chodorow335f0672015-11-16 23:19:13 +0000186 }
187
John Caterb4f461e2016-10-25 16:16:35 +0000188 protected PackageLookupValue lookupPackage(String packageName) throws InterruptedException {
Brian Silvermand7d6d622016-03-17 09:53:39 +0000189 return lookupPackage(PackageIdentifier.createInMainRepo(packageName));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000190 }
191
John Caterb4f461e2016-10-25 16:16:35 +0000192 protected PackageLookupValue lookupPackage(PackageIdentifier packageId)
Kristina Chodorow335f0672015-11-16 23:19:13 +0000193 throws InterruptedException {
194 SkyKey key = PackageLookupValue.key(packageId);
John Caterb4f461e2016-10-25 16:16:35 +0000195 return lookupPackage(key).get(key);
196 }
197
198 protected EvaluationResult<PackageLookupValue> lookupPackage(SkyKey packageIdentifierSkyKey)
199 throws InterruptedException {
Googler10028672018-10-25 12:14:34 -0700200 EvaluationContext evaluationContext =
201 EvaluationContext.newBuilder()
202 .setKeepGoing(false)
203 .setNumThreads(SkyframeExecutor.DEFAULT_THREAD_COUNT)
michajlo7a485be2020-07-30 11:08:46 -0700204 .setEventHandler(NullEventHandler.INSTANCE)
Googler10028672018-10-25 12:14:34 -0700205 .build();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000206 return driver.<PackageLookupValue>evaluate(
Googler10028672018-10-25 12:14:34 -0700207 ImmutableList.of(packageIdentifierSkyKey), evaluationContext);
Kristina Chodorow335f0672015-11-16 23:19:13 +0000208 }
209
Florian Weikert92b22362015-12-03 10:17:18 +0000210 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000211 public void testNoBuildFile() throws Exception {
212 scratch.file("parentpackage/nobuildfile/foo.txt");
213 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/nobuildfile");
lberkiaea56b32017-05-30 12:35:33 +0200214 assertThat(packageLookupValue.packageExists()).isFalse();
215 assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.NO_BUILD_FILE);
216 assertThat(packageLookupValue.getErrorMsg()).isNotNull();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000217 }
218
Florian Weikert92b22362015-12-03 10:17:18 +0000219 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000220 public void testNoBuildFileAndNoParentPackage() throws Exception {
221 scratch.file("noparentpackage/foo.txt");
222 PackageLookupValue packageLookupValue = lookupPackage("noparentpackage");
lberkiaea56b32017-05-30 12:35:33 +0200223 assertThat(packageLookupValue.packageExists()).isFalse();
224 assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.NO_BUILD_FILE);
225 assertThat(packageLookupValue.getErrorMsg()).isNotNull();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000226 }
227
Florian Weikert92b22362015-12-03 10:17:18 +0000228 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000229 public void testDeletedPackage() throws Exception {
230 scratch.file("parentpackage/deletedpackage/BUILD");
231 deletedPackages.set(ImmutableSet.of(
Brian Silvermand7d6d622016-03-17 09:53:39 +0000232 PackageIdentifier.createInMainRepo("parentpackage/deletedpackage")));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000233 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/deletedpackage");
lberkiaea56b32017-05-30 12:35:33 +0200234 assertThat(packageLookupValue.packageExists()).isFalse();
235 assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.DELETED_PACKAGE);
236 assertThat(packageLookupValue.getErrorMsg()).isNotNull();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000237 }
238
Florian Weikert92b22362015-12-03 10:17:18 +0000239 @Test
kkress1847a012020-06-24 12:30:11 -0700240 public void testIgnoredPackage() throws Exception {
241 scratch.file("ignored/subdir/BUILD");
242 scratch.file("ignored/BUILD");
243 Path ignored = scratch.overwriteFile(IGNORED_PACKAGE_PREFIXES_FILE_PATH_STRING, "ignored");
Kristina Chodorow335f0672015-11-16 23:19:13 +0000244
kkress1847a012020-06-24 12:30:11 -0700245 ImmutableSet<String> pkgs = ImmutableSet.of("ignored/subdir", "ignored");
Kristina Chodorow335f0672015-11-16 23:19:13 +0000246 for (String pkg : pkgs) {
247 PackageLookupValue packageLookupValue = lookupPackage(pkg);
lberkiaea56b32017-05-30 12:35:33 +0200248 assertThat(packageLookupValue.packageExists()).isFalse();
249 assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.DELETED_PACKAGE);
250 assertThat(packageLookupValue.getErrorMsg()).isNotNull();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000251 }
252
kkress1847a012020-06-24 12:30:11 -0700253 scratch.overwriteFile(IGNORED_PACKAGE_PREFIXES_FILE_PATH_STRING, "not_ignored");
254 RootedPath rootedIgnoreFile =
tomluee6a6862018-01-17 14:36:26 -0800255 RootedPath.toRootedPath(
kkress1847a012020-06-24 12:30:11 -0700256 Root.fromPath(ignored.getParentDirectory().getParentDirectory()),
257 PathFragment.create("config/ignored.txt"));
258 differencer.invalidate(ImmutableSet.of(FileStateValue.key(rootedIgnoreFile)));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000259 for (String pkg : pkgs) {
260 PackageLookupValue packageLookupValue = lookupPackage(pkg);
lberkiaea56b32017-05-30 12:35:33 +0200261 assertThat(packageLookupValue.packageExists()).isTrue();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000262 }
263 }
264
Florian Weikert92b22362015-12-03 10:17:18 +0000265 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000266 public void testInvalidPackageName() throws Exception {
twerthc4f2d802018-05-15 05:02:31 -0700267 scratch.file("parentpackage/invalidpackagename:42/BUILD");
268 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/invalidpackagename:42");
lberkiaea56b32017-05-30 12:35:33 +0200269 assertThat(packageLookupValue.packageExists()).isFalse();
270 assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.INVALID_PACKAGE_NAME);
271 assertThat(packageLookupValue.getErrorMsg()).isNotNull();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000272 }
273
Florian Weikert92b22362015-12-03 10:17:18 +0000274 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000275 public void testDirectoryNamedBuild() throws Exception {
276 scratch.dir("parentpackage/isdirectory/BUILD");
277 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/isdirectory");
lberkiaea56b32017-05-30 12:35:33 +0200278 assertThat(packageLookupValue.packageExists()).isFalse();
279 assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.NO_BUILD_FILE);
280 assertThat(packageLookupValue.getErrorMsg()).isNotNull();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000281 }
282
Florian Weikert92b22362015-12-03 10:17:18 +0000283 @Test
John Cater0c0735a2016-11-11 01:52:02 +0000284 public void testEverythingIsGood_BUILD() throws Exception {
Kristina Chodorow335f0672015-11-16 23:19:13 +0000285 scratch.file("parentpackage/everythinggood/BUILD");
286 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/everythinggood");
lberkiaea56b32017-05-30 12:35:33 +0200287 assertThat(packageLookupValue.packageExists()).isTrue();
tomluee6a6862018-01-17 14:36:26 -0800288 assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory));
lberkiaea56b32017-05-30 12:35:33 +0200289 assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD);
Kristina Chodorow335f0672015-11-16 23:19:13 +0000290 }
291
Florian Weikert92b22362015-12-03 10:17:18 +0000292 @Test
John Cater0c0735a2016-11-11 01:52:02 +0000293 public void testEverythingIsGood_BUILD_bazel() throws Exception {
294 scratch.file("parentpackage/everythinggood/BUILD.bazel");
295 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/everythinggood");
lberkiaea56b32017-05-30 12:35:33 +0200296 assertThat(packageLookupValue.packageExists()).isTrue();
tomluee6a6862018-01-17 14:36:26 -0800297 assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory));
lberkiaea56b32017-05-30 12:35:33 +0200298 assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD_DOT_BAZEL);
John Cater0c0735a2016-11-11 01:52:02 +0000299 }
300
301 @Test
302 public void testEverythingIsGood_both() throws Exception {
303 scratch.file("parentpackage/everythinggood/BUILD");
304 scratch.file("parentpackage/everythinggood/BUILD.bazel");
305 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/everythinggood");
lberkiaea56b32017-05-30 12:35:33 +0200306 assertThat(packageLookupValue.packageExists()).isTrue();
tomluee6a6862018-01-17 14:36:26 -0800307 assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory));
lberkiaea56b32017-05-30 12:35:33 +0200308 assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD_DOT_BAZEL);
John Cater0c0735a2016-11-11 01:52:02 +0000309 }
310
311 @Test
312 public void testBuildFilesInMultiplePackagePaths() throws Exception {
313 scratch.file(emptyPackagePath.getPathString() + "/foo/BUILD");
314 scratch.file("foo/BUILD.bazel");
315
316 // BUILD file in the first package path should be preferred to BUILD.bazel in the second.
317 PackageLookupValue packageLookupValue = lookupPackage("foo");
lberkiaea56b32017-05-30 12:35:33 +0200318 assertThat(packageLookupValue.packageExists()).isTrue();
tomluee6a6862018-01-17 14:36:26 -0800319 assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(emptyPackagePath));
lberkiaea56b32017-05-30 12:35:33 +0200320 assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD);
John Cater0c0735a2016-11-11 01:52:02 +0000321 }
322
323 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000324 public void testEmptyPackageName() throws Exception {
325 scratch.file("BUILD");
326 PackageLookupValue packageLookupValue = lookupPackage("");
lberkiaea56b32017-05-30 12:35:33 +0200327 assertThat(packageLookupValue.packageExists()).isTrue();
tomluee6a6862018-01-17 14:36:26 -0800328 assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory));
lberkiaea56b32017-05-30 12:35:33 +0200329 assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD);
Kristina Chodorow335f0672015-11-16 23:19:13 +0000330 }
331
Florian Weikert92b22362015-12-03 10:17:18 +0000332 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000333 public void testWorkspaceLookup() throws Exception {
334 scratch.overwriteFile("WORKSPACE");
Brian Silvermand7d6d622016-03-17 09:53:39 +0000335 PackageLookupValue packageLookupValue = lookupPackage(
336 PackageIdentifier.createInMainRepo("external"));
lberkiaea56b32017-05-30 12:35:33 +0200337 assertThat(packageLookupValue.packageExists()).isTrue();
tomluee6a6862018-01-17 14:36:26 -0800338 assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000339 }
John Cater94695912016-08-03 12:09:39 +0000340
Florian Weikert92b22362015-12-03 10:17:18 +0000341 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000342 public void testPackageLookupValueHashCodeAndEqualsContract() throws Exception {
tomluee6a6862018-01-17 14:36:26 -0800343 Root root1 = Root.fromPath(rootDirectory.getRelative("root1"));
344 Root root2 = Root.fromPath(rootDirectory.getRelative("root2"));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000345 // Our (seeming) duplication of parameters here is intentional. Some of the subclasses of
346 // PackageLookupValue are supposed to have reference equality semantics, and some are supposed
347 // to have logical equality semantics.
348 new EqualsTester()
John Cater94695912016-08-03 12:09:39 +0000349 .addEqualityGroup(
350 PackageLookupValue.success(root1, BuildFileName.BUILD),
351 PackageLookupValue.success(root1, BuildFileName.BUILD))
352 .addEqualityGroup(
353 PackageLookupValue.success(root2, BuildFileName.BUILD),
354 PackageLookupValue.success(root2, BuildFileName.BUILD))
Kristina Chodorow335f0672015-11-16 23:19:13 +0000355 .addEqualityGroup(
janakr677ba952019-03-07 09:50:01 -0800356 PackageLookupValue.NO_BUILD_FILE_VALUE, PackageLookupValue.NO_BUILD_FILE_VALUE)
357 .addEqualityGroup(
Kristina Chodorow335f0672015-11-16 23:19:13 +0000358 PackageLookupValue.DELETED_PACKAGE_VALUE, PackageLookupValue.DELETED_PACKAGE_VALUE)
John Cater94695912016-08-03 12:09:39 +0000359 .addEqualityGroup(
360 PackageLookupValue.invalidPackageName("nope1"),
Kristina Chodorow335f0672015-11-16 23:19:13 +0000361 PackageLookupValue.invalidPackageName("nope1"))
John Cater94695912016-08-03 12:09:39 +0000362 .addEqualityGroup(
363 PackageLookupValue.invalidPackageName("nope2"),
364 PackageLookupValue.invalidPackageName("nope2"))
Kristina Chodorow335f0672015-11-16 23:19:13 +0000365 .testEquals();
366 }
John Cater5e9ce942016-10-12 17:23:30 +0000367
John Caterb4f461e2016-10-25 16:16:35 +0000368 protected void createAndCheckInvalidPackageLabel(boolean expectedPackageExists) throws Exception {
369 scratch.overwriteFile("WORKSPACE", "local_repository(name='local', path='local/repo')");
370 scratch.file("local/repo/WORKSPACE");
371 scratch.file("local/repo/BUILD");
372
373 // First, use the correct label.
374 PackageLookupValue packageLookupValue =
375 lookupPackage(PackageIdentifier.create("@local", PathFragment.EMPTY_FRAGMENT));
lberkiaea56b32017-05-30 12:35:33 +0200376 assertThat(packageLookupValue.packageExists()).isTrue();
John Caterb4f461e2016-10-25 16:16:35 +0000377
378 // Then, use the incorrect label.
379 packageLookupValue = lookupPackage(PackageIdentifier.createInMainRepo("local/repo"));
lberkiaea56b32017-05-30 12:35:33 +0200380 assertThat(packageLookupValue.packageExists()).isEqualTo(expectedPackageExists);
John Caterb4f461e2016-10-25 16:16:35 +0000381 }
382
John Cater5e9ce942016-10-12 17:23:30 +0000383 /**
John Caterb4f461e2016-10-25 16:16:35 +0000384 * Runs all tests in the base {@link PackageLookupFunctionTest} class with the {@link
385 * CrossRepositoryLabelViolationStrategy#IGNORE} enum set, and also additional tests specific to
386 * that setting.
John Cater5e9ce942016-10-12 17:23:30 +0000387 */
388 @RunWith(JUnit4.class)
John Caterb4f461e2016-10-25 16:16:35 +0000389 public static class IgnoreLabelViolationsTest extends PackageLookupFunctionTest {
John Cater5e9ce942016-10-12 17:23:30 +0000390 @Override
391 protected CrossRepositoryLabelViolationStrategy crossRepositoryLabelViolationStrategy() {
392 return CrossRepositoryLabelViolationStrategy.IGNORE;
393 }
394
395 // Add any ignore-specific tests here.
John Caterb4f461e2016-10-25 16:16:35 +0000396
397 @Test
398 public void testInvalidPackageLabelIsIgnored() throws Exception {
399 createAndCheckInvalidPackageLabel(true);
400 }
John Cater5e9ce942016-10-12 17:23:30 +0000401 }
402
403 /**
John Caterb4f461e2016-10-25 16:16:35 +0000404 * Runs all tests in the base {@link PackageLookupFunctionTest} class with the {@link
405 * CrossRepositoryLabelViolationStrategy#ERROR} enum set, and also additional tests specific to
406 * that setting.
John Cater5e9ce942016-10-12 17:23:30 +0000407 */
408 @RunWith(JUnit4.class)
John Caterb4f461e2016-10-25 16:16:35 +0000409 public static class ErrorLabelViolationsTest extends PackageLookupFunctionTest {
John Cater5e9ce942016-10-12 17:23:30 +0000410 @Override
411 protected CrossRepositoryLabelViolationStrategy crossRepositoryLabelViolationStrategy() {
412 return CrossRepositoryLabelViolationStrategy.ERROR;
413 }
414
415 // Add any error-specific tests here.
John Caterb4f461e2016-10-25 16:16:35 +0000416
417 @Test
418 public void testInvalidPackageLabelIsError() throws Exception {
419 createAndCheckInvalidPackageLabel(false);
420 }
421
422 @Test
423 public void testSymlinkCycleInWorkspace() throws Exception {
424 scratch.overwriteFile("WORKSPACE", "local_repository(name='local', path='local/repo')");
425 Path localRepoWorkspace = scratch.resolve("local/repo/WORKSPACE");
426 Path localRepoWorkspaceLink = scratch.resolve("local/repo/WORKSPACE.link");
427 FileSystemUtils.createDirectoryAndParents(localRepoWorkspace.getParentDirectory());
428 FileSystemUtils.createDirectoryAndParents(localRepoWorkspaceLink.getParentDirectory());
429 localRepoWorkspace.createSymbolicLink(localRepoWorkspaceLink);
430 localRepoWorkspaceLink.createSymbolicLink(localRepoWorkspace);
431 scratch.file("local/repo/BUILD");
432
433 SkyKey skyKey = PackageLookupValue.key(PackageIdentifier.createInMainRepo("local/repo"));
434 EvaluationResult<PackageLookupValue> result = lookupPackage(skyKey);
435 assertThatEvaluationResult(result)
436 .hasErrorEntryForKeyThat(skyKey)
437 .hasExceptionThat()
438 .isInstanceOf(BuildFileNotFoundException.class);
439 assertThatEvaluationResult(result)
440 .hasErrorEntryForKeyThat(skyKey)
441 .hasExceptionThat()
diamondm4d8d1d62019-03-25 16:05:02 -0700442 .hasMessageThat()
443 .isEqualTo(
John Caterb4f461e2016-10-25 16:16:35 +0000444 "no such package 'local/repo': Unable to determine the local repository for "
445 + "directory /workspace/local/repo");
446 }
John Cater5e9ce942016-10-12 17:23:30 +0000447 }
John Cater5b4b7a32017-10-12 16:30:03 +0200448
449 /** Tests for detection of invalid package identifiers for local repositories. */
450 @RunWith(Parameterized.class)
451 public static class CorrectedLocalRepositoryTest extends PackageLookupFunctionTest {
452
453 /**
454 * Create parameters for this test. The contents are:
455 *
456 * <ol>
457 * <li>description
458 * <li>repository path
459 * <li>package path - under the repository
460 * <li>expected corrected package identifier
461 * </ol>
462 */
463 @Parameters(name = "{0}")
464 public static List<Object[]> parameters() {
465 List<Object[]> params = new ArrayList<>();
466
467 params.add(new String[] {"simpleRepo_emptyPackage", "local", "", "@local//"});
468 params.add(new String[] {"simpleRepo_singlePackage", "local", "package", "@local//package"});
469 params.add(
470 new String[] {
471 "simpleRepo_subPackage", "local", "package/subpackage", "@local//package/subpackage"
472 });
473 params.add(new String[] {"deepRepo_emptyPackage", "local/repo", "", "@local//"});
474 params.add(new String[] {"deepRepo_subPackage", "local/repo", "package", "@local//package"});
475
476 return params;
477 }
478
jcater25fb5b12017-10-13 17:23:44 +0200479 private final String repositoryPath;
480 private final String packagePath;
481 private final String expectedCorrectedPackageIdentifier;
John Cater5b4b7a32017-10-12 16:30:03 +0200482
483 public CorrectedLocalRepositoryTest(
jcater25fb5b12017-10-13 17:23:44 +0200484 String unusedDescription,
John Cater5b4b7a32017-10-12 16:30:03 +0200485 String repositoryPath,
486 String packagePath,
487 String expectedCorrectedPackageIdentifier) {
John Cater5b4b7a32017-10-12 16:30:03 +0200488 this.repositoryPath = repositoryPath;
489 this.packagePath = packagePath;
490 this.expectedCorrectedPackageIdentifier = expectedCorrectedPackageIdentifier;
491 }
492
493 @Override
494 protected CrossRepositoryLabelViolationStrategy crossRepositoryLabelViolationStrategy() {
495 return CrossRepositoryLabelViolationStrategy.ERROR;
496 }
497
498 @Test
499 public void testCorrectPackageDetection_relativePath() throws Exception {
John Cater5b4b7a32017-10-12 16:30:03 +0200500 scratch.overwriteFile(
501 "WORKSPACE", "local_repository(name='local', path='" + repositoryPath + "')");
502 scratch.file(PathFragment.create(repositoryPath).getRelative("WORKSPACE").getPathString());
503 scratch.file(
504 PathFragment.create(repositoryPath)
505 .getRelative(packagePath)
506 .getRelative("BUILD")
507 .getPathString());
508
509 PackageIdentifier packageIdentifier =
510 PackageIdentifier.createInMainRepo(
511 PathFragment.create(repositoryPath).getRelative(packagePath));
512 PackageLookupValue packageLookupValue = lookupPackage(packageIdentifier);
513 assertThat(packageLookupValue.packageExists()).isFalse();
514 assertThat(packageLookupValue)
515 .isInstanceOf(IncorrectRepositoryReferencePackageLookupValue.class);
516
517 IncorrectRepositoryReferencePackageLookupValue incorrectPackageLookupValue =
518 (IncorrectRepositoryReferencePackageLookupValue) packageLookupValue;
519 assertThat(incorrectPackageLookupValue.getInvalidPackageIdentifier())
520 .isEqualTo(packageIdentifier);
521 assertThat(incorrectPackageLookupValue.getCorrectedPackageIdentifier().toString())
522 .isEqualTo(expectedCorrectedPackageIdentifier);
523 }
524
525 @Test
526 public void testCorrectPackageDetection_absolutePath() throws Exception {
John Cater5b4b7a32017-10-12 16:30:03 +0200527 scratch.overwriteFile(
528 "WORKSPACE",
529 "local_repository(name='local', path=__workspace_dir__ + '/" + repositoryPath + "')");
530 scratch.file(PathFragment.create(repositoryPath).getRelative("WORKSPACE").getPathString());
531 scratch.file(
532 PathFragment.create(repositoryPath)
533 .getRelative(packagePath)
534 .getRelative("BUILD")
535 .getPathString());
536
537 PackageIdentifier packageIdentifier =
538 PackageIdentifier.createInMainRepo(
539 PathFragment.create(repositoryPath).getRelative(packagePath));
540 PackageLookupValue packageLookupValue = lookupPackage(packageIdentifier);
541 assertThat(packageLookupValue.packageExists()).isFalse();
542 assertThat(packageLookupValue)
543 .isInstanceOf(IncorrectRepositoryReferencePackageLookupValue.class);
544
545 IncorrectRepositoryReferencePackageLookupValue incorrectPackageLookupValue =
546 (IncorrectRepositoryReferencePackageLookupValue) packageLookupValue;
547 assertThat(incorrectPackageLookupValue.getInvalidPackageIdentifier())
548 .isEqualTo(packageIdentifier);
549 assertThat(incorrectPackageLookupValue.getCorrectedPackageIdentifier().toString())
550 .isEqualTo(expectedCorrectedPackageIdentifier);
551 }
552 }
Kristina Chodorow335f0672015-11-16 23:19:13 +0000553}