Repository navigation
Keep New-MockObject properties when the mock is passed through Select-Object - #3093
Open
dirtyvanity wants to merge 4 commits into
Open
dirtyvanity wants to merge 4 commits into
dirtyvanity wants to merge 4 commits into
Conversation
Properties added with $mock.PSObject.Properties.Add() were lost when the mock was a property value expanded with Select-Object -ExpandProperty. Add them with Add-Member -NotePropertyMembers -PassThru and keep the returned object instead. One call adds all properties, so it is no slower. 🤖
The old code passed each value through a .NET method, which unwraps its PSObject. Select-Object -ExpandProperty then wraps the bare value in a new PSObject that stores members locally and so doesn't see the ones added earlier. 🤖
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3093 +/- ##
=======================================
Coverage ? 91.84%
=======================================
Files ? 2
Lines ? 8656
Branches ? 0
=======================================
Hits ? 7950
Misses ? 706
Partials ? 0
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
Add-Member reports a failed add as a non-terminating error, so a property that can't be added (for example Process.Name, an alias from the type data) made New-MockObject return $null instead of failing the test as before. -ErrorAction Stop restores the failure. A nested mock with only -Methods still lost its methods through Select-Object -ExpandProperty, as it did before this change; the mock is now returned wrapped either way. Tests: a failed add throws; nested mocks in an array and a methods-only nested mock survive expansion; the first test is renamed, since it passed before too. 🤖
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.
PR Summary
New-MockObject -Propertiesadded its note properties with$mock.PSObject.Properties.Add(). Those members are lost when the mock is a property value expanded withSelect-Object -ExpandProperty, so the issue's nested case returned$null:The cause: each value went through
[Pester.Factory]::CreateNoteProperty(), and passing a value to a .NET method unwraps itsPSObject.Select-Object -ExpandPropertythen wraps the bare value withPSObject.AsPSObject(value, storeTypeNameAndInstanceMembersLocally: true)(Select-Object.cs), and that new wrapper doesn't see members added to the object earlier.As suggested in the issue, this now uses
Add-Member(via$SafeCommands). As fflaten found,Add-Member -Forcealone isn't enough: the mock has to be the object-PassThrureturns, so it's kept as aPSObject. All properties are added in one call with-NotePropertyMembers, with-ErrorAction Stopso a property that can't be added (for exampleProcess.Name, an alias from the type data) still fails the test as before. A mock with only-Methodsis wrapped too, so a nested one keeps its methods.Speed (pwsh 7.6, Linux, per call, two runs): about the same with 1 property (27–43 µs before, 29–36 µs after), the same or faster with 10, and faster with 50 (115–130 µs before, 69–84 µs after).
Fix #2393
PR Checklist
Tests added to
tst/functions/New-MockObject.Tests.ps1: a nested mock's property, nested mocks in an array, and a methods-only nested mock throughSelect-Object -ExpandProperty(these three fail onmain); a property that can't be added throws; an empty-Properties @{}; replacing an existing property; a typed mock keeps its type and methods.Tested on PowerShell 7.4 and 7.6 (Linux). I couldn't run Windows PowerShell 5.1 locally.
🤖