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
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