Skip to content

Add -Aggregate switch to Mock for pipeline aggregation - #2947

Merged
nohwnd merged 5 commits into
mainfrom
nohwnd-mock-end-block-2154
Nov 26, 2026
Merged

nohwnd merged 5 commits into
mainfrom
nohwnd-mock-end-block-2154

Conversation

@nohwnd

@nohwnd nohwnd commented Jul 31, 2026 •

Copy link
Copy Markdown
Member

Fix #2154

Mocking a command that takes pipeline input and returns once from its end block never worked. MockWith runs once per piped item, so a command that aggregates the pipeline and returns a single object returns one object per input instead, and Should -Invoke counts one call per item.

function outer { 1..3 | inner }
function inner {
    param ([Parameter(ValueFromPipeline)] $InputObject)
    "RETURN"   # implicit end block, runs once
}

Mock inner { "FOO" }
@(outer).Count   # 3, should be 1

The change

Add -Aggregate to Mock. With it the process block only collects the pipeline, and MockWith runs once from the end block over all the input.

Mock inner { "FOO" } -Aggregate
@(outer).Count   # 1

The collected input is piped into MockWith, so it is there as $input and MockWith can have its own begin/process/end blocks, just like the real command.

Mock Save-State { "saved $(@($input).Count) items" } -Aggregate
1..3 | Save-State   # "saved 3 items"

Should -Invoke counts one call per pipeline, and the parameter filter sees the whole collected input bound to the pipeline parameter, so you can assert on all of it at once.

Mock inner { "FOO" } -Aggregate
$null = outer
Should -Invoke inner -Times 1 -Exactly
Should -Invoke inner -Times 1 -Exactly -ParameterFilter { $InputObject -contains 2 }

Design

The idea: one mock type per command.

  • A per-call mock (today) runs MockWith per item and records one call per item.
  • An -Aggregate mock collects the pipeline and records one call per pipeline.

All mocks for a single command must be the same type. Mixing the two would put two different granularities in the call history and Should -Invoke could not count it coherently, so it is rejected at definition time. That is what keeps Should -Invoke a plain counter, it does not have to know about the two types, it just counts the history entries. Per-call mocks behave exactly as before, -Aggregate is purely additive.

Because the type carries the granularity, the filter naturally sees the aggregate for an -Aggregate mock, so there is no separate -AggregateParameterFilter.

The switch is named -Aggregate and not -End because MockWith can already have its own begin, process and end blocks, so "End" would name the wrong thing.

Known limit

Piping to a mocked native application has no ValueFromPipeline parameter, so nothing binds by name and you cannot assert on the input by parameter name. The input is still collected and piped into MockWith.

The homogeneity check runs in the current scope only, so a parent-scope -Aggregate mock and a child-scope per-call mock for the same command slip past it. There is no runtime conflict, the effect is a confusing count under Should -Invoke -Scope Describe, and the invocation history in the failure message shows the aggregated call with an array value next to the individual ones.

Verification

262 existing Mock tests still pass, plus 16 in Mock.Aggregate.Tests.ps1 covering output shape, per-pipeline counting, the filter over the aggregate, -Verifiable, module scope and the homogeneity guard. Full gate on current main: 3060 passed, 0 failed, 3 skipped.

🤖

Mocking a command that consumes the pipeline and returns once from its end
block did not work. The mock runs MockWith once per piped item, so a command
that returns a single object returned one object per input instead, and
Should -Invoke counted one call per item.

Add -End to Mock. With it the process block only collects the pipeline, and
MockWith runs once from the end block over all the input. The collected input
is piped into MockWith, so it is available as $input and MockWith can have its
own begin/process/end blocks. Should -Invoke counts one call per pipeline, and
the parameter filter sees the whole collected input bound to the pipeline
parameter, so you can assert on all of it at once.

All mocks for one command must be the same type. Mixing an -End mock and a
per-call mock for the same command would put two different granularities in the
call history and Should -Invoke could not count it, so it is rejected at
definition time. This keeps the change additive, per-call mocks behave exactly
as before.
Comment thread src/functions/Mock.ps1 Resolved
@fflaten

fflaten commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

This is great! 🥳 Maybe even default behavior some day.

  • Name. I went with -End because that is what we said, but it reads a bit odd as a bare
    switch (Mock inner { } -End). -Aggregate / -PipelineEnd / -OnEnd are alternatives.

Yeah. I think even mentioning End is wrong when users can do { begin {} process {} end {} }. It's closer to -WithPipeline/AsPipeline/Advanced/AdvancedFunction even though it's not 1:1. -Aggregate best of the above.

  • Homogeneity is checked in the current scope only. A parent-scope -End mock and a
    child-scope per-call mock for the same command would slip through the definition-time check.
    Do we care, or add a resolution-time guard too?

No need if there's no runtime conflict in my opinion. Looks like the biggest side effect of mixing modes at different levels is confusing invocation count when using e.g. Should -Invoke -Scope Describe. The new Should -Invoke error with invocation history shows -End call with array value vs individual for normal, so I think users will figure it out.

  • Native commands. Piping to a mocked application (e.g. pwsh, see Can't mock a function that takes pipeline input #2154 powercode comment)
    has no ValueFromPipeline parameter, so nothing binds by name for the filter. The input is
    still collected and piped into MockWith, but you cannot assert on it by parameter name.
    Probably fine for v1, but worth calling out.

👍

  • Should we fold the tests into Mock.Tests.ps1 instead of a separate file?

Separate - don't see any benefits of merging. Mock.Tests.ps1 is already massive.

Comment thread src/functions/Mock.ps1 Resolved
-End names the wrong thing: MockWith can already have its own begin, process
and end blocks, so "End" says nothing about what the switch does. -Aggregate
says it collects the pipeline and runs once over all of it. Frode picked this
one in #2947.

The internal behavior property was already called Aggregate, so only the
public switch and the help change. Test file renamed to match.

🤖
@nohwnd nohwnd changed the title Add -End switch to Mock for pipeline aggregation Add -Aggregate switch to Mock for pipeline aggregation Nov 26, 2026
@nohwnd
nohwnd marked this pull request as ready for review November 26, 2026 16:40
The new -Aggregate error path copied the ForEach-Object that indents the list of
parameter filters that did not match. Use a foreach loop, which is what the
Measure-ObjectCmdlets rule asks for.

New-MockBehavior declared -RemoveParameterType and -RemoveParameterValidation and
used neither. Its only caller does not pass them either: Create-MockHook and
Repair-ConflictingParameters take those names themselves. Dropped both.

🤖
@nohwnd
nohwnd enabled auto-merge (squash) November 26, 2026 20:53
@nohwnd
nohwnd merged commit d59a2c9 into main Nov 26, 2026
14 checks passed
@nohwnd
nohwnd deleted the nohwnd-mock-end-block-2154 branch November 26, 2026 21:05
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.

Can't mock a function that takes pipeline input

3 participants