Skip to content
Closed
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
13 changes: 13 additions & 0 deletions src/Format2.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ function Format-String2 ($Value) {
return '<empty>'
}

# Escape control characters so they are visible in error messages.
# Without this, chars like ESC (0x1B used in ANSI sequences) are invisible
# and make error output confusing. See https://github.com/pester/Pester/issues/2561
$Value = $Value `
-replace "`0", '␀' `
-replace "`a", '␇' `
-replace "`b", '␈' `
-replace "`t", '␉' `
-replace "`f", '␌' `
-replace "`r", '␍' `
-replace "`n", '␊' `
-replace "`e", '␛'

"'$Value'"
}

Expand Down
9 changes: 9 additions & 0 deletions src/functions/assert/General/Should-BeNull.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@

$collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput -UnrollInput
$Actual = $collectedInput.Actual

# When a function returns no output and the result is piped to Should-BeNull,
# PowerShell sends an empty array @() through the pipeline. Treat empty pipeline
# input as $null since "no output" is effectively null.
# See https://github.com/pester/Pester/issues/2555
if ($collectedInput.IsPipelineInput -and $Actual -is [array] -and $Actual.Count -eq 0) {
$Actual = $null
}

if ($null -ne $Actual) {
$Message = Get-AssertionMessage -Expected $null -Actual $Actual -Because $Because -DefaultMessage "Expected `$null,<because> but got <actualType> '<actual>'."
throw [Pester.Factory]::CreateShouldErrorRecord($Message, $MyInvocation.ScriptName, $MyInvocation.ScriptLineNumber, $MyInvocation.Line.TrimEnd([System.Environment]::NewLine), $true)
Expand Down
11 changes: 9 additions & 2 deletions tst/functions/assert/General/Should-BeNull.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ Describe "Should-BeNull" {
{ 1 | Should-BeNull } | Verify-AssertionFailed
}

It "Given empty array it fails" {
{ @() | Should-BeNull } | Verify-AssertionFailed
It "Given empty array piped it passes (void function output is empty array)" {
# When a function returns no output, PowerShell sends @() through the pipeline.
# Should-BeNull treats this as $null since "no output" is effectively null.
# See https://github.com/pester/Pester/issues/2555
@() | Should-BeNull
}

It "Given empty array by parameter it fails" {
{ Should-BeNull -Actual @() } | Verify-AssertionFailed
}

It "Returns the given value" {
Expand Down
Loading