Skip to content

Commit

Permalink
Move helpers to Pester.Utility
Browse files Browse the repository at this point in the history
  • Loading branch information
fflaten committed Jun 30, 2024
1 parent 42a5565 commit e4ce70f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 54 deletions.
54 changes: 0 additions & 54 deletions src/Pester.Runtime.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,8 @@ else {
# # here I have doubts if that is too much to expose
# 'Get-CurrentTest'
# 'Get-CurrentBlock'
# 'Recurse-Up',
# 'Is-Discovery'

# # those are quickly implemented to be useful for demo
# 'Where-Failed'
# 'View-Flat'

# # those need to be refined and probably wrapped to something
# # that is like an object builder
# 'New-FilterObject'
Expand Down Expand Up @@ -2324,38 +2319,6 @@ function PostProcess-ExecutedBlock {
}
}

function Where-Failed {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
$Block
)

$Block | View-Flat | & $SafeCommands['Where-Object'] { $_.ShouldRun -and (-not $_.Executed -or -not $_.Passed) }
}

function View-Flat {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
$Block
)

begin {
$tests = [System.Collections.Generic.List[Object]]@()
}
process {
# TODO: normally I would output to pipeline but in fold there is accumulator and so it does not output
foreach ($b in $Block) {
Fold-Container $b -OnTest { param($t) $tests.Add($t) }
}
}

end {
$tests
}
}

function New-FilterObject {
[CmdletBinding()]
param (
Expand Down Expand Up @@ -2576,23 +2539,6 @@ function New-ParametrizedTest () {
}
}

function Recurse-Up {
param(
[Parameter(Mandatory)]
$InputObject,
[ScriptBlock] $Action
)

$i = $InputObject
$level = 0
while ($null -ne $i) {
&$Action $i

$level--
$i = $i.Parent
}
}

function Invoke-InNewScriptScope ([ScriptBlock] $ScriptBlock, $SessionState) {
# running in a script file will push a new script scope up the stack in the provided
# session state. To do this from a module we need to transport the file invocation into the
Expand Down
50 changes: 50 additions & 0 deletions src/Pester.Utility.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,53 @@ function Contain-AnyStringLike ($Filter, $Collection) {
}
return $false
}

# TODO: Remove?
function Recurse-Up {
param(
[Parameter(Mandatory)]
$InputObject,
[ScriptBlock] $Action
)

$i = $InputObject
$level = 0
while ($null -ne $i) {
&$Action $i

$level--
$i = $i.Parent
}
}

function Where-Failed {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
$Block

Check warning

Code scanning / PSScriptAnalyzer

Command accepts pipeline input but has not defined a process block. Warning

Command accepts pipeline input but has not defined a process block.
)

$Block | View-Flat | & $SafeCommands['Where-Object'] { $_.ShouldRun -and (-not $_.Executed -or -not $_.Passed) }

Check notice

Code scanning / PSScriptAnalyzer

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`. Note

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`.
}

function View-Flat {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
$Block
)

begin {
$tests = [System.Collections.Generic.List[Object]]@()
}
process {
# TODO: normally I would output to pipeline but in fold there is accumulator and so it does not output
foreach ($b in $Block) {
Fold-Container $b -OnTest { param($t) $tests.Add($t) }
}
}

end {
$tests
}
}

0 comments on commit e4ce70f

Please sign in to comment.