Eran Shalom | f1db75c | 2018-08-27 07:09:19 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.devtools.build.lib.runtime; |
| 16 | |
Googler | ba6fdb9 | 2018-12-08 06:29:58 -0800 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertThat; |
Googler | a670577 | 2019-05-28 09:39:18 -0700 | [diff] [blame] | 18 | import static org.mockito.ArgumentMatchers.any; |
Googler | ba6fdb9 | 2018-12-08 06:29:58 -0800 | [diff] [blame] | 19 | import static org.mockito.Mockito.mock; |
Eran Shalom | f1db75c | 2018-08-27 07:09:19 -0700 | [diff] [blame] | 20 | import static org.mockito.Mockito.never; |
| 21 | import static org.mockito.Mockito.verify; |
| 22 | import static org.mockito.Mockito.when; |
| 23 | |
| 24 | import com.google.common.collect.ImmutableMap; |
Googler | ba6fdb9 | 2018-12-08 06:29:58 -0800 | [diff] [blame] | 25 | import com.google.common.collect.ImmutableSet; |
Eran Shalom | f1db75c | 2018-08-27 07:09:19 -0700 | [diff] [blame] | 26 | import com.google.devtools.build.lib.cmdline.Label; |
Googler | ba6fdb9 | 2018-12-08 06:29:58 -0800 | [diff] [blame] | 27 | import com.google.devtools.build.lib.cmdline.LabelSyntaxException; |
Eran Shalom | f1db75c | 2018-08-27 07:09:19 -0700 | [diff] [blame] | 28 | import com.google.devtools.build.lib.exec.ExecutionOptions; |
| 29 | import com.google.devtools.build.lib.exec.TestStrategy.TestSummaryFormat; |
| 30 | import com.google.devtools.build.lib.runtime.TerminalTestResultNotifier.TestSummaryOptions; |
| 31 | import com.google.devtools.build.lib.util.io.AnsiTerminalPrinter; |
nharmata | ccc4295 | 2018-10-25 15:39:26 -0700 | [diff] [blame] | 32 | import com.google.devtools.build.lib.vfs.Path; |
Eran Shalom | f1db75c | 2018-08-27 07:09:19 -0700 | [diff] [blame] | 33 | import com.google.devtools.build.lib.view.test.TestStatus.BlazeTestStatus; |
| 34 | import com.google.devtools.build.lib.view.test.TestStatus.TestCase; |
| 35 | import com.google.devtools.build.lib.view.test.TestStatus.TestCase.Status; |
| 36 | import com.google.devtools.common.options.OptionsParsingResult; |
Eran Shalom | f1db75c | 2018-08-27 07:09:19 -0700 | [diff] [blame] | 37 | import java.util.Collections; |
Googler | ba6fdb9 | 2018-12-08 06:29:58 -0800 | [diff] [blame] | 38 | import java.util.List; |
Eran Shalom | f1db75c | 2018-08-27 07:09:19 -0700 | [diff] [blame] | 39 | import org.junit.Test; |
| 40 | import org.junit.runner.RunWith; |
| 41 | import org.junit.runners.JUnit4; |
Googler | ba6fdb9 | 2018-12-08 06:29:58 -0800 | [diff] [blame] | 42 | import org.mockito.ArgumentCaptor; |
Eran Shalom | f1db75c | 2018-08-27 07:09:19 -0700 | [diff] [blame] | 43 | |
Googler | ba6fdb9 | 2018-12-08 06:29:58 -0800 | [diff] [blame] | 44 | /** Tests for {@link TerminalTestResultNotifier}. */ |
Eran Shalom | f1db75c | 2018-08-27 07:09:19 -0700 | [diff] [blame] | 45 | @RunWith(JUnit4.class) |
Googler | ba6fdb9 | 2018-12-08 06:29:58 -0800 | [diff] [blame] | 46 | public final class TerminalTestResultNotifierTest { |
Eran Shalom | f1db75c | 2018-08-27 07:09:19 -0700 | [diff] [blame] | 47 | |
Googler | ba6fdb9 | 2018-12-08 06:29:58 -0800 | [diff] [blame] | 48 | private static final String ALL_TEST_CASES_PASSED_BUT_TARGET_FAILED_DISCLAIMER = |
| 49 | "however note that at least one target failed"; |
Eran Shalom | f1db75c | 2018-08-27 07:09:19 -0700 | [diff] [blame] | 50 | |
Googler | ba6fdb9 | 2018-12-08 06:29:58 -0800 | [diff] [blame] | 51 | 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 Shalom | f1db75c | 2018-08-27 07:09:19 -0700 | [diff] [blame] | 230 | ExecutionOptions executionOptions = ExecutionOptions.DEFAULTS; |
Googler | ba6fdb9 | 2018-12-08 06:29:58 -0800 | [diff] [blame] | 231 | executionOptions.testSummary = testSummaryFormat; |
Eran Shalom | f1db75c | 2018-08-27 07:09:19 -0700 | [diff] [blame] | 232 | 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 Shalom | f1db75c | 2018-08-27 07:09:19 -0700 | [diff] [blame] | 237 | |
Googler | ba6fdb9 | 2018-12-08 06:29:58 -0800 | [diff] [blame] | 238 | TestSummary testSummary = mock(TestSummary.class); |
| 239 | when(testSummary.getTotalTestCases()).thenReturn(numTotalTestCases); |
Eran Shalom | f1db75c | 2018-08-27 07:09:19 -0700 | [diff] [blame] | 240 | TestCase failedTestCase = TestCase.newBuilder().setStatus(Status.FAILED).build(); |
Googler | ba6fdb9 | 2018-12-08 06:29:58 -0800 | [diff] [blame] | 241 | List<TestCase> failedTestCases = Collections.nCopies(numFailedTestCases, failedTestCase); |
Eran Shalom | f1db75c | 2018-08-27 07:09:19 -0700 | [diff] [blame] | 242 | |
| 243 | Label labelA = Label.parseAbsolute("//foo/bar:baz", ImmutableMap.of()); |
Googler | ba6fdb9 | 2018-12-08 06:29:58 -0800 | [diff] [blame] | 244 | when(testSummary.getFailedTestCases()).thenReturn(failedTestCases); |
| 245 | when(testSummary.getStatus()).thenReturn(targetStatus); |
Eran Shalom | f1db75c | 2018-08-27 07:09:19 -0700 | [diff] [blame] | 246 | when(testSummary.getLabel()).thenReturn(labelA); |
| 247 | |
Googler | ba6fdb9 | 2018-12-08 06:29:58 -0800 | [diff] [blame] | 248 | TerminalTestResultNotifier terminalTestResultNotifier = |
| 249 | new TerminalTestResultNotifier( |
| 250 | ansiTerminalPrinter, Path::getPathString, optionsParsingResult); |
| 251 | terminalTestResultNotifier.notify(ImmutableSet.of(testSummary), 1); |
Eran Shalom | f1db75c | 2018-08-27 07:09:19 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Googler | ba6fdb9 | 2018-12-08 06:29:58 -0800 | [diff] [blame] | 254 | private String getPrintedMessage() { |
| 255 | ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class); |
| 256 | verify(ansiTerminalPrinter).printLn(messageCaptor.capture()); |
| 257 | return messageCaptor.getValue(); |
Eran Shalom | f1db75c | 2018-08-27 07:09:19 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Googler | ba6fdb9 | 2018-12-08 06:29:58 -0800 | [diff] [blame] | 260 | private void verifyNoSummaryPrinted() { |
| 261 | verify(ansiTerminalPrinter, never()).printLn(any()); |
Eran Shalom | f1db75c | 2018-08-27 07:09:19 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Googler | ba6fdb9 | 2018-12-08 06:29:58 -0800 | [diff] [blame] | 264 | private static String info(String message) { |
| 265 | return AnsiTerminalPrinter.Mode.INFO + message + AnsiTerminalPrinter.Mode.DEFAULT; |
Eran Shalom | f1db75c | 2018-08-27 07:09:19 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Googler | ba6fdb9 | 2018-12-08 06:29:58 -0800 | [diff] [blame] | 268 | private static String error(String message) { |
| 269 | return AnsiTerminalPrinter.Mode.ERROR + message + AnsiTerminalPrinter.Mode.DEFAULT; |
Eran Shalom | f1db75c | 2018-08-27 07:09:19 -0700 | [diff] [blame] | 270 | } |
| 271 | } |