Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 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.actions; |
| 15 | |
Ulf Adams | 795895a | 2015-03-06 15:58:35 +0000 | [diff] [blame] | 16 | import static com.google.common.truth.Truth.assertThat; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 17 | import static com.google.devtools.build.lib.actions.util.ActionsTestUtil.NULL_ACTION_OWNER; |
jcater | ecd2abd | 2019-04-30 13:31:13 -0700 | [diff] [blame^] | 18 | import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | |
| 20 | import com.google.common.collect.ImmutableList; |
| 21 | import com.google.devtools.build.lib.testutil.Scratch; |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 22 | import java.util.Collection; |
| 23 | import java.util.Collections; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.junit.runner.RunWith; |
| 27 | import org.junit.runners.JUnit4; |
| 28 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 29 | @RunWith(JUnit4.class) |
| 30 | public 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; |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 38 | private final ActionKeyContext actionKeyContext = new ActionKeyContext(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 39 | |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 40 | protected MutableActionGraph actionGraph = new MapBasedActionGraph(actionKeyContext); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 41 | |
| 42 | @Before |
Florian Weikert | 0220dc7 | 2015-12-01 14:38:00 +0000 | [diff] [blame] | 43 | public final void setUp() throws Exception { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 44 | errorMessage = "An error just happened."; |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 45 | anOutput = |
| 46 | new Artifact( |
| 47 | scratch.file("/out/foo"), |
| 48 | ArtifactRoot.asDerivedRoot(scratch.dir("/"), scratch.dir("/out"))); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 49 | outputs = ImmutableList.of(anOutput); |
| 50 | failAction = new FailAction(NULL_ACTION_OWNER, outputs, errorMessage); |
| 51 | actionGraph.registerAction(failAction); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 52 | assertThat(actionGraph.getGeneratingAction(anOutput)).isSameAs(failAction); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Test |
| 56 | public void testExecutingItYieldsExceptionWithErrorMessage() { |
jcater | ecd2abd | 2019-04-30 13:31:13 -0700 | [diff] [blame^] | 57 | ActionExecutionException e = |
| 58 | assertThrows(ActionExecutionException.class, () -> failAction.execute(null)); |
| 59 | assertThat(e).hasMessageThat().isEqualTo(errorMessage); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void testInputsAreEmptySet() { |
Carmi Grushko | fd8acab | 2015-11-10 17:19:13 +0000 | [diff] [blame] | 64 | assertThat(failAction.getInputs()).containsExactlyElementsIn(Collections.emptySet()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Test |
| 68 | public void testRetainsItsOutputs() { |
Carmi Grushko | fd8acab | 2015-11-10 17:19:13 +0000 | [diff] [blame] | 69 | assertThat(failAction.getOutputs()).containsExactlyElementsIn(outputs); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Test |
| 73 | public void testPrimaryOutput() { |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 74 | assertThat(failAction.getPrimaryOutput()).isSameAs(anOutput); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 75 | } |
| 76 | } |