Skip to content

Collect-Input: empty pipeline unwraps to $null for value assertions#2708

Merged
nohwnd merged 2 commits into
pester:mainfrom
nohwnd:fix-collect-input-empty
Jun 11, 2026
Merged

Collect-Input: empty pipeline unwraps to $null for value assertions#2708
nohwnd merged 2 commits into
pester:mainfrom
nohwnd:fix-collect-input-empty

Conversation

@nohwnd

@nohwnd nohwnd commented Jun 11, 2026

Copy link
Copy Markdown
Member

docs/assertion-types.md says a value assertion should treat @() from the pipeline the same as $null (since there is no scalar to unwrap to). The existing TODO on line 46 flagged that we did not actually match that — Collect-Input returned @() for both value and collection assertions, which made these fail:

@() | Should-Be -Expected $null
@() | Should-BeNull

and made this wrongly pass:

@() | Should-NotBeNull

Collect-Input now returns $null when -UnrollInput is set and the pipeline collected no items. Collection assertions are unchanged: they still see @().

Updated the Should-BeNull test that previously asserted the buggy "fails" behavior to assert the documented "passes" behavior, and added matching coverage on Should-Be, Should-NotBeNull, and Collect-Input itself. Dropped the TODO and a stray code fence in assertion-types.md.

Verified with the full Pester suite: 2174 passed, 0 failed.

Follow-up to #2707.

docs/assertion-types.md says a value assertion should treat @() from the
pipeline the same as $null (since there is no scalar to unwrap to), and
the existing TODO on line 46 flagged that we did not match that. We
returned @() for both value and collection assertions, which made these
fail:

    @() | Should-Be -Expected $null
    @() | Should-BeNull

and made this wrongly pass:

    @() | Should-NotBeNull

Collect-Input now returns $null when -UnrollInput is set and the
pipeline collected no items. Collection assertions are unchanged: they
still see @().

Updated Should-BeNull test that previously asserted the buggy "fails"
behavior to assert the documented "passes" behavior, and added matching
coverage on Should-Be, Should-NotBeNull, and Collect-Input itself.
Dropped the TODO and the stray code fence in assertion-types.md.

Verified with the full Pester suite: 2174 passed, 0 failed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment thread docs/assertion-types.md
Answering the review question on pester#2708: clarify and lock how the value
assertions handle arrays containing $null.

A single-item pipeline of $null (whether produced by $null, @(\),
,\, or @(@(\))) is what PowerShell actually puts on the
pipeline: one $null item. With -UnrollInput this collapses to the
scalar $null, so all four pass Should-BeNull / Should-Be -Expected \
and fail Should-NotBeNull. A multi-item pipeline (e.g. @(\, \)
or 1, \) is a collection of two items, stays as a collection after
unrolling, and is therefore not equal to \.

Adds tests for Should-Be, Should-BeNull, Should-NotBeNull, and the
underlying Collect-Input to make this explicit.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@nohwnd nohwnd enabled auto-merge (squash) June 11, 2026 16:28
@nohwnd nohwnd merged commit 840c44f into pester:main Jun 11, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant