Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix function definition in parameter filter #1293

Merged
merged 7 commits into from
May 1, 2019
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
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' {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@clcaldwell Some of these tests seem to be very similar. They could be candidate to be refactored using the TestCases option


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