blob: ff83c4a4ddfb98de5734baa4431640ec717b2702 [file] [log] [blame]
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +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
janakr93e3eea2017-03-30 22:09:37 +000016import com.google.common.base.Suppliers;
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +000017import com.google.common.collect.ImmutableList;
18import com.google.common.collect.ImmutableMap;
Rumou Duan73876202016-06-06 18:52:08 +000019import com.google.devtools.build.lib.actions.ActionAnalysisMetadata;
tomlu3d1a1942017-11-29 14:01:21 -080020import com.google.devtools.build.lib.actions.ActionKeyContext;
janakr93e3eea2017-03-30 22:09:37 +000021import com.google.devtools.build.lib.actions.ActionLookupValue.ActionLookupKey;
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +000022import com.google.devtools.build.lib.analysis.BlazeDirectories;
janakr3b63a4e2017-09-14 09:55:40 +020023import com.google.devtools.build.lib.analysis.ServerDirectories;
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +000024import 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;
Luis Fernando Pino Duquebe102182016-05-23 14:03:55 +000027import com.google.devtools.build.lib.testutil.TestConstants;
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +000028import com.google.devtools.build.lib.testutil.TestRuleClassProvider;
29import com.google.devtools.build.lib.testutil.TestUtils;
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +000030import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor;
31import com.google.devtools.build.lib.vfs.FileSystemUtils;
32import com.google.devtools.build.lib.vfs.Path;
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +000033import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
34import com.google.devtools.build.skyframe.InMemoryMemoizingEvaluator;
janakrbfdad902017-05-03 21:38:28 +020035import com.google.devtools.build.skyframe.LegacySkyKey;
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +000036import com.google.devtools.build.skyframe.MemoizingEvaluator;
37import com.google.devtools.build.skyframe.RecordingDifferencer;
janakr1cde8722017-10-10 03:22:21 +020038import com.google.devtools.build.skyframe.SequencedRecordingDifferencer;
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +000039import com.google.devtools.build.skyframe.SequentialBuildDriver;
40import com.google.devtools.build.skyframe.SkyFunction;
41import com.google.devtools.build.skyframe.SkyFunctionException;
42import com.google.devtools.build.skyframe.SkyFunctionName;
43import com.google.devtools.build.skyframe.SkyKey;
44import com.google.devtools.build.skyframe.SkyValue;
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +000045import java.io.IOException;
46import java.util.HashSet;
47import java.util.Set;
48import java.util.UUID;
49import java.util.concurrent.atomic.AtomicReference;
John Cater5e9ce942016-10-12 17:23:30 +000050import org.junit.Before;
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +000051
52abstract class ArtifactFunctionTestCase {
Janak Ramakrishnan5cb65542017-03-20 20:37:09 +000053 static final ActionLookupKey ALL_OWNER = new SingletonActionLookupKey();
janakrbfdad902017-05-03 21:38:28 +020054 static final SkyKey OWNER_KEY = LegacySkyKey.create(SkyFunctions.ACTION_LOOKUP, ALL_OWNER);
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +000055
Rumou Duan73876202016-06-06 18:52:08 +000056 protected Set<ActionAnalysisMetadata> actions;
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +000057 protected boolean fastDigest = false;
janakr1cde8722017-10-10 03:22:21 +020058 protected RecordingDifferencer differencer = new SequencedRecordingDifferencer();
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +000059 protected SequentialBuildDriver driver;
60 protected MemoizingEvaluator evaluator;
61 protected Path root;
Janak Ramakrishnana5578af2017-03-21 17:28:39 +000062 protected Path middlemanPath;
tomlu3d1a1942017-11-29 14:01:21 -080063 protected final ActionKeyContext actionKeyContext = new ActionKeyContext();
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +000064
65 /**
66 * The test action execution function. The Skyframe evaluator's action execution function
67 * delegates to this one.
68 */
69 protected SkyFunction delegateActionExecutionFunction;
70
71 @Before
72 public void baseSetUp() throws Exception {
73 setupRoot(new CustomInMemoryFs());
John Catere0d1d0e2017-11-28 20:47:41 -080074 AtomicReference<PathPackageLocator> pkgLocator =
75 new AtomicReference<>(
76 new PathPackageLocator(
77 root.getFileSystem().getPath("/outputbase"),
78 ImmutableList.of(root),
79 BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY));
janakr3b63a4e2017-09-14 09:55:40 +020080 BlazeDirectories directories =
81 new BlazeDirectories(new ServerDirectories(root, root), root, TestConstants.PRODUCT_NAME);
Kristina Chodorow5a2936f2016-04-22 17:02:19 +000082 ExternalFilesHelper externalFilesHelper = new ExternalFilesHelper(
Nathan Harmatad4f75942016-10-18 08:55:17 +000083 pkgLocator,
84 ExternalFileAction.DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_PATHS,
85 directories);
janakr1cde8722017-10-10 03:22:21 +020086 differencer = new SequencedRecordingDifferencer();
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +000087 evaluator =
88 new InMemoryMemoizingEvaluator(
89 ImmutableMap.<SkyFunctionName, SkyFunction>builder()
John Cater5e9ce942016-10-12 17:23:30 +000090 .put(
91 SkyFunctions.FILE_STATE,
Ulf Adamsc73051c62016-03-23 09:18:13 +000092 new FileStateFunction(
93 new AtomicReference<TimestampGranularityMonitor>(), externalFilesHelper))
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +000094 .put(SkyFunctions.FILE, new FileFunction(pkgLocator))
Googler17810ad2017-11-29 10:54:23 -080095 .put(SkyFunctions.ARTIFACT, new ArtifactFunction())
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +000096 .put(SkyFunctions.ACTION_EXECUTION, new SimpleActionExecutionFunction())
97 .put(
98 SkyFunctions.PACKAGE,
99 new PackageFunction(null, null, null, null, null, null, null))
John Cater5e9ce942016-10-12 17:23:30 +0000100 .put(
101 SkyFunctions.PACKAGE_LOOKUP,
John Cater0c0735a2016-11-11 01:52:02 +0000102 new PackageLookupFunction(
103 null,
104 CrossRepositoryLabelViolationStrategy.ERROR,
John Catere0d1d0e2017-11-28 20:47:41 -0800105 BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY))
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +0000106 .put(
107 SkyFunctions.WORKSPACE_AST,
108 new WorkspaceASTFunction(TestRuleClassProvider.getRuleClassProvider()))
109 .put(
110 SkyFunctions.WORKSPACE_FILE,
111 new WorkspaceFileFunction(
112 TestRuleClassProvider.getRuleClassProvider(),
John Catere0d1d0e2017-11-28 20:47:41 -0800113 TestConstants.PACKAGE_FACTORY_BUILDER_FACTORY_FOR_TESTING
114 .builder()
115 .build(
116 TestRuleClassProvider.getRuleClassProvider(), root.getFileSystem()),
Kristina Chodorow5a2936f2016-04-22 17:02:19 +0000117 directories))
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +0000118 .put(SkyFunctions.EXTERNAL_PACKAGE, new ExternalPackageFunction())
janakr93e3eea2017-03-30 22:09:37 +0000119 .put(
120 SkyFunctions.ACTION_TEMPLATE_EXPANSION,
tomlu3d1a1942017-11-29 14:01:21 -0800121 new ActionTemplateExpansionFunction(
122 actionKeyContext, Suppliers.ofInstance(false)))
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +0000123 .build(),
124 differencer);
125 driver = new SequentialBuildDriver(evaluator);
126 PrecomputedValue.BUILD_ID.set(differencer, UUID.randomUUID());
127 PrecomputedValue.PATH_PACKAGE_LOCATOR.set(differencer, pkgLocator.get());
128 actions = new HashSet<>();
129 }
130
131 protected void setupRoot(CustomInMemoryFs fs) throws IOException {
Janak Ramakrishnana5578af2017-03-21 17:28:39 +0000132 Path tmpDir = fs.getPath(TestUtils.tmpDir());
133 root = tmpDir.getChild("root");
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +0000134 FileSystemUtils.createDirectoryAndParents(root);
135 FileSystemUtils.createEmptyFile(root.getRelative("WORKSPACE"));
Janak Ramakrishnana5578af2017-03-21 17:28:39 +0000136 middlemanPath = tmpDir.getChild("middlemanRoot");
137 FileSystemUtils.createDirectoryAndParents(middlemanPath);
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +0000138 }
139
140 protected static void writeFile(Path path, String contents) throws IOException {
141 FileSystemUtils.createDirectoryAndParents(path.getParentDirectory());
142 FileSystemUtils.writeContentAsLatin1(path, contents);
143 }
144
145 /** ActionExecutionFunction that delegates to our delegate. */
146 private class SimpleActionExecutionFunction implements SkyFunction {
147 @Override
148 public SkyValue compute(SkyKey skyKey, Environment env)
149 throws SkyFunctionException, InterruptedException {
150 return delegateActionExecutionFunction.compute(skyKey, env);
151 }
152
153 @Override
154 public String extractTag(SkyKey skyKey) {
155 return delegateActionExecutionFunction.extractTag(skyKey);
156 }
157 }
158
159 private static class SingletonActionLookupKey extends ActionLookupKey {
160 @Override
janakr93e3eea2017-03-30 22:09:37 +0000161 protected SkyKey getSkyKeyInternal() {
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +0000162 return OWNER_KEY;
163 }
164
165 @Override
janakr93e3eea2017-03-30 22:09:37 +0000166 protected SkyFunctionName getType() {
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +0000167 throw new UnsupportedOperationException();
168 }
169 }
170
171 /** InMemoryFileSystem that can pretend to do a fast digest. */
172 protected class CustomInMemoryFs extends InMemoryFileSystem {
173 @Override
Ola Rozenfeld39dbc982016-11-17 20:14:56 +0000174 protected byte[] getFastDigest(Path path, HashFunction hashFunction) throws IOException {
175 return fastDigest ? getDigest(path, hashFunction) : null;
Michael Thvedt8d5a7bb2016-02-09 03:06:34 +0000176 }
177 }
178}