Fix the clean block for generated proxy function (#16827)

This commit is contained in:
Dongbo Wang
2022-02-03 11:39:08 -08:00
committed by GitHub
parent b7f8556bcd
commit bbb11dd07a
2 changed files with 16 additions and 1 deletions
@@ -1128,7 +1128,9 @@ clean
// 1. the 'Clean' block doesn't propagate up any exception (terminating error);
// 2. only one expression in the script, so nothing else needs to be stopped when invoking the method fails.
return @"
$steppablePipeline.Clean()
if ($null -ne $steppablePipeline) {
$steppablePipeline.Clean()
}
";
}
@@ -478,6 +478,19 @@ Describe 'Function Pipeline Behaviour' -Tag 'CI' {
## Dispose the steppable pipeline.
$step.Dispose()
}
It "Clean block runs fine in a proxy function when a dynamic parameter fails to bind" {
$function:TestProxyGci = [scriptblock]::Create(
[Management.Automation.ProxyCommand]::Create(
(Get-Command Get-ChildItem)))
## The proxy function 'TestProxyGci' contains the 'dynamicparam' block, which will
## run during parameter binding. However, the parameter binding failed, and thus
## the 'begin', 'process', and 'end' blocks will not run, so '$steppablePipeline'
## in the proxy function is null (never created). The 'clean' block will run anyway,
## but it should skip calling '$steppablePipeline.Clean()' in this case.
{ TestProxyGci -Attributes } | Should -Throw -ErrorId 'MissingArgument,TestProxyGci'
}
}
Context "'exit' statement in command" {