Skip to content

Commit

Permalink
Update ExpandedPath before executing tests and blocks (#2254)
Browse files Browse the repository at this point in the history
  • Loading branch information
fflaten authored Nov 6, 2022
1 parent b088165 commit 4e6eeb5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Pester.Runtime.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ function Invoke-Block ($previousBlock) {
$block.ExecutedAt = [DateTime]::Now
$block.Executed = $true

# update ExpandedPath to included expanded parent name in case this fails in setup
if (-not $block.Parent.IsRoot) { $block.ExpandedPath = "$($block.Parent.ExpandedPath).$($block.Name)" }

if ($PesterPreference.Debug.WriteDebugMessages.Value) {
Write-PesterDebugMessage -Scope Runtime "Executing body of block '$($block.Name)'"
}
Expand Down Expand Up @@ -578,6 +581,9 @@ function Invoke-TestItem {
}
}

# update ExpandedPath to included expanded parent name in case this fails in setup
$Test.ExpandedPath = "$($block.ExpandedPath).$($Test.Name)"

if ($Test.Skip) {
if ($PesterPreference.Debug.WriteDebugMessages.Value) {
$path = $Test.Path -join '.'
Expand Down
42 changes: 42 additions & 0 deletions tst/Pester.RSpec.ts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1769,6 +1769,48 @@ i -PassThru:$PassThru {
$r.Containers[0].Blocks[0].ExpandedName | Verify-Equal "d 1"
}

t "ExpandedPath is expanded for parent blocks when block setup fails" {
$sb = {
Describe 'd <_>' {
Describe 'd2 <_>' {
BeforeAll { throw 'oh no' }
It 'i <_>' { $_ | Should -Be 1 }
}
} -ForEach 1
}

$container = New-PesterContainer -ScriptBlock $sb
$r = Invoke-Pester -Container $container -PassThru
$r.Containers[0].Blocks[0].Blocks[0].Result | Verify-Equal 'Failed'
$r.Containers[0].Blocks[0].Blocks[0].ExpandedName | Verify-Equal 'd2 <_>'
# ExpandedPath is updated as far as possible (parent block) before failure in block
$r.Containers[0].Blocks[0].Blocks[0].ExpandedPath | Verify-Equal 'd 1.d2 <_>'
}

t "ExpandedPath is expanded for parent blocks when test is skipped or fails in BeforeEach" {
$sb = {
Describe 'd <_>' {
It 'i <_>' -Skip { $_ | Should -Be 2 }
} -ForEach 1
Describe 'd <_>' {
BeforeEach { throw 'oh no' }
It 'i <_>' { $_ | Should -Be 2 }
} -ForEach 2
}

$container = New-PesterContainer -ScriptBlock $sb
$r = Invoke-Pester -Container $container -PassThru
# when test skipped
$r.Containers[0].Blocks[0].ExpandedName | Verify-Equal 'd 1'
$r.Containers[0].Blocks[0].Tests[0].Result | Verify-Equal 'Skipped'
$r.Containers[0].Blocks[0].Tests[0].ExpandedName | Verify-Equal 'i <_>'
$r.Containers[0].Blocks[0].Tests[0].ExpandedPath | Verify-Equal 'd 1.i <_>'
# when test setup failed
$r.Containers[0].Blocks[1].ExpandedName | Verify-Equal 'd 2'
$r.Containers[0].Blocks[1].Tests[0].ExpandedName | Verify-Equal 'i <_>'
$r.Containers[0].Blocks[1].Tests[0].ExpandedPath | Verify-Equal 'd 2.i <_>'
}

t "<user.name> expands to `$user.name" {
$sb = {
Describe "d <user.name>" {
Expand Down

0 comments on commit 4e6eeb5

Please sign in to comment.