blob: 72a47a5067b600bb27a64d8b4c648a42ace71508 [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
16import com.google.common.collect.ImmutableList;
17import com.google.common.collect.ImmutableSet;
18import com.google.common.testing.EqualsTester;
19import com.google.devtools.build.lib.cmdline.PackageIdentifier;
20import com.google.devtools.build.lib.events.NullEventHandler;
21import com.google.devtools.build.lib.pkgcache.PathPackageLocator;
22import com.google.devtools.build.lib.testutil.FoundationTestCase;
23import com.google.devtools.build.lib.util.BlazeClock;
24import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor;
Eric Fellheimere040fe92015-11-09 23:54:46 +000025import com.google.devtools.build.lib.vfs.PathFragment;
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000026import com.google.devtools.build.skyframe.InMemoryMemoizingEvaluator;
27import com.google.devtools.build.skyframe.MemoizingEvaluator;
28import com.google.devtools.build.skyframe.RecordingDifferencer;
29import com.google.devtools.build.skyframe.SequentialBuildDriver;
30import com.google.devtools.build.skyframe.SkyFunction;
31import com.google.devtools.build.skyframe.SkyFunctionName;
32import com.google.devtools.build.skyframe.SkyKey;
33
34import java.util.HashMap;
35import java.util.Map;
36import java.util.UUID;
37import java.util.concurrent.atomic.AtomicReference;
38
39/**
40 * Tests for {@link ContainingPackageLookupFunction}.
41 */
42public 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 Berkid3262d12015-10-30 14:33:51 +000052 new AtomicReference<>(new PathPackageLocator(outputBase, ImmutableList.of(rootDirectory)));
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000053 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 Fellheimer7ef96d72015-11-12 02:28:44 +000060 skyFunctions.put(SkyFunctions.BLACKLISTED_PACKAGE_PREFIXES,
61 new BlacklistedPackagePrefixesFunction());
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000062 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 Fellheimer7ef96d72015-11-12 02:28:44 +000069 PrecomputedValue.BLACKLISTED_PACKAGE_PREFIXES_FILE.set(differencer,
70 PathFragment.EMPTY_FRAGMENT);
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000071 }
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}