Pester icon indicating copy to clipboard operation
Pester copied to clipboard

CRTL-C during a test keeps AfterAll from executing

Open JamesWTruher opened this issue 9 years ago • 4 comments

I should think that AfterAll is also executed

Describe 'before all after all' {
    BeforeAll{
        Write-Verbose "in BeforeAll" -Verbose
        
    }
    it "it" {
        Start-Sleep -Seconds 2

    }
    AfterAll {
        Write-Verbose "in AfterAll" -Verbose
        
    }
}

Example hitting ctl-c during it:

Describing before all after all VERBOSE: in BeforeAll VERBOSE: in it Note: After all was not run

Example of full run

Describing before all after all VERBOSE: in BeforeAll VERBOSE: in it [+] it 2.13s VERBOSE: in AfterAll

JamesWTruher avatar Dec 06 '16 21:12 JamesWTruher

How would you suggest we fix that? :) We already use try/finally to execute the AfterAll blocks; I don't know of a way to make PowerShell code any more "guaranteed" than with a finally block. If the PowerShell engine isn't executing those after Control+C is pressed, what more can we do?

dlwyatt avatar Dec 06 '16 22:12 dlwyatt

could something like this be implemented? http://sushihangover.blogspot.com/2012/03/powershell-using-try-finally-block-to.html

I think I could add the [console]::TreatControlCAsInput lines to the try/catch in Descripe.ps1. I just don't know where would be a good place to poll for Ctrl-C input.

eweilnau avatar Dec 07 '16 17:12 eweilnau

Normally, Ctrl+C wouldn't blow past finally, right? Try stopping this loop:

& {
    try {
        foreach($i in 1..100){ Sleep -Milli 500; Write-Host $i }
    }
    catch {
        Write-Warning "Catch"
    }
    finally {
        Write-Warning "Finally"
    }
}

Jaykul avatar Dec 07 '16 21:12 Jaykul

Ah, I see the problem now. Fix coming shortly.

dlwyatt avatar Dec 08 '16 04:12 dlwyatt