Pester icon indicating copy to clipboard operation
Pester copied to clipboard

Pester crashes due to native command output in `BeforeAll`

Open SteveL-MSFT opened this issue 5 months ago • 2 comments

Checklist

What is the issue?

Within BeforeAll{} if running a native command and it emits output to STDOUT, it can cause Pester to have an unhandled exception later.

Expected Behavior

Test should run and Pester should not have unhandled exception

Steps To Reproduce

In this test script within the BeforeAll{} it runs winrm quickconfig -quiet -force, despite -quiet, the command runs net start winrm which itself emits that WinRM service is already running on this machine. to STDOUT. This is captured by Pester and during execution, at this line where it's adding a "container", $i happens to be the output string from above and Pester fails with:

System.Management.Automation.MethodException: Cannot find an overload for "Add" and the argument count: "1".
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at Invoke-Pester<End>, C:\Users\slee\OneDrive - Microsoft\Documents\PowerShell\Modules\pester\5.7.1\Pester.psm1: line 4985
at <ScriptBlock>, <No file>: line 1

Fix in the test was to redirect the output to $null, but this wasn't obvious and was working with previous version of Pester 5.

Describe your environment

This is with Pester 5.7.1

Possible Solution?

In the test, redirect the native command output to $null

SteveL-MSFT avatar Jul 29 '25 20:07 SteveL-MSFT

I don't see it repro, I made this test: https://github.com/pester/Pester/pull/2656/files

And I see different output than you, but I also see the test pass.

Image

nohwnd avatar Jul 30 '25 07:07 nohwnd

https://github.com/pester/Pester/blob/main/src/Pester.Runtime.ps1#L1619-L1624 this is where we should be capturing the output that leaves the scriptblock and does not go directly to screen.

also writing a console app like this:

using System.Diagnostics;

if (args.Length > 0 && args.Contains("child"))
{
    var process = Process.GetCurrentProcess().MainModule.FileName;

    Process.Start(process, args.Contains("err") ? "err" : "").WaitForExit();
    return;
}


if (args.Length > 0 && args.Contains("err"))
{
    Console.Error.WriteLine("Error: output.");
    return;
}

Console.WriteLine("Hello, World!");

And running it with standard output, standard output from child process, err output , and err output from child process I don't see this repro in either case.

I am on pester 5.7.1.

Describe 'd' {
    BeforeAll {
        & S:\source\ConsoleApp22\ConsoleApp22\bin\Debug\net9.0\ConsoleApp22.exe  err
        
    }

    It 'i' {
        1 | Should -Be 1
    }
}

nohwnd avatar Jul 30 '25 07:07 nohwnd