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; |
janakr | 13b737a | 2021-07-02 14:24:25 -0700 | [diff] [blame] | 26 | import com.google.devtools.build.lib.actions.ThreadStateReceiver; |
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; |
janakr | e2af68f | 2021-03-18 15:11:30 -0700 | [diff] [blame] | 33 | import com.google.devtools.build.lib.io.FileSymlinkCycleUniquenessFunction; |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 34 | import com.google.devtools.build.lib.packages.BuildFileName; |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 35 | import com.google.devtools.build.lib.packages.BuildFileNotFoundException; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 36 | import com.google.devtools.build.lib.packages.RuleClassProvider; |
janakr | 15e15c2 | 2019-01-30 11:24:49 -0800 | [diff] [blame] | 37 | import com.google.devtools.build.lib.packages.WorkspaceFileValue; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 38 | import com.google.devtools.build.lib.pkgcache.PathPackageLocator; |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 39 | import com.google.devtools.build.lib.rules.repository.LocalRepositoryFunction; |
| 40 | import com.google.devtools.build.lib.rules.repository.LocalRepositoryRule; |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 41 | import com.google.devtools.build.lib.rules.repository.RepositoryDelegatorFunction; |
| 42 | import com.google.devtools.build.lib.rules.repository.RepositoryFunction; |
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; |
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; |
adonovan | 450c7ad | 2020-09-14 13:00:21 -0700 | [diff] [blame] | 73 | import net.starlark.java.eval.StarlarkSemantics; |
John Cater | 9469591 | 2016-08-03 12:09:39 +0000 | [diff] [blame] | 74 | import org.junit.Before; |
| 75 | import org.junit.Test; |
| 76 | import org.junit.runner.RunWith; |
| 77 | import org.junit.runners.JUnit4; |
John Cater | 5b4b7a3 | 2017-10-12 16:30:03 +0200 | [diff] [blame] | 78 | import org.junit.runners.Parameterized; |
| 79 | import org.junit.runners.Parameterized.Parameters; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 80 | |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 81 | /** Tests for {@link PackageLookupFunction}. */ |
| 82 | public abstract class PackageLookupFunctionTest extends FoundationTestCase { |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 83 | private AtomicReference<ImmutableSet<PackageIdentifier>> deletedPackages; |
| 84 | private MemoizingEvaluator evaluator; |
| 85 | private SequentialBuildDriver driver; |
| 86 | private RecordingDifferencer differencer; |
John Cater | 0c0735a | 2016-11-11 01:52:02 +0000 | [diff] [blame] | 87 | private Path emptyPackagePath; |
kkress | 1847a01 | 2020-06-24 12:30:11 -0700 | [diff] [blame] | 88 | 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] | 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( |
janakr | e4fcd47 | 2021-05-26 08:47:39 -0700 | [diff] [blame] | 123 | SkyFunctions.PACKAGE, |
| 124 | new PackageFunction( |
| 125 | null, |
| 126 | null, |
| 127 | null, |
| 128 | null, |
| 129 | null, |
janakr | e4fcd47 | 2021-05-26 08:47:39 -0700 | [diff] [blame] | 130 | /*packageProgress=*/ null, |
| 131 | PackageFunction.ActionOnIOExceptionReadingBuildFile.UseOriginalIOException.INSTANCE, |
janakr | 13b737a | 2021-07-02 14:24:25 -0700 | [diff] [blame] | 132 | PackageFunction.IncrementalityIntent.INCREMENTAL, |
| 133 | k -> ThreadStateReceiver.NULL_INSTANCE)); |
shahan | 602cc85 | 2018-06-06 20:09:57 -0700 | [diff] [blame] | 134 | skyFunctions.put( |
| 135 | FileStateValue.FILE_STATE, |
| 136 | new FileStateFunction( |
djasper | 7faa0ef | 2019-03-28 10:00:00 -0700 | [diff] [blame] | 137 | new AtomicReference<TimestampGranularityMonitor>(), |
Googler | 166c3a3 | 2019-11-05 13:25:10 -0800 | [diff] [blame] | 138 | new AtomicReference<>(UnixGlob.DEFAULT_SYSCALLS), |
djasper | 7faa0ef | 2019-03-28 10:00:00 -0700 | [diff] [blame] | 139 | externalFilesHelper)); |
shahan | 602cc85 | 2018-06-06 20:09:57 -0700 | [diff] [blame] | 140 | skyFunctions.put(FileValue.FILE, new FileFunction(pkgLocator)); |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 141 | skyFunctions.put(SkyFunctions.DIRECTORY_LISTING, new DirectoryListingFunction()); |
| 142 | skyFunctions.put( |
| 143 | SkyFunctions.DIRECTORY_LISTING_STATE, |
djasper | b109a5f | 2019-02-14 01:05:34 -0800 | [diff] [blame] | 144 | new DirectoryListingStateFunction( |
| 145 | externalFilesHelper, new AtomicReference<>(UnixGlob.DEFAULT_SYSCALLS))); |
nharmata | a32ac67 | 2020-01-08 12:54:25 -0800 | [diff] [blame] | 146 | skyFunctions.put( |
kkress | 1847a01 | 2020-06-24 12:30:11 -0700 | [diff] [blame] | 147 | SkyFunctions.IGNORED_PACKAGE_PREFIXES, |
| 148 | new IgnoredPackagePrefixesFunction( |
| 149 | PathFragment.create(IGNORED_PACKAGE_PREFIXES_FILE_PATH_STRING))); |
Ulf Adams | 015aad9 | 2016-07-13 16:49:40 +0000 | [diff] [blame] | 150 | RuleClassProvider ruleClassProvider = analysisMock.createRuleClassProvider(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 151 | skyFunctions.put( |
janakr | 15e15c2 | 2019-01-30 11:24:49 -0800 | [diff] [blame] | 152 | WorkspaceFileValue.WORKSPACE_FILE, |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 153 | new WorkspaceFileFunction( |
| 154 | ruleClassProvider, |
Ulf Adams | 015aad9 | 2016-07-13 16:49:40 +0000 | [diff] [blame] | 155 | analysisMock |
janakr | 52d05e8 | 2017-09-22 13:27:14 -0400 | [diff] [blame] | 156 | .getPackageFactoryBuilderForTesting(directories) |
nharmata | fde0bd2f | 2018-12-21 10:17:56 -0800 | [diff] [blame] | 157 | .build(ruleClassProvider, fileSystem), |
mjhalupka | f0e4811 | 2019-01-14 13:01:56 -0800 | [diff] [blame] | 158 | directories, |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 159 | /*bzlLoadFunctionForInlining=*/ null)); |
ajurkowski | 2018b87 | 2020-04-14 09:59:00 -0700 | [diff] [blame] | 160 | 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 Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 166 | skyFunctions.put( |
janakr | e2af68f | 2021-03-18 15:11:30 -0700 | [diff] [blame] | 167 | FileSymlinkCycleUniquenessFunction.NAME, new FileSymlinkCycleUniquenessFunction()); |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 168 | |
| 169 | ImmutableMap<String, RepositoryFunction> repositoryHandlers = |
John Cater | b25a034 | 2016-10-26 10:17:40 +0000 | [diff] [blame] | 170 | ImmutableMap.of( |
| 171 | LocalRepositoryRule.NAME, (RepositoryFunction) new LocalRepositoryFunction()); |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 172 | skyFunctions.put( |
| 173 | SkyFunctions.REPOSITORY_DIRECTORY, |
janakr | ca6b46d | 2017-09-22 14:07:15 -0400 | [diff] [blame] | 174 | new RepositoryDelegatorFunction( |
Googler | 2291342 | 2019-05-02 12:42:20 -0700 | [diff] [blame] | 175 | repositoryHandlers, |
| 176 | null, |
| 177 | new AtomicBoolean(true), |
| 178 | ImmutableMap::of, |
| 179 | directories, |
ajurkowski | 2018b87 | 2020-04-14 09:59:00 -0700 | [diff] [blame] | 180 | ManagedDirectoriesKnowledge.NO_MANAGED_DIRECTORIES, |
| 181 | BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER)); |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 182 | |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 183 | differencer = new SequencedRecordingDifferencer(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 184 | evaluator = new InMemoryMemoizingEvaluator(skyFunctions, differencer); |
| 185 | driver = new SequentialBuildDriver(evaluator); |
| 186 | PrecomputedValue.BUILD_ID.set(differencer, UUID.randomUUID()); |
brandjon | dbff8b8 | 2017-10-23 19:02:31 +0200 | [diff] [blame] | 187 | PrecomputedValue.PATH_PACKAGE_LOCATOR.set(differencer, pkgLocator.get()); |
adonovan | b85d0b7 | 2020-05-08 11:59:19 -0700 | [diff] [blame] | 188 | PrecomputedValue.STARLARK_SEMANTICS.set(differencer, StarlarkSemantics.DEFAULT); |
kchodorow | dfcd5da8 | 2017-04-19 18:58:50 +0200 | [diff] [blame] | 189 | RepositoryDelegatorFunction.REPOSITORY_OVERRIDES.set( |
| 190 | differencer, ImmutableMap.<RepositoryName, PathFragment>of()); |
Klaus Aehlig | 93fe20c | 2018-06-14 05:48:46 -0700 | [diff] [blame] | 191 | RepositoryDelegatorFunction.DEPENDENCY_FOR_UNCONDITIONAL_FETCHING.set( |
| 192 | differencer, RepositoryDelegatorFunction.DONT_FETCH_UNCONDITIONALLY); |
Klaus Aehlig | 8eb4748 | 2018-09-17 09:14:58 -0700 | [diff] [blame] | 193 | RepositoryDelegatorFunction.RESOLVED_FILE_INSTEAD_OF_WORKSPACE.set( |
jhorvitz | dd1d841 | 2020-08-01 05:59:14 -0700 | [diff] [blame] | 194 | differencer, Optional.empty()); |
pcloudy | 7bc0199 | 2021-07-13 03:16:52 -0700 | [diff] [blame] | 195 | RepositoryDelegatorFunction.ENABLE_BZLMOD.set(differencer, false); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 196 | } |
| 197 | |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 198 | protected PackageLookupValue lookupPackage(String packageName) throws InterruptedException { |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 199 | return lookupPackage(PackageIdentifier.createInMainRepo(packageName)); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 200 | } |
| 201 | |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 202 | protected PackageLookupValue lookupPackage(PackageIdentifier packageId) |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 203 | throws InterruptedException { |
| 204 | SkyKey key = PackageLookupValue.key(packageId); |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 205 | return lookupPackage(key).get(key); |
| 206 | } |
| 207 | |
| 208 | protected EvaluationResult<PackageLookupValue> lookupPackage(SkyKey packageIdentifierSkyKey) |
| 209 | throws InterruptedException { |
Googler | 1002867 | 2018-10-25 12:14:34 -0700 | [diff] [blame] | 210 | EvaluationContext evaluationContext = |
| 211 | EvaluationContext.newBuilder() |
| 212 | .setKeepGoing(false) |
| 213 | .setNumThreads(SkyframeExecutor.DEFAULT_THREAD_COUNT) |
michajlo | 7a485be | 2020-07-30 11:08:46 -0700 | [diff] [blame] | 214 | .setEventHandler(NullEventHandler.INSTANCE) |
Googler | 1002867 | 2018-10-25 12:14:34 -0700 | [diff] [blame] | 215 | .build(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 216 | return driver.<PackageLookupValue>evaluate( |
Googler | 1002867 | 2018-10-25 12:14:34 -0700 | [diff] [blame] | 217 | ImmutableList.of(packageIdentifierSkyKey), evaluationContext); |
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 testNoBuildFile() throws Exception { |
| 222 | scratch.file("parentpackage/nobuildfile/foo.txt"); |
| 223 | PackageLookupValue packageLookupValue = lookupPackage("parentpackage/nobuildfile"); |
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 testNoBuildFileAndNoParentPackage() throws Exception { |
| 231 | scratch.file("noparentpackage/foo.txt"); |
| 232 | PackageLookupValue packageLookupValue = lookupPackage("noparentpackage"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 233 | assertThat(packageLookupValue.packageExists()).isFalse(); |
| 234 | assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.NO_BUILD_FILE); |
| 235 | assertThat(packageLookupValue.getErrorMsg()).isNotNull(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 238 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 239 | public void testDeletedPackage() throws Exception { |
| 240 | scratch.file("parentpackage/deletedpackage/BUILD"); |
| 241 | deletedPackages.set(ImmutableSet.of( |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 242 | PackageIdentifier.createInMainRepo("parentpackage/deletedpackage"))); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 243 | PackageLookupValue packageLookupValue = lookupPackage("parentpackage/deletedpackage"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 244 | assertThat(packageLookupValue.packageExists()).isFalse(); |
| 245 | assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.DELETED_PACKAGE); |
| 246 | assertThat(packageLookupValue.getErrorMsg()).isNotNull(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 249 | @Test |
kkress | 1847a01 | 2020-06-24 12:30:11 -0700 | [diff] [blame] | 250 | 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 Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 254 | |
kkress | 1847a01 | 2020-06-24 12:30:11 -0700 | [diff] [blame] | 255 | ImmutableSet<String> pkgs = ImmutableSet.of("ignored/subdir", "ignored"); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 256 | for (String pkg : pkgs) { |
| 257 | PackageLookupValue packageLookupValue = lookupPackage(pkg); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 258 | assertThat(packageLookupValue.packageExists()).isFalse(); |
| 259 | assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.DELETED_PACKAGE); |
| 260 | assertThat(packageLookupValue.getErrorMsg()).isNotNull(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 261 | } |
| 262 | |
kkress | 1847a01 | 2020-06-24 12:30:11 -0700 | [diff] [blame] | 263 | scratch.overwriteFile(IGNORED_PACKAGE_PREFIXES_FILE_PATH_STRING, "not_ignored"); |
| 264 | RootedPath rootedIgnoreFile = |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 265 | RootedPath.toRootedPath( |
kkress | 1847a01 | 2020-06-24 12:30:11 -0700 | [diff] [blame] | 266 | Root.fromPath(ignored.getParentDirectory().getParentDirectory()), |
| 267 | PathFragment.create("config/ignored.txt")); |
| 268 | differencer.invalidate(ImmutableSet.of(FileStateValue.key(rootedIgnoreFile))); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 269 | for (String pkg : pkgs) { |
| 270 | PackageLookupValue packageLookupValue = lookupPackage(pkg); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 271 | assertThat(packageLookupValue.packageExists()).isTrue(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 275 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 276 | public void testInvalidPackageName() throws Exception { |
twerth | c4f2d80 | 2018-05-15 05:02:31 -0700 | [diff] [blame] | 277 | scratch.file("parentpackage/invalidpackagename:42/BUILD"); |
| 278 | PackageLookupValue packageLookupValue = lookupPackage("parentpackage/invalidpackagename:42"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 279 | assertThat(packageLookupValue.packageExists()).isFalse(); |
| 280 | assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.INVALID_PACKAGE_NAME); |
| 281 | assertThat(packageLookupValue.getErrorMsg()).isNotNull(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 284 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 285 | public void testDirectoryNamedBuild() throws Exception { |
| 286 | scratch.dir("parentpackage/isdirectory/BUILD"); |
| 287 | PackageLookupValue packageLookupValue = lookupPackage("parentpackage/isdirectory"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 288 | assertThat(packageLookupValue.packageExists()).isFalse(); |
| 289 | assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.NO_BUILD_FILE); |
| 290 | assertThat(packageLookupValue.getErrorMsg()).isNotNull(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 293 | @Test |
John Cater | 0c0735a | 2016-11-11 01:52:02 +0000 | [diff] [blame] | 294 | public void testEverythingIsGood_BUILD() throws Exception { |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 295 | scratch.file("parentpackage/everythinggood/BUILD"); |
| 296 | PackageLookupValue packageLookupValue = lookupPackage("parentpackage/everythinggood"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 297 | assertThat(packageLookupValue.packageExists()).isTrue(); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 298 | assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory)); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 299 | assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 302 | @Test |
John Cater | 0c0735a | 2016-11-11 01:52:02 +0000 | [diff] [blame] | 303 | public void testEverythingIsGood_BUILD_bazel() throws Exception { |
| 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 testEverythingIsGood_both() throws Exception { |
| 313 | scratch.file("parentpackage/everythinggood/BUILD"); |
| 314 | scratch.file("parentpackage/everythinggood/BUILD.bazel"); |
| 315 | PackageLookupValue packageLookupValue = lookupPackage("parentpackage/everythinggood"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 316 | assertThat(packageLookupValue.packageExists()).isTrue(); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 317 | assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory)); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 318 | assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD_DOT_BAZEL); |
John Cater | 0c0735a | 2016-11-11 01:52:02 +0000 | [diff] [blame] | 319 | } |
| 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"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 328 | assertThat(packageLookupValue.packageExists()).isTrue(); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 329 | assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(emptyPackagePath)); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 330 | assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD); |
John Cater | 0c0735a | 2016-11-11 01:52:02 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 334 | public void testEmptyPackageName() throws Exception { |
| 335 | scratch.file("BUILD"); |
| 336 | PackageLookupValue packageLookupValue = lookupPackage(""); |
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)); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 339 | assertThat(packageLookupValue.getBuildFileName()).isEqualTo(BuildFileName.BUILD); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 342 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 343 | public void testWorkspaceLookup() throws Exception { |
| 344 | scratch.overwriteFile("WORKSPACE"); |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 345 | PackageLookupValue packageLookupValue = lookupPackage( |
| 346 | PackageIdentifier.createInMainRepo("external")); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 347 | assertThat(packageLookupValue.packageExists()).isTrue(); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 348 | assertThat(packageLookupValue.getRoot()).isEqualTo(Root.fromPath(rootDirectory)); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 349 | } |
John Cater | 9469591 | 2016-08-03 12:09:39 +0000 | [diff] [blame] | 350 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 351 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 352 | public void testPackageLookupValueHashCodeAndEqualsContract() throws Exception { |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 353 | Root root1 = Root.fromPath(rootDirectory.getRelative("root1")); |
| 354 | Root root2 = Root.fromPath(rootDirectory.getRelative("root2")); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 355 | // 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 Cater | 9469591 | 2016-08-03 12:09:39 +0000 | [diff] [blame] | 359 | .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 Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 365 | .addEqualityGroup( |
janakr | 677ba95 | 2019-03-07 09:50:01 -0800 | [diff] [blame] | 366 | PackageLookupValue.NO_BUILD_FILE_VALUE, PackageLookupValue.NO_BUILD_FILE_VALUE) |
| 367 | .addEqualityGroup( |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 368 | PackageLookupValue.DELETED_PACKAGE_VALUE, PackageLookupValue.DELETED_PACKAGE_VALUE) |
John Cater | 9469591 | 2016-08-03 12:09:39 +0000 | [diff] [blame] | 369 | .addEqualityGroup( |
| 370 | PackageLookupValue.invalidPackageName("nope1"), |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 371 | PackageLookupValue.invalidPackageName("nope1")) |
John Cater | 9469591 | 2016-08-03 12:09:39 +0000 | [diff] [blame] | 372 | .addEqualityGroup( |
| 373 | PackageLookupValue.invalidPackageName("nope2"), |
| 374 | PackageLookupValue.invalidPackageName("nope2")) |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 375 | .testEquals(); |
| 376 | } |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 377 | |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 378 | 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 = |
Googler | 80ada0f | 2021-12-22 07:15:59 -0800 | [diff] [blame] | 385 | lookupPackage(PackageIdentifier.create("@local", PathFragment.EMPTY_FRAGMENT)); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 386 | assertThat(packageLookupValue.packageExists()).isTrue(); |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 387 | |
| 388 | // Then, use the incorrect label. |
| 389 | packageLookupValue = lookupPackage(PackageIdentifier.createInMainRepo("local/repo")); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 390 | assertThat(packageLookupValue.packageExists()).isEqualTo(expectedPackageExists); |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 391 | } |
| 392 | |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 393 | /** |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 394 | * 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 Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 397 | */ |
| 398 | @RunWith(JUnit4.class) |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 399 | public static class IgnoreLabelViolationsTest extends PackageLookupFunctionTest { |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 400 | @Override |
| 401 | protected CrossRepositoryLabelViolationStrategy crossRepositoryLabelViolationStrategy() { |
| 402 | return CrossRepositoryLabelViolationStrategy.IGNORE; |
| 403 | } |
| 404 | |
| 405 | // Add any ignore-specific tests here. |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 406 | |
| 407 | @Test |
| 408 | public void testInvalidPackageLabelIsIgnored() throws Exception { |
| 409 | createAndCheckInvalidPackageLabel(true); |
| 410 | } |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | /** |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 414 | * 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 Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 417 | */ |
| 418 | @RunWith(JUnit4.class) |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 419 | public static class ErrorLabelViolationsTest extends PackageLookupFunctionTest { |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 420 | @Override |
| 421 | protected CrossRepositoryLabelViolationStrategy crossRepositoryLabelViolationStrategy() { |
| 422 | return CrossRepositoryLabelViolationStrategy.ERROR; |
| 423 | } |
| 424 | |
| 425 | // Add any error-specific tests here. |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 426 | |
| 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() |
diamondm | 4d8d1d6 | 2019-03-25 16:05:02 -0700 | [diff] [blame] | 452 | .hasMessageThat() |
| 453 | .isEqualTo( |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 454 | "no such package 'local/repo': Unable to determine the local repository for " |
| 455 | + "directory /workspace/local/repo"); |
| 456 | } |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 457 | } |
John Cater | 5b4b7a3 | 2017-10-12 16:30:03 +0200 | [diff] [blame] | 458 | |
| 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 | |
jcater | 25fb5b1 | 2017-10-13 17:23:44 +0200 | [diff] [blame] | 489 | private final String repositoryPath; |
| 490 | private final String packagePath; |
| 491 | private final String expectedCorrectedPackageIdentifier; |
John Cater | 5b4b7a3 | 2017-10-12 16:30:03 +0200 | [diff] [blame] | 492 | |
| 493 | public CorrectedLocalRepositoryTest( |
jcater | 25fb5b1 | 2017-10-13 17:23:44 +0200 | [diff] [blame] | 494 | String unusedDescription, |
John Cater | 5b4b7a3 | 2017-10-12 16:30:03 +0200 | [diff] [blame] | 495 | String repositoryPath, |
| 496 | String packagePath, |
| 497 | String expectedCorrectedPackageIdentifier) { |
John Cater | 5b4b7a3 | 2017-10-12 16:30:03 +0200 | [diff] [blame] | 498 | 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 Cater | 5b4b7a3 | 2017-10-12 16:30:03 +0200 | [diff] [blame] | 510 | 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 Cater | 5b4b7a3 | 2017-10-12 16:30:03 +0200 | [diff] [blame] | 537 | 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 Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 563 | } |