Skip to content

Commit 5805415

Browse files
Copilotbaronfel
andcommitted
Fix tests for .NET Framework environment variable behavior
- Moved empty string environment variable tests under #if NET conditional - On .NET Framework, SetEnvironmentVariable("", "") deletes the variable - Tests now only validate empty string semantics on .NET Core/.NET where supported - Core tests (non-empty values) run on both .NET Framework and .NET Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com>
1 parent 1e0fd3e commit 5805415

File tree

2 files changed

+86
-4
lines changed

2 files changed

+86
-4
lines changed

src/Build.UnitTests/ConsoleLogger_Tests.cs

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,7 +1965,6 @@ internal bool IsRunningWithCharacterFileType()
19651965

19661966
[Theory]
19671967
[InlineData(null, null)] // Default: colors enabled
1968-
[InlineData("", null)] // NO_COLOR="" (empty string) means colors disabled (variable is set)
19691968
[InlineData("1", null)] // NO_COLOR set, colors disabled
19701969
[InlineData("1", "1")] // NO_COLOR supersedes FORCE_COLOR
19711970
public void ColorEnvironmentVariables_DisablesColor(string noColor, string forceColor)
@@ -2011,9 +2010,38 @@ public void ColorEnvironmentVariables_DisablesColor(string noColor, string force
20112010
}
20122011
}
20132012

2013+
#if NET
2014+
// On .NET, empty string environment variables are supported
2015+
[Theory]
2016+
[InlineData("")] // NO_COLOR="" (empty string) means colors disabled (variable is set)
2017+
public void ColorEnvironmentVariables_DisablesColor_EmptyString(string noColor)
2018+
{
2019+
using (TestEnvironment env = TestEnvironment.Create())
2020+
{
2021+
env.SetEnvironmentVariable("NO_COLOR", noColor);
2022+
2023+
EventSourceSink es = new EventSourceSink();
2024+
SimulatedConsole sc = new SimulatedConsole();
2025+
ConsoleLogger logger = new ConsoleLogger(LoggerVerbosity.Normal, sc.Write, sc.SetColor, sc.ResetColor);
2026+
logger.Initialize(es);
2027+
2028+
// Log an error
2029+
es.Consume(new BuildStartedEventArgs("Build started", null));
2030+
BuildEventContext context = new BuildEventContext(1, 1, 1, 1);
2031+
BuildErrorEventArgs errorArgs = new BuildErrorEventArgs(null, "ERR001", "file.cs", 1, 1, 0, 0, "Test error", null, null);
2032+
errorArgs.BuildEventContext = context;
2033+
es.Consume(errorArgs);
2034+
2035+
string log = sc.ToString();
2036+
log.ShouldNotContain("<red>");
2037+
log.ShouldNotContain("<yellow>");
2038+
log.ShouldNotContain("<green>");
2039+
}
2040+
}
2041+
#endif
2042+
20142043
[Theory]
20152044
[InlineData("1")] // FORCE_COLOR set, colors enabled (will use ANSI codes)
2016-
[InlineData("")] // FORCE_COLOR="" (empty string) means colors enabled (variable is set)
20172045
public void ColorEnvironmentVariables_ForcesColor(string forceColor)
20182046
{
20192047
using (TestEnvironment env = TestEnvironment.Create())
@@ -2042,6 +2070,36 @@ public void ColorEnvironmentVariables_ForcesColor(string forceColor)
20422070
}
20432071
}
20442072

2073+
#if NET
2074+
[Theory]
2075+
[InlineData("")] // FORCE_COLOR="" (empty string) means colors enabled (variable is set)
2076+
public void ColorEnvironmentVariables_ForcesColor_EmptyString(string forceColor)
2077+
{
2078+
using (TestEnvironment env = TestEnvironment.Create())
2079+
{
2080+
if (forceColor is not null)
2081+
{
2082+
env.SetEnvironmentVariable("FORCE_COLOR", forceColor);
2083+
}
2084+
2085+
EventSourceSink es = new EventSourceSink();
2086+
SimulatedConsole sc = new SimulatedConsole();
2087+
ConsoleLogger logger = new ConsoleLogger(LoggerVerbosity.Normal, sc.Write, sc.SetColor, sc.ResetColor);
2088+
logger.Initialize(es);
2089+
2090+
// When FORCE_COLOR is set, logger uses ANSI codes which write directly to Console.Out
2091+
es.Consume(new BuildStartedEventArgs("Build started", null));
2092+
BuildEventContext context = new BuildEventContext(1, 1, 1, 1);
2093+
BuildErrorEventArgs errorArgs = new BuildErrorEventArgs(null, "ERR001", "file.cs", 1, 1, 0, 0, "Test error", null, null);
2094+
errorArgs.BuildEventContext = context;
2095+
es.Consume(errorArgs);
2096+
2097+
// Just verify no crash
2098+
sc.ToString().ShouldNotBeNull();
2099+
}
2100+
}
2101+
#endif
2102+
20452103
[Theory]
20462104
[InlineData("false")]
20472105
public void UseColorParameter_DisablesColor(string paramValue)

src/Build.UnitTests/TerminalLogger_Tests.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,11 +1139,9 @@ public async Task DisplayNodesRestoresStatusAfterMSBuildTaskYields_TestProject(b
11391139

11401140
[Theory]
11411141
[InlineData(null, null, true)] // Default: colors enabled
1142-
[InlineData("", null, false)] // NO_COLOR="" (empty string) means colors disabled (variable is set)
11431142
[InlineData("1", null, false)] // NO_COLOR set, colors disabled
11441143
[InlineData("1", "1", false)] // NO_COLOR supersedes FORCE_COLOR
11451144
[InlineData(null, "1", true)] // FORCE_COLOR set, colors enabled
1146-
[InlineData(null, "", true)] // FORCE_COLOR="" (empty string) means colors enabled (variable is set)
11471145
public void ColorEnvironmentVariables(string? noColor, string? forceColor, bool shouldUseColor)
11481146
{
11491147
using (var env = TestEnvironment.Create())
@@ -1164,6 +1162,32 @@ public void ColorEnvironmentVariables(string? noColor, string? forceColor, bool
11641162
}
11651163
}
11661164

1165+
#if NET
1166+
// On .NET, empty string environment variables are supported and different from null
1167+
[Theory]
1168+
[InlineData("", null, false)] // NO_COLOR="" (empty string) means colors disabled (variable is set)
1169+
[InlineData(null, "", true)] // FORCE_COLOR="" (empty string) means colors enabled (variable is set)
1170+
public void ColorEnvironmentVariables_EmptyString(string? noColor, string? forceColor, bool shouldUseColor)
1171+
{
1172+
using (var env = TestEnvironment.Create())
1173+
{
1174+
if (noColor is not null)
1175+
{
1176+
env.SetEnvironmentVariable("NO_COLOR", noColor);
1177+
}
1178+
if (forceColor is not null)
1179+
{
1180+
env.SetEnvironmentVariable("FORCE_COLOR", forceColor);
1181+
}
1182+
1183+
TerminalLogger logger = new TerminalLogger();
1184+
logger.Initialize(_centralNodeEventSource);
1185+
1186+
logger.ShouldUseColor().ShouldBe(shouldUseColor);
1187+
}
1188+
}
1189+
#endif
1190+
11671191
[Theory]
11681192
[InlineData("true", true)]
11691193
[InlineData("false", false)]

0 commit comments

Comments
 (0)