Skip to content

Commit e27e607

Browse files
committed
Fix force color check
The original logic passed on each platform, but failed on WSL. These changes look more correct and gets the desired results on all platforms and WSL.
1 parent 0062d6c commit e27e607

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

src/ConsoleHelpers.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ private static ConsoleFormatInfo FormatInfoBuilder(IEnvironment environment)
1414
SupportsAnsiCodes = ConsoleFormatInfo.CurrentInfo.SupportsAnsiCodes,
1515
};
1616

17-
if (consoleFormatProvider.SupportsAnsiCodes)
17+
if (environment.GetEnvironmentVariable("NO_COLOR") is not null)
1818
{
19-
consoleFormatProvider.SupportsAnsiCodes = environment.GetEnvironmentVariable("NO_COLOR") is null;
19+
consoleFormatProvider.SupportsAnsiCodes = false;
20+
21+
return consoleFormatProvider;
2022
}
21-
else
22-
{
23-
var envVar = environment.GetEnvironmentVariable("DOTNET_SYSTEM_CONSOLE_ALLOW_ANSI_COLOR_REDIRECTION");
2423

25-
consoleFormatProvider.SupportsAnsiCodes = envVar is not null && (envVar == "1" || envVar.Equals("true", StringComparison.OrdinalIgnoreCase));
24+
var envVar = environment.GetEnvironmentVariable("DOTNET_SYSTEM_CONSOLE_ALLOW_ANSI_COLOR_REDIRECTION");
25+
26+
if (envVar is not null)
27+
{
28+
consoleFormatProvider.SupportsAnsiCodes = envVar == "1" || envVar.Equals("true", StringComparison.OrdinalIgnoreCase);
2629
}
2730

2831
return consoleFormatProvider;

test/ConsoleHelpersTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class ConsoleHelpersTests
1616
public void FormatInfoBuilder_should_support_NO_COLOR_env_var(string value)
1717
{
1818
// Given
19-
var environment = new TestEnvironment(isWindows: true);
19+
var environment = new TestEnvironment();
2020

2121
environment.SetEnvironmentVariable("NO_COLOR", value);
2222

@@ -41,7 +41,7 @@ public void FormatInfoBuilder_should_support_NO_COLOR_env_var(string value)
4141
public void FormatInfoBuilder_should_support_DOTNET_SYSTEM_CONSOLE_ALLOW_ANSI_COLOR_REDIRECTION_env_var(string value, bool expected)
4242
{
4343
// Given
44-
var environment = new TestEnvironment(isWindows: true);
44+
var environment = new TestEnvironment();
4545

4646
environment.SetEnvironmentVariable("DOTNET_SYSTEM_CONSOLE_ALLOW_ANSI_COLOR_REDIRECTION", value);
4747

test/TestEnvironment.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
namespace RunScript;
22

33
using System.Collections;
4+
using System.Runtime.InteropServices;
45

56
internal class TestEnvironment : IEnvironment
67
{
78
private readonly Dictionary<string, string?> _variables = new(StringComparer.OrdinalIgnoreCase);
89

9-
public TestEnvironment(bool isWindows)
10+
public TestEnvironment(bool? isWindows = null)
1011
{
1112
CurrentDirectory = AttributeReader.GetProjectDirectory(GetType().Assembly);
12-
IsWindows = isWindows;
13+
IsWindows = isWindows ?? RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
1314
}
1415

1516
public string CurrentDirectory { get; }

0 commit comments

Comments
 (0)