Skip to content

Commit

Permalink
Bug fix in ExecuteBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
dlwyatt committed Jul 22, 2016
1 parent 9517657 commit e1cb585
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
19 changes: 19 additions & 0 deletions Functions/Mock.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1690,3 +1690,22 @@ Describe 'Globbing characters in command name' {
}

}

Describe 'Naming conflicts in mocked functions' {
function Sample {
param(
[string]
${Metadata}
)
}

function Wrapper {
Sample -Metadata 'test'
}

Mock -CommandName Sample { 'mocked' }

It 'Works with commands that contain variables named Metadata' {
Wrapper | Should Be 'mocked'
}
}
33 changes: 17 additions & 16 deletions Functions/Mock.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1023,41 +1023,42 @@ function ExecuteBlock
param (
[Parameter(Mandatory = $true)]
[scriptblock]
$ScriptBlock,
${Script Block},

[hashtable]
$BoundParameters = @{},
$___BoundParameters___ = @{},

[object[]]
$ArgumentList = @(),
$___ArgumentList___ = @(),

[System.Management.Automation.CommandMetadata]
$Metadata,
${Meta data},

[System.Management.Automation.SessionState]
$SessionState
${Session State}
)

# This script block exists to hold variables without polluting the test script's current scope.
# Dynamic parameters in functions, for some reason, only exist in $PSBoundParameters instead
# of being assigned a local variable the way static parameters do. By calling Set-DynamicParameterValues,
# of being assigned a local variable the way static parameters do. By calling Set-DynamicParameterVariables,
# we create these variables for the caller's use in a Parameter Filter or within the mock itself, and
# by doing it inside this temporary script block, those variables don't stick around longer than they
# should.

# Because Set-DynamicParameterVariables might potentially overwrite our $ScriptBlock, $BoundParameters and/or $ArgumentList variables,
# we'll stash them in names unlikely to be overwritten.

$___ScriptBlock___ = $ScriptBlock
$___BoundParameters___ = $BoundParameters
$___ArgumentList___ = $ArgumentList

Set-DynamicParameterVariables -SessionState $SessionState -Parameters $BoundParameters -Metadata $Metadata
& $___ScriptBlock___ @___BoundParameters___ @___ArgumentList___
Set-DynamicParameterVariables -SessionState ${Session State} -Parameters $___BoundParameters___ -Metadata ${Meta data}
& ${Script Block} @___BoundParameters___ @___ArgumentList___
}

Set-ScriptBlockScope -ScriptBlock $scriptBlock -SessionState $mock.SessionState
& $scriptBlock -ScriptBlock $block.Mock -ArgumentList $ArgumentList -BoundParameters $BoundParameters -Metadata $mock.Metadata -SessionState $mock.SessionState
$splat = @{
'Script Block' = $block.Mock
'___ArgumentList___' = $ArgumentList
'___BoundParameters___' = $BoundParameters
'Meta data' = $mock.Metadata
'Session State' = $mock.SessionState
}

& $scriptBlock @splat
}

function Invoke-InMockScope
Expand Down

0 comments on commit e1cb585

Please sign in to comment.