|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +Describe 'CommandInfoCache' { |
| 5 | + BeforeEach { |
| 6 | + $cacheType = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper].Assembly.GetType( |
| 7 | + 'Microsoft.Windows.PowerShell.ScriptAnalyzer.CommandInfoCache', $true) |
| 8 | + $cache = [Activator]::CreateInstance($cacheType) |
| 9 | + } |
| 10 | + |
| 11 | + AfterEach { |
| 12 | + $cache.Dispose() |
| 13 | + } |
| 14 | + |
| 15 | + It 'Returns the cached command for differently cased names and equivalent command types' { |
| 16 | + $command = $cache.GetCommandInfo('Get-Item', $null, $false) |
| 17 | + $command | Should -Not -BeNullOrEmpty |
| 18 | + $cached = $cache.GetCommandInfo('gET-iTEM', [System.Management.Automation.CommandTypes]::All, $false) |
| 19 | + [object]::ReferenceEquals($command, $cached) | Should -BeTrue |
| 20 | + } |
| 21 | + |
| 22 | + It 'Keeps command-type filters separate from an existing cache entry' { |
| 23 | + $cache.GetCommandInfo('Get-Item', [System.Management.Automation.CommandTypes]::Function, $false) | |
| 24 | + Should -BeNullOrEmpty |
| 25 | + $command = $cache.GetCommandInfo('Get-Item', [System.Management.Automation.CommandTypes]::Cmdlet, $false) |
| 26 | + $command.CommandType | Should -Be ([System.Management.Automation.CommandTypes]::Cmdlet) |
| 27 | + $cache.GetCommandInfo('GET-ITEM', [System.Management.Automation.CommandTypes]::Function, $false) | |
| 28 | + Should -BeNullOrEmpty |
| 29 | + } |
| 30 | + |
| 31 | + It 'Uses ordinal case-insensitive lookup independently of the current culture' { |
| 32 | + $originalCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture |
| 33 | + try { |
| 34 | + $command = $cache.GetCommandInfo('Get-Item', $null, $false) |
| 35 | + [System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo]::GetCultureInfo('tr-TR') |
| 36 | + $cached = $cache.GetCommandInfo('get-item', $null, $false) |
| 37 | + [object]::ReferenceEquals($command, $cached) | Should -BeTrue |
| 38 | + } finally { |
| 39 | + [System.Threading.Thread]::CurrentThread.CurrentCulture = $originalCulture |
| 40 | + } |
| 41 | + } |
| 42 | +} |
0 commit comments