Skip to content

Commit

Permalink
Invoke Set-ScriptBlockHint indirectly (pester#1371)
Browse files Browse the repository at this point in the history
  • Loading branch information
nohwnd authored Sep 24, 2019
1 parent f9b119f commit 5b4860f
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions Pester.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ function Add-AssertionOperator {

$script:AssertionOperators[$Name] = $entry

foreach ($string in $Alias | Where { -not (Test-NullOrWhiteSpace $_)}) {
foreach ($string in $Alias | Where { -not (Test-NullOrWhiteSpace $_) }) {
Assert-ValidAssertionAlias -Alias $string
$script:AssertionAliases[$string] = $Name
}
Expand All @@ -509,7 +509,7 @@ function Assert-AssertionOperatorNameIsUnique {
[string[]] $Name
)

foreach ($string in $name | Where { -not (Test-NullOrWhiteSpace $_)}) {
foreach ($string in $name | Where { -not (Test-NullOrWhiteSpace $_) }) {
Assert-ValidAssertionName -Name $string

if ($script:AssertionOperators.ContainsKey($string)) {
Expand Down Expand Up @@ -617,14 +617,14 @@ function Get-AssertionDynamicParams {

$Script:PesterRoot = & $SafeCommands['Split-Path'] -Path $MyInvocation.MyCommand.Path
"$PesterRoot\Functions\*.ps1", "$PesterRoot\Functions\Assertions\*.ps1" |
& $script:SafeCommands['Resolve-Path'] |
& $script:SafeCommands['Where-Object'] { -not ($_.ProviderPath.ToLower().Contains(".tests.")) } |
& $script:SafeCommands['ForEach-Object'] { . $_.ProviderPath }
& $script:SafeCommands['Resolve-Path'] |
& $script:SafeCommands['Where-Object'] { -not ($_.ProviderPath.ToLower().Contains(".tests.")) } |
& $script:SafeCommands['ForEach-Object'] { . $_.ProviderPath }

if (& $script:SafeCommands['Test-Path'] "$PesterRoot\Dependencies") {
# sub-modules
& $script:SafeCommands['Get-ChildItem'] "$PesterRoot\Dependencies\*\*.psm1" |
& $script:SafeCommands['ForEach-Object'] { & $script:SafeCommands['Import-Module'] $_.FullName -Force -DisableNameChecking }
& $script:SafeCommands['ForEach-Object'] { & $script:SafeCommands['Import-Module'] $_.FullName -Force -DisableNameChecking }
}

Add-Type -TypeDefinition @"
Expand Down Expand Up @@ -1075,7 +1075,7 @@ function Invoke-Pester {
}
}

$script:mockTable = @{}
$script:mockTable = @{ }
Remove-MockFunctionsAndAliases
$sessionState = Set-SessionStateHint -PassThru -Hint "Caller - Captured in Invoke-Pester" -SessionState $PSCmdlet.SessionState
$pester = New-PesterState -TestNameFilter $TestName -TagFilter $Tag -ExcludeTagFilter $ExcludeTag -SessionState $SessionState -Strict:$Strict -Show:$Show -PesterOption $PesterOption -RunningViaInvokePester
Expand All @@ -1089,16 +1089,17 @@ function Invoke-Pester {
[Parameter(Position = 0)]
[string] $Path,
[string] $Script,
$Set_ScriptBlockHint,
[object[]] $Arguments = @(),
[System.Collections.IDictionary] $Parameters = @{}
[System.Collections.IDictionary] $Parameters = @{ }
)

if (-not [string]::IsNullOrEmpty($Path)) {
& $Path @Parameters @Arguments
}
elseif (-not [string]::IsNullOrEmpty($Script)) {
$scriptBlock = [scriptblock]::Create($Script)
Set-ScriptBlockHint -Hint "Unbound ScriptBlock from Invoke-Pester" -ScriptBlock $scriptBlock
& $Set_ScriptBlockHint -Hint "Unbound ScriptBlock from Invoke-Pester" -ScriptBlock $scriptBlock
Invoke-Command -ScriptBlock ($scriptBlock)
}
}
Expand All @@ -1120,7 +1121,7 @@ function Invoke-Pester {
$pester.EnterTestGroup($testDescription, 'Script')
Write-Describe $testDescription -CommandUsed Script
do {
$testOutput = & $invokeTestScript -Path $testScript.Path -Script $testScript.Script -Arguments $testScript.Arguments -Parameters $testScript.Parameters
$testOutput = & $invokeTestScript -Path $testScript.Path -Script $testScript.Script -Arguments $testScript.Arguments -Parameters $testScript.Parameters -Set_ScriptBlockHint $script:SafeCommands['Set-ScriptBlockHint']
} until ($true)
}
catch {
Expand Down Expand Up @@ -1271,7 +1272,7 @@ function ResolveTestScripts {
$parameters = Get-DictionaryValueFromFirstKeyFound -Dictionary $object -Key 'Parameters', 'params'

if ($null -eq $Parameters) {
$Parameters = @{}
$Parameters = @{ }
}

if ($unresolvedPath -isnot [string] -or $unresolvedPath -notmatch '\S' -and ($script -isnot [string] -or $script -notmatch '\S')) {
Expand All @@ -1286,7 +1287,7 @@ function ResolveTestScripts {
$unresolvedPath = [string] $object
$script = [string] $object
$arguments = @()
$parameters = @{}
$parameters = @{ }
}

if (-not [string]::IsNullOrEmpty($unresolvedPath)) {
Expand All @@ -1310,19 +1311,19 @@ function ResolveTestScripts {
# World's longest pipeline?

& $script:SafeCommands['Resolve-Path'] -Path $unresolvedPath |
& $script:SafeCommands['Where-Object'] { $_.Provider.Name -eq 'FileSystem' } |
& $script:SafeCommands['Select-Object'] -ExpandProperty ProviderPath |
& $script:SafeCommands['Get-ChildItem'] -Include *.Tests.ps1 -Recurse |
& $script:SafeCommands['Where-Object'] { -not $_.PSIsContainer } |
& $script:SafeCommands['Select-Object'] -ExpandProperty FullName -Unique |
& $script:SafeCommands['ForEach-Object'] {
& $script:SafeCommands['New-Object'] psobject -Property @{
Path = $_
Script = $null
Arguments = $arguments
Parameters = $parameters
}
& $script:SafeCommands['Where-Object'] { $_.Provider.Name -eq 'FileSystem' } |
& $script:SafeCommands['Select-Object'] -ExpandProperty ProviderPath |
& $script:SafeCommands['Get-ChildItem'] -Include *.Tests.ps1 -Recurse |
& $script:SafeCommands['Where-Object'] { -not $_.PSIsContainer } |
& $script:SafeCommands['Select-Object'] -ExpandProperty FullName -Unique |
& $script:SafeCommands['ForEach-Object'] {
& $script:SafeCommands['New-Object'] psobject -Property @{
Path = $_
Script = $null
Arguments = $arguments
Parameters = $parameters
}
}
}
}
elseif (-not [string]::IsNullOrEmpty($script)) {
Expand Down Expand Up @@ -1515,6 +1516,8 @@ function Assert-VerifiableMocks {

}

$script:SafeCommands['Set-ScriptBlockHint'] = & $script:SafeCommands['Get-Command'] -Name Set-ScriptBlockHint -ErrorAction Stop

Set-SessionStateHint -Hint Pester -SessionState $ExecutionContext.SessionState
# in the future rename the function to Add-ShouldOperator
Set-Alias -Name Add-ShouldOperator -Value Add-AssertionOperator
Expand Down

0 comments on commit 5b4860f

Please sign in to comment.