1+ Describe " Get-Command Tests" - Tags " CI" {
2+ BeforeAll {
3+ function TestGetCommand-DynamicParametersDCR
4+ {
5+ [CmdletBinding ()]
6+ param (
7+ [Parameter (Mandatory = $true , Position = 0 )]
8+ [ValidateSet (" ReturnNull" , " ReturnThis" , " Return1" , " Return2" , " Return3" , " ReturnDuplicateParameter" , " ReturnAlias" , " ReturnDuplicateAlias" , " ReturnObjectNoParameters" , " ReturnGenericParameter" , " ThrowException" )]
9+ [string ] $TestToRun ,
10+
11+ [Parameter ()]
12+ [Type ]$ParameterType
13+ )
14+
15+ DynamicParam {
16+ if ( ! $testToRun ) {
17+ $testToRun = " returnnull"
18+ }
19+ $dynamicParamDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary ]::new()
20+ switch ( $testToRun )
21+ {
22+ " returnnull" {
23+ $dynamicParamDictionary = $null
24+ break
25+ }
26+ " return1" {
27+ $attr = [System.Management.Automation.ParameterAttribute ]::new()
28+ $attr.Mandatory = $true
29+ $dynamicParameter = [System.Management.Automation.RuntimeDefinedParameter ]::new(" OneString" , [string ], $attr )
30+ $dynamicParamDictionary.Add (" OneString" , $dynamicParameter )
31+ break
32+ }
33+ " return2" {
34+ $attr1 = [System.Management.Automation.ParameterAttribute ]::new()
35+ $attr1.Mandatory = $true
36+ $attr1.ParameterSetName = " __AllParameterSets"
37+ $ac1 = [Collections.ObjectModel.Collection [Attribute ]]::new()
38+ $ac1.Add ($attr1 )
39+ $p1 = [System.Management.Automation.RuntimeDefinedParameter ]::new(" OneString" , [string ], $ac1 )
40+ $dynamicParamDictionary.Add (" OneString" , $p1 )
41+
42+
43+ $attr2 = [System.Management.Automation.ParameterAttribute ]::new()
44+ $attr2.Mandatory = $false
45+ $attr2.ParameterSetName = " __AllParameterSets"
46+ $VRattr = [System.Management.Automation.ValidateRangeAttribute ]::new(5 , 9 )
47+
48+ $ac2 = [Collections.ObjectModel.Collection [Attribute ]]::new()
49+ $ac2.Add ($attr2 )
50+ $ac2.Add ($VRattr )
51+ $p2 = [System.Management.Automation.RuntimeDefinedParameter ]::New(" TwoInt" , [int ], $ac2 )
52+ $dynamicParamDictionary.Add (" TwoInt" , $p2 )
53+ break
54+ }
55+ " return3" {
56+ $attr1 = [System.Management.Automation.ParameterAttribute ]::new()
57+ $attr1.Mandatory = $true
58+ $attr1.ParameterSetName = " __AllParameterSets"
59+ $ac1 = [Collections.ObjectModel.Collection [Attribute ]]::new()
60+ $ac1.Add ($attr1 )
61+ $p1 = [System.Management.Automation.RuntimeDefinedParameter ]::new(" OneString" , [string ], $ac1 )
62+ $dynamicParamDictionary.Add (" OneString" , $p1 )
63+
64+
65+ $attr2 = [System.Management.Automation.ParameterAttribute ]::new()
66+ $attr2.Mandatory = $false
67+ $attr2.ParameterSetName = " __AllParameterSets"
68+ $VRattr = [System.Management.Automation.ValidateRangeAttribute ]::new(5 , 9 )
69+
70+ $ac2 = [Collections.ObjectModel.Collection [Attribute ]]::new()
71+ $ac2.Add ($attr2 )
72+ $ac2.Add ($VRattr )
73+ $p2 = [System.Management.Automation.RuntimeDefinedParameter ]::New(" TwoInt" , [int ], $ac2 )
74+ $dynamicParamDictionary.Add (" TwoInt" , $p2 )
75+
76+ $attr3 = [System.Management.Automation.ParameterAttribute ]::new()
77+ $attr3.Mandatory = $false
78+ $attr3.ParameterSetName = " __AllParameterSets"
79+ $ac3 = [Collections.ObjectModel.Collection [Attribute ]]::new()
80+ $ac3.Add ($attr3 )
81+ $p3 = [System.Management.Automation.RuntimeDefinedParameter ]::new(" ThreeBool" , [bool ], $ac3 )
82+ $dynamicParamDictionary.Add (" ThreeBool" , $p3 )
83+ break
84+ }
85+ " returnduplicateparameter" {
86+ $attr1 = [System.Management.Automation.ParameterAttribute ]::new()
87+ $attr1.Mandatory = $false
88+ $attr1.ParameterSetName = " __AllParameterSets"
89+ $ac1 = [Collections.ObjectModel.Collection [Attribute ]]::new()
90+ $ac1.Add ($attr1 )
91+ $p1 = [System.Management.Automation.RuntimeDefinedParameter ]::new(" TestToRun" , [int ], $ac1 )
92+ $dynamicParamDictionary.Add (" TestToRun" , $p1 )
93+ break
94+ }
95+ " returngenericparameter" {
96+ $attr1 = [System.Management.Automation.ParameterAttribute ]::new()
97+ $attr1.ParameterSetName = " __AllParameterSets"
98+ $ac1 = [Collections.ObjectModel.Collection [Attribute ]]::new()
99+ $ac1.Add ($attr1 )
100+ $p1 = [System.Management.Automation.RuntimeDefinedParameter ]::new(" TypedValue" , $ParameterType , $ac1 )
101+ $dynamicParamDictionary.Add (" TypedValue" , $p1 )
102+ break
103+ }
104+ default {
105+ throw ([invalidoperationexception ]::new(" unable to determine which dynamic parameters to return!" ))
106+ break
107+ }
108+ }
109+ return $dynamicParamDictionary
110+ }
111+
112+ BEGIN {
113+ $ReturnNull = " ReturnNull"
114+ $ReturnThis = " ReturnThis"
115+ $ReturnAlias = " ReturnAlias"
116+ $ReturnDuplicateAlias = " ReturnDuplicateAlias"
117+ $Return1 = " Return1"
118+ $Return2 = " Return2"
119+ $Return3 = " Return3"
120+ $ReturnDuplicateParameter = " ReturnDuplicateParameter"
121+ $ReturnObjectNoParameters = " ReturnObjectNoParameters"
122+ $ReturnGenericParameter = " ReturnGenericParameter"
123+ $ThrowException = " ThrowException"
124+ return $dynamicParamDictionary
125+ }
126+ }
127+
128+ function GetDynamicParameter ($cmdlet , $parameterName )
129+ {
130+ foreach ($paramSet in $cmdlet.ParameterSets )
131+ {
132+ foreach ($pinfo in $paramSet.Parameters )
133+ {
134+ if ($pinfo.Name -eq $parameterName )
135+ {
136+ $foundParam = $pinfo
137+ break
138+ }
139+ }
140+ if ($foundParam -ne $null )
141+ {
142+ break
143+ }
144+ }
145+ return $foundParam
146+ }
147+
148+ function VerifyDynamicParametersExist ($cmdlet , $parameterNames )
149+ {
150+ foreach ($paramName in $parameterNames )
151+ {
152+ $foundParam = GetDynamicParameter - cmdlet $cmdlet - parameterName $paramName
153+ $foundParam.Name | Should Be $paramName
154+ }
155+ }
156+
157+ function VerifyParameterType ($cmdlet , $parameterName , $ParameterType )
158+ {
159+ $foundParam = GetDynamicParameter - cmdlet $cmdlet - parameterName $parameterName
160+ $foundParam.ParameterType | Should Be $ParameterType
161+ }
162+ }
163+
164+ It " Verify that get-command get-content includes the dynamic parameters when the cmdlet is checked against the file system provider implementation" {
165+ $fullPath = Join-Path $TestDrive - ChildPath " blah"
166+ New-Item - Path $fullPath - ItemType directory - Force
167+ $results = get-command get-content - path $fullPath
168+ $dynamicParameter = " Wait" , " Encoding" , " Delimiter"
169+ VerifyDynamicParametersExist - cmdlet $results [0 ] - parameterNames $dynamicParameter
170+ }
171+
172+ It " Verify that get-command get-content doesn't have any dynamic parameters for Function provider" {
173+ $results = get-command get-content - path function:
174+ $dynamicParameter = " Wait" , " Encoding" , " Delimiter"
175+ foreach ($dynamicPara in $dynamicParameter )
176+ {
177+ $results [0 ].ParameterSets.Parameters.Name -contains $dynamicPara | Should be $false
178+ }
179+ }
180+
181+ It " Verify that the specified dynamic parameter exists in the CmdletInfo result returned" {
182+ $results = get-command testgetcommand- dynamicparametersdcr - testtorun return1
183+ $dynamicParameter = " OneString"
184+ VerifyDynamicParametersExist - cmdlet $results [0 ] - parameterNames $dynamicParameter
185+ VerifyParameterType - cmdlet $results [0 ] - parameterName $dynamicParameter - ParameterType string
186+ }
187+
188+ It " Verify three dynamic parameters are created properly" {
189+ $results = get-command testgetcommand- dynamicparametersdcr - testtorun return3
190+ $dynamicParameter = " OneString" , " TwoInt" , " ThreeBool"
191+
192+ VerifyDynamicParametersExist - cmdlet $results [0 ] - parameterNames $dynamicParameter
193+ VerifyParameterType - cmdlet $results [0 ] - parameterName " OneString" - parameterType string
194+ VerifyParameterType - cmdlet $results [0 ] - parameterName " TwoInt" - parameterType Int
195+ VerifyParameterType - cmdlet $results [0 ] - parameterName " ThreeBool" - parameterType bool
196+ }
197+
198+ It " Verify dynamic parameter type is process" {
199+ $results = get-command testgetcommand- dynamicparametersdcr - args ' -testtorun' , ' returngenericparameter' , ' -parametertype' , ' System.Diagnostics.Process'
200+ VerifyParameterType - cmdlet $results [0 ] - parameterName " TypedValue" - parameterType System.Diagnostics.Process
201+ }
202+
203+ It " Verify a single cmdlet returned using verb and noun parameter set syntax works properly" {
204+ $paramName = " OneString"
205+ $results = get-command - verb testgetcommand - noun dynamicparametersdcr - testtorun Return1
206+ VerifyDynamicParametersExist - cmdlet $results [0 ] - parameterNames $paramName
207+ VerifyParameterType - cmdlet $results [0 ] - parameterName $paramName - parameterType string
208+ }
209+
210+ It " Verify Single Cmdlet Using Verb&Noun ParameterSet" {
211+ $paramName = " Encoding"
212+ $results = get-command - verb get - noun content - Encoding Unicode
213+ VerifyDynamicParametersExist - cmdlet $results [0 ] - parameterNames $paramName
214+ VerifyParameterType - cmdlet $results [0 ] - parameterName $paramName - parameterType Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding
215+ }
216+
217+ It " Verify Single Cmdlet Using Verb&Noun ParameterSet With Usage" {
218+ $results = get-command - verb get - noun content - Encoding Unicode - syntax
219+ $results.ToString () | Should Match " -Encoding"
220+ $results.ToString () | Should Match " -Wait"
221+ $results.ToString () | Should Match " -Delimiter"
222+ }
223+
224+
225+ It " Test Script Lookup Positive Script Info" {
226+ $tempFile = " mytempfile.ps1"
227+ $fullPath = Join-Path $TestDrive - ChildPath $tempFile
228+ " $a = dir" > $fullPath
229+ $results = get-command $fullPath
230+
231+ $results.Name | Should Be $tempFile
232+ $results.Definition | Should Be $fullPath
233+ }
234+
235+ It " Two dynamic parameters are created properly" {
236+ $results = get-command testgetcommand- dynamicparametersdcr - testtorun return2
237+ $dynamicParameter = " OneString" , " TwoInt"
238+ VerifyDynamicParametersExist - cmdlet $results [0 ] - parameterNames $dynamicParameter
239+ VerifyParameterType - cmdlet $results [0 ] - parameterName " OneString" - ParameterType string
240+ VerifyParameterType - cmdlet $results [0 ] - parameterName " TwoInt" - ParameterType int
241+ }
242+
243+ It " Throw an Exception when set testtorun to 'returnduplicateparameter'" {
244+ try
245+ {
246+ Get-Command testgetcommand- dynamicparametersdcr - testtorun returnduplicateparameter - ErrorAction SilentlyContinue
247+ }
248+ catch
249+ {
250+ $_.FullyQualifiedErrorId | Should Be " GetCommandMetadataError,Microsoft.PowerShell.Commands.GetCommandCommand"
251+ }
252+ }
253+
254+ It " verify if get the proper dynamic parameter type skipped by issue #1430" - Pending {
255+ $results = Get-Command testgetcommand- dynamicparametersdcr - testtorun returngenericparameter - parametertype System.Diagnostics.Process
256+ VerifyParameterType - cmdlet $results [0 ] - parameterName " TypedValue" - parameterType System.Diagnostics.Process
257+ }
258+
259+ It " It works with Single Cmdlet Using Verb&Noun ParameterSet" {
260+ $paramName = " Encoding"
261+ $results = Get-Command - verb get - noun content - encoding UTF8
262+ VerifyDynamicParametersExist - cmdlet $results [0 ] - parameterNames $paramName
263+ VerifyParameterType - cmdlet $results [0 ] - parameterName $paramName - ParameterType Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding
264+ }
265+
266+ # unsupported parameter: -synop
267+ It " [Unsupported]It works with Single Cmdlet Using Verb&Noun ParameterSet With Synopsis" - Pending {
268+ $paramName = " Encoding"
269+ $results = get-command - verb get - noun content - encoding UTF8 - synop
270+ VerifyDynamicParametersExist - cmdlet $results [0 ] - parameterNames $paramName
271+ VerifyParameterType - cmdlet $results [0 ] - parameterName $paramName - ParameterType Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding
272+ }
273+ }
0 commit comments