Repository navigation
Add -Aggregate switch to Mock for pipeline aggregation - #2947
Merged
Merged
Conversation
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.
Contributor
|
This is great! 🥳 Maybe even default behavior some day.
Yeah. I think even mentioning
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.
👍
Separate - don't see any benefits of merging. |
-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
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. 🤖
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix #2154
Mocking a command that takes pipeline input and returns once from its end block never worked.
MockWithruns once per piped item, so a command that aggregates the pipeline and returns a single object returns one object per input instead, andShould -Invokecounts one call per item.The change
Add
-AggregatetoMock. With it the process block only collects the pipeline, andMockWithruns once from the end block over all the input.Mock inner { "FOO" } -Aggregate @(outer).Count # 1The collected input is piped into
MockWith, so it is there as$inputandMockWithcan have its ownbegin/process/endblocks, just like the real command.Should -Invokecounts 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.
MockWithper item and records one call per item.-Aggregatemock 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 -Invokecould not count it coherently, so it is rejected at definition time. That is what keepsShould -Invokea 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,-Aggregateis purely additive.Because the type carries the granularity, the filter naturally sees the aggregate for an
-Aggregatemock, so there is no separate-AggregateParameterFilter.The switch is named
-Aggregateand not-EndbecauseMockWithcan already have its ownbegin,processandendblocks, so "End" would name the wrong thing.Known limit
Piping to a mocked native application has no
ValueFromPipelineparameter, so nothing binds by name and you cannot assert on the input by parameter name. The input is still collected and piped intoMockWith.The homogeneity check runs in the current scope only, so a parent-scope
-Aggregatemock 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 underShould -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.ps1covering 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.🤖