ruperts | 720d66f | 2017-11-29 04:05:57 -0800 | [diff] [blame] | 1 | // Copyright 2017 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 | |
| 15 | package com.google.devtools.build.lib.shell; |
| 16 | |
| 17 | import static com.google.common.truth.Truth.assertThat; |
| 18 | import static com.google.common.truth.Truth8.assertThat; |
cushon | 978cb00 | 2018-02-24 14:05:37 -0800 | [diff] [blame] | 19 | import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows; |
ruperts | 720d66f | 2017-11-29 04:05:57 -0800 | [diff] [blame] | 20 | |
| 21 | import java.time.Duration; |
| 22 | import org.junit.Test; |
| 23 | import org.junit.runner.RunWith; |
| 24 | import org.junit.runners.JUnit4; |
| 25 | |
| 26 | /** Unit tests for {@link CommandResult}. */ |
| 27 | @RunWith(JUnit4.class) |
| 28 | public final class CommandResultTest { |
| 29 | |
| 30 | @Test |
| 31 | public void testBuilder_WithNoStderr() { |
| 32 | Exception e = |
cushon | 978cb00 | 2018-02-24 14:05:37 -0800 | [diff] [blame] | 33 | assertThrows( |
ruperts | 720d66f | 2017-11-29 04:05:57 -0800 | [diff] [blame] | 34 | IllegalStateException.class, |
| 35 | () -> |
| 36 | CommandResult.builder() |
| 37 | .setStdoutStream(CommandResult.EMPTY_OUTPUT) |
| 38 | .setTerminationStatus(new TerminationStatus(0, false)) |
| 39 | .build()); |
| 40 | assertThat(e).hasMessageThat().contains("stderrStream"); |
| 41 | } |
| 42 | |
| 43 | @Test |
| 44 | public void testBuilder_WithNoStdout() { |
| 45 | Exception e = |
cushon | 978cb00 | 2018-02-24 14:05:37 -0800 | [diff] [blame] | 46 | assertThrows( |
ruperts | 720d66f | 2017-11-29 04:05:57 -0800 | [diff] [blame] | 47 | IllegalStateException.class, |
| 48 | () -> |
| 49 | CommandResult.builder() |
| 50 | .setStderrStream(CommandResult.EMPTY_OUTPUT) |
| 51 | .setTerminationStatus(new TerminationStatus(0, false)) |
| 52 | .build()); |
| 53 | assertThat(e).hasMessageThat().contains("stdoutStream"); |
| 54 | } |
| 55 | |
| 56 | @Test |
| 57 | public void testBuilder_WithNoTerminationStatus() { |
| 58 | Exception e = |
cushon | 978cb00 | 2018-02-24 14:05:37 -0800 | [diff] [blame] | 59 | assertThrows( |
ruperts | 720d66f | 2017-11-29 04:05:57 -0800 | [diff] [blame] | 60 | IllegalStateException.class, |
| 61 | () -> |
| 62 | CommandResult.builder() |
| 63 | .setStdoutStream(CommandResult.EMPTY_OUTPUT) |
| 64 | .setStderrStream(CommandResult.EMPTY_OUTPUT) |
| 65 | .build()); |
| 66 | assertThat(e).hasMessageThat().contains("terminationStatus"); |
| 67 | } |
| 68 | |
| 69 | @Test |
| 70 | public void testBuilder_WithNoExecutionTime() { |
| 71 | CommandResult commandResult = |
| 72 | CommandResult.builder() |
| 73 | .setStdoutStream(CommandResult.EMPTY_OUTPUT) |
| 74 | .setStderrStream(CommandResult.EMPTY_OUTPUT) |
| 75 | .setTerminationStatus(new TerminationStatus(0, false)) |
| 76 | .build(); |
| 77 | assertThat(commandResult.getWallExecutionTime()).isEmpty(); |
| 78 | assertThat(commandResult.getUserExecutionTime()).isEmpty(); |
| 79 | assertThat(commandResult.getSystemExecutionTime()).isEmpty(); |
| 80 | } |
| 81 | |
| 82 | @Test |
| 83 | public void testBuilder_WithExecutionTime() { |
| 84 | CommandResult commandResult = |
| 85 | CommandResult.builder() |
| 86 | .setStdoutStream(CommandResult.EMPTY_OUTPUT) |
| 87 | .setStderrStream(CommandResult.EMPTY_OUTPUT) |
| 88 | .setTerminationStatus(new TerminationStatus(0, false)) |
| 89 | .setWallExecutionTime(Duration.ofMillis(1929)) |
| 90 | .setUserExecutionTime(Duration.ofMillis(1492)) |
| 91 | .setSystemExecutionTime(Duration.ofMillis(1787)) |
| 92 | .build(); |
| 93 | assertThat(commandResult.getWallExecutionTime()).hasValue(Duration.ofMillis(1929)); |
| 94 | assertThat(commandResult.getUserExecutionTime()).hasValue(Duration.ofMillis(1492)); |
| 95 | assertThat(commandResult.getSystemExecutionTime()).hasValue(Duration.ofMillis(1787)); |
| 96 | } |
| 97 | } |