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