From 2d6418b4fb70fc65c27af4a496c0e60bffa2a79c Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Tue, 21 May 2024 21:39:52 +0200 Subject: [PATCH] Fix NRE in mock debug log for native commands (#2480) * Fix NRE in mock debug log for native commands * Exclude for commands without parameters --- src/functions/Mock.ps1 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/functions/Mock.ps1 b/src/functions/Mock.ps1 index fde382c4e..ab4c3df8e 100644 --- a/src/functions/Mock.ps1 +++ b/src/functions/Mock.ps1 @@ -411,7 +411,10 @@ function Should-InvokeInternal { $nonMatchingCalls = [System.Collections.Generic.List[object]]@() # Check for variables in ParameterFilter that already exists in session. Risk of conflict - if ($PesterPreference.Debug.WriteDebugMessages.Value) { + # Excluding native applications as they don't have parameters or metadata. Will always use $args + if ($PesterPreference.Debug.WriteDebugMessages.Value -and + $null -ne $ContextInfo.Hook.Metadata -and + $ContextInfo.Hook.Metadata.Parameters.Count -gt 0) { $preExistingFilterVariables = @{} foreach ($v in $filter.Ast.FindAll( { $args[0] -is [System.Management.Automation.Language.VariableExpressionAst] }, $true)) { if (-not $preExistingFilterVariables.ContainsKey($v.VariablePath.UserPath)) {