Fix PSModuleInfo.CaptureLocals to not do ValidateAttribute check when capturing variables from the caller's scope (#3149)

Fix PSModuleInfo.CaptureLocals to not do ValidateAttribute check when capturing variables from the caller's scope
This commit is contained in:
Dongbo Wang 2017-02-16 21:25:23 -08:00 committed by GitHub
parent c266c8ebca
commit 5a8fa57278
2 changed files with 46 additions and 1 deletions

View file

@ -1378,7 +1378,10 @@ namespace System.Management.Automation
// Only copy simple mutable variables...
if (v.Options == ScopedItemOptions.None && !(v is NullVariable))
{
PSVariable newVar = new PSVariable(v.Name, v.Value, v.Options, v.Attributes, v.Description);
PSVariable newVar = new PSVariable(v.Name, v.Value, v.Options, v.Description);
// The variable is already defined/set in the scope, and that means the attributes
// have already been checked if it was needed, so we don't do it again.
newVar.AddParameterAttributesNoChecks(v.Attributes);
SessionState.Internal.NewVariable(newVar, false);
}
}

View file

@ -0,0 +1,42 @@
Describe "ScriptBlock.GetNewClosure()" -tags "CI" {
BeforeAll {
## No error should occur when calling GetNewClosure because:
## 1. ValidateAttributes are not evaluated on parameter default values
## 2. GetNewClosure no longer forces validation on existing variables
function SimpleFunction_GetNewClosure
{
param([ValidateNotNull()] $Name)
& { 'OK' }.GetNewClosure()
}
function ScriptCmdlet_GetNewClosure
{
[CmdletBinding()]
param(
[Parameter()]
[ValidateNotNullOrEmpty()]
[string] $Name = "",
[Parameter()]
[ValidateRange(1,3)]
[int] $Value = 4
)
& { $Value; $Name }.GetNewClosure()
}
}
It "Parameter attributes should not get evaluated again in GetNewClosure - SimpleFunction" {
SimpleFunction_GetNewClosure | Should Be "OK"
}
It "Parameter attributes should not get evaluated again in GetNewClosure - ScriptCmdlet" {
$result = ScriptCmdlet_GetNewClosure
$result.Count | Should Be 2
$result[0] | Should Be 4
$result[1] | Should Be ""
}
}