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 | |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.common.testing.EqualsTester; |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.analysis.BlazeDirectories; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
| 23 | import com.google.devtools.build.lib.events.NullEventHandler; |
| 24 | import com.google.devtools.build.lib.pkgcache.PathPackageLocator; |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.skyframe.ExternalFilesHelper.ExternalFileAction; |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.skyframe.PackageLookupFunction.CrossRepositoryLabelViolationStrategy; |
John Cater | 0c0735a | 2016-11-11 01:52:02 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.skyframe.PackageLookupValue.BuildFileName; |
Florian Weikert | cca703a | 2015-12-07 09:56:38 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.testutil.FoundationTestCase; |
Luis Fernando Pino Duque | be10218 | 2016-05-23 14:03:55 +0000 | [diff] [blame] | 29 | import com.google.devtools.build.lib.testutil.TestConstants; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor; |
Eric Fellheimer | e040fe9 | 2015-11-09 23:54:46 +0000 | [diff] [blame] | 31 | import com.google.devtools.build.lib.vfs.PathFragment; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 32 | import com.google.devtools.build.skyframe.InMemoryMemoizingEvaluator; |
| 33 | import com.google.devtools.build.skyframe.MemoizingEvaluator; |
| 34 | import com.google.devtools.build.skyframe.RecordingDifferencer; |
| 35 | import com.google.devtools.build.skyframe.SequentialBuildDriver; |
| 36 | import com.google.devtools.build.skyframe.SkyFunction; |
| 37 | import com.google.devtools.build.skyframe.SkyFunctionName; |
| 38 | import com.google.devtools.build.skyframe.SkyKey; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 39 | import java.util.HashMap; |
| 40 | import java.util.Map; |
| 41 | import java.util.UUID; |
| 42 | import java.util.concurrent.atomic.AtomicReference; |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 43 | import org.junit.Before; |
| 44 | import org.junit.Test; |
| 45 | import org.junit.runner.RunWith; |
| 46 | import org.junit.runners.JUnit4; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Tests for {@link ContainingPackageLookupFunction}. |
| 50 | */ |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 51 | @RunWith(JUnit4.class) |
Florian Weikert | cca703a | 2015-12-07 09:56:38 +0000 | [diff] [blame] | 52 | public class ContainingPackageLookupFunctionTest extends FoundationTestCase { |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 53 | |
| 54 | private AtomicReference<ImmutableSet<PackageIdentifier>> deletedPackages; |
| 55 | private MemoizingEvaluator evaluator; |
| 56 | private SequentialBuildDriver driver; |
| 57 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 58 | @Before |
| 59 | public final void setUp() throws Exception { |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 60 | AtomicReference<PathPackageLocator> pkgLocator = |
Lukacs Berki | d3262d1 | 2015-10-30 14:33:51 +0000 | [diff] [blame] | 61 | new AtomicReference<>(new PathPackageLocator(outputBase, ImmutableList.of(rootDirectory))); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 62 | deletedPackages = new AtomicReference<>(ImmutableSet.<PackageIdentifier>of()); |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 63 | ExternalFilesHelper externalFilesHelper = new ExternalFilesHelper( |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 64 | pkgLocator, |
| 65 | ExternalFileAction.DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_PATHS, |
| 66 | new BlazeDirectories( |
| 67 | rootDirectory, rootDirectory, rootDirectory, TestConstants.PRODUCT_NAME)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 68 | |
| 69 | Map<SkyFunctionName, SkyFunction> skyFunctions = new HashMap<>(); |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 70 | skyFunctions.put( |
| 71 | SkyFunctions.PACKAGE_LOOKUP, |
John Cater | 0c0735a | 2016-11-11 01:52:02 +0000 | [diff] [blame] | 72 | new PackageLookupFunction( |
| 73 | deletedPackages, |
| 74 | CrossRepositoryLabelViolationStrategy.ERROR, |
| 75 | ImmutableList.of(BuildFileName.BUILD_DOT_BAZEL, BuildFileName.BUILD))); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 76 | skyFunctions.put(SkyFunctions.CONTAINING_PACKAGE_LOOKUP, new ContainingPackageLookupFunction()); |
Eric Fellheimer | 7ef96d7 | 2015-11-12 02:28:44 +0000 | [diff] [blame] | 77 | skyFunctions.put(SkyFunctions.BLACKLISTED_PACKAGE_PREFIXES, |
| 78 | new BlacklistedPackagePrefixesFunction()); |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 79 | skyFunctions.put(SkyFunctions.FILE_STATE, new FileStateFunction( |
| 80 | new AtomicReference<TimestampGranularityMonitor>(), externalFilesHelper)); |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 81 | skyFunctions.put(SkyFunctions.FILE, new FileFunction(pkgLocator)); |
John Cater | b4f461e | 2016-10-25 16:16:35 +0000 | [diff] [blame] | 82 | skyFunctions.put(SkyFunctions.DIRECTORY_LISTING, new DirectoryListingFunction()); |
| 83 | skyFunctions.put( |
| 84 | SkyFunctions.DIRECTORY_LISTING_STATE, |
| 85 | new DirectoryListingStateFunction(externalFilesHelper)); |
| 86 | skyFunctions.put(SkyFunctions.LOCAL_REPOSITORY_LOOKUP, new LocalRepositoryLookupFunction()); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 87 | RecordingDifferencer differencer = new RecordingDifferencer(); |
| 88 | evaluator = new InMemoryMemoizingEvaluator(skyFunctions, differencer); |
| 89 | driver = new SequentialBuildDriver(evaluator); |
| 90 | PrecomputedValue.BUILD_ID.set(differencer, UUID.randomUUID()); |
| 91 | PrecomputedValue.PATH_PACKAGE_LOCATOR.set(differencer, pkgLocator.get()); |
Eric Fellheimer | 7ef96d7 | 2015-11-12 02:28:44 +0000 | [diff] [blame] | 92 | PrecomputedValue.BLACKLISTED_PACKAGE_PREFIXES_FILE.set(differencer, |
| 93 | PathFragment.EMPTY_FRAGMENT); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | private ContainingPackageLookupValue lookupContainingPackage(String packageName) |
| 97 | throws InterruptedException { |
| 98 | SkyKey key = |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 99 | ContainingPackageLookupValue.key(PackageIdentifier.createInMainRepo(packageName)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 100 | return driver |
| 101 | .<ContainingPackageLookupValue>evaluate( |
| 102 | ImmutableList.of(key), |
| 103 | false, |
| 104 | SkyframeExecutor.DEFAULT_THREAD_COUNT, |
| 105 | NullEventHandler.INSTANCE) |
| 106 | .get(key); |
| 107 | } |
| 108 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 109 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 110 | public void testNoContainingPackage() throws Exception { |
| 111 | ContainingPackageLookupValue value = lookupContainingPackage("a/b"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame^] | 112 | assertThat(value.hasContainingPackage()).isFalse(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 115 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 116 | public void testContainingPackageIsParent() throws Exception { |
| 117 | scratch.file("a/BUILD"); |
| 118 | ContainingPackageLookupValue value = lookupContainingPackage("a/b"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame^] | 119 | assertThat(value.hasContainingPackage()).isTrue(); |
| 120 | assertThat(value.getContainingPackageName()).isEqualTo(PackageIdentifier.createInMainRepo("a")); |
| 121 | assertThat(value.getContainingPackageRoot()).isEqualTo(rootDirectory); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 124 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 125 | public void testContainingPackageIsSelf() throws Exception { |
| 126 | scratch.file("a/b/BUILD"); |
| 127 | ContainingPackageLookupValue value = lookupContainingPackage("a/b"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame^] | 128 | assertThat(value.hasContainingPackage()).isTrue(); |
| 129 | assertThat(value.getContainingPackageName()) |
| 130 | .isEqualTo(PackageIdentifier.createInMainRepo("a/b")); |
| 131 | assertThat(value.getContainingPackageRoot()).isEqualTo(rootDirectory); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 134 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 135 | public void testEqualsAndHashCodeContract() throws Exception { |
| 136 | ContainingPackageLookupValue valueA1 = ContainingPackageLookupValue.NONE; |
| 137 | ContainingPackageLookupValue valueA2 = ContainingPackageLookupValue.NONE; |
| 138 | ContainingPackageLookupValue valueB1 = |
| 139 | ContainingPackageLookupValue.withContainingPackage( |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 140 | PackageIdentifier.createInMainRepo("b"), rootDirectory); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 141 | ContainingPackageLookupValue valueB2 = |
| 142 | ContainingPackageLookupValue.withContainingPackage( |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 143 | PackageIdentifier.createInMainRepo("b"), rootDirectory); |
| 144 | PackageIdentifier cFrag = PackageIdentifier.createInMainRepo("c"); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 145 | ContainingPackageLookupValue valueC1 = |
| 146 | ContainingPackageLookupValue.withContainingPackage(cFrag, rootDirectory); |
| 147 | ContainingPackageLookupValue valueC2 = |
| 148 | ContainingPackageLookupValue.withContainingPackage(cFrag, rootDirectory); |
| 149 | ContainingPackageLookupValue valueCOther = |
| 150 | ContainingPackageLookupValue.withContainingPackage( |
| 151 | cFrag, rootDirectory.getRelative("other_root")); |
| 152 | new EqualsTester() |
| 153 | .addEqualityGroup(valueA1, valueA2) |
| 154 | .addEqualityGroup(valueB1, valueB2) |
| 155 | .addEqualityGroup(valueC1, valueC2) |
| 156 | .addEqualityGroup(valueCOther) |
| 157 | .testEquals(); |
| 158 | } |
| 159 | } |