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