MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Describe "Get-Alias DRT Unit Tests" -Tags "CI" {5 It "Get-Alias Bogus Scope Name should throw PSArgumentException"{6 { Get-Alias -Name "ABCD" -Scope "bogus" } | Should -Throw -ErrorId "Argument,Microsoft.PowerShell.Commands.GetAliasCommand"7 }8 It "Get-Alias OutOfRange Scope"{9 { Get-Alias -Name "ABCD" -Scope "99999" } | Should -Throw -ErrorId "ArgumentOutOfRange,Microsoft.PowerShell.Commands.GetAliasCommand"10 }11 It "Get-Alias Named Single Valid"{12 Set-Alias -Name ABCD -Value "foo"13 $result=Get-Alias -Name ABCD14 $result.Name | Should -BeExactly "ABCD"15 $result.Definition | Should -BeExactly "foo"16 $result.Description | Should -BeNullOrEmpty17 $result.Options | Should -BeExactly "None"18 }19 It "Get-Alias Positional Single Valid"{20 Set-Alias -Name ABCD -Value "foo"21 $result=Get-Alias ABCD22 $result.Name | Should -BeExactly "ABCD"23 $result.Definition | Should -BeExactly "foo"24 $result.Description | Should -BeNullOrEmpty25 $result.Options | Should -BeExactly "None"26 }27 It "Get-Alias Named Multiple Valid"{28 Set-Alias -Name ABCD -Value "foo"29 Set-Alias -Name AEFG -Value "bar"30 $result=Get-Alias -Name ABCD,AEFG31 $result[0].Name | Should -BeExactly "ABCD"32 $result[0].Definition | Should -BeExactly "foo"33 $result[0].Description | Should -BeNullOrEmpty34 $result[0].Options | Should -BeExactly "None"35 $result[1].Name | Should -BeExactly "AEFG"36 $result[1].Definition | Should -BeExactly "bar"37 $result[1].Description | Should -BeNullOrEmpty38 $result[1].Options | Should -BeExactly "None"39 }40 It "Get-Alias Named Wildcard Valid"{41 Set-Alias -Name ABCD -Value "foo"42 Set-Alias -Name ABCG -Value "bar"43 $result=Get-Alias -Name ABC*44 $result[0].Name | Should -BeExactly "ABCD"45 $result[0].Definition | Should -BeExactly "foo"46 $result[0].Description | Should -BeNullOrEmpty47 $result[0].Options | Should -BeExactly "None"48 $result[1].Name | Should -BeExactly "ABCG"49 $result[1].Definition | Should -BeExactly "bar"50 $result[1].Description | Should -BeNullOrEmpty51 $result[1].Options | Should -BeExactly "None"52 }53 It "Get-Alias Positional Wildcard Valid"{54 Set-Alias -Name ABCD -Value "foo"55 Set-Alias -Name ABCG -Value "bar"56 $result=Get-Alias ABC*57 $result[0].Name | Should -BeExactly "ABCD"58 $result[0].Definition | Should -BeExactly "foo"59 $result[0].Description | Should -BeNullOrEmpty60 $result[0].Options | Should -BeExactly "None"61 $result[1].Name | Should -BeExactly "ABCG"62 $result[1].Definition | Should -BeExactly "bar"63 $result[1].Description | Should -BeNullOrEmpty64 $result[1].Options | Should -BeExactly "None"65 }66 It "Get-Alias Named Wildcard And Exclude Valid"{67 Set-Alias -Name ABCD -Value "foo"68 Set-Alias -Name ABCG -Value "bar"69 $result=Get-Alias -Name ABC* -Exclude "*BCG"70 $result[0].Name | Should -BeExactly "ABCD"71 $result[0].Definition | Should -BeExactly "foo"72 $result[0].Description | Should -BeNullOrEmpty73 $result[0].Options | Should -BeExactly "None"74 }75 It "Get-Alias Scope Valid"{76 Set-Alias -Name ABCD -Value "foo"77 $result=Get-Alias -Name ABCD78 $result.Name | Should -BeExactly "ABCD"79 $result.Definition | Should -BeExactly "foo"80 $result.Description | Should -BeNullOrEmpty81 $result.Options | Should -BeExactly "None"82 83 Set-Alias -Name ABCD -Value "localfoo" -Scope local84 $result=Get-Alias -Name ABCD -Scope local85 $result.Name | Should -BeExactly "ABCD"86 $result.Definition | Should -BeExactly "localfoo"87 $result.Description | Should -BeNullOrEmpty88 $result.Options | Should -BeExactly "None"89 90 Set-Alias -Name ABCD -Value "globalfoo" -Scope global91 Set-Alias -Name ABCD -Value "scriptfoo" -Scope "script"92 Set-Alias -Name ABCD -Value "foo0" -Scope "0"93 Set-Alias -Name ABCD -Value "foo1" -Scope "1"94 95 $result=Get-Alias -Name ABCD96 $result.Name | Should -BeExactly "ABCD"97 $result.Definition | Should -BeExactly "foo0"98 $result.Description | Should -BeNullOrEmpty99 $result.Options | Should -BeExactly "None"100 101 $result=Get-Alias -Name ABCD -Scope local102 $result.Name | Should -BeExactly "ABCD"103 $result.Definition | Should -BeExactly "foo0"104 $result.Description | Should -BeNullOrEmpty105 $result.Options | Should -BeExactly "None"106 107 $result=Get-Alias -Name ABCD -Scope global108 $result.Name | Should -BeExactly "ABCD"109 $result.Definition | Should -BeExactly "globalfoo"110 $result.Description | Should -BeNullOrEmpty111 $result.Options | Should -BeExactly "None"112 113 $result=Get-Alias -Name ABCD -Scope "script"114 $result.Name | Should -BeExactly "ABCD"115 $result.Definition | Should -BeExactly "scriptfoo"116 $result.Description | Should -BeNullOrEmpty117 $result.Options | Should -BeExactly "None"118 119 $result=Get-Alias -Name ABCD -Scope "0"120 $result.Name | Should -BeExactly "ABCD"121 $result.Definition | Should -BeExactly "foo0"122 $result.Description | Should -BeNullOrEmpty123 $result.Options | Should -BeExactly "None"124 125 $result=Get-Alias -Name ABCD -Scope "1"126 $result.Name | Should -BeExactly "ABCD"127 $result.Definition | Should -BeExactly "foo1"128 $result.Description | Should -BeNullOrEmpty129 $result.Options | Should -BeExactly "None"130 }131 It "Get-Alias Expose Bug 1065828, BugId:905235"{132 { Get-Alias -Name "ABCD" -Scope "100" } | Should -Throw -ErrorId "ArgumentOutOfRange,Microsoft.PowerShell.Commands.GetAliasCommand"133 }134 It "Get-Alias Zero Scope Valid"{135 Set-Alias -Name ABCD -Value "foo"136 $result=Get-Alias -Name ABCD137 $result.Name | Should -BeExactly "ABCD"138 $result.Definition | Should -BeExactly "foo"139 $result.Description | Should -BeNullOrEmpty140 $result.Options | Should -BeExactly "None"141 142 $result=Get-Alias -Name ABCD -Scope "0"143 $result.Name | Should -BeExactly "ABCD"144 $result.Definition | Should -BeExactly "foo"145 $result.Description | Should -BeNullOrEmpty146 $result.Options | Should -BeExactly "None"147 }148 149 It "Test get-alias with Definition parameter" {150 $returnObject = Get-Alias -Definition Get-Command151 For($i = 0; $i -lt $returnObject.Length;$i++)152 {153 $returnObject[$i] | Should -Not -BeNullOrEmpty154 $returnObject[$i].CommandType | Should -Be 'Alias'155 $returnObject[$i].Definition | Should -Be 'Get-Command'156 }157 }158 159 It "Get-Alias DisplayName should always show AliasName -> ResolvedCommand for all aliases" {160 Set-Alias -Name Test-MyAlias -Value Get-Command -Force161 Set-Alias -Name tma -Value Test-MyAlias -force162 $aliases = Get-Alias Test-MyAlias, tma163 $aliases | ForEach-Object {164 $_.DisplayName | Should -Be "$($_.Name) -> Get-Command"165 }166 $aliases.Name.foreach{Remove-Item Alias:$_ -ErrorAction SilentlyContinue}167 }168}169 170Describe "Get-Alias" -Tags "CI" {171 It "Should have a return type of System.Array when gal returns more than one object" {172 $val1=(Get-Alias a*)173 $val2=(Get-Alias c*)174 $i=0175 176 $val1 | Should -Not -BeNullOrEmpty177 $val2 | Should -Not -BeNullOrEmpty178 179 $val1 | ForEach-Object{ $i++};180 if($i -lt 2) {181 $val1 | Should -BeOfType System.Management.Automation.CommandInfo182 }183 else184 {185 ,$val1 | Should -BeOfType System.Array186 }187 188 $val2 | ForEach-Object{ $i++};189 if($i -lt 2) {190 $val2 | Should -BeOfType System.Management.Automation.CommandInfo191 }192 else193 {194 ,$val2 | Should -BeOfType System.Array195 }196 }197 198 It "should return an array of objects" {199 $val = Get-Alias a*200 $alias = gal a*201 202 $val.Count | Should -Be $alias.Count203 for ($i=0; $i -lt $val.Count;$i++)204 {205 $val[$i].CommandType | Should -Be $alias[$i].CommandType206 $val[$i].Name | Should -Be $alias[$i].Name207 $val[$i].ModuleName | Should -Be $alias[$i].ModuleName208 }209 }210}211 212Describe "Get-Alias null tests" -Tags "CI" {213 214 $testCases =215 @{ data = $null; value = 'null' },216 @{ data = [String]::Empty; value = 'empty string' }217 218 Context 'Check null or empty value to the -Name parameter' {219 It 'Should throw if <value> is passed to -Name parameter' -TestCases $testCases {220 param($data)221 { Get-Alias -Name $data } | Should -Throw -ErrorId 'ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetAliasCommand'222 }223 }224 Context 'Check null or empty value to the -Name parameter via pipeline' {225 It 'Should throw if <value> is passed through pipeline to -Name parameter' -TestCases $testCases {226 param($data)227 { $data | Get-Alias -ErrorAction Stop } | Should -Throw -ErrorId 'ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetAliasCommand'228 }229 }230}231 