blob: 74c987a4b8c41cb5d9481249aea37ea27edb35c4 [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.assertSame;
17import static org.junit.Assert.assertTrue;
18
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000019import com.google.common.collect.ImmutableList;
20import com.google.common.collect.Sets;
21import com.google.devtools.build.lib.actions.AbstractAction;
22import com.google.devtools.build.lib.actions.Action;
23import com.google.devtools.build.lib.actions.ActionExecutionContext;
24import com.google.devtools.build.lib.actions.ActionExecutionException;
25import com.google.devtools.build.lib.actions.Actions;
26import com.google.devtools.build.lib.actions.Artifact;
27import com.google.devtools.build.lib.actions.Executor;
28import com.google.devtools.build.lib.actions.ResourceSet;
29import com.google.devtools.build.lib.actions.Root;
30import com.google.devtools.build.lib.actions.util.ActionsTestUtil;
31import com.google.devtools.build.lib.actions.util.DummyExecutor;
32import com.google.devtools.build.lib.vfs.FileSystemUtils;
33
Florian Weikert92b22362015-12-03 10:17:18 +000034import org.junit.Test;
35import org.junit.runner.RunWith;
36import org.junit.runners.JUnit4;
37
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000038import java.io.IOException;
39import java.util.Collection;
40import java.util.Set;
41
42/**
43 * Tests that the data passed from the application to the Builder is passed
44 * down to each Action executed.
45 */
Florian Weikert92b22362015-12-03 10:17:18 +000046@RunWith(JUnit4.class)
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000047public class ActionDataTest extends TimestampBuilderTestCase {
48
Florian Weikert92b22362015-12-03 10:17:18 +000049 @Test
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +000050 public void testArgumentToBuildArtifactsIsPassedDownToAction() throws Exception {
51
52 class MyAction extends AbstractAction {
53
54 Object executor = null;
55
56 public MyAction(Collection<Artifact> outputs) {
57 super(ActionsTestUtil.NULL_ACTION_OWNER, ImmutableList.<Artifact>of(), outputs);
58 }
59
60 @Override
61 public void execute(ActionExecutionContext actionExecutionContext)
62 throws ActionExecutionException {
63 this.executor = actionExecutionContext.getExecutor();
64 try {
65 FileSystemUtils.createEmptyFile(getPrimaryOutput().getPath());
66 } catch (IOException e) {
67 throw new ActionExecutionException("failed: ", e, this, false);
68 }
69 }
70
71 @Override
72 public ResourceSet estimateResourceConsumption(Executor executor) {
73 return ResourceSet.ZERO;
74 }
75
76 @Override
77 protected String computeKey() {
78 return "MyAction";
79 }
80
81 @Override
82 public String describeStrategy(Executor executor) {
83 return "";
84 }
85
86 @Override
87 public String getMnemonic() {
88 return "MyAction";
89 }
90 }
91
92 Artifact output = createDerivedArtifact("foo");
93 Set<Artifact> outputs = Sets.newHashSet(output);
94
95 MyAction action = new MyAction(outputs);
96 registerAction(action);
97
98 Executor executor = new DummyExecutor(scratch.dir("/"));
99 amnesiacBuilder()
100 .buildArtifacts(
101 reporter, outputs, null, null, null, null, executor, null, /*explain=*/ false, null);
102 assertSame(executor, action.executor);
103
104 executor = new DummyExecutor(scratch.dir("/"));
105 amnesiacBuilder()
106 .buildArtifacts(
107 reporter, outputs, null, null, null, null, executor, null, /*explain=*/ false, null);
108 assertSame(executor, action.executor);
109 }
110
111 private static class InputDiscoveringAction extends AbstractAction {
112 private final Collection<Artifact> discoveredInputs;
113
114 public InputDiscoveringAction(Artifact output, Collection<Artifact> discoveredInputs) {
115 super(
116 ActionsTestUtil.NULL_ACTION_OWNER,
117 ImmutableList.<Artifact>of(),
118 ImmutableList.of(output));
119 this.discoveredInputs = discoveredInputs;
120 }
121
122 @Override
123 public boolean discoversInputs() {
124 return true;
125 }
126
127 @Override
128 public boolean inputsKnown() {
129 return true;
130 }
131
132 @Override
133 public Iterable<Artifact> getMandatoryInputs() {
134 return ImmutableList.of();
135 }
136
137 @Override
138 public Iterable<Artifact> getInputs() {
139 return discoveredInputs;
140 }
141
142 @Override
143 public void execute(ActionExecutionContext actionExecutionContext) {
144 throw new IllegalStateException();
145 }
146
147 @Override
148 public String describeStrategy(Executor executor) {
149 return "";
150 }
151
152 @Override
153 public String getMnemonic() {
154 return "InputDiscovering";
155 }
156
157 @Override
158 protected String computeKey() {
159 return "";
160 }
161
162 @Override
163 public ResourceSet estimateResourceConsumption(Executor executor) {
164 return ResourceSet.ZERO;
165 }
166 }
167
Florian Weikert92b22362015-12-03 10:17:18 +0000168 @Test
Han-Wen Nienhuys81b90832015-10-26 16:57:27 +0000169 public void testActionSharabilityAndDiscoveredInputs() throws Exception {
170 Artifact output =
171 new Artifact(
172 scratch.file("/out/output"), Root.asDerivedRoot(scratch.dir("/"), scratch.dir("/out")));
173 Artifact discovered =
174 new Artifact(
175 scratch.file("/bin/discovered"),
176 Root.asDerivedRoot(scratch.dir("/"), scratch.dir("/bin")));
177
178 Action a = new InputDiscoveringAction(output, ImmutableList.of(discovered));
179 Action b = new InputDiscoveringAction(output, ImmutableList.<Artifact>of());
180
181 assertTrue(Actions.canBeShared(a, b));
182 }
183}