blob: d774450087430acfc2c40910c24f56a2010fd09b [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;
Ulf Adamsed5be452016-11-02 16:33:57 +000035import com.google.devtools.build.lib.packages.PackageFactory;
nharmatad922e652017-05-17 20:29:19 +020036import com.google.devtools.build.lib.packages.PackageFactory.EnvironmentExtension;
Kristina Chodorow335f0672015-11-16 23:19:13 +000037import com.google.devtools.build.lib.packages.RuleClassProvider;
janakr15e15c22019-01-30 11:24:49 -080038import com.google.devtools.build.lib.packages.WorkspaceFileValue;
Kristina Chodorow335f0672015-11-16 23:19:13 +000039import com.google.devtools.build.lib.pkgcache.PathPackageLocator;
John Caterb4f461e2016-10-25 16:16:35 +000040import com.google.devtools.build.lib.rules.repository.LocalRepositoryFunction;
41import com.google.devtools.build.lib.rules.repository.LocalRepositoryRule;
42import com.google.devtools.build.lib.rules.repository.RepositoryDelegatorFunction;
43import com.google.devtools.build.lib.rules.repository.RepositoryFunction;
44import com.google.devtools.build.lib.rules.repository.RepositoryLoaderFunction;
Nathan Harmatad4f75942016-10-18 08:55:17 +000045import com.google.devtools.build.lib.skyframe.ExternalFilesHelper.ExternalFileAction;
John Cater5e9ce942016-10-12 17:23:30 +000046import com.google.devtools.build.lib.skyframe.PackageLookupFunction.CrossRepositoryLabelViolationStrategy;
Kristina Chodorow335f0672015-11-16 23:19:13 +000047import com.google.devtools.build.lib.skyframe.PackageLookupValue.ErrorReason;
John Cater4a12e662017-09-06 17:51:18 +020048import com.google.devtools.build.lib.skyframe.PackageLookupValue.IncorrectRepositoryReferencePackageLookupValue;
laurentlb6659b4c2019-02-18 07:23:36 -080049import com.google.devtools.build.lib.syntax.StarlarkSemantics;
Florian Weikertcca703a2015-12-07 09:56:38 +000050import com.google.devtools.build.lib.testutil.FoundationTestCase;
Kristina Chodorow335f0672015-11-16 23:19:13 +000051import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor;
John Caterb4f461e2016-10-25 16:16:35 +000052import com.google.devtools.build.lib.vfs.FileSystemUtils;
Kristina Chodorow335f0672015-11-16 23:19:13 +000053import com.google.devtools.build.lib.vfs.Path;
54import com.google.devtools.build.lib.vfs.PathFragment;
tomluee6a6862018-01-17 14:36:26 -080055import com.google.devtools.build.lib.vfs.Root;
Kristina Chodorow335f0672015-11-16 23:19:13 +000056import com.google.devtools.build.lib.vfs.RootedPath;
djasperb109a5f2019-02-14 01:05:34 -080057import com.google.devtools.build.lib.vfs.UnixGlob;
Googler10028672018-10-25 12:14:34 -070058import com.google.devtools.build.skyframe.EvaluationContext;
John Caterb4f461e2016-10-25 16:16:35 +000059import com.google.devtools.build.skyframe.EvaluationResult;
Kristina Chodorow335f0672015-11-16 23:19:13 +000060import com.google.devtools.build.skyframe.InMemoryMemoizingEvaluator;
61import com.google.devtools.build.skyframe.MemoizingEvaluator;
62import com.google.devtools.build.skyframe.RecordingDifferencer;
janakr1cde8722017-10-10 03:22:21 +020063import com.google.devtools.build.skyframe.SequencedRecordingDifferencer;
Kristina Chodorow335f0672015-11-16 23:19:13 +000064import com.google.devtools.build.skyframe.SequentialBuildDriver;
65import com.google.devtools.build.skyframe.SkyFunction;
66import com.google.devtools.build.skyframe.SkyFunctionName;
67import com.google.devtools.build.skyframe.SkyKey;
John Cater5b4b7a32017-10-12 16:30:03 +020068import java.util.ArrayList;
Kristina Chodorow335f0672015-11-16 23:19:13 +000069import java.util.HashMap;
John Cater5b4b7a32017-10-12 16:30:03 +020070import java.util.List;
Kristina Chodorow335f0672015-11-16 23:19:13 +000071import java.util.Map;
72import java.util.UUID;
John Caterb4f461e2016-10-25 16:16:35 +000073import java.util.concurrent.atomic.AtomicBoolean;
Kristina Chodorow335f0672015-11-16 23:19:13 +000074import java.util.concurrent.atomic.AtomicReference;
John Cater94695912016-08-03 12:09:39 +000075import org.junit.Before;
76import org.junit.Test;
77import org.junit.runner.RunWith;
78import org.junit.runners.JUnit4;
John Cater5b4b7a32017-10-12 16:30:03 +020079import org.junit.runners.Parameterized;
80import org.junit.runners.Parameterized.Parameters;
Kristina Chodorow335f0672015-11-16 23:19:13 +000081
John Cater5e9ce942016-10-12 17:23:30 +000082/** Tests for {@link PackageLookupFunction}. */
83public abstract class PackageLookupFunctionTest extends FoundationTestCase {
Kristina Chodorow335f0672015-11-16 23:19:13 +000084 private AtomicReference<ImmutableSet<PackageIdentifier>> deletedPackages;
85 private MemoizingEvaluator evaluator;
86 private SequentialBuildDriver driver;
87 private RecordingDifferencer differencer;
John Cater0c0735a2016-11-11 01:52:02 +000088 private Path emptyPackagePath;
nharmatae4eb23f2017-12-05 09:27:45 -080089 private static final String ADDITIONAL_BLACKLISTED_PACKAGE_PREFIXES_FILE_PATH_STRING =
90 "config/blacklisted.txt";
Kristina Chodorow335f0672015-11-16 23:19:13 +000091
John Cater5e9ce942016-10-12 17:23:30 +000092 protected abstract CrossRepositoryLabelViolationStrategy crossRepositoryLabelViolationStrategy();
93
Florian Weikert92b22362015-12-03 10:17:18 +000094 @Before
95 public final void setUp() throws Exception {
John Cater0c0735a2016-11-11 01:52:02 +000096 emptyPackagePath = rootDirectory.getRelative("somewhere/else");
Kristina Chodorow335f0672015-11-16 23:19:13 +000097 scratch.file("parentpackage/BUILD");
98
Ulf Adams015aad92016-07-13 16:49:40 +000099 AnalysisMock analysisMock = AnalysisMock.get();
John Catere0d1d0e2017-11-28 20:47:41 -0800100 AtomicReference<PathPackageLocator> pkgLocator =
101 new AtomicReference<>(
102 new PathPackageLocator(
103 outputBase,
tomluee6a6862018-01-17 14:36:26 -0800104 ImmutableList.of(Root.fromPath(emptyPackagePath), Root.fromPath(rootDirectory)),
John Catere0d1d0e2017-11-28 20:47:41 -0800105 BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000106 deletedPackages = new AtomicReference<>(ImmutableSet.<PackageIdentifier>of());
Ulf Adams015aad92016-07-13 16:49:40 +0000107 BlazeDirectories directories =
108 new BlazeDirectories(
Klaus Aehligc2499c42018-02-27 05:47:21 -0800109 new ServerDirectories(rootDirectory, outputBase, rootDirectory),
janakr3b63a4e2017-09-14 09:55:40 +0200110 rootDirectory,
cushon849df362018-05-14 01:51:45 -0700111 /* defaultSystemJavabase= */ null,
janakr3b63a4e2017-09-14 09:55:40 +0200112 analysisMock.getProductName());
nharmata3fb7d342018-02-23 11:37:51 -0800113 ExternalFilesHelper externalFilesHelper = ExternalFilesHelper.createForTesting(
Nathan Harmatad4f75942016-10-18 08:55:17 +0000114 pkgLocator, ExternalFileAction.DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_PATHS, directories);
Kristina Chodorow335f0672015-11-16 23:19:13 +0000115
116 Map<SkyFunctionName, SkyFunction> skyFunctions = new HashMap<>();
John Cater5e9ce942016-10-12 17:23:30 +0000117 skyFunctions.put(
118 SkyFunctions.PACKAGE_LOOKUP,
John Cater0c0735a2016-11-11 01:52:02 +0000119 new PackageLookupFunction(
120 deletedPackages,
121 crossRepositoryLabelViolationStrategy(),
John Catere0d1d0e2017-11-28 20:47:41 -0800122 BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000123 skyFunctions.put(
124 SkyFunctions.PACKAGE,
125 new PackageFunction(null, null, null, null, null, null, null));
shahan602cc852018-06-06 20:09:57 -0700126 skyFunctions.put(
127 FileStateValue.FILE_STATE,
128 new FileStateFunction(
129 new AtomicReference<TimestampGranularityMonitor>(), externalFilesHelper));
130 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)));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000136 skyFunctions.put(SkyFunctions.BLACKLISTED_PACKAGE_PREFIXES,
nharmatae4eb23f2017-12-05 09:27:45 -0800137 new BlacklistedPackagePrefixesFunction(
138 /*hardcodedBlacklistedPackagePrefixes=*/ ImmutableSet.of(),
139 PathFragment.create(ADDITIONAL_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)
nharmatad922e652017-05-17 20:29:19 +0200148 .setEnvironmentExtensions(
149 ImmutableList.<EnvironmentExtension>of(
150 new PackageFactory.EmptyEnvironmentExtension()))
nharmatafde0bd2f2018-12-21 10:17:56 -0800151 .build(ruleClassProvider, fileSystem),
mjhalupkaf0e48112019-01-14 13:01:56 -0800152 directories,
153 /*skylarkImportLookupFunctionForInlining=*/ null));
Damien Martin-Guillerezbc8b5e02016-02-05 22:09:09 +0000154 skyFunctions.put(SkyFunctions.EXTERNAL_PACKAGE, new ExternalPackageFunction());
John Caterb4f461e2016-10-25 16:16:35 +0000155 skyFunctions.put(SkyFunctions.LOCAL_REPOSITORY_LOOKUP, new LocalRepositoryLookupFunction());
156 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(
165 repositoryHandlers, null, new AtomicBoolean(true), ImmutableMap::of, directories));
John Caterb4f461e2016-10-25 16:16:35 +0000166 skyFunctions.put(SkyFunctions.REPOSITORY, new RepositoryLoaderFunction());
167
janakr1cde8722017-10-10 03:22:21 +0200168 differencer = new SequencedRecordingDifferencer();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000169 evaluator = new InMemoryMemoizingEvaluator(skyFunctions, differencer);
170 driver = new SequentialBuildDriver(evaluator);
171 PrecomputedValue.BUILD_ID.set(differencer, UUID.randomUUID());
brandjondbff8b82017-10-23 19:02:31 +0200172 PrecomputedValue.PATH_PACKAGE_LOCATOR.set(differencer, pkgLocator.get());
laurentlb8c02aff2019-02-18 10:53:34 -0800173 PrecomputedValue.STARLARK_SEMANTICS.set(differencer, StarlarkSemantics.DEFAULT_SEMANTICS);
kchodorowdfcd5da82017-04-19 18:58:50 +0200174 RepositoryDelegatorFunction.REPOSITORY_OVERRIDES.set(
175 differencer, ImmutableMap.<RepositoryName, PathFragment>of());
Klaus Aehlig93fe20c2018-06-14 05:48:46 -0700176 RepositoryDelegatorFunction.DEPENDENCY_FOR_UNCONDITIONAL_FETCHING.set(
177 differencer, RepositoryDelegatorFunction.DONT_FETCH_UNCONDITIONALLY);
Klaus Aehlig8eb47482018-09-17 09:14:58 -0700178 RepositoryDelegatorFunction.RESOLVED_FILE_INSTEAD_OF_WORKSPACE.set(
179 differencer, Optional.<RootedPath>absent());
Kristina Chodorow335f0672015-11-16 23:19:13 +0000180 }
181
John Caterb4f461e2016-10-25 16:16:35 +0000182 protected PackageLookupValue lookupPackage(String packageName) throws InterruptedException {
Brian Silvermand7d6d622016-03-17 09:53:39 +0000183 return lookupPackage(PackageIdentifier.createInMainRepo(packageName));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000184 }
185
John Caterb4f461e2016-10-25 16:16:35 +0000186 protected PackageLookupValue lookupPackage(PackageIdentifier packageId)
Kristina Chodorow335f0672015-11-16 23:19:13 +0000187 throws InterruptedException {
188 SkyKey key = PackageLookupValue.key(packageId);
John Caterb4f461e2016-10-25 16:16:35 +0000189 return lookupPackage(key).get(key);
190 }
191
192 protected EvaluationResult<PackageLookupValue> lookupPackage(SkyKey packageIdentifierSkyKey)
193 throws InterruptedException {
Googler10028672018-10-25 12:14:34 -0700194 EvaluationContext evaluationContext =
195 EvaluationContext.newBuilder()
196 .setKeepGoing(false)
197 .setNumThreads(SkyframeExecutor.DEFAULT_THREAD_COUNT)
198 .setEventHander(NullEventHandler.INSTANCE)
199 .build();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000200 return driver.<PackageLookupValue>evaluate(
Googler10028672018-10-25 12:14:34 -0700201 ImmutableList.of(packageIdentifierSkyKey), evaluationContext);
Kristina Chodorow335f0672015-11-16 23:19:13 +0000202 }
203
Florian Weikert92b22362015-12-03 10:17:18 +0000204 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000205 public void testNoBuildFile() throws Exception {
206 scratch.file("parentpackage/nobuildfile/foo.txt");
207 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/nobuildfile");
lberkiaea56b32017-05-30 12:35:33 +0200208 assertThat(packageLookupValue.packageExists()).isFalse();
209 assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.NO_BUILD_FILE);
210 assertThat(packageLookupValue.getErrorMsg()).isNotNull();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000211 }
212
Florian Weikert92b22362015-12-03 10:17:18 +0000213 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000214 public void testNoBuildFileAndNoParentPackage() throws Exception {
215 scratch.file("noparentpackage/foo.txt");
216 PackageLookupValue packageLookupValue = lookupPackage("noparentpackage");
lberkiaea56b32017-05-30 12:35:33 +0200217 assertThat(packageLookupValue.packageExists()).isFalse();
218 assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.NO_BUILD_FILE);
219 assertThat(packageLookupValue.getErrorMsg()).isNotNull();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000220 }
221
Florian Weikert92b22362015-12-03 10:17:18 +0000222 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000223 public void testDeletedPackage() throws Exception {
224 scratch.file("parentpackage/deletedpackage/BUILD");
225 deletedPackages.set(ImmutableSet.of(
Brian Silvermand7d6d622016-03-17 09:53:39 +0000226 PackageIdentifier.createInMainRepo("parentpackage/deletedpackage")));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000227 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/deletedpackage");
lberkiaea56b32017-05-30 12:35:33 +0200228 assertThat(packageLookupValue.packageExists()).isFalse();
229 assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.DELETED_PACKAGE);
230 assertThat(packageLookupValue.getErrorMsg()).isNotNull();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000231 }
232
Florian Weikert92b22362015-12-03 10:17:18 +0000233 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000234 public void testBlacklistedPackage() throws Exception {
235 scratch.file("blacklisted/subdir/BUILD");
236 scratch.file("blacklisted/BUILD");
nharmatae4eb23f2017-12-05 09:27:45 -0800237 Path blacklist = scratch.overwriteFile(
238 ADDITIONAL_BLACKLISTED_PACKAGE_PREFIXES_FILE_PATH_STRING, "blacklisted");
Kristina Chodorow335f0672015-11-16 23:19:13 +0000239
240 ImmutableSet<String> pkgs = ImmutableSet.of("blacklisted/subdir", "blacklisted");
241 for (String pkg : pkgs) {
242 PackageLookupValue packageLookupValue = lookupPackage(pkg);
lberkiaea56b32017-05-30 12:35:33 +0200243 assertThat(packageLookupValue.packageExists()).isFalse();
244 assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.DELETED_PACKAGE);
245 assertThat(packageLookupValue.getErrorMsg()).isNotNull();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000246 }
247
nharmatae4eb23f2017-12-05 09:27:45 -0800248 scratch.overwriteFile(
249 ADDITIONAL_BLACKLISTED_PACKAGE_PREFIXES_FILE_PATH_STRING, "not_blacklisted");
tomluee6a6862018-01-17 14:36:26 -0800250 RootedPath rootedBlacklist =
251 RootedPath.toRootedPath(
252 Root.fromPath(blacklist.getParentDirectory().getParentDirectory()),
253 PathFragment.create("config/blacklisted.txt"));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000254 differencer.invalidate(ImmutableSet.of(FileStateValue.key(rootedBlacklist)));
255 for (String pkg : pkgs) {
256 PackageLookupValue packageLookupValue = lookupPackage(pkg);
lberkiaea56b32017-05-30 12:35:33 +0200257 assertThat(packageLookupValue.packageExists()).isTrue();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000258 }
259 }
260
Florian Weikert92b22362015-12-03 10:17:18 +0000261 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000262 public void testInvalidPackageName() throws Exception {
twerthc4f2d802018-05-15 05:02:31 -0700263 scratch.file("parentpackage/invalidpackagename:42/BUILD");
264 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/invalidpackagename:42");
lberkiaea56b32017-05-30 12:35:33 +0200265 assertThat(packageLookupValue.packageExists()).isFalse();
266 assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.INVALID_PACKAGE_NAME);
267 assertThat(packageLookupValue.getErrorMsg()).isNotNull();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000268 }
269
Florian Weikert92b22362015-12-03 10:17:18 +0000270 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000271 public void testDirectoryNamedBuild() throws Exception {
272 scratch.dir("parentpackage/isdirectory/BUILD");
273 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/isdirectory");
lberkiaea56b32017-05-30 12:35:33 +0200274 assertThat(packageLookupValue.packageExists()).isFalse();
275 assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.NO_BUILD_FILE);
276 assertThat(packageLookupValue.getErrorMsg()).isNotNull();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000277 }
278
Florian Weikert92b22362015-12-03 10:17:18 +0000279 @Test
John Cater0c0735a2016-11-11 01:52:02 +0000280 public void testEverythingIsGood_BUILD() throws Exception {
Kristina Chodorow335f0672015-11-16 23:19:13 +0000281 scratch.file("parentpackage/everythinggood/BUILD");
282 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/everythinggood");
lberkiaea56b32017-05-30 12:35:33 +0200283 assertThat(packageLookupValue.packageExists()).isTrue();
tomluee6a6862018-01-17 14:36:26 -0800284 assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory));
lberkiaea56b32017-05-30 12:35:33 +0200285 assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD);
Kristina Chodorow335f0672015-11-16 23:19:13 +0000286 }
287
Florian Weikert92b22362015-12-03 10:17:18 +0000288 @Test
John Cater0c0735a2016-11-11 01:52:02 +0000289 public void testEverythingIsGood_BUILD_bazel() throws Exception {
290 scratch.file("parentpackage/everythinggood/BUILD.bazel");
291 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/everythinggood");
lberkiaea56b32017-05-30 12:35:33 +0200292 assertThat(packageLookupValue.packageExists()).isTrue();
tomluee6a6862018-01-17 14:36:26 -0800293 assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory));
lberkiaea56b32017-05-30 12:35:33 +0200294 assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD_DOT_BAZEL);
John Cater0c0735a2016-11-11 01:52:02 +0000295 }
296
297 @Test
298 public void testEverythingIsGood_both() throws Exception {
299 scratch.file("parentpackage/everythinggood/BUILD");
300 scratch.file("parentpackage/everythinggood/BUILD.bazel");
301 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/everythinggood");
lberkiaea56b32017-05-30 12:35:33 +0200302 assertThat(packageLookupValue.packageExists()).isTrue();
tomluee6a6862018-01-17 14:36:26 -0800303 assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory));
lberkiaea56b32017-05-30 12:35:33 +0200304 assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD_DOT_BAZEL);
John Cater0c0735a2016-11-11 01:52:02 +0000305 }
306
307 @Test
308 public void testBuildFilesInMultiplePackagePaths() throws Exception {
309 scratch.file(emptyPackagePath.getPathString() + "/foo/BUILD");
310 scratch.file("foo/BUILD.bazel");
311
312 // BUILD file in the first package path should be preferred to BUILD.bazel in the second.
313 PackageLookupValue packageLookupValue = lookupPackage("foo");
lberkiaea56b32017-05-30 12:35:33 +0200314 assertThat(packageLookupValue.packageExists()).isTrue();
tomluee6a6862018-01-17 14:36:26 -0800315 assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(emptyPackagePath));
lberkiaea56b32017-05-30 12:35:33 +0200316 assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD);
John Cater0c0735a2016-11-11 01:52:02 +0000317 }
318
319 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000320 public void testEmptyPackageName() throws Exception {
321 scratch.file("BUILD");
322 PackageLookupValue packageLookupValue = lookupPackage("");
lberkiaea56b32017-05-30 12:35:33 +0200323 assertThat(packageLookupValue.packageExists()).isTrue();
tomluee6a6862018-01-17 14:36:26 -0800324 assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory));
lberkiaea56b32017-05-30 12:35:33 +0200325 assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD);
Kristina Chodorow335f0672015-11-16 23:19:13 +0000326 }
327
Florian Weikert92b22362015-12-03 10:17:18 +0000328 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000329 public void testWorkspaceLookup() throws Exception {
330 scratch.overwriteFile("WORKSPACE");
Brian Silvermand7d6d622016-03-17 09:53:39 +0000331 PackageLookupValue packageLookupValue = lookupPackage(
332 PackageIdentifier.createInMainRepo("external"));
lberkiaea56b32017-05-30 12:35:33 +0200333 assertThat(packageLookupValue.packageExists()).isTrue();
tomluee6a6862018-01-17 14:36:26 -0800334 assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000335 }
John Cater94695912016-08-03 12:09:39 +0000336
Florian Weikert92b22362015-12-03 10:17:18 +0000337 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000338 public void testPackageLookupValueHashCodeAndEqualsContract() throws Exception {
tomluee6a6862018-01-17 14:36:26 -0800339 Root root1 = Root.fromPath(rootDirectory.getRelative("root1"));
340 Root root2 = Root.fromPath(rootDirectory.getRelative("root2"));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000341 // Our (seeming) duplication of parameters here is intentional. Some of the subclasses of
342 // PackageLookupValue are supposed to have reference equality semantics, and some are supposed
343 // to have logical equality semantics.
344 new EqualsTester()
John Cater94695912016-08-03 12:09:39 +0000345 .addEqualityGroup(
346 PackageLookupValue.success(root1, BuildFileName.BUILD),
347 PackageLookupValue.success(root1, BuildFileName.BUILD))
348 .addEqualityGroup(
349 PackageLookupValue.success(root2, BuildFileName.BUILD),
350 PackageLookupValue.success(root2, BuildFileName.BUILD))
Kristina Chodorow335f0672015-11-16 23:19:13 +0000351 .addEqualityGroup(
Kristina Chodorow335f0672015-11-16 23:19:13 +0000352 PackageLookupValue.DELETED_PACKAGE_VALUE, PackageLookupValue.DELETED_PACKAGE_VALUE)
John Cater94695912016-08-03 12:09:39 +0000353 .addEqualityGroup(
354 PackageLookupValue.invalidPackageName("nope1"),
Kristina Chodorow335f0672015-11-16 23:19:13 +0000355 PackageLookupValue.invalidPackageName("nope1"))
John Cater94695912016-08-03 12:09:39 +0000356 .addEqualityGroup(
357 PackageLookupValue.invalidPackageName("nope2"),
358 PackageLookupValue.invalidPackageName("nope2"))
Kristina Chodorow335f0672015-11-16 23:19:13 +0000359 .testEquals();
360 }
John Cater5e9ce942016-10-12 17:23:30 +0000361
John Caterb4f461e2016-10-25 16:16:35 +0000362 protected void createAndCheckInvalidPackageLabel(boolean expectedPackageExists) throws Exception {
363 scratch.overwriteFile("WORKSPACE", "local_repository(name='local', path='local/repo')");
364 scratch.file("local/repo/WORKSPACE");
365 scratch.file("local/repo/BUILD");
366
367 // First, use the correct label.
368 PackageLookupValue packageLookupValue =
369 lookupPackage(PackageIdentifier.create("@local", PathFragment.EMPTY_FRAGMENT));
lberkiaea56b32017-05-30 12:35:33 +0200370 assertThat(packageLookupValue.packageExists()).isTrue();
John Caterb4f461e2016-10-25 16:16:35 +0000371
372 // Then, use the incorrect label.
373 packageLookupValue = lookupPackage(PackageIdentifier.createInMainRepo("local/repo"));
lberkiaea56b32017-05-30 12:35:33 +0200374 assertThat(packageLookupValue.packageExists()).isEqualTo(expectedPackageExists);
John Caterb4f461e2016-10-25 16:16:35 +0000375 }
376
John Cater5e9ce942016-10-12 17:23:30 +0000377 /**
John Caterb4f461e2016-10-25 16:16:35 +0000378 * Runs all tests in the base {@link PackageLookupFunctionTest} class with the {@link
379 * CrossRepositoryLabelViolationStrategy#IGNORE} enum set, and also additional tests specific to
380 * that setting.
John Cater5e9ce942016-10-12 17:23:30 +0000381 */
382 @RunWith(JUnit4.class)
John Caterb4f461e2016-10-25 16:16:35 +0000383 public static class IgnoreLabelViolationsTest extends PackageLookupFunctionTest {
John Cater5e9ce942016-10-12 17:23:30 +0000384 @Override
385 protected CrossRepositoryLabelViolationStrategy crossRepositoryLabelViolationStrategy() {
386 return CrossRepositoryLabelViolationStrategy.IGNORE;
387 }
388
389 // Add any ignore-specific tests here.
John Caterb4f461e2016-10-25 16:16:35 +0000390
391 @Test
392 public void testInvalidPackageLabelIsIgnored() throws Exception {
393 createAndCheckInvalidPackageLabel(true);
394 }
John Cater5e9ce942016-10-12 17:23:30 +0000395 }
396
397 /**
John Caterb4f461e2016-10-25 16:16:35 +0000398 * Runs all tests in the base {@link PackageLookupFunctionTest} class with the {@link
399 * CrossRepositoryLabelViolationStrategy#ERROR} enum set, and also additional tests specific to
400 * that setting.
John Cater5e9ce942016-10-12 17:23:30 +0000401 */
402 @RunWith(JUnit4.class)
John Caterb4f461e2016-10-25 16:16:35 +0000403 public static class ErrorLabelViolationsTest extends PackageLookupFunctionTest {
John Cater5e9ce942016-10-12 17:23:30 +0000404 @Override
405 protected CrossRepositoryLabelViolationStrategy crossRepositoryLabelViolationStrategy() {
406 return CrossRepositoryLabelViolationStrategy.ERROR;
407 }
408
409 // Add any error-specific tests here.
John Caterb4f461e2016-10-25 16:16:35 +0000410
411 @Test
412 public void testInvalidPackageLabelIsError() throws Exception {
413 createAndCheckInvalidPackageLabel(false);
414 }
415
416 @Test
417 public void testSymlinkCycleInWorkspace() throws Exception {
418 scratch.overwriteFile("WORKSPACE", "local_repository(name='local', path='local/repo')");
419 Path localRepoWorkspace = scratch.resolve("local/repo/WORKSPACE");
420 Path localRepoWorkspaceLink = scratch.resolve("local/repo/WORKSPACE.link");
421 FileSystemUtils.createDirectoryAndParents(localRepoWorkspace.getParentDirectory());
422 FileSystemUtils.createDirectoryAndParents(localRepoWorkspaceLink.getParentDirectory());
423 localRepoWorkspace.createSymbolicLink(localRepoWorkspaceLink);
424 localRepoWorkspaceLink.createSymbolicLink(localRepoWorkspace);
425 scratch.file("local/repo/BUILD");
426
427 SkyKey skyKey = PackageLookupValue.key(PackageIdentifier.createInMainRepo("local/repo"));
428 EvaluationResult<PackageLookupValue> result = lookupPackage(skyKey);
429 assertThatEvaluationResult(result)
430 .hasErrorEntryForKeyThat(skyKey)
431 .hasExceptionThat()
432 .isInstanceOf(BuildFileNotFoundException.class);
433 assertThatEvaluationResult(result)
434 .hasErrorEntryForKeyThat(skyKey)
435 .hasExceptionThat()
436 .hasMessage(
437 "no such package 'local/repo': Unable to determine the local repository for "
438 + "directory /workspace/local/repo");
439 }
John Cater5e9ce942016-10-12 17:23:30 +0000440 }
John Cater5b4b7a32017-10-12 16:30:03 +0200441
442 /** Tests for detection of invalid package identifiers for local repositories. */
443 @RunWith(Parameterized.class)
444 public static class CorrectedLocalRepositoryTest extends PackageLookupFunctionTest {
445
446 /**
447 * Create parameters for this test. The contents are:
448 *
449 * <ol>
450 * <li>description
451 * <li>repository path
452 * <li>package path - under the repository
453 * <li>expected corrected package identifier
454 * </ol>
455 */
456 @Parameters(name = "{0}")
457 public static List<Object[]> parameters() {
458 List<Object[]> params = new ArrayList<>();
459
460 params.add(new String[] {"simpleRepo_emptyPackage", "local", "", "@local//"});
461 params.add(new String[] {"simpleRepo_singlePackage", "local", "package", "@local//package"});
462 params.add(
463 new String[] {
464 "simpleRepo_subPackage", "local", "package/subpackage", "@local//package/subpackage"
465 });
466 params.add(new String[] {"deepRepo_emptyPackage", "local/repo", "", "@local//"});
467 params.add(new String[] {"deepRepo_subPackage", "local/repo", "package", "@local//package"});
468
469 return params;
470 }
471
jcater25fb5b12017-10-13 17:23:44 +0200472 private final String repositoryPath;
473 private final String packagePath;
474 private final String expectedCorrectedPackageIdentifier;
John Cater5b4b7a32017-10-12 16:30:03 +0200475
476 public CorrectedLocalRepositoryTest(
jcater25fb5b12017-10-13 17:23:44 +0200477 String unusedDescription,
John Cater5b4b7a32017-10-12 16:30:03 +0200478 String repositoryPath,
479 String packagePath,
480 String expectedCorrectedPackageIdentifier) {
John Cater5b4b7a32017-10-12 16:30:03 +0200481 this.repositoryPath = repositoryPath;
482 this.packagePath = packagePath;
483 this.expectedCorrectedPackageIdentifier = expectedCorrectedPackageIdentifier;
484 }
485
486 @Override
487 protected CrossRepositoryLabelViolationStrategy crossRepositoryLabelViolationStrategy() {
488 return CrossRepositoryLabelViolationStrategy.ERROR;
489 }
490
491 @Test
492 public void testCorrectPackageDetection_relativePath() throws Exception {
John Cater5b4b7a32017-10-12 16:30:03 +0200493 scratch.overwriteFile(
494 "WORKSPACE", "local_repository(name='local', path='" + repositoryPath + "')");
495 scratch.file(PathFragment.create(repositoryPath).getRelative("WORKSPACE").getPathString());
496 scratch.file(
497 PathFragment.create(repositoryPath)
498 .getRelative(packagePath)
499 .getRelative("BUILD")
500 .getPathString());
501
502 PackageIdentifier packageIdentifier =
503 PackageIdentifier.createInMainRepo(
504 PathFragment.create(repositoryPath).getRelative(packagePath));
505 PackageLookupValue packageLookupValue = lookupPackage(packageIdentifier);
506 assertThat(packageLookupValue.packageExists()).isFalse();
507 assertThat(packageLookupValue)
508 .isInstanceOf(IncorrectRepositoryReferencePackageLookupValue.class);
509
510 IncorrectRepositoryReferencePackageLookupValue incorrectPackageLookupValue =
511 (IncorrectRepositoryReferencePackageLookupValue) packageLookupValue;
512 assertThat(incorrectPackageLookupValue.getInvalidPackageIdentifier())
513 .isEqualTo(packageIdentifier);
514 assertThat(incorrectPackageLookupValue.getCorrectedPackageIdentifier().toString())
515 .isEqualTo(expectedCorrectedPackageIdentifier);
516 }
517
518 @Test
519 public void testCorrectPackageDetection_absolutePath() throws Exception {
John Cater5b4b7a32017-10-12 16:30:03 +0200520 scratch.overwriteFile(
521 "WORKSPACE",
522 "local_repository(name='local', path=__workspace_dir__ + '/" + repositoryPath + "')");
523 scratch.file(PathFragment.create(repositoryPath).getRelative("WORKSPACE").getPathString());
524 scratch.file(
525 PathFragment.create(repositoryPath)
526 .getRelative(packagePath)
527 .getRelative("BUILD")
528 .getPathString());
529
530 PackageIdentifier packageIdentifier =
531 PackageIdentifier.createInMainRepo(
532 PathFragment.create(repositoryPath).getRelative(packagePath));
533 PackageLookupValue packageLookupValue = lookupPackage(packageIdentifier);
534 assertThat(packageLookupValue.packageExists()).isFalse();
535 assertThat(packageLookupValue)
536 .isInstanceOf(IncorrectRepositoryReferencePackageLookupValue.class);
537
538 IncorrectRepositoryReferencePackageLookupValue incorrectPackageLookupValue =
539 (IncorrectRepositoryReferencePackageLookupValue) packageLookupValue;
540 assertThat(incorrectPackageLookupValue.getInvalidPackageIdentifier())
541 .isEqualTo(packageIdentifier);
542 assertThat(incorrectPackageLookupValue.getCorrectedPackageIdentifier().toString())
543 .isEqualTo(expectedCorrectedPackageIdentifier);
544 }
545 }
Kristina Chodorow335f0672015-11-16 23:19:13 +0000546}