blob: d65779f3721b33cabde726f2f4697665c8e01ff9 [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
lberkiaea56b32017-05-30 12:35:33 +020016import static com.google.common.truth.Truth.assertThat;
Florian Weikert92b22362015-12-03 10:17:18 +000017
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000018import com.google.common.collect.ImmutableList;
19import com.google.common.collect.ImmutableSet;
20import com.google.common.testing.EqualsTester;
Kristina Chodorow5a2936f2016-04-22 17:02:19 +000021import com.google.devtools.build.lib.analysis.BlazeDirectories;
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000022import com.google.devtools.build.lib.cmdline.PackageIdentifier;
23import com.google.devtools.build.lib.events.NullEventHandler;
24import com.google.devtools.build.lib.pkgcache.PathPackageLocator;
Nathan Harmatad4f75942016-10-18 08:55:17 +000025import com.google.devtools.build.lib.skyframe.ExternalFilesHelper.ExternalFileAction;
John Cater5e9ce942016-10-12 17:23:30 +000026import com.google.devtools.build.lib.skyframe.PackageLookupFunction.CrossRepositoryLabelViolationStrategy;
John Cater0c0735a2016-11-11 01:52:02 +000027import com.google.devtools.build.lib.skyframe.PackageLookupValue.BuildFileName;
Florian Weikertcca703a2015-12-07 09:56:38 +000028import com.google.devtools.build.lib.testutil.FoundationTestCase;
Luis Fernando Pino Duquebe102182016-05-23 14:03:55 +000029import com.google.devtools.build.lib.testutil.TestConstants;
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000030import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor;
Eric Fellheimere040fe92015-11-09 23:54:46 +000031import com.google.devtools.build.lib.vfs.PathFragment;
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000032import com.google.devtools.build.skyframe.InMemoryMemoizingEvaluator;
33import com.google.devtools.build.skyframe.MemoizingEvaluator;
34import com.google.devtools.build.skyframe.RecordingDifferencer;
35import com.google.devtools.build.skyframe.SequentialBuildDriver;
36import com.google.devtools.build.skyframe.SkyFunction;
37import com.google.devtools.build.skyframe.SkyFunctionName;
38import com.google.devtools.build.skyframe.SkyKey;
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000039import java.util.HashMap;
40import java.util.Map;
41import java.util.UUID;
42import java.util.concurrent.atomic.AtomicReference;
John Cater5e9ce942016-10-12 17:23:30 +000043import org.junit.Before;
44import org.junit.Test;
45import org.junit.runner.RunWith;
46import org.junit.runners.JUnit4;
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000047
48/**
49 * Tests for {@link ContainingPackageLookupFunction}.
50 */
Florian Weikert92b22362015-12-03 10:17:18 +000051@RunWith(JUnit4.class)
Florian Weikertcca703a2015-12-07 09:56:38 +000052public class ContainingPackageLookupFunctionTest extends FoundationTestCase {
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000053
54 private AtomicReference<ImmutableSet<PackageIdentifier>> deletedPackages;
55 private MemoizingEvaluator evaluator;
56 private SequentialBuildDriver driver;
57
Florian Weikert92b22362015-12-03 10:17:18 +000058 @Before
59 public final void setUp() throws Exception {
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000060 AtomicReference<PathPackageLocator> pkgLocator =
Lukacs Berkid3262d12015-10-30 14:33:51 +000061 new AtomicReference<>(new PathPackageLocator(outputBase, ImmutableList.of(rootDirectory)));
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000062 deletedPackages = new AtomicReference<>(ImmutableSet.<PackageIdentifier>of());
Kristina Chodorow5a2936f2016-04-22 17:02:19 +000063 ExternalFilesHelper externalFilesHelper = new ExternalFilesHelper(
Nathan Harmatad4f75942016-10-18 08:55:17 +000064 pkgLocator,
65 ExternalFileAction.DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_PATHS,
66 new BlazeDirectories(
67 rootDirectory, rootDirectory, rootDirectory, TestConstants.PRODUCT_NAME));
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000068
69 Map<SkyFunctionName, SkyFunction> skyFunctions = new HashMap<>();
John Cater5e9ce942016-10-12 17:23:30 +000070 skyFunctions.put(
71 SkyFunctions.PACKAGE_LOOKUP,
John Cater0c0735a2016-11-11 01:52:02 +000072 new PackageLookupFunction(
73 deletedPackages,
74 CrossRepositoryLabelViolationStrategy.ERROR,
75 ImmutableList.of(BuildFileName.BUILD_DOT_BAZEL, BuildFileName.BUILD)));
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000076 skyFunctions.put(SkyFunctions.CONTAINING_PACKAGE_LOOKUP, new ContainingPackageLookupFunction());
Eric Fellheimer7ef96d72015-11-12 02:28:44 +000077 skyFunctions.put(SkyFunctions.BLACKLISTED_PACKAGE_PREFIXES,
78 new BlacklistedPackagePrefixesFunction());
Ulf Adamsc73051c62016-03-23 09:18:13 +000079 skyFunctions.put(SkyFunctions.FILE_STATE, new FileStateFunction(
80 new AtomicReference<TimestampGranularityMonitor>(), externalFilesHelper));
Kristina Chodorowf9fdc8d2015-12-08 12:49:31 +000081 skyFunctions.put(SkyFunctions.FILE, new FileFunction(pkgLocator));
John Caterb4f461e2016-10-25 16:16:35 +000082 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 Nienhuys81b90832015-10-26 16:57:27 +000087 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 Fellheimer7ef96d72015-11-12 02:28:44 +000092 PrecomputedValue.BLACKLISTED_PACKAGE_PREFIXES_FILE.set(differencer,
93 PathFragment.EMPTY_FRAGMENT);
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000094 }
95
96 private ContainingPackageLookupValue lookupContainingPackage(String packageName)
97 throws InterruptedException {
98 SkyKey key =
Brian Silvermand7d6d622016-03-17 09:53:39 +000099 ContainingPackageLookupValue.key(PackageIdentifier.createInMainRepo(packageName));
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000100 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 Weikert92b22362015-12-03 10:17:18 +0000109 @Test
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000110 public void testNoContainingPackage() throws Exception {
111 ContainingPackageLookupValue value = lookupContainingPackage("a/b");
lberkiaea56b32017-05-30 12:35:33 +0200112 assertThat(value.hasContainingPackage()).isFalse();
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000113 }
114
Florian Weikert92b22362015-12-03 10:17:18 +0000115 @Test
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000116 public void testContainingPackageIsParent() throws Exception {
117 scratch.file("a/BUILD");
118 ContainingPackageLookupValue value = lookupContainingPackage("a/b");
lberkiaea56b32017-05-30 12:35:33 +0200119 assertThat(value.hasContainingPackage()).isTrue();
120 assertThat(value.getContainingPackageName()).isEqualTo(PackageIdentifier.createInMainRepo("a"));
121 assertThat(value.getContainingPackageRoot()).isEqualTo(rootDirectory);
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000122 }
123
Florian Weikert92b22362015-12-03 10:17:18 +0000124 @Test
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000125 public void testContainingPackageIsSelf() throws Exception {
126 scratch.file("a/b/BUILD");
127 ContainingPackageLookupValue value = lookupContainingPackage("a/b");
lberkiaea56b32017-05-30 12:35:33 +0200128 assertThat(value.hasContainingPackage()).isTrue();
129 assertThat(value.getContainingPackageName())
130 .isEqualTo(PackageIdentifier.createInMainRepo("a/b"));
131 assertThat(value.getContainingPackageRoot()).isEqualTo(rootDirectory);
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000132 }
133
Florian Weikert92b22362015-12-03 10:17:18 +0000134 @Test
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000135 public void testEqualsAndHashCodeContract() throws Exception {
136 ContainingPackageLookupValue valueA1 = ContainingPackageLookupValue.NONE;
137 ContainingPackageLookupValue valueA2 = ContainingPackageLookupValue.NONE;
138 ContainingPackageLookupValue valueB1 =
139 ContainingPackageLookupValue.withContainingPackage(
Brian Silvermand7d6d622016-03-17 09:53:39 +0000140 PackageIdentifier.createInMainRepo("b"), rootDirectory);
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000141 ContainingPackageLookupValue valueB2 =
142 ContainingPackageLookupValue.withContainingPackage(
Brian Silvermand7d6d622016-03-17 09:53:39 +0000143 PackageIdentifier.createInMainRepo("b"), rootDirectory);
144 PackageIdentifier cFrag = PackageIdentifier.createInMainRepo("c");
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000145 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}