Skip to content

Commit ad0b4cc

Browse files
authored
Add tests for ProxyCommand APIs to improve coverage (#4791)
Examine the code in ProxyCommand.cs to find out specific missing areas.
1 parent a3e193a commit ad0b4cc

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
using namespace System.Management.Automation
2+
using namespace System.Collections.ObjectModel
3+
4+
Describe 'ProxyCommand Tests' -Tags "CI" {
5+
BeforeAll {
6+
function GetSectionText {
7+
param ($object)
8+
$texts = $object | Out-String -Stream | ForEach-Object {
9+
if (![string]::IsNullOrWhiteSpace($_)) { $_.Trim() }
10+
}
11+
$texts -join ""
12+
}
13+
14+
function ProxyTest {
15+
[CmdletBinding(DefaultParameterSetName='Name')]
16+
param (
17+
[Parameter(ParameterSetName="Name", Mandatory=$true)]
18+
[ValidateSet("Orange", "Apple")]
19+
[string] $Name,
20+
21+
[Parameter(ParameterSetName="Id", Mandatory=$true)]
22+
[ValidateRange(1,5)]
23+
[int] $Id,
24+
25+
[Parameter(ValueFromPipeline)]
26+
[string] $Message
27+
)
28+
29+
DynamicParam {
30+
$ParamAttrib = [parameter]::new()
31+
$ParamAttrib.Mandatory = $true
32+
33+
$AttribColl = [Collection[System.Attribute]]::new()
34+
$AttribColl.Add($ParamAttrib)
35+
36+
$RuntimeParam = [RuntimeDefinedParameter]::new('LastName', [string], $AttribColl)
37+
$RuntimeParamDic = [RuntimeDefinedParameterDictionary]::new()
38+
$RuntimeParamDic.Add('LastName', $RuntimeParam)
39+
return $RuntimeParamDic
40+
}
41+
42+
Begin {
43+
$AllMessages = @()
44+
if ($PSCmdlet.ParameterSetName -eq "Name") {
45+
$MyString = $Name, $PSBoundParameters['LastName'] -join ","
46+
} else {
47+
$MyString = $Id, $PSBoundParameters['LastName'] -join ","
48+
}
49+
}
50+
51+
Process {
52+
if ($Message) {
53+
$AllMessages += $Message
54+
}
55+
}
56+
57+
End {
58+
$MyString + " - " + ($AllMessages -join ";")
59+
}
60+
}
61+
}
62+
63+
It "Test ProxyCommand.GetHelpComments" {
64+
$helpObj = Get-Help Get-Alias -Full
65+
$helpContent = [System.Management.Automation.ProxyCommand]::GetHelpComments($helpObj)
66+
67+
$funcBody = @"
68+
<#
69+
{0}
70+
#>
71+
72+
param({1})
73+
"@
74+
$params = $helpObj.parameters.parameter
75+
$paramString = '${0}' -f $params[0].name
76+
for ($i = 1; $i -lt $params.Length; $i++) {
77+
$paramString += ',${0}' -f $params[$i].name
78+
}
79+
80+
$bodyToUse = $funcBody -f $helpContent, $paramString
81+
$bodySB = [scriptblock]::Create($bodyToUse)
82+
Set-Item -Path function:\TestHelpComment -Value $bodySB
83+
$newHelpObj = Get-Help TestHelpComment -Full
84+
85+
$helpObj.Synopsis | Should Be $newHelpObj.Synopsis
86+
$oldDespText = GetSectionText $helpObj.description
87+
$newDespText = GetSectionText $newHelpObj.description
88+
$oldDespText | Should Be $newDespText
89+
90+
$oldParameters = @($helpObj.parameters.parameter)
91+
$newParameters = @($newHelpObj.parameters.parameter)
92+
$oldParameters.Length | Should Be $newParameters.Length
93+
$oldParameters.name -join "," | Should Be ($newParameters.name -join ",")
94+
95+
$oldExamples = @($helpObj.examples.example)
96+
$newExamples = @($newHelpObj.examples.example)
97+
$oldExamples.Length | Should Be $newExamples.Length
98+
}
99+
100+
It "Test generate proxy command" {
101+
$cmdInfo = Get-Command -Name Get-Content
102+
$cmdMetadata = [CommandMetadata]::new($cmdInfo)
103+
$proxyBody = [ProxyCommand]::Create($cmdMetadata, "--DummyHelpContent--")
104+
$proxyBody | Should Match '--DummyHelpContent--'
105+
106+
$proxyBodySB = [scriptblock]::Create($proxyBody)
107+
Set-Item -Path function:\MyGetContent -Value $proxyBodySB
108+
109+
$expectedContent = "Hello World"
110+
Set-Content -Path $TestDrive\content.txt -Value $expectedContent -Encoding Unicode
111+
$myContent = MyGetContent -Path $TestDrive\content.txt -Encoding Unicode
112+
$myContent | Should Be $expectedContent
113+
}
114+
115+
It "Test generate individual components" {
116+
$cmdInfo = Get-Command -Name ProxyTest
117+
$cmdMetadata = [CommandMetadata]::new($cmdInfo)
118+
$template = @"
119+
{0}
120+
param(
121+
{1}
122+
)
123+
124+
DynamicParam {{
125+
{2}
126+
}}
127+
128+
Begin {{
129+
{3}
130+
}}
131+
132+
Process {{
133+
{4}
134+
}}
135+
136+
End {{
137+
{5}
138+
}}
139+
"@
140+
141+
$cmdletBindig = [ProxyCommand]::GetCmdletBindingAttribute($cmdMetadata)
142+
$params = [ProxyCommand]::GetParamBlock($cmdMetadata)
143+
$dynamicParams = [ProxyCommand]::GetDynamicParam($cmdMetadata)
144+
$begin = [ProxyCommand]::GetBegin($cmdMetadata)
145+
$process = [ProxyCommand]::GetProcess($cmdMetadata)
146+
$end = [ProxyCommand]::GetEnd($cmdMetadata)
147+
148+
$funcBody = $template -f $cmdletBindig, $params, $dynamicParams, $begin, $process, $end
149+
$bodySB = [scriptblock]::Create($funcBody)
150+
Set-Item -Path function:\MyProxyTest -Value $bodySB
151+
152+
$cmdMyProxyTest = Get-Command MyProxyTest
153+
$dyParam = $cmdMyProxyTest.Parameters.GetEnumerator() | Where-Object { $_.Value.IsDynamic }
154+
$dyParam.Key | Should Be 'LastName'
155+
156+
$result = "Msg1", "Msg2" | MyProxyTest -Name Apple -LastName Last
157+
$result | Should Be "Apple,Last - Msg1;Msg2"
158+
159+
$result = "Msg1", "Msg2" | MyProxyTest -Id 3 -LastName Last
160+
$result | Should Be "3,Last - Msg1;Msg2"
161+
}
162+
}

0 commit comments

Comments
 (0)