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