Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 16f4937

Browse files
authored
Fixed CurrentCulture static property assignment causing side effects in other tests (#19740)
* Fixed CurrentCulture static set * Moved another test to RemoteInvoke * Fixed transform of string array with an empty string element
1 parent 977712e commit 16f4937

File tree

4 files changed

+84
-37
lines changed

4 files changed

+84
-37
lines changed

src/Common/tests/System/Diagnostics/RemoteExecutorTestBase.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,37 @@ internal static RemoteInvokeHandle RemoteInvoke(
9191
return RemoteInvoke(method.GetMethodInfo(), new[] { arg1, arg2, arg3 }, options);
9292
}
9393

94+
/// <summary>Invokes the method from this assembly in another process using the specified arguments.</summary>
95+
/// <param name="method">The method to invoke.</param>
96+
/// <param name="arg1">The first argument to pass to the method.</param>
97+
/// <param name="arg2">The second argument to pass to the method.</param>
98+
/// <param name="arg3">The third argument to pass to the method.</param>
99+
/// <param name="arg4">The fourth argument to pass to the method.</param>
100+
/// <param name="options">Options to use for the invocation.</param>
101+
internal static RemoteInvokeHandle RemoteInvoke(
102+
Func<string, string, string, string, int> method,
103+
string arg1, string arg2, string arg3, string arg4,
104+
RemoteInvokeOptions options = null)
105+
{
106+
return RemoteInvoke(method.GetMethodInfo(), new[] { arg1, arg2, arg3, arg4 }, options);
107+
}
108+
109+
/// <summary>Invokes the method from this assembly in another process using the specified arguments.</summary>
110+
/// <param name="method">The method to invoke.</param>
111+
/// <param name="arg1">The first argument to pass to the method.</param>
112+
/// <param name="arg2">The second argument to pass to the method.</param>
113+
/// <param name="arg3">The third argument to pass to the method.</param>
114+
/// <param name="arg4">The fourth argument to pass to the method.</param>
115+
/// <param name="arg5">The fifth argument to pass to the method.</param>
116+
/// <param name="options">Options to use for the invocation.</param>
117+
internal static RemoteInvokeHandle RemoteInvoke(
118+
Func<string, string, string, string, string, int> method,
119+
string arg1, string arg2, string arg3, string arg4, string arg5,
120+
RemoteInvokeOptions options = null)
121+
{
122+
return RemoteInvoke(method.GetMethodInfo(), new[] { arg1, arg2, arg3, arg4, arg5 }, options);
123+
}
124+
94125
/// <summary>Invokes the method from this assembly in another process using the specified arguments.</summary>
95126
/// <param name="method">The method to invoke.</param>
96127
/// <param name="args">The arguments to pass to the method.</param>

src/System.Text.RegularExpressions/tests/Regex.Groups.Tests.cs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System.Collections.Generic;
6+
using System.Diagnostics;
67
using System.Globalization;
8+
using System.Linq;
79
using Xunit;
810

911
namespace System.Text.RegularExpressions.Tests
1012
{
11-
public class RegexGroupTests
13+
public class RegexGroupTests : RemoteExecutorTestBase
1214
{
1315
private static readonly CultureInfo s_enUSCulture = new CultureInfo("en-US");
1416
private static readonly CultureInfo s_invariantCulture = new CultureInfo("");
@@ -622,43 +624,53 @@ public static IEnumerable<object[]> Groups_CustomCulture_TestData()
622624
[MemberData(nameof(Groups_CustomCulture_TestData))]
623625
public void Groups(string pattern, string input, RegexOptions options, CultureInfo cultureInfo, string[] expectedGroups)
624626
{
625-
CultureInfo originalCulture = CultureInfo.CurrentCulture;
626-
try
627+
const string EmptyPlaceholder = "-";
628+
const char Seperator = ';';
629+
630+
string outerPattern = Convert.ToBase64String(Encoding.UTF8.GetBytes(pattern));
631+
string outerInput = Convert.ToBase64String(Encoding.UTF8.GetBytes(input));
632+
string outerOptions = ((int)options).ToString();
633+
string outerCultureInfo = cultureInfo != null ? cultureInfo.ToString() : EmptyPlaceholder;
634+
string outerExpectedGroups = expectedGroups != null && expectedGroups.Length > 0 ? "\"" + Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Join(Seperator.ToString(), expectedGroups.Select(s => s == string.Empty ? EmptyPlaceholder : s).ToArray()))) + "\"" : EmptyPlaceholder;
635+
636+
RemoteInvoke((innerPatternEnc, innerInputEnc, innerOptionsEnc, innerCultureInfoEnc, innerExpectedGroupsEnc) =>
627637
{
638+
string innerPattern = Encoding.UTF8.GetString(Convert.FromBase64String(innerPatternEnc));
639+
string innerInput = Encoding.UTF8.GetString(Convert.FromBase64String(innerInputEnc));
640+
RegexOptions innerOptions = (RegexOptions)int.Parse(innerOptionsEnc);
641+
CultureInfo innerCultureInfo = innerCultureInfoEnc != EmptyPlaceholder ? new CultureInfo(innerCultureInfoEnc) : null;
642+
string[] innerExpectedGroups = innerExpectedGroupsEnc != EmptyPlaceholder ? Encoding.UTF8.GetString(Convert.FromBase64String(innerExpectedGroupsEnc.Trim('"'))).Split(Seperator).Select(s => s == EmptyPlaceholder ? string.Empty : s).ToArray() : new string[] { };
643+
628644
// In invariant culture, the unicode char matches differ from expected values provided.
629-
if (originalCulture.Equals(CultureInfo.InvariantCulture))
645+
if (CultureInfo.CurrentCulture.Equals(CultureInfo.InvariantCulture))
630646
{
631-
CultureInfo.CurrentCulture = s_enUSCulture;
647+
CultureInfo.CurrentCulture = new CultureInfo("en-US");
632648
}
633-
if (cultureInfo != null)
649+
if (innerCultureInfo != null)
634650
{
635-
CultureInfo.CurrentCulture = cultureInfo;
651+
CultureInfo.CurrentCulture = innerCultureInfo;
636652
}
637-
Regex regex = new Regex(pattern, options);
638-
Match match = regex.Match(input);
653+
654+
Regex regex = new Regex(innerPattern, innerOptions);
655+
Match match = regex.Match(innerInput);
639656
Assert.True(match.Success);
640657

641-
Assert.Equal(expectedGroups.Length, match.Groups.Count);
642-
Assert.True(expectedGroups[0] == match.Value, string.Format("Culture used: {0}", CultureInfo.CurrentCulture));
658+
Assert.Equal(innerExpectedGroups.Length, match.Groups.Count);
659+
Assert.True(innerExpectedGroups[0] == match.Value, string.Format("Culture used: {0}", CultureInfo.CurrentCulture));
643660

644661
int[] groupNumbers = regex.GetGroupNumbers();
645662
string[] groupNames = regex.GetGroupNames();
646-
for (int i = 0; i < expectedGroups.Length; i++)
663+
for (int i = 0; i < innerExpectedGroups.Length; i++)
647664
{
648-
Assert.Equal(expectedGroups[i], match.Groups[groupNumbers[i]].Value);
665+
Assert.Equal(innerExpectedGroups[i], match.Groups[groupNumbers[i]].Value);
649666
Assert.Equal(match.Groups[groupNumbers[i]], match.Groups[groupNames[i]]);
650667

651668
Assert.Equal(groupNumbers[i], regex.GroupNumberFromName(groupNames[i]));
652669
Assert.Equal(groupNames[i], regex.GroupNameFromNumber(groupNumbers[i]));
653670
}
654-
}
655-
finally
656-
{
657-
if (cultureInfo != null || originalCulture.Equals(CultureInfo.InvariantCulture))
658-
{
659-
CultureInfo.CurrentCulture = originalCulture;
660-
}
661-
}
671+
672+
return SuccessExitCode;
673+
}, outerPattern, outerInput, outerOptions, outerCultureInfo, outerExpectedGroups).Dispose();
662674
}
663675
}
664676
}

src/System.Text.RegularExpressions/tests/Regex.Match.Tests.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System.Collections.Generic;
6+
using System.Diagnostics;
67
using System.Globalization;
78
using Xunit;
89

910
namespace System.Text.RegularExpressions.Tests
1011
{
11-
public class RegexMatchTests
12+
public class RegexMatchTests : RemoteExecutorTestBase
1213
{
1314
public static IEnumerable<object[]> Match_Basic_TestData()
1415
{
@@ -692,11 +693,9 @@ public void Result_Invalid()
692693
[Fact]
693694
public void Match_SpecialUnicodeCharacters()
694695
{
695-
CultureInfo currentCulture = CultureInfo.CurrentCulture;
696-
CultureInfo enUSCulture = new CultureInfo("en-US");
697-
try
696+
RemoteInvoke(() =>
698697
{
699-
CultureInfo.CurrentCulture = enUSCulture;
698+
CultureInfo.CurrentCulture = new CultureInfo("en-US");
700699
Match("\u0131", "\u0049", RegexOptions.IgnoreCase, 0, 1, false, string.Empty);
701700
Match("\u0131", "\u0069", RegexOptions.IgnoreCase, 0, 1, false, string.Empty);
702701

@@ -705,11 +704,9 @@ public void Match_SpecialUnicodeCharacters()
705704
Match("\u0131", "\u0069", RegexOptions.IgnoreCase, 0, 1, false, string.Empty);
706705
Match("\u0130", "\u0049", RegexOptions.IgnoreCase, 0, 1, false, string.Empty);
707706
Match("\u0130", "\u0069", RegexOptions.IgnoreCase, 0, 1, false, string.Empty);
708-
}
709-
finally
710-
{
711-
CultureInfo.CurrentCulture = currentCulture;
712-
}
707+
708+
return SuccessExitCode;
709+
}).Dispose();
713710
}
714711

715712
[Fact]

src/System.Text.RegularExpressions/tests/System.Text.RegularExpressions.Tests.csproj

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
<Compile Include="$(CommonTestPath)\System\PlatformDetection.cs">
1616
<Link>Common\tests\System\PlatformDetection.cs</Link>
1717
</Compile>
18+
<Compile Include="$(CommonTestPath)\System\Diagnostics\RemoteExecutorTestBase.cs">
19+
<Link>Common\System\Diagnostics\RemoteExecutorTestBase.cs</Link>
20+
</Compile>
21+
<Compile Include="$(CommonTestPath)\System\IO\FileCleanupTestBase.cs">
22+
<Link>Common\System\IO\FileCleanupTestBase.cs</Link>
23+
</Compile>
1824
<Compile Include="CaptureCollectionTests.cs" />
1925
<Compile Include="GroupCollectionTests.cs" />
2026
<Compile Include="MatchCollectionTests.cs" />
@@ -25,12 +31,19 @@
2531
<Compile Include="Regex.UnicodeChar.Tests.cs" />
2632
<Compile Include="Regex.Groups.Tests.cs" />
2733
<Compile Include="Regex.Replace.Tests.cs" />
34+
<Compile Include="Regex.Serialization.cs" />
2835
<Compile Include="Regex.Split.Tests.cs" />
2936
<Compile Include="Regex.Match.Tests.cs" />
3037
<Compile Include="Regex.Tests.Common.cs" />
3138
<Compile Include="$(CommonTestPath)\System\AssertExtensions.cs">
3239
<Link>Common\System\AssertExtensions.cs</Link>
3340
</Compile>
41+
<Compile Include="$(CommonTestPath)\System\Runtime\Serialization\Formatters\BinaryFormatterHelpers.cs">
42+
<Link>System\Runtime\Serialization\Formatters\BinaryFormatterHelpers.cs</Link>
43+
</Compile>
44+
<ProjectReference Include="$(CommonTestPath)\System\Diagnostics\RemoteExecutorConsoleApp\RemoteExecutorConsoleApp.csproj">
45+
<Name>RemoteExecutorConsoleApp</Name>
46+
</ProjectReference>
3447
</ItemGroup>
3548
<ItemGroup Condition="'$(TargetGroup)' == 'netcoreapp'">
3649
<Compile Include="PrecompiledRegexScenarioTest.cs" />
@@ -42,12 +55,6 @@
4255
<Link>Common\System\Diagnostics\DebuggerAttributes.cs</Link>
4356
</Compile>
4457
</ItemGroup>
45-
<ItemGroup>
46-
<Compile Include="Regex.Serialization.cs" />
47-
<Compile Include="$(CommonTestPath)\System\Runtime\Serialization\Formatters\BinaryFormatterHelpers.cs">
48-
<Link>System\Runtime\Serialization\Formatters\BinaryFormatterHelpers.cs</Link>
49-
</Compile>
50-
</ItemGroup>
5158
<!-- Automatically added by VS -->
5259
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
5360
</Project>

0 commit comments

Comments
 (0)