2020-03-24 11:08:37 -07:00
# Copyright (c) Microsoft Corporation.
2018-02-13 09:23:53 -08:00
# Licensed under the MIT License.
Describe "Get-Command Tests" -Tags "CI" {
2017-01-16 13:31:14 -08:00
BeforeAll {
function TestGetCommand-DynamicParametersDCR
2016-08-08 20:19:11 -07:00
{
[ CmdletBinding ()]
param (
[ Parameter ( Mandatory = $true , Position = 0 )]
[ ValidateSet ( "ReturnNull" , "ReturnThis" , "Return1" , "Return2" , "Return3" , "ReturnDuplicateParameter" , "ReturnAlias" , "ReturnDuplicateAlias" , "ReturnObjectNoParameters" , "ReturnGenericParameter" , "ThrowException" )]
[ string ] $TestToRun ,
[ Parameter ()]
[ Type ] $ParameterType
)
2017-01-16 13:31:14 -08:00
2016-08-08 20:19:11 -07:00
DynamicParam {
2017-10-23 19:46:27 -07:00
if ( ! $TestToRun ) {
$TestToRun = "returnnull"
2016-08-08 20:19:11 -07:00
}
$dynamicParamDictionary = [ System.Management.Automation.RuntimeDefinedParameterDictionary ]:: new ()
2017-10-23 19:46:27 -07:00
switch ( $TestToRun )
2016-08-08 20:19:11 -07:00
{
2017-01-16 13:31:14 -08:00
"returnnull" {
2016-08-08 20:19:11 -07:00
$dynamicParamDictionary = $null
break
}
"return1" {
$attr = [ System.Management.Automation.ParameterAttribute ]:: new ()
$attr . Mandatory = $true
$dynamicParameter = [ System.Management.Automation.RuntimeDefinedParameter ]:: new ( "OneString" ,[ string ], $attr )
$dynamicParamDictionary . Add ( "OneString" , $dynamicParameter )
2017-01-16 13:31:14 -08:00
break
2016-08-08 20:19:11 -07:00
}
"return2" {
$attr1 = [ System.Management.Automation.ParameterAttribute ]:: new ()
$attr1 . Mandatory = $true
$attr1 . ParameterSetName = "__AllParameterSets"
$ac1 = [ Collections.ObjectModel.Collection[Attribute] ]:: new ()
$ac1 . Add ( $attr1 )
$p1 = [ System.Management.Automation.RuntimeDefinedParameter ]:: new ( "OneString" ,[ string ], $ac1 )
$dynamicParamDictionary . Add ( "OneString" , $p1 )
$attr2 = [ System.Management.Automation.ParameterAttribute ]:: new ()
$attr2 . Mandatory = $false
$attr2 . ParameterSetName = "__AllParameterSets"
$VRattr = [ System.Management.Automation.ValidateRangeAttribute ]:: new ( 5 , 9 )
$ac2 = [ Collections.ObjectModel.Collection[Attribute] ]:: new ()
$ac2 . Add ( $attr2 )
$ac2 . Add ( $VRattr )
$p2 = [ System.Management.Automation.RuntimeDefinedParameter ]:: New ( "TwoInt" ,[ int ], $ac2 )
$dynamicParamDictionary . Add ( "TwoInt" , $p2 )
break
}
"return3" {
$attr1 = [ System.Management.Automation.ParameterAttribute ]:: new ()
$attr1 . Mandatory = $true
$attr1 . ParameterSetName = "__AllParameterSets"
$ac1 = [ Collections.ObjectModel.Collection[Attribute] ]:: new ()
$ac1 . Add ( $attr1 )
$p1 = [ System.Management.Automation.RuntimeDefinedParameter ]:: new ( "OneString" ,[ string ], $ac1 )
$dynamicParamDictionary . Add ( "OneString" , $p1 )
$attr2 = [ System.Management.Automation.ParameterAttribute ]:: new ()
$attr2 . Mandatory = $false
$attr2 . ParameterSetName = "__AllParameterSets"
$VRattr = [ System.Management.Automation.ValidateRangeAttribute ]:: new ( 5 , 9 )
$ac2 = [ Collections.ObjectModel.Collection[Attribute] ]:: new ()
$ac2 . Add ( $attr2 )
$ac2 . Add ( $VRattr )
$p2 = [ System.Management.Automation.RuntimeDefinedParameter ]:: New ( "TwoInt" ,[ int ], $ac2 )
$dynamicParamDictionary . Add ( "TwoInt" , $p2 )
$attr3 = [ System.Management.Automation.ParameterAttribute ]:: new ()
$attr3 . Mandatory = $false
$attr3 . ParameterSetName = "__AllParameterSets"
$ac3 = [ Collections.ObjectModel.Collection[Attribute] ]:: new ()
$ac3 . Add ( $attr3 )
$p3 = [ System.Management.Automation.RuntimeDefinedParameter ]:: new ( "ThreeBool" ,[ bool ], $ac3 )
$dynamicParamDictionary . Add ( "ThreeBool" , $p3 )
break
}
"returnduplicateparameter" {
$attr1 = [ System.Management.Automation.ParameterAttribute ]:: new ()
$attr1 . Mandatory = $false
$attr1 . ParameterSetName = "__AllParameterSets"
$ac1 = [ Collections.ObjectModel.Collection[Attribute] ]:: new ()
$ac1 . Add ( $attr1 )
$p1 = [ System.Management.Automation.RuntimeDefinedParameter ]:: new ( "TestToRun" ,[ int ], $ac1 )
$dynamicParamDictionary . Add ( "TestToRun" , $p1 )
break
}
"returngenericparameter" {
$attr1 = [ System.Management.Automation.ParameterAttribute ]:: new ()
$attr1 . ParameterSetName = "__AllParameterSets"
$ac1 = [ Collections.ObjectModel.Collection[Attribute] ]:: new ()
$ac1 . Add ( $attr1 )
2017-01-16 13:31:14 -08:00
$p1 = [ System.Management.Automation.RuntimeDefinedParameter ]:: new ( "TypedValue" , $ParameterType , $ac1 )
$dynamicParamDictionary . Add ( "TypedValue" , $p1 )
break
2016-08-08 20:19:11 -07:00
}
2017-01-16 13:31:14 -08:00
default {
2016-08-08 20:19:11 -07:00
throw ([ invalidoperationexception ]:: new ( "unable to determine which dynamic parameters to return!" ))
break
}
}
return $dynamicParamDictionary
}
BEGIN {
$ReturnNull = "ReturnNull"
$ReturnThis = "ReturnThis"
$ReturnAlias = "ReturnAlias"
$ReturnDuplicateAlias = "ReturnDuplicateAlias"
$Return1 = "Return1"
$Return2 = "Return2"
$Return3 = "Return3"
$ReturnDuplicateParameter = "ReturnDuplicateParameter"
$ReturnObjectNoParameters = "ReturnObjectNoParameters"
$ReturnGenericParameter = "ReturnGenericParameter"
$ThrowException = "ThrowException"
return $dynamicParamDictionary
}
2017-01-16 13:31:14 -08:00
}
2016-08-08 20:19:11 -07:00
function GetDynamicParameter($cmdlet , $parameterName )
{
foreach ( $paramSet in $cmdlet . ParameterSets )
{
foreach ( $pinfo in $paramSet . Parameters )
{
if ( $pinfo . Name -eq $parameterName )
{
$foundParam = $pinfo
break
}
}
2017-07-22 05:03:49 +01:00
if ( $null -ne $foundParam )
2016-08-08 20:19:11 -07:00
{
break
}
}
return $foundParam
}
function VerifyDynamicParametersExist($cmdlet , $parameterNames )
{
foreach ( $paramName in $parameterNames )
{
$foundParam = GetDynamicParameter -cmdlet $cmdlet -parameterName $paramName
2018-03-23 10:36:11 -07:00
$foundParam . Name | Should -BeExactly $paramName
2017-01-16 13:31:14 -08:00
}
2016-08-08 20:19:11 -07:00
}
function VerifyParameterType($cmdlet , $parameterName , $ParameterType )
{
$foundParam = GetDynamicParameter -cmdlet $cmdlet -parameterName $parameterName
2018-03-23 10:36:11 -07:00
$foundParam . ParameterType | Should -Be $ParameterType
2017-01-16 13:31:14 -08:00
}
2016-08-08 20:19:11 -07:00
}
2017-10-23 19:46:27 -07:00
It "Verify that Get-Command Get-Content includes the dynamic parameters when the cmdlet is checked against the file system provider implementation" {
2016-08-08 20:19:11 -07:00
$fullPath = Join-Path $TestDrive -ChildPath "blah"
New-Item -Path $fullPath -ItemType directory -Force
2017-10-23 19:46:27 -07:00
$results = Get-Command Get-Content -Path $fullPath
2016-08-08 20:19:11 -07:00
$dynamicParameter = "Wait" , "Encoding" , "Delimiter"
2017-01-16 13:31:14 -08:00
VerifyDynamicParametersExist -cmdlet $results [ 0 ] -parameterNames $dynamicParameter
2016-08-08 20:19:11 -07:00
}
2017-10-23 19:46:27 -07:00
It "Verify that Get-Command Get-Content doesn't have any dynamic parameters for Function provider" {
$results = Get-Command Get-Content -Path function :
2016-08-08 20:19:11 -07:00
$dynamicParameter = "Wait" , "Encoding" , "Delimiter"
foreach ( $dynamicPara in $dynamicParameter )
{
2018-03-23 10:36:11 -07:00
$results [ 0 ]. ParameterSets . Parameters . Name -contains $dynamicPara | Should -BeFalse
2016-08-08 20:19:11 -07:00
}
}
2017-01-16 13:31:14 -08:00
2016-08-08 20:19:11 -07:00
It "Verify that the specified dynamic parameter exists in the CmdletInfo result returned" {
2017-10-23 19:46:27 -07:00
$results = Get-Command TestGetCommand-DynamicParametersDCR -TestToRun return1
2016-08-08 20:19:11 -07:00
$dynamicParameter = "OneString"
VerifyDynamicParametersExist -cmdlet $results [ 0 ] -parameterNames $dynamicParameter
VerifyParameterType -cmdlet $results [ 0 ] -parameterName $dynamicParameter -ParameterType string
2017-01-16 13:31:14 -08:00
}
2016-08-08 20:19:11 -07:00
It "Verify three dynamic parameters are created properly" {
2017-10-23 19:46:27 -07:00
$results = Get-Command TestGetCommand-DynamicParametersDCR -TestToRun return3
2016-08-08 20:19:11 -07:00
$dynamicParameter = "OneString" , "TwoInt" , "ThreeBool"
2017-01-16 13:31:14 -08:00
2016-08-08 20:19:11 -07:00
VerifyDynamicParametersExist -cmdlet $results [ 0 ] -parameterNames $dynamicParameter
VerifyParameterType -cmdlet $results [ 0 ] -parameterName "OneString" -parameterType string
VerifyParameterType -cmdlet $results [ 0 ] -parameterName "TwoInt" -parameterType Int
2017-01-16 13:31:14 -08:00
VerifyParameterType -cmdlet $results [ 0 ] -parameterName "ThreeBool" -parameterType bool
}
2016-08-08 20:19:11 -07:00
It "Verify dynamic parameter type is process" {
2017-10-23 19:46:27 -07:00
$results = Get-Command TestGetCommand-DynamicParametersDCR -Args '-TestToRun' , 'returngenericparameter' , '-parametertype' , 'System.Diagnostics.Process'
2016-08-08 20:19:11 -07:00
VerifyParameterType -cmdlet $results [ 0 ] -parameterName "TypedValue" -parameterType System . Diagnostics . Process
}
2017-01-16 13:31:14 -08:00
2016-08-08 20:19:11 -07:00
It "Verify a single cmdlet returned using verb and noun parameter set syntax works properly" {
$paramName = "OneString"
2017-10-23 19:46:27 -07:00
$results = Get-Command -Verb TestGetCommand -Noun DynamicParametersDCR -TestToRun Return1
2016-08-08 20:19:11 -07:00
VerifyDynamicParametersExist -cmdlet $results [ 0 ] -parameterNames $paramName
VerifyParameterType -cmdlet $results [ 0 ] -parameterName $paramName -parameterType string
}
2017-01-16 13:31:14 -08:00
2016-08-08 20:19:11 -07:00
It "Verify Single Cmdlet Using Verb&Noun ParameterSet" {
$paramName = "Encoding"
2017-10-23 19:46:27 -07:00
$results = Get-Command -Verb get -Noun content -Encoding Unicode
2016-08-08 20:19:11 -07:00
VerifyDynamicParametersExist -cmdlet $results [ 0 ] -parameterNames $paramName
2017-10-23 19:46:27 -07:00
VerifyParameterType -cmdlet $results [ 0 ] -parameterName $paramName -parameterType System . Text . Encoding
2017-01-16 13:31:14 -08:00
}
2016-08-08 20:19:11 -07:00
It "Verify Single Cmdlet Using Verb&Noun ParameterSet With Usage" {
2017-10-23 19:46:27 -07:00
$results = Get-Command -Verb get -Noun content -Encoding Unicode -Syntax
2018-03-23 10:36:11 -07:00
$results . ToString () | Should -Match "-Encoding"
$results . ToString () | Should -Match "-Wait"
$results . ToString () | Should -Match "-Delimiter"
2016-08-08 20:19:11 -07:00
}
2017-01-16 13:31:14 -08:00
2016-08-08 20:19:11 -07:00
It "Test Script Lookup Positive Script Info" {
$tempFile = "mytempfile.ps1"
$fullPath = Join-Path $TestDrive -ChildPath $tempFile
" $a = dir" > $fullPath
2017-10-23 19:46:27 -07:00
$results = Get-Command $fullPath
2017-01-16 13:31:14 -08:00
2018-03-23 10:36:11 -07:00
$results . Name | Should -BeExactly $tempFile
$results . Definition | Should -BeExactly $fullPath
2016-08-08 20:19:11 -07:00
}
It "Two dynamic parameters are created properly" {
2017-10-23 19:46:27 -07:00
$results = Get-Command TestGetCommand-DynamicParametersDCR -TestToRun return2
2016-08-08 20:19:11 -07:00
$dynamicParameter = "OneString" , "TwoInt"
VerifyDynamicParametersExist -cmdlet $results [ 0 ] -parameterNames $dynamicParameter
VerifyParameterType -cmdlet $results [ 0 ] -parameterName "OneString" -ParameterType string
VerifyParameterType -cmdlet $results [ 0 ] -parameterName "TwoInt" -ParameterType int
}
2017-10-23 19:46:27 -07:00
It "Throw an Exception when set TestToRun to 'returnduplicateparameter'" {
2018-03-23 10:36:11 -07:00
{ Get-Command TestGetCommand-DynamicParametersDCR -TestToRun returnduplicateparameter -ErrorAction Stop } |
Should -Throw -ErrorId "GetCommandMetadataError,Microsoft.PowerShell.Commands.GetCommandCommand"
2016-08-08 20:19:11 -07:00
}
It "verify if get the proper dynamic parameter type skipped by issue #1430" -Pending {
2020-05-07 13:00:30 +01:00
$results = Get-Command TestGetCommand-DynamicParametersDCR -TestToRun returngenericparameter -ParameterType System . Diagnostics . Process
2016-08-08 20:19:11 -07:00
VerifyParameterType -cmdlet $results [ 0 ] -parameterName "TypedValue" -parameterType System . Diagnostics . Process
}
It "It works with Single Cmdlet Using Verb&Noun ParameterSet" {
$paramName = "Encoding"
2017-10-23 19:46:27 -07:00
$results = Get-Command -Verb get -Noun content -encoding UTF8
2016-08-08 20:19:11 -07:00
VerifyDynamicParametersExist -cmdlet $results [ 0 ] -parameterNames $paramName
2017-10-23 19:46:27 -07:00
VerifyParameterType -cmdlet $results [ 0 ] -parameterName $paramName -ParameterType System . Text . Encoding
2016-08-08 20:19:11 -07:00
}
#unsupported parameter: -synop
It "[Unsupported]It works with Single Cmdlet Using Verb&Noun ParameterSet With Synopsis" -Pending {
$paramName = "Encoding"
2017-10-23 19:46:27 -07:00
$results = Get-Command -Verb get -Noun content -encoding UTF8 -Synop
2016-08-08 20:19:11 -07:00
VerifyDynamicParametersExist -cmdlet $results [ 0 ] -parameterNames $paramName
2017-10-23 19:46:27 -07:00
VerifyParameterType -cmdlet $results [ 0 ] -parameterName $paramName -ParameterType System . Text . Encoding
2017-01-16 13:31:14 -08:00
}
2019-11-07 11:59:24 -08:00
It "Piping more than one CommandInfo works" {
$result = Get-Command -Name Add-Content , Get-Content | Get-Command
$result . Count | Should -Be 2
$result . Name | Should -Be "Add-Content" , "Get-Content"
}
2024-12-30 20:06:28 +01:00
It "Excluding modules works" {
$result = Get-Command -Name Get-Command -ExcludeModule Microsoft . PowerShell . Core
$result | Should -Be $null
}
2017-10-23 19:46:27 -07:00
}
2026-08-17 15:31:31 -07:00
Describe "Test relative path in PATH env var" -Tags "CI" {
BeforeAll {
$originalPath = $env:PATH
$originalLocation = Get-Location
$subDir1 = Join-Path $TestDrive -ChildPath "subdir1"
$subDir2 = Join-Path $TestDrive -ChildPath "subdir2"
$toolsUnderSubDir1 = Join-Path $subDir1 -ChildPath "tools"
$toolsUnderSubDir2 = Join-Path $subDir2 -ChildPath "tools"
$null = New-Item -Path $subDir1 -ItemType Directory -Force
$null = New-Item -Path $subDir2 -ItemType Directory -Force
$null = New-Item -Path $toolsUnderSubDir1 -ItemType Directory -Force
$null = New-Item -Path $toolsUnderSubDir2 -ItemType Directory -Force
$helloScript = Join-Path $toolsUnderSubDir1 -ChildPath "hello.ps1"
$byeScript = Join-Path $toolsUnderSubDir2 -ChildPath "bye.ps1"
$null = New-Item -Path $helloScript -ItemType File -Force
$null = New-Item -Path $byeScript -ItemType File -Force
}
AfterAll {
Set-Location $originalLocation
$env:PATH = $originalPath
}
It "Get-Command should resolve relative path in PATH env var based on user's current working directory" {
$dirSep = [ System.IO.Path ]:: DirectorySeparatorChar
$pathSep = [ System.IO.Path ]:: PathSeparator
Set-Location $subDir1
$env:PATH = ". ${dirSep} tools ${pathSep}$env:PATH "
$result = Get-Command "hello.ps1" -ErrorAction silentlycontinue
$result | Should -Not -BeNullOrEmpty -Because "CWD is $subDir1 , so '. ${dirSep} tools' in PATH should be resolved to ' $toolsUnderSubDir1 ', which contains 'hello.ps1'"
$result . Path | Should -BeExactly $helloScript
$result = Get-Command "bye.ps1" -ErrorAction silentlycontinue
$result | Should -BeNullOrEmpty -Because "'bye.ps1' is not in ' $toolsUnderSubDir1 '"
Set-Location $subDir2
$result = Get-Command "hello.ps1" -ErrorAction silentlycontinue
$result | Should -BeNullOrEmpty -Because "CWD is $subDir2 , so '. ${dirSep} tools' in PATH should be resolved to ' $toolsUnderSubDir2 ', which contains 'bye.ps1'"
$result = Get-Command "bye.ps1" -ErrorAction silentlycontinue
$result | Should -Not -BeNullOrEmpty
$result . Path | Should -BeExactly $byeScript
}
}