blob: e66f05db045ae5cefb1d5b46c745747b10b0b90e [file] [log] [blame]
ruperts720d66f2017-11-29 04:05:57 -08001// 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
15package com.google.devtools.build.lib.shell;
16
17import static com.google.common.truth.Truth.assertThat;
18import static com.google.common.truth.Truth8.assertThat;
cushon978cb002018-02-24 14:05:37 -080019import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
ruperts720d66f2017-11-29 04:05:57 -080020
21import java.time.Duration;
22import org.junit.Test;
23import org.junit.runner.RunWith;
24import org.junit.runners.JUnit4;
25
26/** Unit tests for {@link CommandResult}. */
27@RunWith(JUnit4.class)
28public final class CommandResultTest {
29
30 @Test
31 public void testBuilder_WithNoStderr() {
32 Exception e =
cushon978cb002018-02-24 14:05:37 -080033 assertThrows(
ruperts720d66f2017-11-29 04:05:57 -080034 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 =
cushon978cb002018-02-24 14:05:37 -080046 assertThrows(
ruperts720d66f2017-11-29 04:05:57 -080047 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 =
cushon978cb002018-02-24 14:05:37 -080059 assertThrows(
ruperts720d66f2017-11-29 04:05:57 -080060 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}