Skip to content

Commit

Permalink
Fix function definition in parameter filter (#1293)
Browse files Browse the repository at this point in the history
Allow ${function:} to be passed to Mock -ParameterFilter
  • Loading branch information
clcaldwell authored and nohwnd committed May 1, 2019
1 parent c300092 commit ea65cc6
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 4 deletions.
87 changes: 84 additions & 3 deletions Functions/Mock.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2216,11 +2216,92 @@ Describe "Mock definition output" {
}
}

Describe 'Mocking using ParameterFilter with scriptblock' {
$filter = [scriptblock]::Create( ('$Path -eq ''C:\Windows''') )
Mock -CommandName 'Test-Path' -ParameterFilter $filter
Describe 'Mocking using ParameterFilter' {

Context 'Scriptblock [Scriptblock]::Create() passed to ParameterFilter as var' {
BeforeAll{
$filter = [scriptblock]::Create( ('$Path -eq ''C:\Windows''') )
Mock Test-Path { $True }
Mock Test-Path -ParameterFilter $filter -MockWith { $False }
}

It "Returns default mock" {
Test-Path -Path C:\AwesomePath | Should -Be $True
}

It "returns mock that matches parameter filter block" {
Test-Path -Path C:\Windows | Should -Be $false
}
}

Context 'Scriptblock expression $( [Scriptblock]::Create() ) passed to ParameterFilter' {
BeforeAll{
$filter = [scriptblock]::Create( ('$Path -eq ''C:\Windows''') )
Mock Test-Path { $True }
Mock Test-Path -ParameterFilter $( [scriptblock]::Create(('$Path -eq ''C:\Windows''')) ) -MockWith { $False }
}

It "Returns default mock" {
Test-Path -Path C:\AwesomePath | Should -Be $True
}

It "returns mock that matches parameter filter block" {
Test-Path -Path C:\Windows | Should -Be $false
}
}

Context 'Scriptblock {} passed to ParameterFilter' {
BeforeAll{
Mock Test-Path { $True }
Mock Test-Path -ParameterFilter { $Path -eq "C:\Windows" } -MockWith { $False }
}

It "Returns default mock" {
Test-Path -Path C:\AwesomePath | Should -Be $True
}

It "returns mock that matches parameter filter block" {
Test-Path -Path C:\Windows | Should -Be $false
}
}
Context 'Scriptblock {} passed to ParameterFilter as var' {
BeforeAll{
$filter = {
$Path -eq "C:\Windows"
}
Mock Test-Path { $True }
Mock Test-Path -ParameterFilter $filter -MockWith { $False }
}

It "Returns default mock" {
Test-Path -Path C:\AwesomePath | Should -Be $True
}

It "returns mock that matches parameter filter block" {
Test-Path -Path C:\Windows | Should -Be $false
}
}

Context 'Function Definition ${} passed to ParameterFilter' {
BeforeAll {
Function ParamFilter {
$Path -eq "C:\Windows"
}
Mock Test-Path { $True }
Mock Test-Path -ParameterFilter ${function:ParamFilter} -MockWith { $False }
}

It "Returns default mock" {
Test-Path -Path C:\AwesomePath | Should -Be $True
}

It "returns mock that matches parameter filter block" {
Test-Path -Path C:\Windows | Should -Be $false
}
}
}


if ($PSVersionTable.PSVersion.Major -ge 3) {
Describe "-RemoveParameterType" {
BeforeAll {
Expand Down
20 changes: 19 additions & 1 deletion Functions/Mock.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1642,6 +1642,24 @@ function Get-ConflictingParameterNames {
$script:ConflictingParameterNames
}

function Get-ScriptBlockAST {
param (
[scriptblock]
$ScriptBlock
)

if ($ScriptBlock.Ast -is [System.Management.Automation.Language.ScriptBlockAst]) {
$ast = $Block.Ast.EndBlock
}
elseif ($ScriptBlock.Ast -is [System.Management.Automation.Language.FunctionDefinitionAst]) {
$ast = $Block.Ast.Body.EndBlock
}
else {
throw "Pester failed to parse ParameterFilter, scriptblock is invalid type. Please reformat your ParameterFilter."
}

return $ast
}

function New-BlockWithoutParameterAliases {
[CmdletBinding()]
Expand All @@ -1659,7 +1677,7 @@ function New-BlockWithoutParameterAliases {
try {
if ($PSVersionTable.PSVersion.Major -ge 3) {
$params = $Metadata.Parameters.Values
$ast = $Block.Ast.EndBlock
$ast = Get-ScriptBlockAST $Block
$blockText = $ast.Extent.Text
$variables = [array]($Ast.FindAll( { param($ast) $ast -is [System.Management.Automation.Language.VariableExpressionAst]}, $true))
[array]::Reverse($variables)
Expand Down

0 comments on commit ea65cc6

Please sign in to comment.