Repository navigation
fix: the recorded command no longer renders an object through its own ToString() - #2780
Conversation
… ToString()
Invoke-Maester records the command that produced a run, and that string is stored
in the result file and shown in the report. Every bound parameter that was not a
switch, array or string went through string interpolation, which calls the
object's own ToString(). A type supplied by the caller decides what that returns,
so -Config with an object whose ToString() reports its contents put those contents
in the recorded command:
class Leaky { [string] $Secret = 'p@ssw0rd-123'; [string] ToString() { "Config(ApiKey=$($this.Secret))" } }
Invoke-Maester -Config Config(ApiKey=p@ssw0rd-123)
The array branch had the same exposure: -join calls ToString() on every element.
Rendering now lives in ConvertTo-MtCommandParameterValue, one function per file
next to ConvertFrom-MtJsonArray. Strings stay quoted and value types are written
as they are, so nothing readable is lost; any other object is recorded as its type
name in angle brackets. Invoke-Maester's loop drops from four branches to two.
Raised by @SamErde while reviewing maester365#2635, where -Config made it reachable. The
defect is older than that PR and is on main, which is why this is separate.
Verified with the repo's runner on its pinned Pester: 8 of 8 for the new tests,
and the full suite at 8,516 passing. Removing the protection fails exactly the two
cases that assert the type name instead of the content, so the tests discriminate.
|
This pull request changes files that a website explainer describes:
If the change alters what the explainer shows (a gate, a reason code, a command, a count), update its composition and its chapters in |
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Up to standards ✅🟢 Issues
|
merill
left a comment
There was a problem hiding this comment.
Thanks @earbona23, nice clean split from #2635. Approving.
Optional nit, which predates this PR: a single quote in a string value isn't escaped, so -Path "C:\it's" records as 'C:\it's' and won't paste back. Doubling it ($Value -replace "'", "''") in both the scalar and array branches would fix that. Fine to leave for another time.
Separate PR for the
ToString()exposure @SamErde raised while reviewing #2635, as offered there. The defect is onmainand predates that PR;-Configjust made it reachable with an object the caller controls.What happens today
Invoke-Maesterrecords the command that produced a run. That string is stored in the result file and shown in the report. Every bound parameter that is not a switch, array or string goes through string interpolation, which calls the object's ownToString():The object decides what that returns. Reproduced:
The array branch had the same exposure for a different reason —
-joincallsToString()on every element.What this changes
Rendering moves to
ConvertTo-MtCommandParameterValue, one function per file next toConvertFrom-MtJsonArray:'Entra''Entra'42,$true(value types)42,True@('Entra','Teams')@('Entra', 'Teams')<TypeName>$nullNothing readable is lost: strings and value types are every parameter type that currently produces useful output.
Invoke-Maester's loop drops from four branches to two.Verification
Run through the repository's own runner on its pinned Pester, not a direct
Invoke-Pester:The tests discriminate rather than passing for free — with the protection removed, exactly the two cases that assert the type name instead of the content fail:
@SamErde — this is the one I said I would send. Happy to fold it into #2635 instead if you would rather have it there.
Summary by CodeRabbit