Browse Source

test(items): assert that Get-ChildItem2 restores the culture of the caller

The tr-TR test reset the culture in its own finally block, so a cmdlet
that did not restore it, or that wrote an item inside the scope of the
invariant culture, would still pass. The test now reads the culture in
the next pipeline stage for each item and once more after the call.

Both mutations fail it: writing the item inside the scope, and leaving
the scope without restoring the culture.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: AI Assistant <ai@example.com>
pull/125/head
Raimund Andree 3 weeks ago
parent
commit
88ed362455
  1. 9
      Tests/ItemCmdlets.Tests.ps1

9
Tests/ItemCmdlets.Tests.ps1

@ -180,13 +180,20 @@ Describe 'Get-ChildItem2' {
[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::GetCultureInfo('tr-TR') [System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::GetCultureInfo('tr-TR')
'I'.ToLower() | Should -Be ([string][char]0x131) -Because 'the culture of the thread has to be Turkish for the test to mean anything' 'I'.ToLower() | Should -Be ([string][char]0x131) -Because 'the culture of the thread has to be Turkish for the test to mean anything'
$result = @(Get-ChildItem2 -Path $folder -Filter $Filter -ErrorAction Stop) # The next stage of the pipeline reads the culture of the thread for each item, and the test reads it once more
# after the cmdlet has returned: Turkish both times shows that the cmdlet restores the culture and writes no
# item inside the scope of the invariant culture.
$result = @(Get-ChildItem2 -Path $folder -Filter $Filter -ErrorAction Stop |
ForEach-Object -Process { [pscustomobject]@{ Name = $_.Name; Culture = [System.Threading.Thread]::CurrentThread.CurrentCulture.Name } })
$cultureAfter = [System.Threading.Thread]::CurrentThread.CurrentCulture.Name
} }
finally { finally {
[System.Threading.Thread]::CurrentThread.CurrentCulture = $original [System.Threading.Thread]::CurrentThread.CurrentCulture = $original
} }
($result.Name | Sort-Object) -join ',' | Should -BeExactly $Expected ($result.Name | Sort-Object) -join ',' | Should -BeExactly $Expected
(@($result.Culture) | Select-Object -Unique) -join ',' | Should -BeExactly 'tr-TR' -Because 'the cmdlet writes each item outside the scope of the invariant culture'
$cultureAfter | Should -BeExactly 'tr-TR' -Because 'the cmdlet restores the culture of the caller'
} }
# Only * and ? are wildcards in -Filter. A bracket stands for itself, so a file with brackets in its name is found # Only * and ? are wildcards in -Filter. A bracket stands for itself, so a file with brackets in its name is found

Loading…
Cancel
Save