Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1 | // 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. |
| 14 | package com.google.devtools.build.lib.skyframe; |
| 15 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 16 | import static org.junit.Assert.assertSame; |
| 17 | import static org.junit.Assert.assertTrue; |
| 18 | |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import com.google.devtools.build.lib.actions.AbstractAction; |
| 22 | import com.google.devtools.build.lib.actions.Action; |
| 23 | import com.google.devtools.build.lib.actions.ActionExecutionContext; |
| 24 | import com.google.devtools.build.lib.actions.ActionExecutionException; |
| 25 | import com.google.devtools.build.lib.actions.Actions; |
| 26 | import com.google.devtools.build.lib.actions.Artifact; |
| 27 | import com.google.devtools.build.lib.actions.Executor; |
| 28 | import com.google.devtools.build.lib.actions.ResourceSet; |
| 29 | import com.google.devtools.build.lib.actions.Root; |
| 30 | import com.google.devtools.build.lib.actions.util.ActionsTestUtil; |
| 31 | import com.google.devtools.build.lib.actions.util.DummyExecutor; |
| 32 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
| 33 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 34 | import org.junit.Test; |
| 35 | import org.junit.runner.RunWith; |
| 36 | import org.junit.runners.JUnit4; |
| 37 | |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 38 | import java.io.IOException; |
| 39 | import java.util.Collection; |
| 40 | import 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 Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 46 | @RunWith(JUnit4.class) |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 47 | public class ActionDataTest extends TimestampBuilderTestCase { |
| 48 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 49 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 50 | 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 Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 168 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 169 | 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 | } |