|
33 | 33 | import org.codehaus.plexus.PlexusContainer; |
34 | 34 | import org.junit.jupiter.api.AfterEach; |
35 | 35 | import org.junit.jupiter.api.BeforeEach; |
| 36 | +import org.junit.jupiter.api.Nested; |
36 | 37 | import org.junit.jupiter.api.Test; |
37 | 38 | import org.mockito.InOrder; |
38 | 39 |
|
|
42 | 43 | import static org.junit.jupiter.api.Assertions.assertThrows; |
43 | 44 | import static org.junit.jupiter.api.Assertions.assertTrue; |
44 | 45 | import static org.junit.jupiter.api.Assertions.fail; |
45 | | -import static org.junit.jupiter.api.Assumptions.assumeTrue; |
46 | 46 | import static org.mockito.Mockito.any; |
47 | 47 | import static org.mockito.Mockito.inOrder; |
48 | 48 | import static org.mockito.Mockito.mock; |
@@ -258,58 +258,86 @@ public void testMVNConfigurationFunkyArguments() throws Exception { |
258 | 258 | assertEquals("-Dpom.xml", request.getCommandLine().getOptionValue(CLIManager.ALTERNATE_POM_FILE)); |
259 | 259 | } |
260 | 260 |
|
261 | | - @Test |
262 | | - public void testStyleColors() throws Exception { |
263 | | - assumeTrue(MessageUtils.isColorEnabled(), "ANSI not supported"); |
264 | | - CliRequest request; |
| 261 | + @Nested |
| 262 | + class StyleColors { |
265 | 263 |
|
266 | | - MessageUtils.setColorEnabled(true); |
267 | | - request = new CliRequest(new String[] {"-B"}, null); |
268 | | - cli.cli(request); |
269 | | - cli.properties(request); |
270 | | - cli.logging(request); |
271 | | - assertFalse(MessageUtils.isColorEnabled()); |
| 264 | + @BeforeEach |
| 265 | + public void setUp() { |
| 266 | + MessageUtils.systemInstall(); |
| 267 | + MessageUtils.getTerminalWidth(); // wait for fast terminal initialize |
| 268 | + } |
272 | 269 |
|
273 | | - MessageUtils.setColorEnabled(true); |
274 | | - request = new CliRequest(new String[] {"-l", "target/temp/mvn.log"}, null); |
275 | | - request.workingDirectory = "target/temp"; |
276 | | - cli.cli(request); |
277 | | - cli.properties(request); |
278 | | - cli.logging(request); |
279 | | - assertFalse(MessageUtils.isColorEnabled()); |
| 270 | + @AfterEach |
| 271 | + public void tearDown() { |
| 272 | + MessageUtils.systemUninstall(); |
| 273 | + } |
280 | 274 |
|
281 | | - MessageUtils.setColorEnabled(false); |
282 | | - request = new CliRequest(new String[] {"-Dstyle.color=always"}, null); |
283 | | - cli.cli(request); |
284 | | - cli.properties(request); |
285 | | - cli.logging(request); |
286 | | - assertTrue(MessageUtils.isColorEnabled()); |
| 275 | + @Test |
| 276 | + public void testStyleColors() throws Exception { |
| 277 | + CliRequest request; |
287 | 278 |
|
288 | | - MessageUtils.setColorEnabled(true); |
289 | | - request = new CliRequest(new String[] {"-Dstyle.color=never"}, null); |
290 | | - cli.cli(request); |
291 | | - cli.properties(request); |
292 | | - cli.logging(request); |
293 | | - assertFalse(MessageUtils.isColorEnabled()); |
| 279 | + MessageUtils.setColorEnabled(true); |
| 280 | + request = new CliRequest(new String[] {"-B"}, null); |
| 281 | + cli.cli(request); |
| 282 | + cli.properties(request); |
| 283 | + cli.logging(request); |
| 284 | + assertFalse(MessageUtils.isColorEnabled()); |
294 | 285 |
|
295 | | - MessageUtils.setColorEnabled(false); |
296 | | - request = new CliRequest(new String[] {"-Dstyle.color=always", "-B", "-l", "target/temp/mvn.log"}, null); |
297 | | - request.workingDirectory = "target/temp"; |
298 | | - cli.cli(request); |
299 | | - cli.properties(request); |
300 | | - cli.logging(request); |
301 | | - assertTrue(MessageUtils.isColorEnabled()); |
| 286 | + MessageUtils.setColorEnabled(true); |
| 287 | + request = new CliRequest(new String[] {"-l", "target/temp/mvn.log"}, null); |
| 288 | + request.workingDirectory = "target/temp"; |
| 289 | + cli.cli(request); |
| 290 | + cli.properties(request); |
| 291 | + cli.logging(request); |
| 292 | + assertFalse(MessageUtils.isColorEnabled()); |
| 293 | + |
| 294 | + MessageUtils.setColorEnabled(false); |
| 295 | + request = new CliRequest(new String[] {"-Dstyle.color=always"}, null); |
| 296 | + cli.cli(request); |
| 297 | + cli.properties(request); |
| 298 | + cli.logging(request); |
| 299 | + assertTrue(MessageUtils.isColorEnabled()); |
| 300 | + |
| 301 | + MessageUtils.setColorEnabled(true); |
| 302 | + request = new CliRequest(new String[] {"-Dstyle.color=never"}, null); |
| 303 | + cli.cli(request); |
| 304 | + cli.properties(request); |
| 305 | + cli.logging(request); |
| 306 | + assertFalse(MessageUtils.isColorEnabled()); |
302 | 307 |
|
303 | | - try { |
304 | 308 | MessageUtils.setColorEnabled(false); |
305 | | - request = new CliRequest(new String[] {"-Dstyle.color=maybe", "-B", "-l", "target/temp/mvn.log"}, null); |
| 309 | + request = new CliRequest(new String[] {"-Dstyle.color=always", "-B", "-l", "target/temp/mvn.log"}, null); |
306 | 310 | request.workingDirectory = "target/temp"; |
307 | 311 | cli.cli(request); |
308 | 312 | cli.properties(request); |
309 | 313 | cli.logging(request); |
310 | | - fail("maybe is not a valid option"); |
311 | | - } catch (IllegalArgumentException e) { |
312 | | - // noop |
| 314 | + assertTrue(MessageUtils.isColorEnabled()); |
| 315 | + |
| 316 | + try { |
| 317 | + MessageUtils.setColorEnabled(false); |
| 318 | + request = new CliRequest(new String[] {"-Dstyle.color=maybe", "-B", "-l", "target/temp/mvn.log"}, null); |
| 319 | + request.workingDirectory = "target/temp"; |
| 320 | + cli.cli(request); |
| 321 | + cli.properties(request); |
| 322 | + cli.logging(request); |
| 323 | + fail("maybe is not a valid option"); |
| 324 | + } catch (IllegalArgumentException e) { |
| 325 | + // noop |
| 326 | + } |
| 327 | + } |
| 328 | + |
| 329 | + @Test |
| 330 | + void testDumpTerminal() throws Exception { |
| 331 | + // test provide own sys out which is not a tty |
| 332 | + // similar will be with redirected output |
| 333 | + // check that colors are disabled |
| 334 | + |
| 335 | + MessageUtils.setColorEnabled(true); |
| 336 | + CliRequest request = new CliRequest(new String[] {}, null); |
| 337 | + cli.cli(request); |
| 338 | + cli.properties(request); |
| 339 | + cli.logging(request); |
| 340 | + assertFalse(MessageUtils.isColorEnabled()); |
313 | 341 | } |
314 | 342 | } |
315 | 343 |
|
@@ -357,17 +385,20 @@ public void testVersionStringWithoutAnsi() throws Exception { |
357 | 385 | PrintStream oldOut = System.out; |
358 | 386 | System.setOut(new PrintStream(systemOut)); |
359 | 387 |
|
| 388 | + System.setProperty(MavenCli.MULTIMODULE_PROJECT_DIRECTORY, "."); |
| 389 | + |
360 | 390 | // when |
361 | 391 | try { |
362 | | - cli.cli(cliRequest); |
363 | | - } catch (MavenCli.ExitException exitException) { |
364 | | - // expected |
| 392 | + assertEquals(0, cli.doMain(cliRequest)); |
365 | 393 | } finally { |
366 | 394 | // restore sysout |
367 | 395 | System.setOut(oldOut); |
| 396 | + System.clearProperty(MavenCli.MULTIMODULE_PROJECT_DIRECTORY); |
368 | 397 | } |
369 | 398 | String versionOut = new String(systemOut.toByteArray(), StandardCharsets.UTF_8); |
370 | 399 |
|
| 400 | + assertTrue(versionOut.contains("Apache Maven")); |
| 401 | + |
371 | 402 | // then |
372 | 403 | assertEquals(MessageUtils.stripAnsiCodes(versionOut), versionOut); |
373 | 404 | } |
|
0 commit comments