blob: baca435e4696edb35344ae5f39265b1a698a5997 [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;
janakr13b737a2021-07-02 14:24:25 -070026import com.google.devtools.build.lib.actions.ThreadStateReceiver;
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;
janakre2af68f2021-03-18 15:11:30 -070033import com.google.devtools.build.lib.io.FileSymlinkCycleUniquenessFunction;
John Catere0d1d0e2017-11-28 20:47:41 -080034import com.google.devtools.build.lib.packages.BuildFileName;
John Caterb4f461e2016-10-25 16:16:35 +000035import com.google.devtools.build.lib.packages.BuildFileNotFoundException;
Kristina Chodorow335f0672015-11-16 23:19:13 +000036import com.google.devtools.build.lib.packages.RuleClassProvider;
janakr15e15c22019-01-30 11:24:49 -080037import com.google.devtools.build.lib.packages.WorkspaceFileValue;
Kristina Chodorow335f0672015-11-16 23:19:13 +000038import com.google.devtools.build.lib.pkgcache.PathPackageLocator;
John Caterb4f461e2016-10-25 16:16:35 +000039import com.google.devtools.build.lib.rules.repository.LocalRepositoryFunction;
40import com.google.devtools.build.lib.rules.repository.LocalRepositoryRule;
John Caterb4f461e2016-10-25 16:16:35 +000041import com.google.devtools.build.lib.rules.repository.RepositoryDelegatorFunction;
42import com.google.devtools.build.lib.rules.repository.RepositoryFunction;
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;
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;
adonovan450c7ad2020-09-14 13:00:21 -070073import net.starlark.java.eval.StarlarkSemantics;
John Cater94695912016-08-03 12:09:39 +000074import org.junit.Before;
75import org.junit.Test;
76import org.junit.runner.RunWith;
77import org.junit.runners.JUnit4;
John Cater5b4b7a32017-10-12 16:30:03 +020078import org.junit.runners.Parameterized;
79import org.junit.runners.Parameterized.Parameters;
Kristina Chodorow335f0672015-11-16 23:19:13 +000080
John Cater5e9ce942016-10-12 17:23:30 +000081/** Tests for {@link PackageLookupFunction}. */
82public abstract class PackageLookupFunctionTest extends FoundationTestCase {
Kristina Chodorow335f0672015-11-16 23:19:13 +000083 private AtomicReference<ImmutableSet<PackageIdentifier>> deletedPackages;
84 private MemoizingEvaluator evaluator;
85 private SequentialBuildDriver driver;
86 private RecordingDifferencer differencer;
John Cater0c0735a2016-11-11 01:52:02 +000087 private Path emptyPackagePath;
kkress1847a012020-06-24 12:30:11 -070088 private static final String IGNORED_PACKAGE_PREFIXES_FILE_PATH_STRING = "config/ignored.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(
janakre4fcd472021-05-26 08:47:39 -0700123 SkyFunctions.PACKAGE,
124 new PackageFunction(
125 null,
126 null,
127 null,
128 null,
129 null,
janakre4fcd472021-05-26 08:47:39 -0700130 /*packageProgress=*/ null,
131 PackageFunction.ActionOnIOExceptionReadingBuildFile.UseOriginalIOException.INSTANCE,
janakr13b737a2021-07-02 14:24:25 -0700132 PackageFunction.IncrementalityIntent.INCREMENTAL,
133 k -> ThreadStateReceiver.NULL_INSTANCE));
shahan602cc852018-06-06 20:09:57 -0700134 skyFunctions.put(
135 FileStateValue.FILE_STATE,
136 new FileStateFunction(
djasper7faa0ef2019-03-28 10:00:00 -0700137 new AtomicReference<TimestampGranularityMonitor>(),
Googler166c3a32019-11-05 13:25:10 -0800138 new AtomicReference<>(UnixGlob.DEFAULT_SYSCALLS),
djasper7faa0ef2019-03-28 10:00:00 -0700139 externalFilesHelper));
shahan602cc852018-06-06 20:09:57 -0700140 skyFunctions.put(FileValue.FILE, new FileFunction(pkgLocator));
John Caterb4f461e2016-10-25 16:16:35 +0000141 skyFunctions.put(SkyFunctions.DIRECTORY_LISTING, new DirectoryListingFunction());
142 skyFunctions.put(
143 SkyFunctions.DIRECTORY_LISTING_STATE,
djasperb109a5f2019-02-14 01:05:34 -0800144 new DirectoryListingStateFunction(
145 externalFilesHelper, new AtomicReference<>(UnixGlob.DEFAULT_SYSCALLS)));
nharmataa32ac672020-01-08 12:54:25 -0800146 skyFunctions.put(
kkress1847a012020-06-24 12:30:11 -0700147 SkyFunctions.IGNORED_PACKAGE_PREFIXES,
148 new IgnoredPackagePrefixesFunction(
149 PathFragment.create(IGNORED_PACKAGE_PREFIXES_FILE_PATH_STRING)));
Ulf Adams015aad92016-07-13 16:49:40 +0000150 RuleClassProvider ruleClassProvider = analysisMock.createRuleClassProvider();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000151 skyFunctions.put(
janakr15e15c22019-01-30 11:24:49 -0800152 WorkspaceFileValue.WORKSPACE_FILE,
Kristina Chodorow335f0672015-11-16 23:19:13 +0000153 new WorkspaceFileFunction(
154 ruleClassProvider,
Ulf Adams015aad92016-07-13 16:49:40 +0000155 analysisMock
janakr52d05e82017-09-22 13:27:14 -0400156 .getPackageFactoryBuilderForTesting(directories)
nharmatafde0bd2f2018-12-21 10:17:56 -0800157 .build(ruleClassProvider, fileSystem),
mjhalupkaf0e48112019-01-14 13:01:56 -0800158 directories,
brandjon771a0292020-05-26 12:04:16 -0700159 /*bzlLoadFunctionForInlining=*/ null));
ajurkowski2018b872020-04-14 09:59:00 -0700160 skyFunctions.put(
161 SkyFunctions.EXTERNAL_PACKAGE,
162 new ExternalPackageFunction(BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER));
163 skyFunctions.put(
164 SkyFunctions.LOCAL_REPOSITORY_LOOKUP,
165 new LocalRepositoryLookupFunction(BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER));
John Caterb4f461e2016-10-25 16:16:35 +0000166 skyFunctions.put(
janakre2af68f2021-03-18 15:11:30 -0700167 FileSymlinkCycleUniquenessFunction.NAME, new FileSymlinkCycleUniquenessFunction());
John Caterb4f461e2016-10-25 16:16:35 +0000168
169 ImmutableMap<String, RepositoryFunction> repositoryHandlers =
John Caterb25a0342016-10-26 10:17:40 +0000170 ImmutableMap.of(
171 LocalRepositoryRule.NAME, (RepositoryFunction) new LocalRepositoryFunction());
John Caterb4f461e2016-10-25 16:16:35 +0000172 skyFunctions.put(
173 SkyFunctions.REPOSITORY_DIRECTORY,
janakrca6b46d2017-09-22 14:07:15 -0400174 new RepositoryDelegatorFunction(
Googler22913422019-05-02 12:42:20 -0700175 repositoryHandlers,
176 null,
177 new AtomicBoolean(true),
178 ImmutableMap::of,
179 directories,
ajurkowski2018b872020-04-14 09:59:00 -0700180 ManagedDirectoriesKnowledge.NO_MANAGED_DIRECTORIES,
181 BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER));
John Caterb4f461e2016-10-25 16:16:35 +0000182
janakr1cde8722017-10-10 03:22:21 +0200183 differencer = new SequencedRecordingDifferencer();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000184 evaluator = new InMemoryMemoizingEvaluator(skyFunctions, differencer);
185 driver = new SequentialBuildDriver(evaluator);
186 PrecomputedValue.BUILD_ID.set(differencer, UUID.randomUUID());
brandjondbff8b82017-10-23 19:02:31 +0200187 PrecomputedValue.PATH_PACKAGE_LOCATOR.set(differencer, pkgLocator.get());
adonovanb85d0b72020-05-08 11:59:19 -0700188 PrecomputedValue.STARLARK_SEMANTICS.set(differencer, StarlarkSemantics.DEFAULT);
kchodorowdfcd5da82017-04-19 18:58:50 +0200189 RepositoryDelegatorFunction.REPOSITORY_OVERRIDES.set(
190 differencer, ImmutableMap.<RepositoryName, PathFragment>of());
Klaus Aehlig93fe20c2018-06-14 05:48:46 -0700191 RepositoryDelegatorFunction.DEPENDENCY_FOR_UNCONDITIONAL_FETCHING.set(
192 differencer, RepositoryDelegatorFunction.DONT_FETCH_UNCONDITIONALLY);
Klaus Aehlig8eb47482018-09-17 09:14:58 -0700193 RepositoryDelegatorFunction.RESOLVED_FILE_INSTEAD_OF_WORKSPACE.set(
jhorvitzdd1d8412020-08-01 05:59:14 -0700194 differencer, Optional.empty());
pcloudy7bc01992021-07-13 03:16:52 -0700195 RepositoryDelegatorFunction.ENABLE_BZLMOD.set(differencer, false);
Kristina Chodorow335f0672015-11-16 23:19:13 +0000196 }
197
John Caterb4f461e2016-10-25 16:16:35 +0000198 protected PackageLookupValue lookupPackage(String packageName) throws InterruptedException {
Brian Silvermand7d6d622016-03-17 09:53:39 +0000199 return lookupPackage(PackageIdentifier.createInMainRepo(packageName));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000200 }
201
John Caterb4f461e2016-10-25 16:16:35 +0000202 protected PackageLookupValue lookupPackage(PackageIdentifier packageId)
Kristina Chodorow335f0672015-11-16 23:19:13 +0000203 throws InterruptedException {
204 SkyKey key = PackageLookupValue.key(packageId);
John Caterb4f461e2016-10-25 16:16:35 +0000205 return lookupPackage(key).get(key);
206 }
207
208 protected EvaluationResult<PackageLookupValue> lookupPackage(SkyKey packageIdentifierSkyKey)
209 throws InterruptedException {
Googler10028672018-10-25 12:14:34 -0700210 EvaluationContext evaluationContext =
211 EvaluationContext.newBuilder()
212 .setKeepGoing(false)
213 .setNumThreads(SkyframeExecutor.DEFAULT_THREAD_COUNT)
michajlo7a485be2020-07-30 11:08:46 -0700214 .setEventHandler(NullEventHandler.INSTANCE)
Googler10028672018-10-25 12:14:34 -0700215 .build();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000216 return driver.<PackageLookupValue>evaluate(
Googler10028672018-10-25 12:14:34 -0700217 ImmutableList.of(packageIdentifierSkyKey), evaluationContext);
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 testNoBuildFile() throws Exception {
222 scratch.file("parentpackage/nobuildfile/foo.txt");
223 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/nobuildfile");
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 testNoBuildFileAndNoParentPackage() throws Exception {
231 scratch.file("noparentpackage/foo.txt");
232 PackageLookupValue packageLookupValue = lookupPackage("noparentpackage");
lberkiaea56b32017-05-30 12:35:33 +0200233 assertThat(packageLookupValue.packageExists()).isFalse();
234 assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.NO_BUILD_FILE);
235 assertThat(packageLookupValue.getErrorMsg()).isNotNull();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000236 }
237
Florian Weikert92b22362015-12-03 10:17:18 +0000238 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000239 public void testDeletedPackage() throws Exception {
240 scratch.file("parentpackage/deletedpackage/BUILD");
241 deletedPackages.set(ImmutableSet.of(
Brian Silvermand7d6d622016-03-17 09:53:39 +0000242 PackageIdentifier.createInMainRepo("parentpackage/deletedpackage")));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000243 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/deletedpackage");
lberkiaea56b32017-05-30 12:35:33 +0200244 assertThat(packageLookupValue.packageExists()).isFalse();
245 assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.DELETED_PACKAGE);
246 assertThat(packageLookupValue.getErrorMsg()).isNotNull();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000247 }
248
Florian Weikert92b22362015-12-03 10:17:18 +0000249 @Test
kkress1847a012020-06-24 12:30:11 -0700250 public void testIgnoredPackage() throws Exception {
251 scratch.file("ignored/subdir/BUILD");
252 scratch.file("ignored/BUILD");
253 Path ignored = scratch.overwriteFile(IGNORED_PACKAGE_PREFIXES_FILE_PATH_STRING, "ignored");
Kristina Chodorow335f0672015-11-16 23:19:13 +0000254
kkress1847a012020-06-24 12:30:11 -0700255 ImmutableSet<String> pkgs = ImmutableSet.of("ignored/subdir", "ignored");
Kristina Chodorow335f0672015-11-16 23:19:13 +0000256 for (String pkg : pkgs) {
257 PackageLookupValue packageLookupValue = lookupPackage(pkg);
lberkiaea56b32017-05-30 12:35:33 +0200258 assertThat(packageLookupValue.packageExists()).isFalse();
259 assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.DELETED_PACKAGE);
260 assertThat(packageLookupValue.getErrorMsg()).isNotNull();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000261 }
262
kkress1847a012020-06-24 12:30:11 -0700263 scratch.overwriteFile(IGNORED_PACKAGE_PREFIXES_FILE_PATH_STRING, "not_ignored");
264 RootedPath rootedIgnoreFile =
tomluee6a6862018-01-17 14:36:26 -0800265 RootedPath.toRootedPath(
kkress1847a012020-06-24 12:30:11 -0700266 Root.fromPath(ignored.getParentDirectory().getParentDirectory()),
267 PathFragment.create("config/ignored.txt"));
268 differencer.invalidate(ImmutableSet.of(FileStateValue.key(rootedIgnoreFile)));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000269 for (String pkg : pkgs) {
270 PackageLookupValue packageLookupValue = lookupPackage(pkg);
lberkiaea56b32017-05-30 12:35:33 +0200271 assertThat(packageLookupValue.packageExists()).isTrue();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000272 }
273 }
274
Florian Weikert92b22362015-12-03 10:17:18 +0000275 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000276 public void testInvalidPackageName() throws Exception {
twerthc4f2d802018-05-15 05:02:31 -0700277 scratch.file("parentpackage/invalidpackagename:42/BUILD");
278 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/invalidpackagename:42");
lberkiaea56b32017-05-30 12:35:33 +0200279 assertThat(packageLookupValue.packageExists()).isFalse();
280 assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.INVALID_PACKAGE_NAME);
281 assertThat(packageLookupValue.getErrorMsg()).isNotNull();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000282 }
283
Florian Weikert92b22362015-12-03 10:17:18 +0000284 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000285 public void testDirectoryNamedBuild() throws Exception {
286 scratch.dir("parentpackage/isdirectory/BUILD");
287 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/isdirectory");
lberkiaea56b32017-05-30 12:35:33 +0200288 assertThat(packageLookupValue.packageExists()).isFalse();
289 assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.NO_BUILD_FILE);
290 assertThat(packageLookupValue.getErrorMsg()).isNotNull();
Kristina Chodorow335f0672015-11-16 23:19:13 +0000291 }
292
Florian Weikert92b22362015-12-03 10:17:18 +0000293 @Test
John Cater0c0735a2016-11-11 01:52:02 +0000294 public void testEverythingIsGood_BUILD() throws Exception {
Kristina Chodorow335f0672015-11-16 23:19:13 +0000295 scratch.file("parentpackage/everythinggood/BUILD");
296 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/everythinggood");
lberkiaea56b32017-05-30 12:35:33 +0200297 assertThat(packageLookupValue.packageExists()).isTrue();
tomluee6a6862018-01-17 14:36:26 -0800298 assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory));
lberkiaea56b32017-05-30 12:35:33 +0200299 assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD);
Kristina Chodorow335f0672015-11-16 23:19:13 +0000300 }
301
Florian Weikert92b22362015-12-03 10:17:18 +0000302 @Test
John Cater0c0735a2016-11-11 01:52:02 +0000303 public void testEverythingIsGood_BUILD_bazel() throws Exception {
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 testEverythingIsGood_both() throws Exception {
313 scratch.file("parentpackage/everythinggood/BUILD");
314 scratch.file("parentpackage/everythinggood/BUILD.bazel");
315 PackageLookupValue packageLookupValue = lookupPackage("parentpackage/everythinggood");
lberkiaea56b32017-05-30 12:35:33 +0200316 assertThat(packageLookupValue.packageExists()).isTrue();
tomluee6a6862018-01-17 14:36:26 -0800317 assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory));
lberkiaea56b32017-05-30 12:35:33 +0200318 assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD_DOT_BAZEL);
John Cater0c0735a2016-11-11 01:52:02 +0000319 }
320
321 @Test
322 public void testBuildFilesInMultiplePackagePaths() throws Exception {
323 scratch.file(emptyPackagePath.getPathString() + "/foo/BUILD");
324 scratch.file("foo/BUILD.bazel");
325
326 // BUILD file in the first package path should be preferred to BUILD.bazel in the second.
327 PackageLookupValue packageLookupValue = lookupPackage("foo");
lberkiaea56b32017-05-30 12:35:33 +0200328 assertThat(packageLookupValue.packageExists()).isTrue();
tomluee6a6862018-01-17 14:36:26 -0800329 assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(emptyPackagePath));
lberkiaea56b32017-05-30 12:35:33 +0200330 assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD);
John Cater0c0735a2016-11-11 01:52:02 +0000331 }
332
333 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000334 public void testEmptyPackageName() throws Exception {
335 scratch.file("BUILD");
336 PackageLookupValue packageLookupValue = lookupPackage("");
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));
lberkiaea56b32017-05-30 12:35:33 +0200339 assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD);
Kristina Chodorow335f0672015-11-16 23:19:13 +0000340 }
341
Florian Weikert92b22362015-12-03 10:17:18 +0000342 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000343 public void testWorkspaceLookup() throws Exception {
344 scratch.overwriteFile("WORKSPACE");
Brian Silvermand7d6d622016-03-17 09:53:39 +0000345 PackageLookupValue packageLookupValue = lookupPackage(
346 PackageIdentifier.createInMainRepo("external"));
lberkiaea56b32017-05-30 12:35:33 +0200347 assertThat(packageLookupValue.packageExists()).isTrue();
tomluee6a6862018-01-17 14:36:26 -0800348 assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000349 }
John Cater94695912016-08-03 12:09:39 +0000350
Florian Weikert92b22362015-12-03 10:17:18 +0000351 @Test
Kristina Chodorow335f0672015-11-16 23:19:13 +0000352 public void testPackageLookupValueHashCodeAndEqualsContract() throws Exception {
tomluee6a6862018-01-17 14:36:26 -0800353 Root root1 = Root.fromPath(rootDirectory.getRelative("root1"));
354 Root root2 = Root.fromPath(rootDirectory.getRelative("root2"));
Kristina Chodorow335f0672015-11-16 23:19:13 +0000355 // Our (seeming) duplication of parameters here is intentional. Some of the subclasses of
356 // PackageLookupValue are supposed to have reference equality semantics, and some are supposed
357 // to have logical equality semantics.
358 new EqualsTester()
John Cater94695912016-08-03 12:09:39 +0000359 .addEqualityGroup(
360 PackageLookupValue.success(root1, BuildFileName.BUILD),
361 PackageLookupValue.success(root1, BuildFileName.BUILD))
362 .addEqualityGroup(
363 PackageLookupValue.success(root2, BuildFileName.BUILD),
364 PackageLookupValue.success(root2, BuildFileName.BUILD))
Kristina Chodorow335f0672015-11-16 23:19:13 +0000365 .addEqualityGroup(
janakr677ba952019-03-07 09:50:01 -0800366 PackageLookupValue.NO_BUILD_FILE_VALUE, PackageLookupValue.NO_BUILD_FILE_VALUE)
367 .addEqualityGroup(
Kristina Chodorow335f0672015-11-16 23:19:13 +0000368 PackageLookupValue.DELETED_PACKAGE_VALUE, PackageLookupValue.DELETED_PACKAGE_VALUE)
John Cater94695912016-08-03 12:09:39 +0000369 .addEqualityGroup(
370 PackageLookupValue.invalidPackageName("nope1"),
Kristina Chodorow335f0672015-11-16 23:19:13 +0000371 PackageLookupValue.invalidPackageName("nope1"))
John Cater94695912016-08-03 12:09:39 +0000372 .addEqualityGroup(
373 PackageLookupValue.invalidPackageName("nope2"),
374 PackageLookupValue.invalidPackageName("nope2"))
Kristina Chodorow335f0672015-11-16 23:19:13 +0000375 .testEquals();
376 }
John Cater5e9ce942016-10-12 17:23:30 +0000377
John Caterb4f461e2016-10-25 16:16:35 +0000378 protected void createAndCheckInvalidPackageLabel(boolean expectedPackageExists) throws Exception {
379 scratch.overwriteFile("WORKSPACE", "local_repository(name='local', path='local/repo')");
380 scratch.file("local/repo/WORKSPACE");
381 scratch.file("local/repo/BUILD");
382
383 // First, use the correct label.
384 PackageLookupValue packageLookupValue =
Googler80ada0f2021-12-22 07:15:59 -0800385 lookupPackage(PackageIdentifier.create("@local", PathFragment.EMPTY_FRAGMENT));
lberkiaea56b32017-05-30 12:35:33 +0200386 assertThat(packageLookupValue.packageExists()).isTrue();
John Caterb4f461e2016-10-25 16:16:35 +0000387
388 // Then, use the incorrect label.
389 packageLookupValue = lookupPackage(PackageIdentifier.createInMainRepo("local/repo"));
lberkiaea56b32017-05-30 12:35:33 +0200390 assertThat(packageLookupValue.packageExists()).isEqualTo(expectedPackageExists);
John Caterb4f461e2016-10-25 16:16:35 +0000391 }
392
John Cater5e9ce942016-10-12 17:23:30 +0000393 /**
John Caterb4f461e2016-10-25 16:16:35 +0000394 * Runs all tests in the base {@link PackageLookupFunctionTest} class with the {@link
395 * CrossRepositoryLabelViolationStrategy#IGNORE} enum set, and also additional tests specific to
396 * that setting.
John Cater5e9ce942016-10-12 17:23:30 +0000397 */
398 @RunWith(JUnit4.class)
John Caterb4f461e2016-10-25 16:16:35 +0000399 public static class IgnoreLabelViolationsTest extends PackageLookupFunctionTest {
John Cater5e9ce942016-10-12 17:23:30 +0000400 @Override
401 protected CrossRepositoryLabelViolationStrategy crossRepositoryLabelViolationStrategy() {
402 return CrossRepositoryLabelViolationStrategy.IGNORE;
403 }
404
405 // Add any ignore-specific tests here.
John Caterb4f461e2016-10-25 16:16:35 +0000406
407 @Test
408 public void testInvalidPackageLabelIsIgnored() throws Exception {
409 createAndCheckInvalidPackageLabel(true);
410 }
John Cater5e9ce942016-10-12 17:23:30 +0000411 }
412
413 /**
John Caterb4f461e2016-10-25 16:16:35 +0000414 * Runs all tests in the base {@link PackageLookupFunctionTest} class with the {@link
415 * CrossRepositoryLabelViolationStrategy#ERROR} enum set, and also additional tests specific to
416 * that setting.
John Cater5e9ce942016-10-12 17:23:30 +0000417 */
418 @RunWith(JUnit4.class)
John Caterb4f461e2016-10-25 16:16:35 +0000419 public static class ErrorLabelViolationsTest extends PackageLookupFunctionTest {
John Cater5e9ce942016-10-12 17:23:30 +0000420 @Override
421 protected CrossRepositoryLabelViolationStrategy crossRepositoryLabelViolationStrategy() {
422 return CrossRepositoryLabelViolationStrategy.ERROR;
423 }
424
425 // Add any error-specific tests here.
John Caterb4f461e2016-10-25 16:16:35 +0000426
427 @Test
428 public void testInvalidPackageLabelIsError() throws Exception {
429 createAndCheckInvalidPackageLabel(false);
430 }
431
432 @Test
433 public void testSymlinkCycleInWorkspace() throws Exception {
434 scratch.overwriteFile("WORKSPACE", "local_repository(name='local', path='local/repo')");
435 Path localRepoWorkspace = scratch.resolve("local/repo/WORKSPACE");
436 Path localRepoWorkspaceLink = scratch.resolve("local/repo/WORKSPACE.link");
437 FileSystemUtils.createDirectoryAndParents(localRepoWorkspace.getParentDirectory());
438 FileSystemUtils.createDirectoryAndParents(localRepoWorkspaceLink.getParentDirectory());
439 localRepoWorkspace.createSymbolicLink(localRepoWorkspaceLink);
440 localRepoWorkspaceLink.createSymbolicLink(localRepoWorkspace);
441 scratch.file("local/repo/BUILD");
442
443 SkyKey skyKey = PackageLookupValue.key(PackageIdentifier.createInMainRepo("local/repo"));
444 EvaluationResult<PackageLookupValue> result = lookupPackage(skyKey);
445 assertThatEvaluationResult(result)
446 .hasErrorEntryForKeyThat(skyKey)
447 .hasExceptionThat()
448 .isInstanceOf(BuildFileNotFoundException.class);
449 assertThatEvaluationResult(result)
450 .hasErrorEntryForKeyThat(skyKey)
451 .hasExceptionThat()
diamondm4d8d1d62019-03-25 16:05:02 -0700452 .hasMessageThat()
453 .isEqualTo(
John Caterb4f461e2016-10-25 16:16:35 +0000454 "no such package 'local/repo': Unable to determine the local repository for "
455 + "directory /workspace/local/repo");
456 }
John Cater5e9ce942016-10-12 17:23:30 +0000457 }
John Cater5b4b7a32017-10-12 16:30:03 +0200458
459 /** Tests for detection of invalid package identifiers for local repositories. */
460 @RunWith(Parameterized.class)
461 public static class CorrectedLocalRepositoryTest extends PackageLookupFunctionTest {
462
463 /**
464 * Create parameters for this test. The contents are:
465 *
466 * <ol>
467 * <li>description
468 * <li>repository path
469 * <li>package path - under the repository
470 * <li>expected corrected package identifier
471 * </ol>
472 */
473 @Parameters(name = "{0}")
474 public static List<Object[]> parameters() {
475 List<Object[]> params = new ArrayList<>();
476
477 params.add(new String[] {"simpleRepo_emptyPackage", "local", "", "@local//"});
478 params.add(new String[] {"simpleRepo_singlePackage", "local", "package", "@local//package"});
479 params.add(
480 new String[] {
481 "simpleRepo_subPackage", "local", "package/subpackage", "@local//package/subpackage"
482 });
483 params.add(new String[] {"deepRepo_emptyPackage", "local/repo", "", "@local//"});
484 params.add(new String[] {"deepRepo_subPackage", "local/repo", "package", "@local//package"});
485
486 return params;
487 }
488
jcater25fb5b12017-10-13 17:23:44 +0200489 private final String repositoryPath;
490 private final String packagePath;
491 private final String expectedCorrectedPackageIdentifier;
John Cater5b4b7a32017-10-12 16:30:03 +0200492
493 public CorrectedLocalRepositoryTest(
jcater25fb5b12017-10-13 17:23:44 +0200494 String unusedDescription,
John Cater5b4b7a32017-10-12 16:30:03 +0200495 String repositoryPath,
496 String packagePath,
497 String expectedCorrectedPackageIdentifier) {
John Cater5b4b7a32017-10-12 16:30:03 +0200498 this.repositoryPath = repositoryPath;
499 this.packagePath = packagePath;
500 this.expectedCorrectedPackageIdentifier = expectedCorrectedPackageIdentifier;
501 }
502
503 @Override
504 protected CrossRepositoryLabelViolationStrategy crossRepositoryLabelViolationStrategy() {
505 return CrossRepositoryLabelViolationStrategy.ERROR;
506 }
507
508 @Test
509 public void testCorrectPackageDetection_relativePath() throws Exception {
John Cater5b4b7a32017-10-12 16:30:03 +0200510 scratch.overwriteFile(
511 "WORKSPACE", "local_repository(name='local', path='" + repositoryPath + "')");
512 scratch.file(PathFragment.create(repositoryPath).getRelative("WORKSPACE").getPathString());
513 scratch.file(
514 PathFragment.create(repositoryPath)
515 .getRelative(packagePath)
516 .getRelative("BUILD")
517 .getPathString());
518
519 PackageIdentifier packageIdentifier =
520 PackageIdentifier.createInMainRepo(
521 PathFragment.create(repositoryPath).getRelative(packagePath));
522 PackageLookupValue packageLookupValue = lookupPackage(packageIdentifier);
523 assertThat(packageLookupValue.packageExists()).isFalse();
524 assertThat(packageLookupValue)
525 .isInstanceOf(IncorrectRepositoryReferencePackageLookupValue.class);
526
527 IncorrectRepositoryReferencePackageLookupValue incorrectPackageLookupValue =
528 (IncorrectRepositoryReferencePackageLookupValue) packageLookupValue;
529 assertThat(incorrectPackageLookupValue.getInvalidPackageIdentifier())
530 .isEqualTo(packageIdentifier);
531 assertThat(incorrectPackageLookupValue.getCorrectedPackageIdentifier().toString())
532 .isEqualTo(expectedCorrectedPackageIdentifier);
533 }
534
535 @Test
536 public void testCorrectPackageDetection_absolutePath() throws Exception {
John Cater5b4b7a32017-10-12 16:30:03 +0200537 scratch.overwriteFile(
538 "WORKSPACE",
539 "local_repository(name='local', path=__workspace_dir__ + '/" + repositoryPath + "')");
540 scratch.file(PathFragment.create(repositoryPath).getRelative("WORKSPACE").getPathString());
541 scratch.file(
542 PathFragment.create(repositoryPath)
543 .getRelative(packagePath)
544 .getRelative("BUILD")
545 .getPathString());
546
547 PackageIdentifier packageIdentifier =
548 PackageIdentifier.createInMainRepo(
549 PathFragment.create(repositoryPath).getRelative(packagePath));
550 PackageLookupValue packageLookupValue = lookupPackage(packageIdentifier);
551 assertThat(packageLookupValue.packageExists()).isFalse();
552 assertThat(packageLookupValue)
553 .isInstanceOf(IncorrectRepositoryReferencePackageLookupValue.class);
554
555 IncorrectRepositoryReferencePackageLookupValue incorrectPackageLookupValue =
556 (IncorrectRepositoryReferencePackageLookupValue) packageLookupValue;
557 assertThat(incorrectPackageLookupValue.getInvalidPackageIdentifier())
558 .isEqualTo(packageIdentifier);
559 assertThat(incorrectPackageLookupValue.getCorrectedPackageIdentifier().toString())
560 .isEqualTo(expectedCorrectedPackageIdentifier);
561 }
562 }
Kristina Chodorow335f0672015-11-16 23:19:13 +0000563}