8000 #783: Add -useansicolor flag to console runner (v2) · xunit/xunit@a8ceb66 · GitHub
[go: up one dir, main page]

Skip to content

Commit a8ceb66

Browse files
committed
#783: Add -useansicolor flag to console runner (v2)
1 parent 7b0ff93 commit a8ceb66

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

src/common/ConsoleHelper.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ static void ResetColorANSI()
6969

7070
static void ResetColorConsole()
7171
=> Console.ResetColor();
72+
73+
internal static void UseAnsiColor()
74+
{
75+
ResetColor = ResetColorANSI;
76+
SetForegroundColor = SetForegroundColorANSI;
77+
}
7278
}
7379
}
7480

src/xunit.console/CommandLine.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ protected CommandLine(string[] args, Predicate<string> fileExists = null)
5656

5757
public bool StopOnFail { get; protected set; }
5858

59+
public bool UseAnsiColor { get; protected set; }
60+
5961
public bool Wait { get; protected set; }
6062

6163
public IRunnerReporter ChooseReporter(IReadOnlyList<IRunnerReporter> reporters)
@@ -390,6 +392,11 @@ protected XunitProject Parse(Predicate<string> fileExists)
390392

391393
project.Filters.ExcludedNamespaces.Add(option.Value);
392394
}
395+
else if (optionName == "useansicolor")
396+
{
397+
GuardNoOptionValue(option);
398+
UseAnsiColor = true;
399+
}
393400
else
394401
{
395402
// Might be a result output file...

src/xunit.console/ConsoleRunner.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public int EntryPoint(string[] args)
3030
{
3131
commandLine = CommandLine.Parse(args);
3232

33+
if (commandLine.UseAnsiColor)
34+
ConsoleHelper.UseAnsiColor();
35+
3336
try
3437
{
3538
var reporters = GetAvailableRunnerReporters();
@@ -76,7 +79,7 @@ public int EntryPoint(string[] args)
7679
if (commandLine.Debug)
7780
Debugger.Launch();
7881

79-
logger = new ConsoleRunnerLogger(!commandLine.NoColor, consoleLock);
82+
logger = new ConsoleRunnerLogger(!commandLine.NoColor, commandLine.UseAnsiColor, consoleLock);
8083
reporterMessageHandler = MessageSinkWithTypesAdapter.Wrap(reporter.CreateMessageHandler(logger));
8184

8285
if (!commandLine.NoLogo)
@@ -240,6 +243,7 @@ void PrintUsage(IReadOnlyList<IRunnerReporter> reporters)
240243
Console.WriteLine(" -pause : pause before doing any work, to help attach a debugger");
241244
#endif
242245
Console.WriteLine(" -debug : launch the debugger to debug the tests");
246+
Console.WriteLine(" -useansicolor : force using ANSI color output on Windows (non-Windows always uses ANSI colors)");
243247
Console.WriteLine(" -serialize : serialize all test cases (for diagnostic purposes only)");
244248
Console.WriteLine(" -trait \"name=value\" : only run tests with matching name/value traits");
245249
Console.WriteLine(" : if specified more than once, acts as an OR operation");

src/xunit.runner.utility/Utility/ConsoleRunnerLogger.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,52 @@
11
#if NETFRAMEWORK || NETCOREAPP
22

33
using System;
4+
using System.ComponentModel;
45

56
namespace Xunit
67
{
78
/// <summary>
8-
/// An implementation of <see cref="IRunnerLogger"/> which logs messages
9-
/// to <see cref="Console"/> and <see cref="Console.Error"/>.
9+
/// An implementation of <see cref="IRunnerLogger"/> which logs messages to <see cref="Console"/>.
1010
/// </summary>
1111
public class ConsoleRunnerLogger : IRunnerLogger
1212
{
1313
readonly object lockObject;
1414
readonly bool useColors;
1515

16+
/// <s 6D4E ummary/>
17+
[EditorBrowsable(EditorBrowsableState.Never)]
18+
[Obsolete("Please use the new overload with the useAnsiColor flag")]
19+
public ConsoleRunnerLogger(bool useColors) : this(useColors, useAnsiColor: false, new object()) { }
20+
21+
/// <summary/>
22+
[EditorBrowsable(EditorBrowsableState.Never)]
23+
[Obsolete("Please use the new overload with the useAnsiColor flag")]
24+
public ConsoleRunnerLogger(bool useColors, object lockObject) : this(useColors, useAnsiColor: false, lockObject) { }
25+
1626
/// <summary>
1727
/// Initializes a new instance of the <see cref="ConsoleRunnerLogger"/> class.
1828
/// </summary>
1929
/// <param name="useColors">A flag to indicate whether colors should be used when
2030
/// logging messages.</param>
21-
public ConsoleRunnerLogger(bool useColors) : this(useColors, new object()) { }
31+
/// <param name="useAnsiColor">A flag to indicate whether ANSI colors should be
32+
/// forced on Windows.</param>
33+
public ConsoleRunnerLogger(bool useColors, bool useAnsiColor) : this(useColors, useAnsiColor, new object()) { }
2234

2335
/// <summary>
2436
/// Initializes a new instance of the <see cref="ConsoleRunnerLogger"/> class.
2537
/// </summary>
2638
/// <param name="useColors">A flag to indicate whether colors should be used when
2739
/// logging messages.</param>
40+
/// <param name="useAnsiColor">A flag to indicate whether ANSI colors should be
41+
/// forced on Windows.</param>
2842
/// <param name="lockObject">The lock object used to prevent console clashes.</param>
29-
public ConsoleRunnerLogger(bool useColors, object lockObject)
43+
public ConsoleRunnerLogger(bool useColors, bool useAnsiColor, object lockObject)
3044
{
3145
this.useColors = useColors;
3246
this.lockObject = lockObject;
47+
48+
if (useAnsiColor)
49+
ConsoleHelper.UseAnsiColor();
3350
}
3451

3552
/// <inheritdoc/>
@@ -40,7 +57,7 @@ public void LogError(StackFrameInfo stackFrame, string message)
4057
{
4158
lock (LockObject)
4259
using (SetColor(ConsoleColor.Red))
43-
Console.Error.WriteLine(message);
60+
Console.WriteLine(message);
4461
}
4562

4663
/// <inheritdoc/>

0 commit comments

Comments
 (0)
0