MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Describe "Get-Command Feature tests" -Tag Feature {5 Context "-UseFuzzyMatch tests" {6 It "Should match cmdlets" {7 $cmds = Get-Command get-hlp -UseFuzzyMatch8 $cmds.Count | Should -BeGreaterThan 09 $cmds[0].Name | Should -BeExactly 'Get-Help' -Because "This should be closest match so shows up first"10 $cmds[0].Score | Should -Be 111 }12 13 It "Should match native commands" {14 $input = "pwsg"15 $expectedcmd = "pwsh"16 if ($IsWindows) {17 $expectedcmd = "pwsh.EXE"18 }19 20 $cmds = Get-Command $input -UseFuzzyMatch21 $cmds.Count | Should -BeGreaterThan 022 $cmds.Name | Should -Contain $expectedcmd23 }24 25 It "Should use minimum distance" {26 $cmds = Get-Command get-hlp -UseFuzzyMatch -FuzzyMinimumDistance 327 $cmds.Count | Should -BeGreaterThan 028 foreach ($cmd in $cmds) {29 $cmd.Score | Should -BeLessOrEqual 330 }31 }32 }33 34 Context "-UseAbbreviationExpansion tests" {35 BeforeAll {36 $testModulesPath = Join-Path $testdrive "Modules"37 $testPSModulePath = [System.IO.Path]::PathSeparator + $testModulesPath38 $null = New-Item -ItemType Directory -Path $testModulesPath39 $null = New-Item -ItemType Directory -Path (Join-Path $testModulesPath "test1")40 $null = New-Item -ItemType Directory -Path (Join-Path $testModulesPath "test2")41 42 Set-Content -Path (Join-Path $testModulesPath "test1/test1.psm1") -Value "function Import-FooZedZed {}"43 Set-Content -Path (Join-Path $testModulesPath "test2/test2.psm1") -Value "function Invoke-FooZedZed {}"44 }45 46 It "Can return multiple results relying on auto module loading" {47 $results = & "$PSHOME/pwsh" -outputformat xml -command "`$env:PSModulePath += '$testPSModulePath'; Get-Command i-fzz -UseAbbreviationExpansion"48 $results | Should -HaveCount 249 $results.Name | Should -Contain "Invoke-FooZedZed"50 $results.Name | Should -Contain "Import-FooZedZed"51 }52 53 It "Valid cmdlets works with name <name> and module <module>" -TestCases @(54 @{ Name = "i-psdf"; expected = "Import-PowerShellDataFile"; module = $null },55 @{ Name = "i-psdf"; expected = "Import-PowerShellDataFile"; module = "Microsoft.PowerShell.Utility" },56 @{ Name = "r-psb" ; expected = "Remove-PSBreakpoint" ; module = $null },57 @{ Name = "r-psb" ; expected = "Remove-PSBreakpoint" ; module = "Microsoft.PowerShell.Utility" }58 ) {59 param($name, $expected, $module)60 61 $params = @{62 UseAbbreviationExpansion = $true;63 Name = $name;64 }65 66 if ($module) {67 $params += @{ Module = $module }68 }69 70 $results = Get-Command @params71 $results | Should -HaveCount 172 $results.Name | Should -BeExactly $expected73 }74 75 It "Can return multiple results for cmdlets matching abbreviation" {76 # use mixed casing to validate case insensitivity77 $results = Get-Command i-C -UseAbbreviationExpansion78 $results.Name | Should -Contain "Invoke-Command"79 $results.Name | Should -Contain "Import-Clixml"80 $results.Name | Should -Contain "Import-Csv"81 }82 83 It "Will return multiple results for functions matching abbreviation" {84 $manifestPath = Join-Path $testdrive "test.psd1"85 $modulePath = Join-Path $testdrive "test.psm1"86 87 New-ModuleManifest -Path $manifestPath -FunctionsToExport "Get-FoodBar","Get-FB" -RootModule test.psm188 @"89 function Get-FoodBar { "foodbar" }90 function Get-FB { "fb" }91"@ > $modulePath92 93 try {94 Import-Module $manifestPath95 $results = Get-Command g-fb -UseAbbreviationExpansion96 $results | Should -HaveCount 297 $results[0].Name | Should -BeIn "Get-FB","Get-FoodBar"98 $results[1].Name | Should -BeIn "Get-FB","Get-FoodBar"99 $results[0].Name | Should -Not -Be $results[1].Name100 }101 finally {102 Remove-Module test103 }104 }105 106 It "Non-existing cmdlets returns non-terminating error" {107 { Get-Command g-adf -ErrorAction Stop } | Should -Throw -ErrorId "CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand"108 }109 110 It "No results if wildcard is used" {111 Get-Command i-psd* -UseAbbreviationExpansion | Should -BeNullOrEmpty112 }113 }114}115 116Describe "Get-Command" -Tag CI {117 BeforeAll {118 Import-Module Microsoft.PowerShell.Management119 }120 Context "-Syntax tests" {121 It "Should return a string object when -Name is an alias and -Syntax is specified" {122 $Result = Get-Command -Name del -Syntax123 124 $Result | Should -BeOfType [String]125 $Result | Should -Match 'del \[-Path\]'126 }127 128 It "Should replace commands with aliases in matching commands when using a wildcard search" {129 $Result = Get-Command -Name sp* -Syntax130 131 $Result | Should -BeOfType [String]132 $Result -join '' | Should -Match 'sp \(alias\) -> Set-ItemProperty'133 $Result -join '' | Should -Match 'sp \[-Path\]'134 }135 136 It "Should not add the alias (alias) -> command decorator for non-alias commands" {137 $Result = Get-Command -Name sp* -Syntax138 139 $Result -join '' | Should -Not -Match 'Split-Path \(alias\) -> Split-Path'140 $Result -join '' | Should -Match 'Split-Path \[-Path\]'141 }142 143 It "Should only replace aliases when given multiple entries including a command and an alias" {144 $Result = Get-Command -Name get-help, del -Syntax145 146 $Result -join '' | Should -Match 'del \(alias\) -> Remove-Item'147 $Result -join '' | Should -Match 'del \[-Path\]'148 $Result -join '' | Should -Match 'Get-Help \[\[-Name\]'149 $Result -join '' | Should -Not -Match 'del \(alias\) -> Get-Help'150 }151 152 It "Should return the path to an aliased script when -Syntax is specified" {153 # First, create a script file154 $TestGcmSyntax = @'155 [CmdletBinding()]156 param(157 [Parameter(Position=0, Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]158 [ValidateNotNullOrEmpty()]159 [string[]]160 $Name161 )162 process {163 "Processing ${Name}"164 }165'@166 Set-Content -Path TestDrive:\Test-GcmSyntax.ps1 -Value $TestGcmSyntax167 168 # Now set up an alias for that file169 New-Alias -Name tgs -Value TestDrive:\Test-GcmSyntax.ps1170 171 $Result = Get-Command -Name tgs -Syntax172 173 $Result | Should -Match "tgs \(alias\) -> $([Regex]::Escape((Get-Item TestDrive:\\Test-GcmSyntax.ps1).FullName))"174 }175 }176 177 Context "-Name tests" {178 It "Should return a AliasInfo object when -Name is an alias" {179 $Result = Get-Command -Name del180 181 $Result | Should -BeOfType [System.Management.Automation.AliasInfo]182 $Result.DisplayName | Should -Be 'del -> Remove-Item'183 }184 185 It "Should return a CommandInfo object when -Name is a command" {186 $Result = Get-Command -Name Remove-Item187 188 $Result | Should -BeOfType [System.Management.Automation.CommandInfo]189 }190 191 It "Throws '<expected>' exception if '<Name>' name is used" -TestCases @(192 @{ Name = 'space'; Value = ' '; expected = "CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand" }193 @{ Name = 'empty'; Value = ''; expected = "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetCommandCommand" }194 @{ Name = 'null'; Value = $null; expected = "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetCommandCommand" }195 ) {196 param($value, $expected)197 { Get-Command $value -ErrorAction Stop } | Should -Throw -ErrorId $expected198 }199 }200}201 