blob: 4204aef7d312e1a2a44edc508ea7ede900593124 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2015 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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.actions;
15
Ulf Adams795895a2015-03-06 15:58:35 +000016import static com.google.common.truth.Truth.assertThat;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010017import static com.google.devtools.build.lib.actions.util.ActionsTestUtil.NULL_ACTION_OWNER;
jcaterecd2abd2019-04-30 13:31:13 -070018import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010019
20import com.google.common.collect.ImmutableList;
21import com.google.devtools.build.lib.testutil.Scratch;
lberkiaea56b32017-05-30 12:35:33 +020022import java.util.Collection;
23import java.util.Collections;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010024import org.junit.Before;
25import org.junit.Test;
26import org.junit.runner.RunWith;
27import org.junit.runners.JUnit4;
28
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010029@RunWith(JUnit4.class)
30public class FailActionTest {
31
32 private Scratch scratch = new Scratch();
33
34 private String errorMessage;
35 private Artifact anOutput;
36 private Collection<Artifact> outputs;
37 private FailAction failAction;
tomlu3d1a1942017-11-29 14:01:21 -080038 private final ActionKeyContext actionKeyContext = new ActionKeyContext();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010039
tomlu3d1a1942017-11-29 14:01:21 -080040 protected MutableActionGraph actionGraph = new MapBasedActionGraph(actionKeyContext);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010041
42 @Before
Florian Weikert0220dc72015-12-01 14:38:00 +000043 public final void setUp() throws Exception {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010044 errorMessage = "An error just happened.";
tomlu1cdcdf92018-01-16 11:07:51 -080045 anOutput =
46 new Artifact(
47 scratch.file("/out/foo"),
48 ArtifactRoot.asDerivedRoot(scratch.dir("/"), scratch.dir("/out")));
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010049 outputs = ImmutableList.of(anOutput);
50 failAction = new FailAction(NULL_ACTION_OWNER, outputs, errorMessage);
51 actionGraph.registerAction(failAction);
lberkiaea56b32017-05-30 12:35:33 +020052 assertThat(actionGraph.getGeneratingAction(anOutput)).isSameAs(failAction);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010053 }
54
55 @Test
56 public void testExecutingItYieldsExceptionWithErrorMessage() {
jcaterecd2abd2019-04-30 13:31:13 -070057 ActionExecutionException e =
58 assertThrows(ActionExecutionException.class, () -> failAction.execute(null));
59 assertThat(e).hasMessageThat().isEqualTo(errorMessage);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010060 }
61
62 @Test
63 public void testInputsAreEmptySet() {
Carmi Grushkofd8acab2015-11-10 17:19:13 +000064 assertThat(failAction.getInputs()).containsExactlyElementsIn(Collections.emptySet());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010065 }
66
67 @Test
68 public void testRetainsItsOutputs() {
Carmi Grushkofd8acab2015-11-10 17:19:13 +000069 assertThat(failAction.getOutputs()).containsExactlyElementsIn(outputs);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010070 }
71
72 @Test
73 public void testPrimaryOutput() {
lberkiaea56b32017-05-30 12:35:33 +020074 assertThat(failAction.getPrimaryOutput()).isSameAs(anOutput);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010075 }
76}