From 31ec7d068ca43ae27c1fd32b6683b2b903f6c057 Mon Sep 17 00:00:00 2001 From: Marc Philipp Date: Sat, 14 Nov 2015 23:08:47 +0100 Subject: [PATCH] Opt-out switch to disable colored console output --- .../console/ColoredPrintingTestListener.java | 19 +++++++++++++++---- .../org/junit/gen5/console/ConsoleRunner.java | 8 +++++++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/junit-console/src/main/java/org/junit/gen5/console/ColoredPrintingTestListener.java b/junit-console/src/main/java/org/junit/gen5/console/ColoredPrintingTestListener.java index a5b4b9e043c..840f7778fe0 100644 --- a/junit-console/src/main/java/org/junit/gen5/console/ColoredPrintingTestListener.java +++ b/junit-console/src/main/java/org/junit/gen5/console/ColoredPrintingTestListener.java @@ -29,9 +29,11 @@ public class ColoredPrintingTestListener implements TestPlanExecutionListener, TestExecutionListener { private final PrintStream out; + private final boolean disableAnsiColors; - public ColoredPrintingTestListener(PrintStream out) { + public ColoredPrintingTestListener(PrintStream out, boolean disableAnsiColors) { this.out = out; + this.disableAnsiColors = disableAnsiColors; } @Override @@ -110,9 +112,18 @@ private void printlnException(Color color, Throwable throwable) { println(color, " => Exception: %s", throwable.getLocalizedMessage()); } - void println(Color color, String format, Object... args) { - // Use string concatenation to avoid ansi disruption on console - out.println(color + String.format(format, args) + NONE); + private void println(Color color, String format, Object... args) { + println(color, String.format(format, args)); + } + + private void println(Color color, String message) { + if (disableAnsiColors) { + out.println(message); + } + else { + // Use string concatenation to avoid ansi disruption on console + out.println(color + message + NONE); + } } enum Color { diff --git a/junit-console/src/main/java/org/junit/gen5/console/ConsoleRunner.java b/junit-console/src/main/java/org/junit/gen5/console/ConsoleRunner.java index 6e5cd43f21d..dfbde8e358f 100644 --- a/junit-console/src/main/java/org/junit/gen5/console/ConsoleRunner.java +++ b/junit-console/src/main/java/org/junit/gen5/console/ConsoleRunner.java @@ -41,6 +41,12 @@ public class ConsoleRunner { private boolean enableExitCode; // @formatter:on + // @formatter:off + @Option(name = { "-C", "--disable-ansi-colors" }, + description = "Disable colored output (not supported by all terminals)") + private boolean disableAnsiColors; + // @formatter:on + @Arguments(description = "Test classes to execute") private List testClasses; @@ -59,7 +65,7 @@ private void run() { TestSummaryReportingTestListener testSummaryListener = new TestSummaryReportingTestListener(System.out); launcher.registerTestPlanExecutionListeners( // @formatter:off - new ColoredPrintingTestListener(System.out), + new ColoredPrintingTestListener(System.out, disableAnsiColors), testSummaryListener // @formatter:on );