Skip to content

Commit

Permalink
Exclude for commands without parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
fflaten committed May 21, 2024
1 parent 46929f7 commit 0451f6e
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/functions/Mock.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,9 @@ function Should-InvokeInternal {

# Check for variables in ParameterFilter that already exists in session. Risk of conflict
# 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) {
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)) {
Expand All @@ -423,7 +425,7 @@ function Should-InvokeInternal {
}

# Check against parameters and aliases in mocked command as it may cause false positives
if ($preExistingFilterVariables.Count -gt 0 -and $null -ne $ContextInfo.Hook.Metadata.Parameters) {
if ($preExistingFilterVariables.Count -gt 0) {
foreach ($p in $ContextInfo.Hook.Metadata.Parameters.GetEnumerator()) {
if ($preExistingFilterVariables.ContainsKey($p.Key)) {
Write-PesterDebugMessage -Scope Mock -Message "! Variable `$$($p.Key) with value '$($preExistingFilterVariables[$p.Key])' exists in current scope and matches a parameter in $CommandName which may cause false matches in ParameterFilter. Consider renaming the existing variable or use `$PesterBoundParameters.$($p.Key) in ParameterFilter."
Expand Down

0 comments on commit 0451f6e

Please sign in to comment.