Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/functions/assertions/Exist.ps1
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand Down
23 changes: 17 additions & 6 deletions tst/functions/assertions/Exist.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}
Expand All @@ -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
Expand Down
Loading