Skip to content

Commit

Permalink
#783: Add -useansicolor flag to console runner (v2)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Apr 24, 2024
1 parent 7b0ff93 commit a8ceb66
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/common/ConsoleHelper.cs
Expand Up @@ -69,6 +69,12 @@ static void ResetColorANSI()

static void ResetColorConsole()
=> Console.ResetColor();

internal static void UseAnsiColor()
{
ResetColor = ResetColorANSI;
SetForegroundColor = SetForegroundColorANSI;
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/xunit.console/CommandLine.cs
Expand Up @@ -56,6 +56,8 @@ protected CommandLine(string[] args, Predicate<string> fileExists = null)

public bool StopOnFail { get; protected set; }

public bool UseAnsiColor { get; protected set; }

public bool Wait { get; protected set; }

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

project.Filters.ExcludedNamespaces.Add(option.Value);
}
else if (optionName == "useansicolor")
{
GuardNoOptionValue(option);
UseAnsiColor = true;
}
else
{
// Might be a result output file...
Expand Down
6 changes: 5 additions & 1 deletion src/xunit.console/ConsoleRunner.cs
Expand Up @@ -30,6 +30,9 @@ public int EntryPoint(string[] args)
{
commandLine = CommandLine.Parse(args);

if (commandLine.UseAnsiColor)
ConsoleHelper.UseAnsiColor();

try
{
var reporters = GetAvailableRunnerReporters();
Expand Down Expand Up @@ -76,7 +79,7 @@ public int EntryPoint(string[] args)
if (commandLine.Debug)
Debugger.Launch();

logger = new ConsoleRunnerLogger(!commandLine.NoColor, consoleLock);
logger = new ConsoleRunnerLogger(!commandLine.NoColor, commandLine.UseAnsiColor, consoleLock);
reporterMessageHandler = MessageSinkWithTypesAdapter.Wrap(reporter.CreateMessageHandler(logger));

if (!commandLine.NoLogo)
Expand Down Expand Up @@ -240,6 +243,7 @@ void PrintUsage(IReadOnlyList<IRunnerReporter> reporters)
Console.WriteLine(" -pause : pause before doing any work, to help attach a debugger");
#endif
Console.WriteLine(" -debug : launch the debugger to debug the tests");
Console.WriteLine(" -useansicolor : force using ANSI color output on Windows (non-Windows always uses ANSI colors)");
Console.WriteLine(" -serialize : serialize all test cases (for diagnostic purposes only)");
Console.WriteLine(" -trait \"name=value\" : only run tests with matching name/value traits");
Console.WriteLine(" : if specified more than once, acts as an OR operation");
Expand Down
27 changes: 22 additions & 5 deletions src/xunit.runner.utility/Utility/ConsoleRunnerLogger.cs
@@ -1,35 +1,52 @@
#if NETFRAMEWORK || NETCOREAPP

using System;
using System.ComponentModel;

namespace Xunit
{
/// <summary>
/// An implementation of <see cref="IRunnerLogger"/> which logs messages
/// to <see cref="Console"/> and <see cref="Console.Error"/>.
/// An implementation of <see cref="IRunnerLogger"/> which logs messages to <see cref="Console"/>.
/// </summary>
public class ConsoleRunnerLogger : IRunnerLogger
{
readonly object lockObject;
readonly bool useColors;

/// <summary/>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("Please use the new overload with the useAnsiColor flag")]
public ConsoleRunnerLogger(bool useColors) : this(useColors, useAnsiColor: false, new object()) { }

/// <summary/>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("Please use the new overload with the useAnsiColor flag")]
public ConsoleRunnerLogger(bool useColors, object lockObject) : this(useColors, useAnsiColor: false, lockObject) { }

/// <summary>
/// Initializes a new instance of the <see cref="ConsoleRunnerLogger"/> class.
/// </summary>
/// <param name="useColors">A flag to indicate whether colors should be used when
/// logging messages.</param>
public ConsoleRunnerLogger(bool useColors) : this(useColors, new object()) { }
/// <param name="useAnsiColor">A flag to indicate whether ANSI colors should be
/// forced on Windows.</param>
public ConsoleRunnerLogger(bool useColors, bool useAnsiColor) : this(useColors, useAnsiColor, new object()) { }

/// <summary>
/// Initializes a new instance of the <see cref="ConsoleRunnerLogger"/> class.
/// </summary>
/// <param name="useColors">A flag to indicate whether colors should be used when
/// logging messages.</param>
/// <param name="useAnsiColor">A flag to indicate whether ANSI colors should be
/// forced on Windows.</param>
/// <param name="lockObject">The lock object used to prevent console clashes.</param>
public ConsoleRunnerLogger(bool useColors, object lockObject)
public ConsoleRunnerLogger(bool useColors, bool useAnsiColor, object lockObject)
{
this.useColors = useColors;
this.lockObject = lockObject;

if (useAnsiColor)
ConsoleHelper.UseAnsiColor();
}

/// <inheritdoc/>
Expand All @@ -40,7 +57,7 @@ public void LogError(StackFrameInfo stackFrame, string message)
{
lock (LockObject)
using (SetColor(ConsoleColor.Red))
Console.Error.WriteLine(message);
Console.WriteLine(message);
}

/// <inheritdoc/>
Expand Down

0 comments on commit a8ceb66

Please sign in to comment.