Skip to content

Commit 3f5bf19

Browse files
committed
Add tests for command info cache keys to verify case-insensitivity and command-type filtering
Adding additional test cases from #2221.
1 parent 7a26d8d commit 3f5bf19

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

‎Tests/Engine/CommandInfoCacheConcurrency.tests.ps1‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,45 @@ public static class ConcurrentCommandLookup
114114
'Test-NonexistentCommandForRetryVerification', $null, $true) | Should -BeNullOrEmpty
115115
}
116116
}
117+
118+
Describe "Command info cache keys" {
119+
BeforeEach {
120+
$cacheType = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper].Assembly.GetType(
121+
'Microsoft.Windows.PowerShell.ScriptAnalyzer.CommandInfoCache', $true)
122+
$cache = [Activator]::CreateInstance($cacheType)
123+
}
124+
125+
AfterEach {
126+
$cache.Dispose()
127+
}
128+
129+
It "returns the cached command for differently cased names and equivalent command types" {
130+
$command = $cache.GetCommandInfo('Get-Item', $null, $false)
131+
$command | Should -Not -BeNullOrEmpty
132+
$cached = $cache.GetCommandInfo('gET-iTEM', [System.Management.Automation.CommandTypes]::All, $false)
133+
134+
[object]::ReferenceEquals($command, $cached) | Should -BeTrue
135+
}
136+
137+
It "keeps command-type filters separate from an existing cache entry" {
138+
$cache.GetCommandInfo('Get-Item', [System.Management.Automation.CommandTypes]::Function, $false) |
139+
Should -BeNullOrEmpty
140+
$command = $cache.GetCommandInfo('Get-Item', [System.Management.Automation.CommandTypes]::Cmdlet, $false)
141+
$command.CommandType | Should -Be ([System.Management.Automation.CommandTypes]::Cmdlet)
142+
$cache.GetCommandInfo('GET-ITEM', [System.Management.Automation.CommandTypes]::Function, $false) |
143+
Should -BeNullOrEmpty
144+
}
145+
146+
It "uses ordinal case-insensitive lookup independently of the current culture" {
147+
$originalCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture
148+
try {
149+
$command = $cache.GetCommandInfo('Get-Item', $null, $false)
150+
[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::GetCultureInfo('tr-TR')
151+
$cached = $cache.GetCommandInfo('get-item', $null, $false)
152+
153+
[object]::ReferenceEquals($command, $cached) | Should -BeTrue
154+
} finally {
155+
[System.Threading.Thread]::CurrentThread.CurrentCulture = $originalCulture
156+
}
157+
}
158+
}

0 commit comments

Comments
 (0)