Skip to content

Commit 4090180

Browse files
AndyAyersMSjkotas
andauthored
Add a helper utility to clear the environment variables for tests (#115752)
This preserves most ambient variables other than LANG and anything prefixed with DOTNET_SYSTEM_GLOBALIZATION. Fixes #100505 Co-authored-by: Jan Kotas <[email protected]>
1 parent 4947a30 commit 4090180

File tree

4 files changed

+27
-25
lines changed

4 files changed

+27
-25
lines changed

src/libraries/Common/tests/TestUtilities/System/TestEnvironment.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Collections.Generic;
5+
46
namespace System
57
{
68
public static class TestEnvironment
@@ -17,5 +19,24 @@ public static bool IsStressModeEnabled
1719
return value != null && (value == "1" || value.Equals("true", StringComparison.OrdinalIgnoreCase));
1820
}
1921
}
22+
23+
/// <summary>
24+
/// Removes LANG and any environment variable starting with DOTNET_SYSTEM_GLOBALIZATION from the given environment dictionary.
25+
/// </summary>
26+
public static void ClearGlobalizationEnvironmentVars(IDictionary<string,string?> environment)
27+
{
28+
var keysToRemove = new List<string>();
29+
foreach (var key in environment.Keys)
30+
{
31+
if (key == "LANG" || key.StartsWith("DOTNET_SYSTEM_GLOBALIZATION", StringComparison.OrdinalIgnoreCase))
32+
{
33+
keysToRemove.Add(key);
34+
}
35+
}
36+
foreach (var key in keysToRemove)
37+
{
38+
environment.Remove(key);
39+
}
40+
}
2041
}
2142
}

src/libraries/System.Memory/tests/Span/StringSearchValues.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ private static void RunUsingInvariantCulture(Action action)
597597
Assert.True(CanTestInvariantCulture);
598598

599599
var psi = new ProcessStartInfo();
600-
psi.Environment.Clear();
600+
TestEnvironment.ClearGlobalizationEnvironmentVars(psi.Environment);
601601
psi.Environment.Add("DOTNET_SYSTEM_GLOBALIZATION_INVARIANT", "true");
602602

603603
RemoteExecutor.Invoke(action, new RemoteInvokeOptions { StartInfo = psi, TimeOut = 10 * 60 * 1000 }).Dispose();
@@ -608,7 +608,7 @@ private static void RunUsingNLS(Action action)
608608
Assert.True(CanTestNls);
609609

610610
var psi = new ProcessStartInfo();
611-
psi.Environment.Clear();
611+
TestEnvironment.ClearGlobalizationEnvironmentVars(psi.Environment);
612612
psi.Environment.Add("DOTNET_SYSTEM_GLOBALIZATION_USENLS", "true");
613613

614614
RemoteExecutor.Invoke(action, new RemoteInvokeOptions { StartInfo = psi, TimeOut = 10 * 60 * 1000 }).Dispose();

src/libraries/System.Runtime/tests/System.Globalization.Tests/CultureInfo/CultureInfoCurrentCulture.cs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,7 @@ public void CurrentUICulture_Set_Null_ThrowsArgumentNullException()
130130
public void CurrentCulture_BasedOnLangEnvVar(string langEnvVar, string expectedCultureName)
131131
{
132132
var psi = new ProcessStartInfo();
133-
psi.Environment.Clear();
134-
135-
CopyEssentialTestEnvironment(psi.Environment);
133+
TestEnvironment.ClearGlobalizationEnvironmentVars(psi.Environment);
136134

137135
psi.Environment["LANG"] = langEnvVar;
138136

@@ -154,9 +152,7 @@ public void CurrentCulture_BasedOnLangEnvVar(string langEnvVar, string expectedC
154152
public void CurrentCulture_DefaultWithNoLang(string langEnvVar)
155153
{
156154
var psi = new ProcessStartInfo();
157-
psi.Environment.Clear();
158-
159-
CopyEssentialTestEnvironment(psi.Environment);
155+
TestEnvironment.ClearGlobalizationEnvironmentVars(psi.Environment);
160156

161157
if (langEnvVar != null)
162158
{
@@ -187,20 +183,5 @@ public void CurrentCulture_DefaultWithNoLang(string langEnvVar)
187183
}
188184
}, new RemoteInvokeOptions { StartInfo = psi }).Dispose();
189185
}
190-
191-
private static void CopyEssentialTestEnvironment(IDictionary<string, string> environment)
192-
{
193-
string[] essentialVariables = { "HOME", "LD_LIBRARY_PATH", "ICU_DATA" };
194-
string[] prefixedVariables = { "DOTNET_", "COMPlus_", "SuperPMIShim" };
195-
196-
foreach (DictionaryEntry de in Environment.GetEnvironmentVariables())
197-
{
198-
if (Array.FindIndex(essentialVariables, x => x.Equals(de.Key)) >= 0 ||
199-
Array.FindIndex(prefixedVariables, x => ((string)de.Key).StartsWith(x, StringComparison.OrdinalIgnoreCase)) >= 0)
200-
{
201-
environment[(string)de.Key] = (string)de.Value;
202-
}
203-
}
204-
}
205186
}
206187
}

src/libraries/System.Runtime/tests/System.Globalization.Tests/CultureInfo/GetCultureInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public void TestFakeCultureNames(string name)
147147
public void PredefinedCulturesOnlyEnvVarTest(string predefinedCulturesOnlyEnvVar, string cultureName)
148148
{
149149
var psi = new ProcessStartInfo();
150-
psi.Environment.Clear();
150+
TestEnvironment.ClearGlobalizationEnvironmentVars(psi.Environment);
151151

152152
psi.Environment.Add("DOTNET_SYSTEM_GLOBALIZATION_PREDEFINED_CULTURES_ONLY", predefinedCulturesOnlyEnvVar);
153153

@@ -176,7 +176,7 @@ public void PredefinedCulturesOnlyEnvVarTest(string predefinedCulturesOnlyEnvVar
176176
public void TestAllowInvariantCultureOnly(bool enableInvariant, bool predefinedCulturesOnly, bool declarePredefinedCulturesOnly)
177177
{
178178
var psi = new ProcessStartInfo();
179-
psi.Environment.Clear();
179+
TestEnvironment.ClearGlobalizationEnvironmentVars(psi.Environment);
180180

181181
if (enableInvariant)
182182
{

0 commit comments

Comments
 (0)