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 | |
| 16 | import com.google.common.collect.ImmutableList; |
| 17 | import com.google.common.collect.ImmutableSet; |
| 18 | import com.google.common.testing.EqualsTester; |
| 19 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
| 20 | import com.google.devtools.build.lib.events.NullEventHandler; |
| 21 | import com.google.devtools.build.lib.pkgcache.PathPackageLocator; |
| 22 | import com.google.devtools.build.lib.testutil.FoundationTestCase; |
| 23 | import com.google.devtools.build.lib.util.BlazeClock; |
| 24 | import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor; |
Eric Fellheimer | e040fe9 | 2015-11-09 23:54:46 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.vfs.PathFragment; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.skyframe.InMemoryMemoizingEvaluator; |
| 27 | import com.google.devtools.build.skyframe.MemoizingEvaluator; |
| 28 | import com.google.devtools.build.skyframe.RecordingDifferencer; |
| 29 | import com.google.devtools.build.skyframe.SequentialBuildDriver; |
| 30 | import com.google.devtools.build.skyframe.SkyFunction; |
| 31 | import com.google.devtools.build.skyframe.SkyFunctionName; |
| 32 | import com.google.devtools.build.skyframe.SkyKey; |
| 33 | |
| 34 | import java.util.HashMap; |
| 35 | import java.util.Map; |
| 36 | import java.util.UUID; |
| 37 | import java.util.concurrent.atomic.AtomicReference; |
| 38 | |
| 39 | /** |
| 40 | * Tests for {@link ContainingPackageLookupFunction}. |
| 41 | */ |
| 42 | public class ContainingPackageLookupFunctionTest extends FoundationTestCase { |
| 43 | |
| 44 | private AtomicReference<ImmutableSet<PackageIdentifier>> deletedPackages; |
| 45 | private MemoizingEvaluator evaluator; |
| 46 | private SequentialBuildDriver driver; |
| 47 | |
| 48 | @Override |
| 49 | protected void setUp() throws Exception { |
| 50 | super.setUp(); |
| 51 | AtomicReference<PathPackageLocator> pkgLocator = |
Lukacs Berki | d3262d1 | 2015-10-30 14:33:51 +0000 | [diff] [blame] | 52 | new AtomicReference<>(new PathPackageLocator(outputBase, ImmutableList.of(rootDirectory))); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 53 | deletedPackages = new AtomicReference<>(ImmutableSet.<PackageIdentifier>of()); |
| 54 | ExternalFilesHelper externalFilesHelper = new ExternalFilesHelper(pkgLocator); |
| 55 | TimestampGranularityMonitor tsgm = new TimestampGranularityMonitor(BlazeClock.instance()); |
| 56 | |
| 57 | Map<SkyFunctionName, SkyFunction> skyFunctions = new HashMap<>(); |
| 58 | skyFunctions.put(SkyFunctions.PACKAGE_LOOKUP, new PackageLookupFunction(deletedPackages)); |
| 59 | skyFunctions.put(SkyFunctions.CONTAINING_PACKAGE_LOOKUP, new ContainingPackageLookupFunction()); |
Eric Fellheimer | 7ef96d7 | 2015-11-12 02:28:44 +0000 | [diff] [blame] | 60 | skyFunctions.put(SkyFunctions.BLACKLISTED_PACKAGE_PREFIXES, |
| 61 | new BlacklistedPackagePrefixesFunction()); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 62 | skyFunctions.put(SkyFunctions.FILE_STATE, new FileStateFunction(tsgm, externalFilesHelper)); |
| 63 | skyFunctions.put(SkyFunctions.FILE, new FileFunction(pkgLocator, tsgm, externalFilesHelper)); |
| 64 | RecordingDifferencer differencer = new RecordingDifferencer(); |
| 65 | evaluator = new InMemoryMemoizingEvaluator(skyFunctions, differencer); |
| 66 | driver = new SequentialBuildDriver(evaluator); |
| 67 | PrecomputedValue.BUILD_ID.set(differencer, UUID.randomUUID()); |
| 68 | PrecomputedValue.PATH_PACKAGE_LOCATOR.set(differencer, pkgLocator.get()); |
Eric Fellheimer | 7ef96d7 | 2015-11-12 02:28:44 +0000 | [diff] [blame] | 69 | PrecomputedValue.BLACKLISTED_PACKAGE_PREFIXES_FILE.set(differencer, |
| 70 | PathFragment.EMPTY_FRAGMENT); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | private ContainingPackageLookupValue lookupContainingPackage(String packageName) |
| 74 | throws InterruptedException { |
| 75 | SkyKey key = |
| 76 | ContainingPackageLookupValue.key(PackageIdentifier.createInDefaultRepo(packageName)); |
| 77 | return driver |
| 78 | .<ContainingPackageLookupValue>evaluate( |
| 79 | ImmutableList.of(key), |
| 80 | false, |
| 81 | SkyframeExecutor.DEFAULT_THREAD_COUNT, |
| 82 | NullEventHandler.INSTANCE) |
| 83 | .get(key); |
| 84 | } |
| 85 | |
| 86 | public void testNoContainingPackage() throws Exception { |
| 87 | ContainingPackageLookupValue value = lookupContainingPackage("a/b"); |
| 88 | assertFalse(value.hasContainingPackage()); |
| 89 | } |
| 90 | |
| 91 | public void testContainingPackageIsParent() throws Exception { |
| 92 | scratch.file("a/BUILD"); |
| 93 | ContainingPackageLookupValue value = lookupContainingPackage("a/b"); |
| 94 | assertTrue(value.hasContainingPackage()); |
| 95 | assertEquals(PackageIdentifier.createInDefaultRepo("a"), value.getContainingPackageName()); |
| 96 | assertEquals(rootDirectory, value.getContainingPackageRoot()); |
| 97 | } |
| 98 | |
| 99 | public void testContainingPackageIsSelf() throws Exception { |
| 100 | scratch.file("a/b/BUILD"); |
| 101 | ContainingPackageLookupValue value = lookupContainingPackage("a/b"); |
| 102 | assertTrue(value.hasContainingPackage()); |
| 103 | assertEquals(PackageIdentifier.createInDefaultRepo("a/b"), value.getContainingPackageName()); |
| 104 | assertEquals(rootDirectory, value.getContainingPackageRoot()); |
| 105 | } |
| 106 | |
| 107 | public void testEqualsAndHashCodeContract() throws Exception { |
| 108 | ContainingPackageLookupValue valueA1 = ContainingPackageLookupValue.NONE; |
| 109 | ContainingPackageLookupValue valueA2 = ContainingPackageLookupValue.NONE; |
| 110 | ContainingPackageLookupValue valueB1 = |
| 111 | ContainingPackageLookupValue.withContainingPackage( |
| 112 | PackageIdentifier.createInDefaultRepo("b"), rootDirectory); |
| 113 | ContainingPackageLookupValue valueB2 = |
| 114 | ContainingPackageLookupValue.withContainingPackage( |
| 115 | PackageIdentifier.createInDefaultRepo("b"), rootDirectory); |
| 116 | PackageIdentifier cFrag = PackageIdentifier.createInDefaultRepo("c"); |
| 117 | ContainingPackageLookupValue valueC1 = |
| 118 | ContainingPackageLookupValue.withContainingPackage(cFrag, rootDirectory); |
| 119 | ContainingPackageLookupValue valueC2 = |
| 120 | ContainingPackageLookupValue.withContainingPackage(cFrag, rootDirectory); |
| 121 | ContainingPackageLookupValue valueCOther = |
| 122 | ContainingPackageLookupValue.withContainingPackage( |
| 123 | cFrag, rootDirectory.getRelative("other_root")); |
| 124 | new EqualsTester() |
| 125 | .addEqualityGroup(valueA1, valueA2) |
| 126 | .addEqualityGroup(valueB1, valueB2) |
| 127 | .addEqualityGroup(valueC1, valueC2) |
| 128 | .addEqualityGroup(valueCOther) |
| 129 | .testEquals(); |
| 130 | } |
| 131 | } |