Skip to content

Commit

Permalink
Filter by scriptblock position (#1220)
Browse files Browse the repository at this point in the history
* Add script block filter

ScriptBlockFilter filters top-level describes by position of the Fixture instead of filtering by name, this is for plugin usage to accomodate tests that expand properties in the describe name, and so AST and runtime values in the name do not match.
  • Loading branch information
nohwnd authored Jan 21, 2019
2 parents cd999fc + d75141e commit 3e77d00
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 11 deletions.
18 changes: 18 additions & 0 deletions Functions/Describe.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,24 @@ function DescribeImpl {
return
}
}

if ($Pester.ScriptBlockFilter) {
$match = $false
foreach ($filter in $Pester.ScriptBlockFilter) {
if ($match) {
break
}

if ($Fixture.File -eq $filter.Path -and $Fixture.StartPosition.StartLine -eq $filter.Line) {
$match = $true
}
}

if (-not $match) {
return
}
}

if ($Pester.TagFilter) {
if (-not (Contain-AnyStringLike -Filter $Pester.TagFilter -Collection $Tag)) {
return
Expand Down
2 changes: 1 addition & 1 deletion Functions/Gherkin.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ function Invoke-Gherkin {
}
}
$sessionState = Set-SessionStateHint -PassThru -Hint "Caller - Captured in Invoke-Gherkin" -SessionState $PSCmdlet.SessionState
$pester = New-PesterState -TagFilter $Tag -ExcludeTagFilter $ExcludeTag -TestNameFilter $ScenarioName -SessionState $sessionState -Strict $Strict -Show $Show -PesterOption $PesterOption |
$pester = New-PesterState -TagFilter $Tag -ExcludeTagFilter $ExcludeTag -TestNameFilter $ScenarioName -SessionState $sessionState -Strict:$Strict -Show $Show -PesterOption $PesterOption |
& $SafeCommands["Add-Member"] -MemberType NoteProperty -Name Features -Value (& $SafeCommands["New-Object"] System.Collections.Generic.List[PSObject] ) -PassThru |
& $SafeCommands["Add-Member"] -MemberType ScriptProperty -Name FailedScenarios -PassThru -Value {
$Names = $this.TestResult | & $SafeCommands["Group-Object"] Describe |
Expand Down
4 changes: 4 additions & 0 deletions Functions/Output.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ function Write-PesterStart {
if ($PesterState.TestNameFilter) {
$message += $ReportStrings.FilterMessage -f "$($PesterState.TestNameFilter)"
}
if ($PesterState.ScriptBlockFilter) {
$m = $(foreach ($m in $PesterState.ScriptBlockFilter) { "$($m.Path):$($m.Line)" }) -join ", "
$message += $ReportStrings.FilterMessage -f $m
}
if ($PesterState.TagFilter) {
$message += $ReportStrings.TagMessage -f "$($PesterState.TagFilter)"
}
Expand Down
13 changes: 11 additions & 2 deletions Functions/PesterState.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,23 @@ InModuleScope Pester {
Context "TestNameFilter and TagFilter parameter is set" {
$p = new-pesterstate -TagFilter "tag", "tag2" -testnamefilter "filter"

it "sets the TestNameFilter property" {
it "sets the TagFilter property" {
$p.TagFilter | should -be ("tag", "tag2")
}

it "sets the TestNameFilter property" {
$p.TagFilter | should -be ("tag", "tag2")
$p.TestNameFilter | should -be "Filter"
}
}

Context "ScritpBlockFilter is set" {
it "sets the ScriptBlockFilter property" {
$o = New-PesterOption -ScriptBlockFilter @(@{Path = "C:\Tests"; Line = 293})
$p = New-PesterState -PesterOption $o
$p.ScriptBlockFilter | Should -Not -BeNullOrEmpty
$p.ScriptBlockFilter[0].Path | Should -Be "C:\Tests"
$p.ScriptBlockFilter[0].Line | Should -Be 293
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion Functions/PesterState.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ function New-PesterState {
[Switch]$Strict,
[Pester.OutputTypes]$Show = 'All',
[object]$PesterOption,
[Switch]$RunningViaInvokePester
[Switch]$RunningViaInvokePester,
[Hashtable[]] $ScriptBlockFilter
)

if ($null -eq $SessionState) {
Expand Down Expand Up @@ -43,6 +44,7 @@ function New-PesterState {
$ExcludeTagFilter = $_excludeTagFilter
$TestNameFilter = $_testNameFilter


$script:SessionState = $_sessionState
$script:Stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
$script:TestStartTime = $null
Expand All @@ -64,6 +66,7 @@ function New-PesterState {

$script:IncludeVSCodeMarker = $PesterOption.IncludeVSCodeMarker
$script:TestSuiteName = $PesterOption.TestSuiteName
$script:ScriptBlockFilter = $PesterOption.ScriptBlockFilter
$script:RunningViaInvokePester = $RunningViaInvokePester

$script:SafeCommands = @{}
Expand Down Expand Up @@ -327,6 +330,7 @@ function New-PesterState {
$ExportedVariables = "TagFilter",
"ExcludeTagFilter",
"TestNameFilter",
"ScriptBlockFilter",
"TestResult",
"SessionState",
"CommandCoverage",
Expand Down
19 changes: 12 additions & 7 deletions Pester.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ New-PesterOption
if ($PassThru) {
# Remove all runtime properties like current* and Scope
$properties = @(
"TagFilter", "ExcludeTagFilter", "TestNameFilter", "TotalCount", "PassedCount", "FailedCount", "SkippedCount", "PendingCount", 'InconclusiveCount', "Time", "TestResult"
"TagFilter", "ExcludeTagFilter", "TestNameFilter", "ScriptBlockFilter", "TotalCount", "PassedCount", "FailedCount", "SkippedCount", "PendingCount", 'InconclusiveCount', "Time", "TestResult"

if ($CodeCoverage) {
@{ Name = 'CodeCoverage'; Expression = { $coverageReport } }
Expand Down Expand Up @@ -1197,6 +1197,8 @@ When generating NUnit XML output, this controls the name assigned to the root "t
Enables experimental features of Pester to be enabled.
.PARAMETER ShowScopeHints
EXPERIMENTAL: Enables debugging output for debugging tranisition among scopes. (Experimental flag needs to be used to enable this.)
.PARAMETER ScriptBlockFilter
Adds a filter that acts as -TestName but instead of the name uses location of the Fixture scriptblock. It takes an array of hashtables @{ Path = <string>; Line = <int> } . This is for plugins like CodeLens that parse the scriptblock, but cannot filter by name because of variables in the Describe description.
.INPUTS
None
You cannot pipe input to this command.
Expand All @@ -1222,7 +1224,9 @@ Invoke-Pester

[switch] $Experimental,

[switch] $ShowScopeHints
[switch] $ShowScopeHints,

[hashtable[]] $ScriptBlockFilter
)

# in PowerShell 2 Add-Member can attach properties only to
Expand All @@ -1245,6 +1249,7 @@ Invoke-Pester
TestSuiteName = $TestSuiteName
ShowScopeHints = $ShowScopeHints
Experimental = $Experimental
ScriptBlockFilter = $ScriptBlockFilter
}
}

Expand Down Expand Up @@ -1429,11 +1434,11 @@ function Set-PesterStatistics($Node) {
if ($action.Type -eq 'TestGroup') {
Set-PesterStatistics -Node $action

$Node.TotalCount += $action.TotalCount
$Node.PassedCount += $action.PassedCount
$Node.FailedCount += $action.FailedCount
$Node.SkippedCount += $action.SkippedCount
$Node.PendingCount += $action.PendingCount
$Node.TotalCount += $action.TotalCount
$Node.PassedCount += $action.PassedCount
$Node.FailedCount += $action.FailedCount
$Node.SkippedCount += $action.SkippedCount
$Node.PendingCount += $action.PendingCount
$Node.InconclusiveCount += $action.InconclusiveCount
}
elseif ($action.Type -eq 'TestCase') {
Expand Down

0 comments on commit 3e77d00

Please sign in to comment.