Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +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 | package com.google.devtools.build.lib.skyframe; |
| 15 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 16 | import static com.google.common.truth.Truth.assertThat; |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 17 | |
Klaus Aehlig | 8eb4748 | 2018-09-17 09:14:58 -0700 | [diff] [blame] | 18 | import com.google.common.base.Optional; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
John Cater | 4593296 | 2017-09-06 21:57:46 +0200 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableMap; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import com.google.common.testing.EqualsTester; |
shahan | 602cc85 | 2018-06-06 20:09:57 -0700 | [diff] [blame] | 23 | import com.google.devtools.build.lib.actions.FileStateValue; |
| 24 | import com.google.devtools.build.lib.actions.FileValue; |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.analysis.BlazeDirectories; |
janakr | 3b63a4e | 2017-09-14 09:55:40 +0200 | [diff] [blame] | 26 | import com.google.devtools.build.lib.analysis.ServerDirectories; |
John Cater | 4593296 | 2017-09-06 21:57:46 +0200 | [diff] [blame] | 27 | import com.google.devtools.build.lib.analysis.util.AnalysisMock; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
John Cater | 4593296 | 2017-09-06 21:57:46 +0200 | [diff] [blame] | 29 | import com.google.devtools.build.lib.cmdline.RepositoryName; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.events.NullEventHandler; |
John Cater | 4593296 | 2017-09-06 21:57:46 +0200 | [diff] [blame] | 31 | import com.google.devtools.build.lib.packages.RuleClassProvider; |
janakr | 15e15c2 | 2019-01-30 11:24:49 -0800 | [diff] [blame] | 32 | import com.google.devtools.build.lib.packages.WorkspaceFileValue; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.pkgcache.PathPackageLocator; |
John Cater | 4593296 | 2017-09-06 21:57:46 +0200 | [diff] [blame] | 34 | import com.google.devtools.build.lib.rules.repository.LocalRepositoryFunction; |
| 35 | import com.google.devtools.build.lib.rules.repository.LocalRepositoryRule; |
| 36 | import com.google.devtools.build.lib.rules.repository.RepositoryDelegatorFunction; |
| 37 | import com.google.devtools.build.lib.rules.repository.RepositoryFunction; |
| 38 | import com.google.devtools.build.lib.rules.repository.RepositoryLoaderFunction; |
Googler | 06eb1bb | 2019-02-26 15:33:15 -0800 | [diff] [blame] | 39 | import com.google.devtools.build.lib.skyframe.ContainingPackageLookupValue.ContainingPackage; |
| 40 | import com.google.devtools.build.lib.skyframe.ContainingPackageLookupValue.NoContainingPackage; |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 41 | import com.google.devtools.build.lib.skyframe.ExternalFilesHelper.ExternalFileAction; |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 42 | import com.google.devtools.build.lib.skyframe.PackageLookupFunction.CrossRepositoryLabelViolationStrategy; |
Googler | 06eb1bb | 2019-02-26 15:33:15 -0800 | [diff] [blame] | 43 | import com.google.devtools.build.lib.skyframe.PackageLookupValue.ErrorReason; |
laurentlb | 6659b4c | 2019-02-18 07:23:36 -0800 | [diff] [blame] | 44 | import com.google.devtools.build.lib.syntax.StarlarkSemantics; |
Florian Weikert | cca703a | 2015-12-07 09:56:38 +0000 | [diff] [blame] | 45 | import com.google.devtools.build.lib.testutil.FoundationTestCase; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 46 | import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor; |
Eric Fellheimer | e040fe9 | 2015-11-09 23:54:46 +0000 | [diff] [blame] | 47 | import com.google.devtools.build.lib.vfs.PathFragment; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 48 | import com.google.devtools.build.lib.vfs.Root; |
Klaus Aehlig | 8eb4748 | 2018-09-17 09:14:58 -0700 | [diff] [blame] | 49 | import com.google.devtools.build.lib.vfs.RootedPath; |
djasper | b109a5f | 2019-02-14 01:05:34 -0800 | [diff] [blame] | 50 | import com.google.devtools.build.lib.vfs.UnixGlob; |
Googler | 1002867 | 2018-10-25 12:14:34 -0700 | [diff] [blame] | 51 | import com.google.devtools.build.skyframe.EvaluationContext; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 52 | import com.google.devtools.build.skyframe.InMemoryMemoizingEvaluator; |
| 53 | import com.google.devtools.build.skyframe.MemoizingEvaluator; |
| 54 | import com.google.devtools.build.skyframe.RecordingDifferencer; |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 55 | import com.google.devtools.build.skyframe.SequencedRecordingDifferencer; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 56 | import com.google.devtools.build.skyframe.SequentialBuildDriver; |
| 57 | import com.google.devtools.build.skyframe.SkyFunction; |
| 58 | import com.google.devtools.build.skyframe.SkyFunctionName; |
| 59 | import com.google.devtools.build.skyframe.SkyKey; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 60 | import java.util.HashMap; |
| 61 | import java.util.Map; |
| 62 | import java.util.UUID; |
John Cater | 4593296 | 2017-09-06 21:57:46 +0200 | [diff] [blame] | 63 | import java.util.concurrent.atomic.AtomicBoolean; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 64 | import java.util.concurrent.atomic.AtomicReference; |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 65 | import org.junit.Before; |
| 66 | import org.junit.Test; |
| 67 | import org.junit.runner.RunWith; |
| 68 | import org.junit.runners.JUnit4; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * Tests for {@link ContainingPackageLookupFunction}. |
| 72 | */ |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 73 | @RunWith(JUnit4.class) |
Florian Weikert | cca703a | 2015-12-07 09:56:38 +0000 | [diff] [blame] | 74 | public class ContainingPackageLookupFunctionTest extends FoundationTestCase { |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 75 | |
| 76 | private AtomicReference<ImmutableSet<PackageIdentifier>> deletedPackages; |
| 77 | private MemoizingEvaluator evaluator; |
| 78 | private SequentialBuildDriver driver; |
John Cater | 4593296 | 2017-09-06 21:57:46 +0200 | [diff] [blame] | 79 | private RecordingDifferencer differencer; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 80 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 81 | @Before |
| 82 | public final void setUp() throws Exception { |
John Cater | 4593296 | 2017-09-06 21:57:46 +0200 | [diff] [blame] | 83 | AnalysisMock analysisMock = AnalysisMock.get(); |
| 84 | |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 85 | AtomicReference<PathPackageLocator> pkgLocator = |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 86 | new AtomicReference<>( |
| 87 | new PathPackageLocator( |
| 88 | outputBase, |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 89 | ImmutableList.of(Root.fromPath(rootDirectory)), |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 90 | BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 91 | deletedPackages = new AtomicReference<>(ImmutableSet.<PackageIdentifier>of()); |
John Cater | 4593296 | 2017-09-06 21:57:46 +0200 | [diff] [blame] | 92 | BlazeDirectories directories = |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 93 | new BlazeDirectories( |
Klaus Aehlig | c2499c4 | 2018-02-27 05:47:21 -0800 | [diff] [blame] | 94 | new ServerDirectories(rootDirectory, outputBase, outputBase), |
janakr | 3b63a4e | 2017-09-14 09:55:40 +0200 | [diff] [blame] | 95 | rootDirectory, |
cushon | 849df36 | 2018-05-14 01:51:45 -0700 | [diff] [blame] | 96 | /* defaultSystemJavabase= */ null, |
janakr | 3b63a4e | 2017-09-14 09:55:40 +0200 | [diff] [blame] | 97 | analysisMock.getProductName()); |
John Cater | 4593296 | 2017-09-06 21:57:46 +0200 | [diff] [blame] | 98 | ExternalFilesHelper externalFilesHelper = |
nharmata | 3fb7d34 | 2018-02-23 11:37:51 -0800 | [diff] [blame] | 99 | ExternalFilesHelper.createForTesting( |
John Cater | 4593296 | 2017-09-06 21:57:46 +0200 | [diff] [blame] | 100 | pkgLocator, |
| 101 | ExternalFileAction.DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_PATHS, |
| 102 | directories); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 103 | |
| 104 | Map<SkyFunctionName, SkyFunction> skyFunctions = new HashMap<>(); |
John Cater | 4593296 | 2017-09-06 21:57:46 +0200 | [diff] [blame] | 105 | skyFunctions.put(SkyFunctions.CONTAINING_PACKAGE_LOOKUP, new ContainingPackageLookupFunction()); |
| 106 | |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 107 | skyFunctions.put( |
| 108 | SkyFunctions.PACKAGE_LOOKUP, |
John Cater | 0c0735a | 2016-11-11 01:52:02 +0000 | [diff] [blame] | 109 | new PackageLookupFunction( |
| 110 | deletedPackages, |
| 111 | CrossRepositoryLabelViolationStrategy.ERROR, |
ajurkowski | 2018b87 | 2020-04-14 09:59:00 -0700 | [diff] [blame] | 112 | BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY, |
| 113 | BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER)); |
John Cater | 4593296 | 2017-09-06 21:57:46 +0200 | [diff] [blame] | 114 | skyFunctions.put( |
ajurkowski | 2018b87 | 2020-04-14 09:59:00 -0700 | [diff] [blame] | 115 | SkyFunctions.PACKAGE, new PackageFunction(null, null, null, null, null, null, null, null)); |
nharmata | a32ac67 | 2020-01-08 12:54:25 -0800 | [diff] [blame] | 116 | skyFunctions.put( |
| 117 | SkyFunctions.BLACKLISTED_PACKAGE_PREFIXES, |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 118 | new BlacklistedPackagePrefixesFunction( |
nharmata | a32ac67 | 2020-01-08 12:54:25 -0800 | [diff] [blame] | 119 | /*blacklistedPackagePrefixesFile=*/ PathFragment.EMPTY_FRAGMENT)); |
shahan | 602cc85 | 2018-06-06 20:09:57 -0700 | [diff] [blame] | 120 | skyFunctions.put( |
| 121 | FileStateValue.FILE_STATE, |
| 122 | new FileStateFunction( |
djasper | 7faa0ef | 2019-03-28 10:00:00 -0700 | [diff] [blame] | 123 | new AtomicReference<TimestampGranularityMonitor>(), |
| 124 | new AtomicReference<>(UnixGlob.DEFAULT_SYSCALLS), |
| 125 | externalFilesHelper)); |
shahan | 602cc85 | 2018-06-06 20:09:57 -0700 | [diff] [blame] | 126 | skyFunctions.put(FileValue.FILE, new FileFunction(pkgLocator)); |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 127 | skyFunctions.put(SkyFunctions.DIRECTORY_LISTING, new DirectoryListingFunction()); |
| 128 | skyFunctions.put( |
| 129 | SkyFunctions.DIRECTORY_LISTING_STATE, |
djasper | b109a5f | 2019-02-14 01:05:34 -0800 | [diff] [blame] | 130 | new DirectoryListingStateFunction( |
| 131 | externalFilesHelper, new AtomicReference<>(UnixGlob.DEFAULT_SYSCALLS))); |
John Cater | 4593296 | 2017-09-06 21:57:46 +0200 | [diff] [blame] | 132 | RuleClassProvider ruleClassProvider = analysisMock.createRuleClassProvider(); |
| 133 | skyFunctions.put(SkyFunctions.WORKSPACE_AST, new WorkspaceASTFunction(ruleClassProvider)); |
| 134 | skyFunctions.put( |
janakr | 15e15c2 | 2019-01-30 11:24:49 -0800 | [diff] [blame] | 135 | WorkspaceFileValue.WORKSPACE_FILE, |
John Cater | 4593296 | 2017-09-06 21:57:46 +0200 | [diff] [blame] | 136 | new WorkspaceFileFunction( |
| 137 | ruleClassProvider, |
| 138 | analysisMock |
janakr | 52d05e8 | 2017-09-22 13:27:14 -0400 | [diff] [blame] | 139 | .getPackageFactoryBuilderForTesting(directories) |
nharmata | fde0bd2f | 2018-12-21 10:17:56 -0800 | [diff] [blame] | 140 | .build(ruleClassProvider, fileSystem), |
mjhalupka | f0e4811 | 2019-01-14 13:01:56 -0800 | [diff] [blame] | 141 | directories, |
gregce | ebd616f | 2020-04-10 16:47:41 -0700 | [diff] [blame] | 142 | /*starlarkImportLookupFunctionForInlining=*/ null)); |
ajurkowski | 2018b87 | 2020-04-14 09:59:00 -0700 | [diff] [blame] | 143 | skyFunctions.put( |
| 144 | SkyFunctions.EXTERNAL_PACKAGE, |
| 145 | new ExternalPackageFunction(BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER)); |
| 146 | skyFunctions.put( |
| 147 | SkyFunctions.LOCAL_REPOSITORY_LOOKUP, |
| 148 | new LocalRepositoryLookupFunction(BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER)); |
John Cater | 4593296 | 2017-09-06 21:57:46 +0200 | [diff] [blame] | 149 | skyFunctions.put( |
| 150 | SkyFunctions.FILE_SYMLINK_CYCLE_UNIQUENESS, new FileSymlinkCycleUniquenessFunction()); |
| 151 | ImmutableMap<String, RepositoryFunction> repositoryHandlers = |
| 152 | ImmutableMap.of( |
| 153 | LocalRepositoryRule.NAME, (RepositoryFunction) new LocalRepositoryFunction()); |
| 154 | skyFunctions.put( |
| 155 | SkyFunctions.REPOSITORY_DIRECTORY, |
janakr | ca6b46d | 2017-09-22 14:07:15 -0400 | [diff] [blame] | 156 | new RepositoryDelegatorFunction( |
Googler | 2291342 | 2019-05-02 12:42:20 -0700 | [diff] [blame] | 157 | repositoryHandlers, |
| 158 | null, |
| 159 | new AtomicBoolean(true), |
| 160 | ImmutableMap::of, |
| 161 | directories, |
ajurkowski | 2018b87 | 2020-04-14 09:59:00 -0700 | [diff] [blame] | 162 | ManagedDirectoriesKnowledge.NO_MANAGED_DIRECTORIES, |
| 163 | BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER)); |
John Cater | 4593296 | 2017-09-06 21:57:46 +0200 | [diff] [blame] | 164 | skyFunctions.put(SkyFunctions.REPOSITORY, new RepositoryLoaderFunction()); |
| 165 | |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 166 | differencer = new SequencedRecordingDifferencer(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 167 | evaluator = new InMemoryMemoizingEvaluator(skyFunctions, differencer); |
| 168 | driver = new SequentialBuildDriver(evaluator); |
| 169 | PrecomputedValue.BUILD_ID.set(differencer, UUID.randomUUID()); |
brandjon | dbff8b8 | 2017-10-23 19:02:31 +0200 | [diff] [blame] | 170 | PrecomputedValue.PATH_PACKAGE_LOCATOR.set(differencer, pkgLocator.get()); |
adonovan | b85d0b7 | 2020-05-08 11:59:19 -0700 | [diff] [blame] | 171 | PrecomputedValue.STARLARK_SEMANTICS.set(differencer, StarlarkSemantics.DEFAULT); |
John Cater | 4593296 | 2017-09-06 21:57:46 +0200 | [diff] [blame] | 172 | RepositoryDelegatorFunction.REPOSITORY_OVERRIDES.set( |
| 173 | differencer, ImmutableMap.<RepositoryName, PathFragment>of()); |
Klaus Aehlig | 93fe20c | 2018-06-14 05:48:46 -0700 | [diff] [blame] | 174 | RepositoryDelegatorFunction.DEPENDENCY_FOR_UNCONDITIONAL_FETCHING.set( |
| 175 | differencer, RepositoryDelegatorFunction.DONT_FETCH_UNCONDITIONALLY); |
Klaus Aehlig | 8eb4748 | 2018-09-17 09:14:58 -0700 | [diff] [blame] | 176 | RepositoryDelegatorFunction.RESOLVED_FILE_INSTEAD_OF_WORKSPACE.set( |
| 177 | differencer, Optional.<RootedPath>absent()); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | private ContainingPackageLookupValue lookupContainingPackage(String packageName) |
| 181 | throws InterruptedException { |
John Cater | 4593296 | 2017-09-06 21:57:46 +0200 | [diff] [blame] | 182 | return lookupContainingPackage(PackageIdentifier.createInMainRepo(packageName)); |
| 183 | } |
| 184 | |
| 185 | private ContainingPackageLookupValue lookupContainingPackage(PackageIdentifier packageIdentifier) |
| 186 | throws InterruptedException { |
| 187 | SkyKey key = ContainingPackageLookupValue.key(packageIdentifier); |
Googler | 1002867 | 2018-10-25 12:14:34 -0700 | [diff] [blame] | 188 | EvaluationContext evaluationContext = |
| 189 | EvaluationContext.newBuilder() |
| 190 | .setKeepGoing(false) |
| 191 | .setNumThreads(SkyframeExecutor.DEFAULT_THREAD_COUNT) |
| 192 | .setEventHander(NullEventHandler.INSTANCE) |
| 193 | .build(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 194 | return driver |
Googler | 1002867 | 2018-10-25 12:14:34 -0700 | [diff] [blame] | 195 | .<ContainingPackageLookupValue>evaluate(ImmutableList.of(key), evaluationContext) |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 196 | .get(key); |
| 197 | } |
| 198 | |
Googler | 06eb1bb | 2019-02-26 15:33:15 -0800 | [diff] [blame] | 199 | private PackageLookupValue lookupPackage(PackageIdentifier packageIdentifier) |
| 200 | throws InterruptedException { |
| 201 | SkyKey key = PackageLookupValue.key(packageIdentifier); |
| 202 | EvaluationContext evaluationContext = |
| 203 | EvaluationContext.newBuilder() |
| 204 | .setKeepGoing(false) |
| 205 | .setNumThreads(SkyframeExecutor.DEFAULT_THREAD_COUNT) |
| 206 | .setEventHander(NullEventHandler.INSTANCE) |
| 207 | .build(); |
| 208 | return driver.<PackageLookupValue>evaluate(ImmutableList.of(key), evaluationContext).get(key); |
| 209 | } |
| 210 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 211 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 212 | public void testNoContainingPackage() throws Exception { |
| 213 | ContainingPackageLookupValue value = lookupContainingPackage("a/b"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 214 | assertThat(value.hasContainingPackage()).isFalse(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 217 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 218 | public void testContainingPackageIsParent() throws Exception { |
| 219 | scratch.file("a/BUILD"); |
| 220 | ContainingPackageLookupValue value = lookupContainingPackage("a/b"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 221 | assertThat(value.hasContainingPackage()).isTrue(); |
| 222 | assertThat(value.getContainingPackageName()).isEqualTo(PackageIdentifier.createInMainRepo("a")); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 223 | assertThat(value.getContainingPackageRoot()).isEqualTo(Root.fromPath(rootDirectory)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 226 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 227 | public void testContainingPackageIsSelf() throws Exception { |
| 228 | scratch.file("a/b/BUILD"); |
| 229 | ContainingPackageLookupValue value = lookupContainingPackage("a/b"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 230 | assertThat(value.hasContainingPackage()).isTrue(); |
| 231 | assertThat(value.getContainingPackageName()) |
| 232 | .isEqualTo(PackageIdentifier.createInMainRepo("a/b")); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 233 | assertThat(value.getContainingPackageRoot()).isEqualTo(Root.fromPath(rootDirectory)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 236 | @Test |
John Cater | 4593296 | 2017-09-06 21:57:46 +0200 | [diff] [blame] | 237 | public void testContainingPackageIsExternalRepositoryViaExternalRepository() throws Exception { |
| 238 | scratch.overwriteFile( |
| 239 | "WORKSPACE", |
| 240 | "local_repository(name='a', path='a')"); |
| 241 | scratch.file("a/WORKSPACE"); |
| 242 | scratch.file("a/BUILD"); |
| 243 | scratch.file("a/b/BUILD"); |
| 244 | ContainingPackageLookupValue value = |
| 245 | lookupContainingPackage( |
| 246 | PackageIdentifier.create(RepositoryName.create("@a"), PathFragment.create("b"))); |
| 247 | assertThat(value.hasContainingPackage()).isTrue(); |
| 248 | assertThat(value.getContainingPackageName()) |
| 249 | .isEqualTo(PackageIdentifier.create(RepositoryName.create("@a"), PathFragment.create("b"))); |
| 250 | } |
| 251 | |
| 252 | @Test |
| 253 | public void testContainingPackageIsExternalRepositoryViaLocalPath() throws Exception { |
| 254 | scratch.overwriteFile( |
| 255 | "WORKSPACE", |
| 256 | "local_repository(name='a', path='a')"); |
| 257 | scratch.file("a/WORKSPACE"); |
| 258 | scratch.file("a/BUILD"); |
| 259 | scratch.file("a/b/BUILD"); |
| 260 | ContainingPackageLookupValue value = lookupContainingPackage("a/b"); |
| 261 | assertThat(value.hasContainingPackage()).isTrue(); |
| 262 | assertThat(value.getContainingPackageName()) |
| 263 | .isEqualTo(PackageIdentifier.create(RepositoryName.create("@a"), PathFragment.create("b"))); |
| 264 | } |
| 265 | |
| 266 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 267 | public void testEqualsAndHashCodeContract() throws Exception { |
| 268 | ContainingPackageLookupValue valueA1 = ContainingPackageLookupValue.NONE; |
| 269 | ContainingPackageLookupValue valueA2 = ContainingPackageLookupValue.NONE; |
| 270 | ContainingPackageLookupValue valueB1 = |
| 271 | ContainingPackageLookupValue.withContainingPackage( |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 272 | PackageIdentifier.createInMainRepo("b"), Root.fromPath(rootDirectory)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 273 | ContainingPackageLookupValue valueB2 = |
| 274 | ContainingPackageLookupValue.withContainingPackage( |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 275 | PackageIdentifier.createInMainRepo("b"), Root.fromPath(rootDirectory)); |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 276 | PackageIdentifier cFrag = PackageIdentifier.createInMainRepo("c"); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 277 | ContainingPackageLookupValue valueC1 = |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 278 | ContainingPackageLookupValue.withContainingPackage(cFrag, Root.fromPath(rootDirectory)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 279 | ContainingPackageLookupValue valueC2 = |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 280 | ContainingPackageLookupValue.withContainingPackage(cFrag, Root.fromPath(rootDirectory)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 281 | ContainingPackageLookupValue valueCOther = |
| 282 | ContainingPackageLookupValue.withContainingPackage( |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 283 | cFrag, Root.fromPath(rootDirectory.getRelative("other_root"))); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 284 | new EqualsTester() |
| 285 | .addEqualityGroup(valueA1, valueA2) |
| 286 | .addEqualityGroup(valueB1, valueB2) |
| 287 | .addEqualityGroup(valueC1, valueC2) |
| 288 | .addEqualityGroup(valueCOther) |
| 289 | .testEquals(); |
| 290 | } |
Googler | 06eb1bb | 2019-02-26 15:33:15 -0800 | [diff] [blame] | 291 | |
| 292 | @Test |
| 293 | public void testNonExistentExternalRepositoryErrorReason() throws Exception { |
| 294 | PackageIdentifier identifier = |
| 295 | PackageIdentifier.create("@some_repo", PathFragment.create(":atarget")); |
| 296 | ContainingPackageLookupValue value = lookupContainingPackage(identifier); |
| 297 | assertThat(value.hasContainingPackage()).isFalse(); |
| 298 | assertThat(value.getClass()).isEqualTo(NoContainingPackage.class); |
| 299 | assertThat(value.getReasonForNoContainingPackage()) |
| 300 | .isEqualTo("The repository '@some_repo' could not be resolved"); |
| 301 | } |
| 302 | |
| 303 | @Test |
| 304 | public void testInvalidPackageLabelErrorReason() throws Exception { |
| 305 | ContainingPackageLookupValue value = lookupContainingPackage("invalidpackagename:42/BUILD"); |
| 306 | assertThat(value.hasContainingPackage()).isFalse(); |
| 307 | assertThat(value.getClass()).isEqualTo(NoContainingPackage.class); |
| 308 | // As for invalid package name we continue to climb up the parent packages, |
| 309 | // we will find the top-level package with the path "" - empty string. |
| 310 | assertThat(value.getReasonForNoContainingPackage()).isNull(); |
| 311 | } |
| 312 | |
| 313 | @Test |
| 314 | public void testDeletedPackageErrorReason() throws Exception { |
| 315 | PackageIdentifier identifier = PackageIdentifier.createInMainRepo("deletedpackage"); |
| 316 | deletedPackages.set(ImmutableSet.of(identifier)); |
| 317 | scratch.file("BUILD"); |
| 318 | |
| 319 | PackageLookupValue packageLookupValue = lookupPackage(identifier); |
| 320 | assertThat(packageLookupValue.packageExists()).isFalse(); |
| 321 | assertThat(packageLookupValue.getErrorReason()).isEqualTo(ErrorReason.DELETED_PACKAGE); |
| 322 | assertThat(packageLookupValue.getErrorMsg()) |
| 323 | .isEqualTo("Package is considered deleted due to --deleted_packages"); |
| 324 | |
| 325 | ContainingPackageLookupValue value = lookupContainingPackage(identifier); |
| 326 | assertThat(value.hasContainingPackage()).isTrue(); |
| 327 | assertThat(value.getContainingPackageName().toString()).isEmpty(); |
| 328 | assertThat(value.getClass()).isEqualTo(ContainingPackage.class); |
| 329 | } |
| 330 | |
| 331 | @Test |
| 332 | public void testNoBuildFileErrorReason() throws Exception { |
| 333 | ContainingPackageLookupValue value = lookupContainingPackage("abc"); |
| 334 | assertThat(value.hasContainingPackage()).isFalse(); |
| 335 | assertThat(value.getClass()).isEqualTo(NoContainingPackage.class); |
| 336 | assertThat(value.getReasonForNoContainingPackage()).isNull(); |
| 337 | } |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 338 | } |