Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.devtools.build.lib.skyframe; |
| 16 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertThat; |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 18 | import static com.google.devtools.build.skyframe.EvaluationResultSubjectFactory.assertThatEvaluationResult; |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 19 | |
Klaus Aehlig | 8eb4748 | 2018-09-17 09:14:58 -0700 | [diff] [blame] | 20 | import com.google.common.base.Optional; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableList; |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableMap; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableSet; |
| 24 | import com.google.common.testing.EqualsTester; |
shahan | 602cc85 | 2018-06-06 20:09:57 -0700 | [diff] [blame] | 25 | import com.google.devtools.build.lib.actions.FileStateValue; |
| 26 | import com.google.devtools.build.lib.actions.FileValue; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.analysis.BlazeDirectories; |
janakr | 3b63a4e | 2017-09-14 09:55:40 +0200 | [diff] [blame] | 28 | import com.google.devtools.build.lib.analysis.ServerDirectories; |
Ulf Adams | 015aad9 | 2016-07-13 16:49:40 +0000 | [diff] [blame] | 29 | import com.google.devtools.build.lib.analysis.util.AnalysisMock; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
kchodorow | dfcd5da8 | 2017-04-19 18:58:50 +0200 | [diff] [blame] | 31 | import com.google.devtools.build.lib.cmdline.RepositoryName; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 32 | import com.google.devtools.build.lib.events.NullEventHandler; |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 33 | import com.google.devtools.build.lib.packages.BuildFileName; |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 34 | import com.google.devtools.build.lib.packages.BuildFileNotFoundException; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 35 | import com.google.devtools.build.lib.packages.RuleClassProvider; |
janakr | 15e15c2 | 2019-01-30 11:24:49 -0800 | [diff] [blame] | 36 | import com.google.devtools.build.lib.packages.WorkspaceFileValue; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 37 | import com.google.devtools.build.lib.pkgcache.PathPackageLocator; |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 38 | import com.google.devtools.build.lib.rules.repository.LocalRepositoryFunction; |
| 39 | import com.google.devtools.build.lib.rules.repository.LocalRepositoryRule; |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 40 | import com.google.devtools.build.lib.rules.repository.RepositoryDelegatorFunction; |
| 41 | import com.google.devtools.build.lib.rules.repository.RepositoryFunction; |
| 42 | import com.google.devtools.build.lib.rules.repository.RepositoryLoaderFunction; |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 43 | import com.google.devtools.build.lib.skyframe.ExternalFilesHelper.ExternalFileAction; |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 44 | import com.google.devtools.build.lib.skyframe.PackageLookupFunction.CrossRepositoryLabelViolationStrategy; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 45 | import com.google.devtools.build.lib.skyframe.PackageLookupValue.ErrorReason; |
John Cater | 4a12e66 | 2017-09-06 17:51:18 +0200 | [diff] [blame] | 46 | import com.google.devtools.build.lib.skyframe.PackageLookupValue.IncorrectRepositoryReferencePackageLookupValue; |
laurentlb | 6659b4c | 2019-02-18 07:23:36 -0800 | [diff] [blame] | 47 | import com.google.devtools.build.lib.syntax.StarlarkSemantics; |
Florian Weikert | cca703a | 2015-12-07 09:56:38 +0000 | [diff] [blame] | 48 | import com.google.devtools.build.lib.testutil.FoundationTestCase; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 49 | import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor; |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 50 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 51 | import com.google.devtools.build.lib.vfs.Path; |
| 52 | import com.google.devtools.build.lib.vfs.PathFragment; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 53 | import com.google.devtools.build.lib.vfs.Root; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 54 | import com.google.devtools.build.lib.vfs.RootedPath; |
djasper | b109a5f | 2019-02-14 01:05:34 -0800 | [diff] [blame] | 55 | import com.google.devtools.build.lib.vfs.UnixGlob; |
Googler | 1002867 | 2018-10-25 12:14:34 -0700 | [diff] [blame] | 56 | import com.google.devtools.build.skyframe.EvaluationContext; |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 57 | import com.google.devtools.build.skyframe.EvaluationResult; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 58 | import com.google.devtools.build.skyframe.InMemoryMemoizingEvaluator; |
| 59 | import com.google.devtools.build.skyframe.MemoizingEvaluator; |
| 60 | import com.google.devtools.build.skyframe.RecordingDifferencer; |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 61 | import com.google.devtools.build.skyframe.SequencedRecordingDifferencer; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 62 | import com.google.devtools.build.skyframe.SequentialBuildDriver; |
| 63 | import com.google.devtools.build.skyframe.SkyFunction; |
| 64 | import com.google.devtools.build.skyframe.SkyFunctionName; |
| 65 | import com.google.devtools.build.skyframe.SkyKey; |
John Cater | 5b4b7a3 | 2017-10-12 16:30:03 +0200 | [diff] [blame] | 66 | import java.util.ArrayList; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 67 | import java.util.HashMap; |
John Cater | 5b4b7a3 | 2017-10-12 16:30:03 +0200 | [diff] [blame] | 68 | import java.util.List; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 69 | import java.util.Map; |
| 70 | import java.util.UUID; |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 71 | import java.util.concurrent.atomic.AtomicBoolean; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 72 | import java.util.concurrent.atomic.AtomicReference; |
John Cater | 9469591 | 2016-08-03 12:09:39 +0000 | [diff] [blame] | 73 | import org.junit.Before; |
| 74 | import org.junit.Test; |
| 75 | import org.junit.runner.RunWith; |
| 76 | import org.junit.runners.JUnit4; |
John Cater | 5b4b7a3 | 2017-10-12 16:30:03 +0200 | [diff] [blame] | 77 | import org.junit.runners.Parameterized; |
| 78 | import org.junit.runners.Parameterized.Parameters; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 79 | |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 80 | /** Tests for {@link PackageLookupFunction}. */ |
| 81 | public abstract class PackageLookupFunctionTest extends FoundationTestCase { |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 82 | private AtomicReference<ImmutableSet<PackageIdentifier>> deletedPackages; |
| 83 | private MemoizingEvaluator evaluator; |
| 84 | private SequentialBuildDriver driver; |
| 85 | private RecordingDifferencer differencer; |
John Cater | 0c0735a | 2016-11-11 01:52:02 +0000 | [diff] [blame] | 86 | private Path emptyPackagePath; |
nharmata | a32ac67 | 2020-01-08 12:54:25 -0800 | [diff] [blame] | 87 | private static final String BLACKLISTED_PACKAGE_PREFIXES_FILE_PATH_STRING = |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 88 | "config/blacklisted.txt"; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 89 | |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 90 | protected abstract CrossRepositoryLabelViolationStrategy crossRepositoryLabelViolationStrategy(); |
| 91 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 92 | @Before |
| 93 | public final void setUp() throws Exception { |
John Cater | 0c0735a | 2016-11-11 01:52:02 +0000 | [diff] [blame] | 94 | emptyPackagePath = rootDirectory.getRelative("somewhere/else"); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 95 | scratch.file("parentpackage/BUILD"); |
| 96 | |
Ulf Adams | 015aad9 | 2016-07-13 16:49:40 +0000 | [diff] [blame] | 97 | AnalysisMock analysisMock = AnalysisMock.get(); |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 98 | AtomicReference<PathPackageLocator> pkgLocator = |
| 99 | new AtomicReference<>( |
| 100 | new PathPackageLocator( |
| 101 | outputBase, |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 102 | ImmutableList.of(Root.fromPath(emptyPackagePath), Root.fromPath(rootDirectory)), |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 103 | BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY)); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 104 | deletedPackages = new AtomicReference<>(ImmutableSet.<PackageIdentifier>of()); |
Ulf Adams | 015aad9 | 2016-07-13 16:49:40 +0000 | [diff] [blame] | 105 | BlazeDirectories directories = |
| 106 | new BlazeDirectories( |
Klaus Aehlig | c2499c4 | 2018-02-27 05:47:21 -0800 | [diff] [blame] | 107 | new ServerDirectories(rootDirectory, outputBase, rootDirectory), |
janakr | 3b63a4e | 2017-09-14 09:55:40 +0200 | [diff] [blame] | 108 | rootDirectory, |
cushon | 849df36 | 2018-05-14 01:51:45 -0700 | [diff] [blame] | 109 | /* defaultSystemJavabase= */ null, |
janakr | 3b63a4e | 2017-09-14 09:55:40 +0200 | [diff] [blame] | 110 | analysisMock.getProductName()); |
nharmata | 3fb7d34 | 2018-02-23 11:37:51 -0800 | [diff] [blame] | 111 | ExternalFilesHelper externalFilesHelper = ExternalFilesHelper.createForTesting( |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 112 | pkgLocator, ExternalFileAction.DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_PATHS, directories); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 113 | |
| 114 | Map<SkyFunctionName, SkyFunction> skyFunctions = new HashMap<>(); |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 115 | skyFunctions.put( |
| 116 | SkyFunctions.PACKAGE_LOOKUP, |
John Cater | 0c0735a | 2016-11-11 01:52:02 +0000 | [diff] [blame] | 117 | new PackageLookupFunction( |
| 118 | deletedPackages, |
| 119 | crossRepositoryLabelViolationStrategy(), |
ajurkowski | 2018b87 | 2020-04-14 09:59:00 -0700 | [diff] [blame] | 120 | BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY, |
| 121 | BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER)); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 122 | skyFunctions.put( |
ajurkowski | 2018b87 | 2020-04-14 09:59:00 -0700 | [diff] [blame] | 123 | SkyFunctions.PACKAGE, new PackageFunction(null, null, null, null, null, null, null, null)); |
shahan | 602cc85 | 2018-06-06 20:09:57 -0700 | [diff] [blame] | 124 | skyFunctions.put( |
| 125 | FileStateValue.FILE_STATE, |
| 126 | new FileStateFunction( |
djasper | 7faa0ef | 2019-03-28 10:00:00 -0700 | [diff] [blame] | 127 | new AtomicReference<TimestampGranularityMonitor>(), |
Googler | 166c3a3 | 2019-11-05 13:25:10 -0800 | [diff] [blame] | 128 | new AtomicReference<>(UnixGlob.DEFAULT_SYSCALLS), |
djasper | 7faa0ef | 2019-03-28 10:00:00 -0700 | [diff] [blame] | 129 | externalFilesHelper)); |
shahan | 602cc85 | 2018-06-06 20:09:57 -0700 | [diff] [blame] | 130 | skyFunctions.put(FileValue.FILE, new FileFunction(pkgLocator)); |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 131 | skyFunctions.put(SkyFunctions.DIRECTORY_LISTING, new DirectoryListingFunction()); |
| 132 | skyFunctions.put( |
| 133 | SkyFunctions.DIRECTORY_LISTING_STATE, |
djasper | b109a5f | 2019-02-14 01:05:34 -0800 | [diff] [blame] | 134 | new DirectoryListingStateFunction( |
| 135 | externalFilesHelper, new AtomicReference<>(UnixGlob.DEFAULT_SYSCALLS))); |
nharmata | a32ac67 | 2020-01-08 12:54:25 -0800 | [diff] [blame] | 136 | skyFunctions.put( |
| 137 | SkyFunctions.BLACKLISTED_PACKAGE_PREFIXES, |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 138 | new BlacklistedPackagePrefixesFunction( |
nharmata | a32ac67 | 2020-01-08 12:54:25 -0800 | [diff] [blame] | 139 | PathFragment.create(BLACKLISTED_PACKAGE_PREFIXES_FILE_PATH_STRING))); |
Ulf Adams | 015aad9 | 2016-07-13 16:49:40 +0000 | [diff] [blame] | 140 | RuleClassProvider ruleClassProvider = analysisMock.createRuleClassProvider(); |
| 141 | skyFunctions.put(SkyFunctions.WORKSPACE_AST, new WorkspaceASTFunction(ruleClassProvider)); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 142 | skyFunctions.put( |
janakr | 15e15c2 | 2019-01-30 11:24:49 -0800 | [diff] [blame] | 143 | WorkspaceFileValue.WORKSPACE_FILE, |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 144 | new WorkspaceFileFunction( |
| 145 | ruleClassProvider, |
Ulf Adams | 015aad9 | 2016-07-13 16:49:40 +0000 | [diff] [blame] | 146 | analysisMock |
janakr | 52d05e8 | 2017-09-22 13:27:14 -0400 | [diff] [blame] | 147 | .getPackageFactoryBuilderForTesting(directories) |
nharmata | fde0bd2f | 2018-12-21 10:17:56 -0800 | [diff] [blame] | 148 | .build(ruleClassProvider, fileSystem), |
mjhalupka | f0e4811 | 2019-01-14 13:01:56 -0800 | [diff] [blame] | 149 | directories, |
gregce | ebd616f | 2020-04-10 16:47:41 -0700 | [diff] [blame] | 150 | /*starlarkImportLookupFunctionForInlining=*/ null)); |
ajurkowski | 2018b87 | 2020-04-14 09:59:00 -0700 | [diff] [blame] | 151 | skyFunctions.put( |
| 152 | SkyFunctions.EXTERNAL_PACKAGE, |
| 153 | new ExternalPackageFunction(BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER)); |
| 154 | skyFunctions.put( |
| 155 | SkyFunctions.LOCAL_REPOSITORY_LOOKUP, |
| 156 | new LocalRepositoryLookupFunction(BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER)); |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 157 | skyFunctions.put( |
| 158 | SkyFunctions.FILE_SYMLINK_CYCLE_UNIQUENESS, new FileSymlinkCycleUniquenessFunction()); |
| 159 | |
| 160 | ImmutableMap<String, RepositoryFunction> repositoryHandlers = |
John Cater | b25a034 | 2016-10-26 10:17:40 +0000 | [diff] [blame] | 161 | ImmutableMap.of( |
| 162 | LocalRepositoryRule.NAME, (RepositoryFunction) new LocalRepositoryFunction()); |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 163 | skyFunctions.put( |
| 164 | SkyFunctions.REPOSITORY_DIRECTORY, |
janakr | ca6b46d | 2017-09-22 14:07:15 -0400 | [diff] [blame] | 165 | new RepositoryDelegatorFunction( |
Googler | 2291342 | 2019-05-02 12:42:20 -0700 | [diff] [blame] | 166 | repositoryHandlers, |
| 167 | null, |
| 168 | new AtomicBoolean(true), |
| 169 | ImmutableMap::of, |
| 170 | directories, |
ajurkowski | 2018b87 | 2020-04-14 09:59:00 -0700 | [diff] [blame] | 171 | ManagedDirectoriesKnowledge.NO_MANAGED_DIRECTORIES, |
| 172 | BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER)); |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 173 | skyFunctions.put(SkyFunctions.REPOSITORY, new RepositoryLoaderFunction()); |
| 174 | |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 175 | differencer = new SequencedRecordingDifferencer(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 176 | evaluator = new InMemoryMemoizingEvaluator(skyFunctions, differencer); |
| 177 | driver = new SequentialBuildDriver(evaluator); |
| 178 | PrecomputedValue.BUILD_ID.set(differencer, UUID.randomUUID()); |
brandjon | dbff8b8 | 2017-10-23 19:02:31 +0200 | [diff] [blame] | 179 | PrecomputedValue.PATH_PACKAGE_LOCATOR.set(differencer, pkgLocator.get()); |
laurentlb | 8c02aff | 2019-02-18 10:53:34 -0800 | [diff] [blame] | 180 | PrecomputedValue.STARLARK_SEMANTICS.set(differencer, StarlarkSemantics.DEFAULT_SEMANTICS); |
kchodorow | dfcd5da8 | 2017-04-19 18:58:50 +0200 | [diff] [blame] | 181 | RepositoryDelegatorFunction.REPOSITORY_OVERRIDES.set( |
| 182 | differencer, ImmutableMap.<RepositoryName, PathFragment>of()); |
Klaus Aehlig | 93fe20c | 2018-06-14 05:48:46 -0700 | [diff] [blame] | 183 | RepositoryDelegatorFunction.DEPENDENCY_FOR_UNCONDITIONAL_FETCHING.set( |
| 184 | differencer, RepositoryDelegatorFunction.DONT_FETCH_UNCONDITIONALLY); |
Klaus Aehlig | 8eb4748 | 2018-09-17 09:14:58 -0700 | [diff] [blame] | 185 | RepositoryDelegatorFunction.RESOLVED_FILE_INSTEAD_OF_WORKSPACE.set( |
| 186 | differencer, Optional.<RootedPath>absent()); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 187 | } |
| 188 | |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 189 | protected PackageLookupValue lookupPackage(String packageName) throws InterruptedException { |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 190 | return lookupPackage(PackageIdentifier.createInMainRepo(packageName)); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 191 | } |
| 192 | |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 193 | protected PackageLookupValue lookupPackage(PackageIdentifier packageId) |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 194 | throws InterruptedException { |
| 195 | SkyKey key = PackageLookupValue.key(packageId); |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 196 | return lookupPackage(key).get(key); |
| 197 | } |
| 198 | |
| 199 | protected EvaluationResult<PackageLookupValue> lookupPackage(SkyKey packageIdentifierSkyKey) |
| 200 | throws InterruptedException { |
Googler | 1002867 | 2018-10-25 12:14:34 -0700 | [diff] [blame] | 201 | EvaluationContext evaluationContext = |
| 202 | EvaluationContext.newBuilder() |
| 203 | .setKeepGoing(false) |
| 204 | .setNumThreads(SkyframeExecutor.DEFAULT_THREAD_COUNT) |
| 205 | .setEventHander(NullEventHandler.INSTANCE) |
| 206 | .build(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 207 | return driver.<PackageLookupValue>evaluate( |
Googler | 1002867 | 2018-10-25 12:14:34 -0700 | [diff] [blame] | 208 | ImmutableList.of(packageIdentifierSkyKey), evaluationContext); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 211 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 212 | public void testNoBuildFile() throws Exception { |
| 213 | scratch.file("parentpackage/nobuildfile/foo.txt"); |
| 214 | PackageLookupValue packageLookupValue = lookupPackage("parentpackage/nobuildfile"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 215 | assertThat(packageLookupValue.packageExists()).isFalse(); |
| 216 | assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.NO_BUILD_FILE); |
| 217 | assertThat(packageLookupValue.getErrorMsg()).isNotNull(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 220 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 221 | public void testNoBuildFileAndNoParentPackage() throws Exception { |
| 222 | scratch.file("noparentpackage/foo.txt"); |
| 223 | PackageLookupValue packageLookupValue = lookupPackage("noparentpackage"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 224 | assertThat(packageLookupValue.packageExists()).isFalse(); |
| 225 | assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.NO_BUILD_FILE); |
| 226 | assertThat(packageLookupValue.getErrorMsg()).isNotNull(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 229 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 230 | public void testDeletedPackage() throws Exception { |
| 231 | scratch.file("parentpackage/deletedpackage/BUILD"); |
| 232 | deletedPackages.set(ImmutableSet.of( |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 233 | PackageIdentifier.createInMainRepo("parentpackage/deletedpackage"))); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 234 | PackageLookupValue packageLookupValue = lookupPackage("parentpackage/deletedpackage"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 235 | assertThat(packageLookupValue.packageExists()).isFalse(); |
| 236 | assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.DELETED_PACKAGE); |
| 237 | assertThat(packageLookupValue.getErrorMsg()).isNotNull(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 240 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 241 | public void testBlacklistedPackage() throws Exception { |
| 242 | scratch.file("blacklisted/subdir/BUILD"); |
| 243 | scratch.file("blacklisted/BUILD"); |
nharmata | a32ac67 | 2020-01-08 12:54:25 -0800 | [diff] [blame] | 244 | Path blacklist = |
| 245 | scratch.overwriteFile(BLACKLISTED_PACKAGE_PREFIXES_FILE_PATH_STRING, "blacklisted"); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 246 | |
| 247 | ImmutableSet<String> pkgs = ImmutableSet.of("blacklisted/subdir", "blacklisted"); |
| 248 | for (String pkg : pkgs) { |
| 249 | PackageLookupValue packageLookupValue = lookupPackage(pkg); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 250 | assertThat(packageLookupValue.packageExists()).isFalse(); |
| 251 | assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.DELETED_PACKAGE); |
| 252 | assertThat(packageLookupValue.getErrorMsg()).isNotNull(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 253 | } |
| 254 | |
nharmata | a32ac67 | 2020-01-08 12:54:25 -0800 | [diff] [blame] | 255 | scratch.overwriteFile(BLACKLISTED_PACKAGE_PREFIXES_FILE_PATH_STRING, "not_blacklisted"); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 256 | RootedPath rootedBlacklist = |
| 257 | RootedPath.toRootedPath( |
| 258 | Root.fromPath(blacklist.getParentDirectory().getParentDirectory()), |
| 259 | PathFragment.create("config/blacklisted.txt")); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 260 | differencer.invalidate(ImmutableSet.of(FileStateValue.key(rootedBlacklist))); |
| 261 | for (String pkg : pkgs) { |
| 262 | PackageLookupValue packageLookupValue = lookupPackage(pkg); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 263 | assertThat(packageLookupValue.packageExists()).isTrue(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 267 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 268 | public void testInvalidPackageName() throws Exception { |
twerth | c4f2d80 | 2018-05-15 05:02:31 -0700 | [diff] [blame] | 269 | scratch.file("parentpackage/invalidpackagename:42/BUILD"); |
| 270 | PackageLookupValue packageLookupValue = lookupPackage("parentpackage/invalidpackagename:42"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 271 | assertThat(packageLookupValue.packageExists()).isFalse(); |
| 272 | assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.INVALID_PACKAGE_NAME); |
| 273 | assertThat(packageLookupValue.getErrorMsg()).isNotNull(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 276 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 277 | public void testDirectoryNamedBuild() throws Exception { |
| 278 | scratch.dir("parentpackage/isdirectory/BUILD"); |
| 279 | PackageLookupValue packageLookupValue = lookupPackage("parentpackage/isdirectory"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 280 | assertThat(packageLookupValue.packageExists()).isFalse(); |
| 281 | assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.NO_BUILD_FILE); |
| 282 | assertThat(packageLookupValue.getErrorMsg()).isNotNull(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 285 | @Test |
John Cater | 0c0735a | 2016-11-11 01:52:02 +0000 | [diff] [blame] | 286 | public void testEverythingIsGood_BUILD() throws Exception { |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 287 | scratch.file("parentpackage/everythinggood/BUILD"); |
| 288 | PackageLookupValue packageLookupValue = lookupPackage("parentpackage/everythinggood"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 289 | assertThat(packageLookupValue.packageExists()).isTrue(); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 290 | assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory)); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 291 | assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 294 | @Test |
John Cater | 0c0735a | 2016-11-11 01:52:02 +0000 | [diff] [blame] | 295 | public void testEverythingIsGood_BUILD_bazel() throws Exception { |
| 296 | scratch.file("parentpackage/everythinggood/BUILD.bazel"); |
| 297 | PackageLookupValue packageLookupValue = lookupPackage("parentpackage/everythinggood"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 298 | assertThat(packageLookupValue.packageExists()).isTrue(); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 299 | assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory)); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 300 | assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD_DOT_BAZEL); |
John Cater | 0c0735a | 2016-11-11 01:52:02 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | @Test |
| 304 | public void testEverythingIsGood_both() throws Exception { |
| 305 | scratch.file("parentpackage/everythinggood/BUILD"); |
| 306 | scratch.file("parentpackage/everythinggood/BUILD.bazel"); |
| 307 | PackageLookupValue packageLookupValue = lookupPackage("parentpackage/everythinggood"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 308 | assertThat(packageLookupValue.packageExists()).isTrue(); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 309 | assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory)); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 310 | assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD_DOT_BAZEL); |
John Cater | 0c0735a | 2016-11-11 01:52:02 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | @Test |
| 314 | public void testBuildFilesInMultiplePackagePaths() throws Exception { |
| 315 | scratch.file(emptyPackagePath.getPathString() + "/foo/BUILD"); |
| 316 | scratch.file("foo/BUILD.bazel"); |
| 317 | |
| 318 | // BUILD file in the first package path should be preferred to BUILD.bazel in the second. |
| 319 | PackageLookupValue packageLookupValue = lookupPackage("foo"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 320 | assertThat(packageLookupValue.packageExists()).isTrue(); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 321 | assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(emptyPackagePath)); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 322 | assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD); |
John Cater | 0c0735a | 2016-11-11 01:52:02 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 326 | public void testEmptyPackageName() throws Exception { |
| 327 | scratch.file("BUILD"); |
| 328 | PackageLookupValue packageLookupValue = lookupPackage(""); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 329 | assertThat(packageLookupValue.packageExists()).isTrue(); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 330 | assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory)); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 331 | assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 334 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 335 | public void testWorkspaceLookup() throws Exception { |
| 336 | scratch.overwriteFile("WORKSPACE"); |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 337 | PackageLookupValue packageLookupValue = lookupPackage( |
| 338 | PackageIdentifier.createInMainRepo("external")); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 339 | assertThat(packageLookupValue.packageExists()).isTrue(); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 340 | assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory)); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 341 | } |
John Cater | 9469591 | 2016-08-03 12:09:39 +0000 | [diff] [blame] | 342 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 343 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 344 | public void testPackageLookupValueHashCodeAndEqualsContract() throws Exception { |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 345 | Root root1 = Root.fromPath(rootDirectory.getRelative("root1")); |
| 346 | Root root2 = Root.fromPath(rootDirectory.getRelative("root2")); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 347 | // Our (seeming) duplication of parameters here is intentional. Some of the subclasses of |
| 348 | // PackageLookupValue are supposed to have reference equality semantics, and some are supposed |
| 349 | // to have logical equality semantics. |
| 350 | new EqualsTester() |
John Cater | 9469591 | 2016-08-03 12:09:39 +0000 | [diff] [blame] | 351 | .addEqualityGroup( |
| 352 | PackageLookupValue.success(root1, BuildFileName.BUILD), |
| 353 | PackageLookupValue.success(root1, BuildFileName.BUILD)) |
| 354 | .addEqualityGroup( |
| 355 | PackageLookupValue.success(root2, BuildFileName.BUILD), |
| 356 | PackageLookupValue.success(root2, BuildFileName.BUILD)) |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 357 | .addEqualityGroup( |
janakr | 677ba95 | 2019-03-07 09:50:01 -0800 | [diff] [blame] | 358 | PackageLookupValue.NO_BUILD_FILE_VALUE, PackageLookupValue.NO_BUILD_FILE_VALUE) |
| 359 | .addEqualityGroup( |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 360 | PackageLookupValue.DELETED_PACKAGE_VALUE, PackageLookupValue.DELETED_PACKAGE_VALUE) |
John Cater | 9469591 | 2016-08-03 12:09:39 +0000 | [diff] [blame] | 361 | .addEqualityGroup( |
| 362 | PackageLookupValue.invalidPackageName("nope1"), |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 363 | PackageLookupValue.invalidPackageName("nope1")) |
John Cater | 9469591 | 2016-08-03 12:09:39 +0000 | [diff] [blame] | 364 | .addEqualityGroup( |
| 365 | PackageLookupValue.invalidPackageName("nope2"), |
| 366 | PackageLookupValue.invalidPackageName("nope2")) |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 367 | .testEquals(); |
| 368 | } |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 369 | |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 370 | protected void createAndCheckInvalidPackageLabel(boolean expectedPackageExists) throws Exception { |
| 371 | scratch.overwriteFile("WORKSPACE", "local_repository(name='local', path='local/repo')"); |
| 372 | scratch.file("local/repo/WORKSPACE"); |
| 373 | scratch.file("local/repo/BUILD"); |
| 374 | |
| 375 | // First, use the correct label. |
| 376 | PackageLookupValue packageLookupValue = |
| 377 | lookupPackage(PackageIdentifier.create("@local", PathFragment.EMPTY_FRAGMENT)); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 378 | assertThat(packageLookupValue.packageExists()).isTrue(); |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 379 | |
| 380 | // Then, use the incorrect label. |
| 381 | packageLookupValue = lookupPackage(PackageIdentifier.createInMainRepo("local/repo")); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 382 | assertThat(packageLookupValue.packageExists()).isEqualTo(expectedPackageExists); |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 383 | } |
| 384 | |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 385 | /** |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 386 | * Runs all tests in the base {@link PackageLookupFunctionTest} class with the {@link |
| 387 | * CrossRepositoryLabelViolationStrategy#IGNORE} enum set, and also additional tests specific to |
| 388 | * that setting. |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 389 | */ |
| 390 | @RunWith(JUnit4.class) |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 391 | public static class IgnoreLabelViolationsTest extends PackageLookupFunctionTest { |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 392 | @Override |
| 393 | protected CrossRepositoryLabelViolationStrategy crossRepositoryLabelViolationStrategy() { |
| 394 | return CrossRepositoryLabelViolationStrategy.IGNORE; |
| 395 | } |
| 396 | |
| 397 | // Add any ignore-specific tests here. |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 398 | |
| 399 | @Test |
| 400 | public void testInvalidPackageLabelIsIgnored() throws Exception { |
| 401 | createAndCheckInvalidPackageLabel(true); |
| 402 | } |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /** |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 406 | * Runs all tests in the base {@link PackageLookupFunctionTest} class with the {@link |
| 407 | * CrossRepositoryLabelViolationStrategy#ERROR} enum set, and also additional tests specific to |
| 408 | * that setting. |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 409 | */ |
| 410 | @RunWith(JUnit4.class) |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 411 | public static class ErrorLabelViolationsTest extends PackageLookupFunctionTest { |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 412 | @Override |
| 413 | protected CrossRepositoryLabelViolationStrategy crossRepositoryLabelViolationStrategy() { |
| 414 | return CrossRepositoryLabelViolationStrategy.ERROR; |
| 415 | } |
| 416 | |
| 417 | // Add any error-specific tests here. |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 418 | |
| 419 | @Test |
| 420 | public void testInvalidPackageLabelIsError() throws Exception { |
| 421 | createAndCheckInvalidPackageLabel(false); |
| 422 | } |
| 423 | |
| 424 | @Test |
| 425 | public void testSymlinkCycleInWorkspace() throws Exception { |
| 426 | scratch.overwriteFile("WORKSPACE", "local_repository(name='local', path='local/repo')"); |
| 427 | Path localRepoWorkspace = scratch.resolve("local/repo/WORKSPACE"); |
| 428 | Path localRepoWorkspaceLink = scratch.resolve("local/repo/WORKSPACE.link"); |
| 429 | FileSystemUtils.createDirectoryAndParents(localRepoWorkspace.getParentDirectory()); |
| 430 | FileSystemUtils.createDirectoryAndParents(localRepoWorkspaceLink.getParentDirectory()); |
| 431 | localRepoWorkspace.createSymbolicLink(localRepoWorkspaceLink); |
| 432 | localRepoWorkspaceLink.createSymbolicLink(localRepoWorkspace); |
| 433 | scratch.file("local/repo/BUILD"); |
| 434 | |
| 435 | SkyKey skyKey = PackageLookupValue.key(PackageIdentifier.createInMainRepo("local/repo")); |
| 436 | EvaluationResult<PackageLookupValue> result = lookupPackage(skyKey); |
| 437 | assertThatEvaluationResult(result) |
| 438 | .hasErrorEntryForKeyThat(skyKey) |
| 439 | .hasExceptionThat() |
| 440 | .isInstanceOf(BuildFileNotFoundException.class); |
| 441 | assertThatEvaluationResult(result) |
| 442 | .hasErrorEntryForKeyThat(skyKey) |
| 443 | .hasExceptionThat() |
diamondm | 4d8d1d6 | 2019-03-25 16:05:02 -0700 | [diff] [blame] | 444 | .hasMessageThat() |
| 445 | .isEqualTo( |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 446 | "no such package 'local/repo': Unable to determine the local repository for " |
| 447 | + "directory /workspace/local/repo"); |
| 448 | } |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 449 | } |
John Cater | 5b4b7a3 | 2017-10-12 16:30:03 +0200 | [diff] [blame] | 450 | |
| 451 | /** Tests for detection of invalid package identifiers for local repositories. */ |
| 452 | @RunWith(Parameterized.class) |
| 453 | public static class CorrectedLocalRepositoryTest extends PackageLookupFunctionTest { |
| 454 | |
| 455 | /** |
| 456 | * Create parameters for this test. The contents are: |
| 457 | * |
| 458 | * <ol> |
| 459 | * <li>description |
| 460 | * <li>repository path |
| 461 | * <li>package path - under the repository |
| 462 | * <li>expected corrected package identifier |
| 463 | * </ol> |
| 464 | */ |
| 465 | @Parameters(name = "{0}") |
| 466 | public static List<Object[]> parameters() { |
| 467 | List<Object[]> params = new ArrayList<>(); |
| 468 | |
| 469 | params.add(new String[] {"simpleRepo_emptyPackage", "local", "", "@local//"}); |
| 470 | params.add(new String[] {"simpleRepo_singlePackage", "local", "package", "@local//package"}); |
| 471 | params.add( |
| 472 | new String[] { |
| 473 | "simpleRepo_subPackage", "local", "package/subpackage", "@local//package/subpackage" |
| 474 | }); |
| 475 | params.add(new String[] {"deepRepo_emptyPackage", "local/repo", "", "@local//"}); |
| 476 | params.add(new String[] {"deepRepo_subPackage", "local/repo", "package", "@local//package"}); |
| 477 | |
| 478 | return params; |
| 479 | } |
| 480 | |
jcater | 25fb5b1 | 2017-10-13 17:23:44 +0200 | [diff] [blame] | 481 | private final String repositoryPath; |
| 482 | private final String packagePath; |
| 483 | private final String expectedCorrectedPackageIdentifier; |
John Cater | 5b4b7a3 | 2017-10-12 16:30:03 +0200 | [diff] [blame] | 484 | |
| 485 | public CorrectedLocalRepositoryTest( |
jcater | 25fb5b1 | 2017-10-13 17:23:44 +0200 | [diff] [blame] | 486 | String unusedDescription, |
John Cater | 5b4b7a3 | 2017-10-12 16:30:03 +0200 | [diff] [blame] | 487 | String repositoryPath, |
| 488 | String packagePath, |
| 489 | String expectedCorrectedPackageIdentifier) { |
John Cater | 5b4b7a3 | 2017-10-12 16:30:03 +0200 | [diff] [blame] | 490 | this.repositoryPath = repositoryPath; |
| 491 | this.packagePath = packagePath; |
| 492 | this.expectedCorrectedPackageIdentifier = expectedCorrectedPackageIdentifier; |
| 493 | } |
| 494 | |
| 495 | @Override |
| 496 | protected CrossRepositoryLabelViolationStrategy crossRepositoryLabelViolationStrategy() { |
| 497 | return CrossRepositoryLabelViolationStrategy.ERROR; |
| 498 | } |
| 499 | |
| 500 | @Test |
| 501 | public void testCorrectPackageDetection_relativePath() throws Exception { |
John Cater | 5b4b7a3 | 2017-10-12 16:30:03 +0200 | [diff] [blame] | 502 | scratch.overwriteFile( |
| 503 | "WORKSPACE", "local_repository(name='local', path='" + repositoryPath + "')"); |
| 504 | scratch.file(PathFragment.create(repositoryPath).getRelative("WORKSPACE").getPathString()); |
| 505 | scratch.file( |
| 506 | PathFragment.create(repositoryPath) |
| 507 | .getRelative(packagePath) |
| 508 | .getRelative("BUILD") |
| 509 | .getPathString()); |
| 510 | |
| 511 | PackageIdentifier packageIdentifier = |
| 512 | PackageIdentifier.createInMainRepo( |
| 513 | PathFragment.create(repositoryPath).getRelative(packagePath)); |
| 514 | PackageLookupValue packageLookupValue = lookupPackage(packageIdentifier); |
| 515 | assertThat(packageLookupValue.packageExists()).isFalse(); |
| 516 | assertThat(packageLookupValue) |
| 517 | .isInstanceOf(IncorrectRepositoryReferencePackageLookupValue.class); |
| 518 | |
| 519 | IncorrectRepositoryReferencePackageLookupValue incorrectPackageLookupValue = |
| 520 | (IncorrectRepositoryReferencePackageLookupValue) packageLookupValue; |
| 521 | assertThat(incorrectPackageLookupValue.getInvalidPackageIdentifier()) |
| 522 | .isEqualTo(packageIdentifier); |
| 523 | assertThat(incorrectPackageLookupValue.getCorrectedPackageIdentifier().toString()) |
| 524 | .isEqualTo(expectedCorrectedPackageIdentifier); |
| 525 | } |
| 526 | |
| 527 | @Test |
| 528 | public void testCorrectPackageDetection_absolutePath() throws Exception { |
John Cater | 5b4b7a3 | 2017-10-12 16:30:03 +0200 | [diff] [blame] | 529 | scratch.overwriteFile( |
| 530 | "WORKSPACE", |
| 531 | "local_repository(name='local', path=__workspace_dir__ + '/" + repositoryPath + "')"); |
| 532 | scratch.file(PathFragment.create(repositoryPath).getRelative("WORKSPACE").getPathString()); |
| 533 | scratch.file( |
| 534 | PathFragment.create(repositoryPath) |
| 535 | .getRelative(packagePath) |
| 536 | .getRelative("BUILD") |
| 537 | .getPathString()); |
| 538 | |
| 539 | PackageIdentifier packageIdentifier = |
| 540 | PackageIdentifier.createInMainRepo( |
| 541 | PathFragment.create(repositoryPath).getRelative(packagePath)); |
| 542 | PackageLookupValue packageLookupValue = lookupPackage(packageIdentifier); |
| 543 | assertThat(packageLookupValue.packageExists()).isFalse(); |
| 544 | assertThat(packageLookupValue) |
| 545 | .isInstanceOf(IncorrectRepositoryReferencePackageLookupValue.class); |
| 546 | |
| 547 | IncorrectRepositoryReferencePackageLookupValue incorrectPackageLookupValue = |
| 548 | (IncorrectRepositoryReferencePackageLookupValue) packageLookupValue; |
| 549 | assertThat(incorrectPackageLookupValue.getInvalidPackageIdentifier()) |
| 550 | .isEqualTo(packageIdentifier); |
| 551 | assertThat(incorrectPackageLookupValue.getCorrectedPackageIdentifier().toString()) |
| 552 | .isEqualTo(expectedCorrectedPackageIdentifier); |
| 553 | } |
| 554 | } |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 555 | } |