Special case the posix locale in WildcardPattern (#10186)

This commit is contained in:
Dongbo Wang 2019-07-26 10:58:37 -07:00 committed by Travis Plunk
parent 8f5b2c241e
commit d2e81dbc13
2 changed files with 23 additions and 3 deletions

View file

@ -146,7 +146,11 @@ namespace System.Management.Automation
{
stringComparison = Options.HasFlag(WildcardOptions.CultureInvariant)
? StringComparison.InvariantCultureIgnoreCase
: StringComparison.CurrentCultureIgnoreCase;
: CultureInfo.CurrentCulture.Name.Equals("en-US-POSIX", StringComparison.OrdinalIgnoreCase)
// The collation behavior of the POSIX locale (also known as the C locale) is case sensitive.
// For this specific locale, we use 'OrdinalIgnoreCase'.
? StringComparison.OrdinalIgnoreCase
: StringComparison.CurrentCultureIgnoreCase;
}
else
{

View file

@ -304,8 +304,8 @@ Describe "Hash expression with if statement as value" -Tags "CI" {
}
}
Describe "Hashtable is case insensitive" -Tag CI {
It "When current culture is en-US-POSIX" -Skip:($IsWindows) {
Describe "Support case-insensitive comparison in Posix locale" -Tag CI {
It "Hashtable is case insensitive" -Skip:($IsWindows) {
try {
$oldCulture = [System.Globalization.CultureInfo]::CurrentCulture
[System.Globalization.CultureInfo]::CurrentCulture = [System.Globalization.CultureInfo]::new('en-US-POSIX')
@ -316,4 +316,20 @@ Describe "Hashtable is case insensitive" -Tag CI {
[System.Globalization.CultureInfo]::CurrentCulture = $oldCulture
}
}
It "Wildcard support case-insensitive matching" -Skip:($IsWindows) {
try {
$oldCulture = [System.Globalization.CultureInfo]::CurrentCulture
[System.Globalization.CultureInfo]::CurrentCulture = [System.Globalization.CultureInfo]::new('en-US-POSIX')
$wildcard1 = [WildcardPattern]::new("AbC*", [System.Management.Automation.WildcardOptions]::IgnoreCase)
$wildcard1.IsMatch("abcd") | Should -BeTrue
$wildcard2 = [WildcardPattern]::new("DeF", [System.Management.Automation.WildcardOptions]::IgnoreCase);
$wildcard2.IsMatch("def") | Should -BeTrue
}
finally {
[System.Globalization.CultureInfo]::CurrentCulture = $oldCulture
}
}
}