Skip to content

Instantly share code, notes, and snippets.

@nohwnd
Last active June 21, 2021 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nohwnd/9eabde97a62df08a002b56265bab84f7 to your computer and use it in GitHub Desktop.
Save nohwnd/9eabde97a62df08a002b56265bab84f7 to your computer and use it in GitHub Desktop.
Get-TestParents
function Get-TestParents {
<#
.SYNOPSIS
Returns any parents not already known, top-down first, so that a hierarchy can be created in a streaming manner
#>
param (
#Test to fetch parents of. For maximum efficiency this should be done one test at a time and then stack processed
[Parameter(Mandatory,ValueFromPipeline)][Pester.Test[]]$Test,
[HashSet[Pester.Block]]$KnownParents = [HashSet[Pester.Block]]::new()
)
begin {
[Stack[Pester.Block]]$NewParents = [Stack[Pester.Block]]::new()
}
process {
# Output all parents that we don't know yet (distinct parents), in order from the top most
# to the child most.
foreach ($TestItem in $Test) {
$NewParents.Clear()
$parent = $TestItem.Block
while ($null -ne $parent -and -not $parent.IsRoot) {
if (-not $KnownParents.Add($parent)) {
# We know this parent, so we must know all of its parents as well.
# We don't need to go further.
break
}
$NewParents.Push($parent)
$parent = $parent.Parent
}
# Output the unknown parent objects from the top most, to the one closest to our test.
foreach ($p in $NewParents) { $p }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment