blob: ea1fc818db056479fdffc7dfd4cb9951c50d0a64 [file] [log] [blame]
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +00001// 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.
14package com.google.devtools.build.lib.skyframe;
15
Florian Weikert92b22362015-12-03 10:17:18 +000016import static org.junit.Assert.assertEquals;
17import static org.junit.Assert.assertFalse;
18import static org.junit.Assert.assertTrue;
19
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000020import com.google.common.collect.ImmutableList;
21import com.google.common.collect.ImmutableSet;
22import com.google.common.testing.EqualsTester;
Kristina Chodorow5a2936f2016-04-22 17:02:19 +000023import com.google.devtools.build.lib.analysis.BlazeDirectories;
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000024import com.google.devtools.build.lib.cmdline.PackageIdentifier;
25import com.google.devtools.build.lib.events.NullEventHandler;
26import com.google.devtools.build.lib.pkgcache.PathPackageLocator;
Nathan Harmatad4f75942016-10-18 08:55:17 +000027import com.google.devtools.build.lib.skyframe.ExternalFilesHelper.ExternalFileAction;
John Cater5e9ce942016-10-12 17:23:30 +000028import com.google.devtools.build.lib.skyframe.PackageLookupFunction.CrossRepositoryLabelViolationStrategy;
Florian Weikertcca703a2015-12-07 09:56:38 +000029import com.google.devtools.build.lib.testutil.FoundationTestCase;
Luis Fernando Pino Duquebe102182016-05-23 14:03:55 +000030import com.google.devtools.build.lib.testutil.TestConstants;
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000031import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor;
Eric Fellheimere040fe92015-11-09 23:54:46 +000032import com.google.devtools.build.lib.vfs.PathFragment;
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000033import com.google.devtools.build.skyframe.InMemoryMemoizingEvaluator;
34import com.google.devtools.build.skyframe.MemoizingEvaluator;
35import com.google.devtools.build.skyframe.RecordingDifferencer;
36import com.google.devtools.build.skyframe.SequentialBuildDriver;
37import com.google.devtools.build.skyframe.SkyFunction;
38import com.google.devtools.build.skyframe.SkyFunctionName;
39import com.google.devtools.build.skyframe.SkyKey;
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000040import java.util.HashMap;
41import java.util.Map;
42import java.util.UUID;
43import java.util.concurrent.atomic.AtomicReference;
John Cater5e9ce942016-10-12 17:23:30 +000044import org.junit.Before;
45import org.junit.Test;
46import org.junit.runner.RunWith;
47import org.junit.runners.JUnit4;
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000048
49/**
50 * Tests for {@link ContainingPackageLookupFunction}.
51 */
Florian Weikert92b22362015-12-03 10:17:18 +000052@RunWith(JUnit4.class)
Florian Weikertcca703a2015-12-07 09:56:38 +000053public class ContainingPackageLookupFunctionTest extends FoundationTestCase {
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000054
55 private AtomicReference<ImmutableSet<PackageIdentifier>> deletedPackages;
56 private MemoizingEvaluator evaluator;
57 private SequentialBuildDriver driver;
58
Florian Weikert92b22362015-12-03 10:17:18 +000059 @Before
60 public final void setUp() throws Exception {
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000061 AtomicReference<PathPackageLocator> pkgLocator =
Lukacs Berkid3262d12015-10-30 14:33:51 +000062 new AtomicReference<>(new PathPackageLocator(outputBase, ImmutableList.of(rootDirectory)));
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000063 deletedPackages = new AtomicReference<>(ImmutableSet.<PackageIdentifier>of());
Kristina Chodorow5a2936f2016-04-22 17:02:19 +000064 ExternalFilesHelper externalFilesHelper = new ExternalFilesHelper(
Nathan Harmatad4f75942016-10-18 08:55:17 +000065 pkgLocator,
66 ExternalFileAction.DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_PATHS,
67 new BlazeDirectories(
68 rootDirectory, rootDirectory, rootDirectory, TestConstants.PRODUCT_NAME));
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000069
70 Map<SkyFunctionName, SkyFunction> skyFunctions = new HashMap<>();
John Cater5e9ce942016-10-12 17:23:30 +000071 skyFunctions.put(
72 SkyFunctions.PACKAGE_LOOKUP,
73 new PackageLookupFunction(deletedPackages, CrossRepositoryLabelViolationStrategy.ERROR));
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000074 skyFunctions.put(SkyFunctions.CONTAINING_PACKAGE_LOOKUP, new ContainingPackageLookupFunction());
Eric Fellheimer7ef96d72015-11-12 02:28:44 +000075 skyFunctions.put(SkyFunctions.BLACKLISTED_PACKAGE_PREFIXES,
76 new BlacklistedPackagePrefixesFunction());
Ulf Adamsc73051c62016-03-23 09:18:13 +000077 skyFunctions.put(SkyFunctions.FILE_STATE, new FileStateFunction(
78 new AtomicReference<TimestampGranularityMonitor>(), externalFilesHelper));
Kristina Chodorowf9fdc8d2015-12-08 12:49:31 +000079 skyFunctions.put(SkyFunctions.FILE, new FileFunction(pkgLocator));
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000080 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 Fellheimer7ef96d72015-11-12 02:28:44 +000085 PrecomputedValue.BLACKLISTED_PACKAGE_PREFIXES_FILE.set(differencer,
86 PathFragment.EMPTY_FRAGMENT);
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000087 }
88
89 private ContainingPackageLookupValue lookupContainingPackage(String packageName)
90 throws InterruptedException {
91 SkyKey key =
Brian Silvermand7d6d622016-03-17 09:53:39 +000092 ContainingPackageLookupValue.key(PackageIdentifier.createInMainRepo(packageName));
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000093 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 Weikert92b22362015-12-03 10:17:18 +0000102 @Test
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000103 public void testNoContainingPackage() throws Exception {
104 ContainingPackageLookupValue value = lookupContainingPackage("a/b");
105 assertFalse(value.hasContainingPackage());
106 }
107
Florian Weikert92b22362015-12-03 10:17:18 +0000108 @Test
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000109 public void testContainingPackageIsParent() throws Exception {
110 scratch.file("a/BUILD");
111 ContainingPackageLookupValue value = lookupContainingPackage("a/b");
112 assertTrue(value.hasContainingPackage());
Brian Silvermand7d6d622016-03-17 09:53:39 +0000113 assertEquals(PackageIdentifier.createInMainRepo("a"), value.getContainingPackageName());
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000114 assertEquals(rootDirectory, value.getContainingPackageRoot());
115 }
116
Florian Weikert92b22362015-12-03 10:17:18 +0000117 @Test
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000118 public void testContainingPackageIsSelf() throws Exception {
119 scratch.file("a/b/BUILD");
120 ContainingPackageLookupValue value = lookupContainingPackage("a/b");
121 assertTrue(value.hasContainingPackage());
Brian Silvermand7d6d622016-03-17 09:53:39 +0000122 assertEquals(PackageIdentifier.createInMainRepo("a/b"), value.getContainingPackageName());
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000123 assertEquals(rootDirectory, value.getContainingPackageRoot());
124 }
125
Florian Weikert92b22362015-12-03 10:17:18 +0000126 @Test
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000127 public void testEqualsAndHashCodeContract() throws Exception {
128 ContainingPackageLookupValue valueA1 = ContainingPackageLookupValue.NONE;
129 ContainingPackageLookupValue valueA2 = ContainingPackageLookupValue.NONE;
130 ContainingPackageLookupValue valueB1 =
131 ContainingPackageLookupValue.withContainingPackage(
Brian Silvermand7d6d622016-03-17 09:53:39 +0000132 PackageIdentifier.createInMainRepo("b"), rootDirectory);
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000133 ContainingPackageLookupValue valueB2 =
134 ContainingPackageLookupValue.withContainingPackage(
Brian Silvermand7d6d622016-03-17 09:53:39 +0000135 PackageIdentifier.createInMainRepo("b"), rootDirectory);
136 PackageIdentifier cFrag = PackageIdentifier.createInMainRepo("c");
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000137 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}