From 8c1e8cbfd7e9982bcd71d8613bc68bd607eae514 Mon Sep 17 00:00:00 2001 From: Jakub Jares Date: Fri, 3 Apr 2026 11:42:04 +0200 Subject: [PATCH] Fix #2628: Add -LiteralPath switch to Should -Exist Keep default wildcard behavior (Test-Path) and add optional -LiteralPath switch that uses Test-Path -LiteralPath for paths with special characters. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/functions/assertions/Exist.ps1 | 9 +++++++-- tst/functions/assertions/Exist.Tests.ps1 | 23 +++++++++++++++++------ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/functions/assertions/Exist.ps1 b/src/functions/assertions/Exist.ps1 index e6b0f58c8..f680075de 100644 --- a/src/functions/assertions/Exist.ps1 +++ b/src/functions/assertions/Exist.ps1 @@ -1,4 +1,4 @@ -function Should-ExistAssertion($ActualValue, [switch] $Negate, [string] $Because) { +function Should-ExistAssertion($ActualValue, [switch] $Negate, [string] $Because, [switch] $LiteralPath) { <# .SYNOPSIS Does not perform any comparison, but checks if the object calling Exist is present in a PS Provider. @@ -12,7 +12,12 @@ `Should -Exist` calls Test-Path. Test-Path expects a file, returns $false because the file was removed, and fails the test. #> - [bool] $succeeded = & $SafeCommands['Test-Path'] $ActualValue + if ($LiteralPath) { + [bool] $succeeded = & $SafeCommands['Test-Path'] -LiteralPath $ActualValue + } + else { + [bool] $succeeded = & $SafeCommands['Test-Path'] $ActualValue + } if ($Negate) { $succeeded = -not $succeeded diff --git a/tst/functions/assertions/Exist.Tests.ps1 b/tst/functions/assertions/Exist.Tests.ps1 index 25e57f637..a4a032ec0 100644 --- a/tst/functions/assertions/Exist.Tests.ps1 +++ b/tst/functions/assertions/Exist.Tests.ps1 @@ -16,6 +16,11 @@ InPesterModuleScope { "TestDrive:\``[test``].txt" | Should -Exist } + It 'matches wildcard patterns by default' { + New-Item -Path "TestDrive:\wildcard1.txt" -ItemType File | Out-Null + "TestDrive:\wild*.txt" | Should -Exist + } + It 'returns correct result for function drive' { function f1 { } @@ -29,18 +34,24 @@ InPesterModuleScope { 'env:test' | Should -Exist } - It 'returns correct result for env drive' { - $env:test = 'somevalue' - - 'env:test' | Should -Exist - } - It 'returns correct assertion message' { $err = { 'c:\nonexistingpath' | Should -Exist -Because 'reason' } | Verify-AssertionFailed $err.Exception.Message | Verify-Equal "Expected path 'c:\nonexistingpath' to exist, because reason, but it did not exist." } } + Describe "Should -Exist -LiteralPath" { + It 'works for path with literal [ ] characters' { + New-Item -Path "TestDrive:\[literal].txt" -ItemType File | Out-Null + "TestDrive:\[literal].txt" | Should -Exist -LiteralPath + } + + It 'does not match wildcard patterns' { + New-Item -Path "TestDrive:\nowild.txt" -ItemType File | Out-Null + "TestDrive:\no*.txt" | Should -Not -Exist -LiteralPath + } + } + Describe "Should -Not -Exist" { It 'returns correct assertion message' { $currentPath = $pwd.Path