blob: 79c932b8a090c39c546f4bf099f1019d7d7066de [file] [log] [blame]
Eran Shalomf1db75c2018-08-27 07:09:19 -07001// 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
15package com.google.devtools.build.lib.runtime;
16
Googlerba6fdb92018-12-08 06:29:58 -080017import static com.google.common.truth.Truth.assertThat;
Googlera6705772019-05-28 09:39:18 -070018import static org.mockito.ArgumentMatchers.any;
Googlerba6fdb92018-12-08 06:29:58 -080019import static org.mockito.Mockito.mock;
Eran Shalomf1db75c2018-08-27 07:09:19 -070020import static org.mockito.Mockito.never;
21import static org.mockito.Mockito.verify;
22import static org.mockito.Mockito.when;
23
24import com.google.common.collect.ImmutableMap;
Googlerba6fdb92018-12-08 06:29:58 -080025import com.google.common.collect.ImmutableSet;
Eran Shalomf1db75c2018-08-27 07:09:19 -070026import com.google.devtools.build.lib.cmdline.Label;
Googlerba6fdb92018-12-08 06:29:58 -080027import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
Eran Shalomf1db75c2018-08-27 07:09:19 -070028import com.google.devtools.build.lib.exec.ExecutionOptions;
29import com.google.devtools.build.lib.exec.TestStrategy.TestSummaryFormat;
30import com.google.devtools.build.lib.runtime.TerminalTestResultNotifier.TestSummaryOptions;
31import com.google.devtools.build.lib.util.io.AnsiTerminalPrinter;
nharmataccc42952018-10-25 15:39:26 -070032import com.google.devtools.build.lib.vfs.Path;
Eran Shalomf1db75c2018-08-27 07:09:19 -070033import com.google.devtools.build.lib.view.test.TestStatus.BlazeTestStatus;
34import com.google.devtools.build.lib.view.test.TestStatus.TestCase;
35import com.google.devtools.build.lib.view.test.TestStatus.TestCase.Status;
36import com.google.devtools.common.options.OptionsParsingResult;
Eran Shalomf1db75c2018-08-27 07:09:19 -070037import java.util.Collections;
Googlerba6fdb92018-12-08 06:29:58 -080038import java.util.List;
Eran Shalomf1db75c2018-08-27 07:09:19 -070039import org.junit.Test;
40import org.junit.runner.RunWith;
41import org.junit.runners.JUnit4;
Googlerba6fdb92018-12-08 06:29:58 -080042import org.mockito.ArgumentCaptor;
Eran Shalomf1db75c2018-08-27 07:09:19 -070043
Googlerba6fdb92018-12-08 06:29:58 -080044/** Tests for {@link TerminalTestResultNotifier}. */
Eran Shalomf1db75c2018-08-27 07:09:19 -070045@RunWith(JUnit4.class)
Googlerba6fdb92018-12-08 06:29:58 -080046public final class TerminalTestResultNotifierTest {
Eran Shalomf1db75c2018-08-27 07:09:19 -070047
Googlerba6fdb92018-12-08 06:29:58 -080048 private static final String ALL_TEST_CASES_PASSED_BUT_TARGET_FAILED_DISCLAIMER =
49 "however note that at least one target failed";
Eran Shalomf1db75c2018-08-27 07:09:19 -070050
Googlerba6fdb92018-12-08 06:29:58 -080051 private final OptionsParsingResult optionsParsingResult = mock(OptionsParsingResult.class);
52 private final AnsiTerminalPrinter ansiTerminalPrinter = mock(AnsiTerminalPrinter.class);
53
54 private BlazeTestStatus targetStatus;
55 private int numFailedTestCases;
56 private int numTotalTestCases;
57 private TestSummaryFormat testSummaryFormat;
58
59 @Test
60 public void testCaseOption_allPass() throws Exception {
61 testSummaryFormat = TestSummaryFormat.TESTCASE;
62 numFailedTestCases = 0;
63 numTotalTestCases = 10;
64 targetStatus = BlazeTestStatus.PASSED;
65
66 printTestCaseSummary();
67
68 String printed = getPrintedMessage();
69 assertThat(printed).contains(info("10 passing"));
70 assertThat(printed).contains("0 failing");
71 assertThat(printed).contains("out of 10 test cases");
72 assertThat(printed).doesNotContain(ALL_TEST_CASES_PASSED_BUT_TARGET_FAILED_DISCLAIMER);
73 assertThat(printed).doesNotContain(AnsiTerminalPrinter.Mode.ERROR.toString());
74 }
75
76 @Test
77 public void testCaseOption_allPassButTargetFails() throws Exception {
78 testSummaryFormat = TestSummaryFormat.TESTCASE;
79 numFailedTestCases = 0;
80 numTotalTestCases = 10;
81 targetStatus = BlazeTestStatus.FAILED;
82
83 printTestCaseSummary();
84
85 String printed = getPrintedMessage();
86 assertThat(printed).contains(info("10 passing"));
87 assertThat(printed).contains("0 failing");
88 assertThat(printed).contains("out of 10 test cases");
89 assertThat(printed).contains(ALL_TEST_CASES_PASSED_BUT_TARGET_FAILED_DISCLAIMER);
90 assertThat(printed).doesNotContain(AnsiTerminalPrinter.Mode.ERROR.toString());
91 }
92
93 @Test
94 public void testCaseOption_someFail() throws Exception {
95 testSummaryFormat = TestSummaryFormat.TESTCASE;
96 numFailedTestCases = 2;
97 numTotalTestCases = 10;
98 targetStatus = BlazeTestStatus.FAILED;
99
100 printTestCaseSummary();
101
102 String printed = getPrintedMessage();
103 assertThat(printed).contains(info("8 passing"));
104 assertThat(printed).contains(error("2 failing"));
105 assertThat(printed).contains("out of 10 test cases");
106 assertThat(printed).doesNotContain(ALL_TEST_CASES_PASSED_BUT_TARGET_FAILED_DISCLAIMER);
107 }
108
109 @Test
110 public void testCaseOption_allFail() throws Exception {
111 testSummaryFormat = TestSummaryFormat.TESTCASE;
112 numFailedTestCases = 10;
113 numTotalTestCases = 10;
114 targetStatus = BlazeTestStatus.FAILED;
115
116 printTestCaseSummary();
117
118 String printed = getPrintedMessage();
119 assertThat(printed).contains("0 passing");
120 assertThat(printed).contains(error("10 failing"));
121 assertThat(printed).contains("out of 10 test cases");
122 assertThat(printed).doesNotContain(ALL_TEST_CASES_PASSED_BUT_TARGET_FAILED_DISCLAIMER);
123 assertThat(printed).doesNotContain(AnsiTerminalPrinter.Mode.INFO.toString());
124 }
125
126 @Test
127 public void detailedOption_allPass() throws Exception {
128 testSummaryFormat = TestSummaryFormat.DETAILED;
129 numFailedTestCases = 0;
130 numTotalTestCases = 10;
131 targetStatus = BlazeTestStatus.PASSED;
132
133 printTestCaseSummary();
134
135 String printed = getPrintedMessage();
136 assertThat(printed).contains(info("10 passing"));
137 assertThat(printed).contains("0 failing");
138 assertThat(printed).contains("out of 10 test cases");
139 assertThat(printed).doesNotContain(ALL_TEST_CASES_PASSED_BUT_TARGET_FAILED_DISCLAIMER);
140 assertThat(printed).doesNotContain(AnsiTerminalPrinter.Mode.ERROR.toString());
141 }
142
143 @Test
144 public void detailedOption_allPassButTargetFails() throws Exception {
145 testSummaryFormat = TestSummaryFormat.DETAILED;
146 numFailedTestCases = 0;
147 numTotalTestCases = 10;
148 targetStatus = BlazeTestStatus.FAILED;
149
150 printTestCaseSummary();
151
152 String printed = getPrintedMessage();
153 assertThat(printed).contains(info("10 passing"));
154 assertThat(printed).contains("0 failing");
155 assertThat(printed).contains("out of 10 test cases");
156 assertThat(printed).contains(ALL_TEST_CASES_PASSED_BUT_TARGET_FAILED_DISCLAIMER);
157 assertThat(printed).doesNotContain(AnsiTerminalPrinter.Mode.ERROR.toString());
158 }
159
160 @Test
161 public void detailedOption_someFail() throws Exception {
162 testSummaryFormat = TestSummaryFormat.DETAILED;
163 numFailedTestCases = 2;
164 numTotalTestCases = 10;
165 targetStatus = BlazeTestStatus.FAILED;
166
167 printTestCaseSummary();
168
169 String printed = getPrintedMessage();
170 assertThat(printed).contains(info("8 passing"));
171 assertThat(printed).contains(error("2 failing"));
172 assertThat(printed).contains("out of 10 test cases");
173 assertThat(printed).doesNotContain(ALL_TEST_CASES_PASSED_BUT_TARGET_FAILED_DISCLAIMER);
174 }
175
176 @Test
177 public void detailedOption_allFail() throws Exception {
178 testSummaryFormat = TestSummaryFormat.DETAILED;
179 numFailedTestCases = 10;
180 numTotalTestCases = 10;
181 targetStatus = BlazeTestStatus.FAILED;
182
183 printTestCaseSummary();
184
185 String printed = getPrintedMessage();
186 assertThat(printed).contains("0 passing");
187 assertThat(printed).contains(error("10 failing"));
188 assertThat(printed).contains("out of 10 test cases");
189 assertThat(printed).doesNotContain(ALL_TEST_CASES_PASSED_BUT_TARGET_FAILED_DISCLAIMER);
190 assertThat(printed).doesNotContain(AnsiTerminalPrinter.Mode.INFO.toString());
191 }
192
193 @Test
194 public void shortOption_noSummaryPrinted() throws Exception {
195 testSummaryFormat = TestSummaryFormat.SHORT;
196 numFailedTestCases = 2;
197 numTotalTestCases = 10;
198 targetStatus = BlazeTestStatus.FAILED;
199
200 printTestCaseSummary();
201
202 verifyNoSummaryPrinted();
203 }
204
205 @Test
206 public void terseOption_noSummaryPrinted() throws Exception {
207 testSummaryFormat = TestSummaryFormat.TERSE;
208 numFailedTestCases = 2;
209 numTotalTestCases = 10;
210 targetStatus = BlazeTestStatus.FAILED;
211
212 printTestCaseSummary();
213
214 verifyNoSummaryPrinted();
215 }
216
217 @Test
218 public void noneOption_noSummaryPrinted() throws Exception {
219 testSummaryFormat = TestSummaryFormat.NONE;
220 numFailedTestCases = 2;
221 numTotalTestCases = 10;
222 targetStatus = BlazeTestStatus.FAILED;
223
224 printTestCaseSummary();
225
226 verifyNoSummaryPrinted();
227 }
228
229 private void printTestCaseSummary() throws LabelSyntaxException {
Eran Shalomf1db75c2018-08-27 07:09:19 -0700230 ExecutionOptions executionOptions = ExecutionOptions.DEFAULTS;
Googlerba6fdb92018-12-08 06:29:58 -0800231 executionOptions.testSummary = testSummaryFormat;
Eran Shalomf1db75c2018-08-27 07:09:19 -0700232 when(optionsParsingResult.getOptions(ExecutionOptions.class)).thenReturn(executionOptions);
233
234 TestSummaryOptions testSummaryOptions = new TestSummaryOptions();
235 testSummaryOptions.verboseSummary = true;
236 when(optionsParsingResult.getOptions(TestSummaryOptions.class)).thenReturn(testSummaryOptions);
Eran Shalomf1db75c2018-08-27 07:09:19 -0700237
Googlerba6fdb92018-12-08 06:29:58 -0800238 TestSummary testSummary = mock(TestSummary.class);
239 when(testSummary.getTotalTestCases()).thenReturn(numTotalTestCases);
Eran Shalomf1db75c2018-08-27 07:09:19 -0700240 TestCase failedTestCase = TestCase.newBuilder().setStatus(Status.FAILED).build();
Googlerba6fdb92018-12-08 06:29:58 -0800241 List<TestCase> failedTestCases = Collections.nCopies(numFailedTestCases, failedTestCase);
Eran Shalomf1db75c2018-08-27 07:09:19 -0700242
243 Label labelA = Label.parseAbsolute("//foo/bar:baz", ImmutableMap.of());
Googlerba6fdb92018-12-08 06:29:58 -0800244 when(testSummary.getFailedTestCases()).thenReturn(failedTestCases);
245 when(testSummary.getStatus()).thenReturn(targetStatus);
Eran Shalomf1db75c2018-08-27 07:09:19 -0700246 when(testSummary.getLabel()).thenReturn(labelA);
247
Googlerba6fdb92018-12-08 06:29:58 -0800248 TerminalTestResultNotifier terminalTestResultNotifier =
249 new TerminalTestResultNotifier(
250 ansiTerminalPrinter, Path::getPathString, optionsParsingResult);
251 terminalTestResultNotifier.notify(ImmutableSet.of(testSummary), 1);
Eran Shalomf1db75c2018-08-27 07:09:19 -0700252 }
253
Googlerba6fdb92018-12-08 06:29:58 -0800254 private String getPrintedMessage() {
255 ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class);
256 verify(ansiTerminalPrinter).printLn(messageCaptor.capture());
257 return messageCaptor.getValue();
Eran Shalomf1db75c2018-08-27 07:09:19 -0700258 }
259
Googlerba6fdb92018-12-08 06:29:58 -0800260 private void verifyNoSummaryPrinted() {
261 verify(ansiTerminalPrinter, never()).printLn(any());
Eran Shalomf1db75c2018-08-27 07:09:19 -0700262 }
263
Googlerba6fdb92018-12-08 06:29:58 -0800264 private static String info(String message) {
265 return AnsiTerminalPrinter.Mode.INFO + message + AnsiTerminalPrinter.Mode.DEFAULT;
Eran Shalomf1db75c2018-08-27 07:09:19 -0700266 }
267
Googlerba6fdb92018-12-08 06:29:58 -0800268 private static String error(String message) {
269 return AnsiTerminalPrinter.Mode.ERROR + message + AnsiTerminalPrinter.Mode.DEFAULT;
Eran Shalomf1db75c2018-08-27 07:09:19 -0700270 }
271}