From a48c9d7f9e28035cffd960e9baeb7834914c71b2 Mon Sep 17 00:00:00 2001 From: nohwnd Date: Sun, 27 Aug 2017 14:39:17 +0200 Subject: [PATCH] Fix exception propagating outside of describe/context when AfterAll throws Fix #584 Fix #662 --- Functions/Describe.ps1 | 61 ++++++++++--------- .../TestsRunningInCleanRunspace.Tests.ps1 | 30 +++++++++ 2 files changed, 62 insertions(+), 29 deletions(-) diff --git a/Functions/Describe.ps1 b/Functions/Describe.ps1 index 12acf12c5..bd1ea6ace 100644 --- a/Functions/Describe.ps1 +++ b/Functions/Describe.ps1 @@ -140,26 +140,44 @@ function DescribeImpl { $testDriveAdded = $false try { - if (-not $NoTestDrive) + try { - if (-not (Test-Path TestDrive:\)) + if (-not $NoTestDrive) { - New-TestDrive - $testDriveAdded = $true + if (-not (Test-Path TestDrive:\)) + { + New-TestDrive + $testDriveAdded = $true + } + else + { + $TestDriveContent = Get-TestDriveChildItem + } } - else - { - $TestDriveContent = Get-TestDriveChildItem - } - } - Add-SetupAndTeardown -ScriptBlock $Fixture - Invoke-TestGroupSetupBlocks + Add-SetupAndTeardown -ScriptBlock $Fixture + Invoke-TestGroupSetupBlocks - do + do + { + $null = & $Fixture + } until ($true) + } + finally { - $null = & $Fixture - } until ($true) + Invoke-TestGroupTeardownBlocks + if (-not $NoTestDrive) + { + if ($testDriveAdded) + { + Remove-TestDrive + } + else + { + Clear-TestDrive -Exclude ($TestDriveContent | & $SafeCommands['Select-Object'] -ExpandProperty FullName) + } + } + } } catch { @@ -170,21 +188,6 @@ function DescribeImpl { & $TestOutputBlock $Pester.TestResult[-1] } } - finally - { - Invoke-TestGroupTeardownBlocks - if (-not $NoTestDrive) - { - if ($testDriveAdded) - { - Remove-TestDrive - } - else - { - Clear-TestDrive -Exclude ($TestDriveContent | & $SafeCommands['Select-Object'] -ExpandProperty FullName) - } - } - } Exit-MockScope diff --git a/Functions/TestsRunningInCleanRunspace.Tests.ps1 b/Functions/TestsRunningInCleanRunspace.Tests.ps1 index 4db531886..073185af2 100644 --- a/Functions/TestsRunningInCleanRunspace.Tests.ps1 +++ b/Functions/TestsRunningInCleanRunspace.Tests.ps1 @@ -174,4 +174,34 @@ Describe 'Guarantee It fail on setup or teardown fail (running in clean runspace $result.FailedCount | Should Be 1 $result.TestResult[0].FailureMessage | Should Be "test exception" } + + Context 'Teardown fails' { + It "Failed teardown does not let exception propagate outside of the scope of Describe/Context in which it failed" { + $testSuite = { + $teardownFailure = $null + + try { + Context 'This is a test context' { + AfterAll { + throw 'I throw in Afterall' + } + } + } + catch { + $teardownFailure = $_ + } + It "Failed teardown does not let exception propagate outside of the scope of Describe/Context in which it failed" { + # issue #584, #662 + $teardownFailure | Should -BeNullOrEmpty + } + } + $result = Invoke-PesterInJob -ScriptBlock $testSuite + + # the second test should pass because correctly the exception does not propagate + $result.PassedCount | Should -Be 1 + + # the first test should fail because after all throws + $result.FailedCount | Should -Be 1 + } + } }