Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,10 @@ internal void CaptureLocals()
// 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);
}
}
Expand Down
42 changes: 42 additions & 0 deletions test/powershell/engine/Api/GetNewClosure.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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 ""
}
}