4

I'm trying to create a wrapper (proxy) for Pester's Should cmdlet. Possible use cases include transparent logging of test input even in case of success and improve the way Pester logs objects of certain types, e. g. hashtable.

As Should is an advanced function, forwarding arguments via $args splatting does not work.

So I tried to generate a wrapper using System.Management.Automation.ProxyCommand::Create(), as described by this answer:

$cmd = Get-Command Should
$wrapperSource = [System.Management.Automation.ProxyCommand]::Create( $cmd )
$wrapperSource >should_wrapper.ps1

When calling the wrapper, Powershell outputs this error message:

Should: Parameter set cannot be resolved using the specified named parameters. One or more parameters issued cannot be used together or an insufficient number of parameters were provided.

It looks like the wrapper generator doesn't understand the dynamicparam declaration of Should.

How to write a generic wrapper for Pester's Should without duplicating Pester code?

zett42
  • 25,437
  • 3
  • 35
  • 72

1 Answers1

5

It looks like the wrapper generator doesn't understand the dynamicparam declaration of Should.

The wrapper generator omits dynamicparam by default. Fortunately, this is easily fixed with a bit of templating:

$cmd = Get-Command Should
$pct = [System.Management.Automation.ProxyCommand]
$wrapperSource = @(
  $pct::GetCmdletBindingAttribute($cmd)
  'param('
    $pct::GetParamBlock($cmd)
  ')'
  'dynamicparam {'
    $pct::GetDynamicParam($cmd)
  '}'
  'begin {'
    $pct::GetBegin($cmd)
  '}'
  'process {'
    $pct::GetProcess($cmd)
  '}'
  'end {'
    $pct::GetEnd($cmd)
  '}'
) -join [Environment]::NewLine
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • There is a typo, it should be `param ( ... )`. After fixing that, when calling the wrapper like `0 | .\shouldWrapper.ps1 -Be 1` I get this error message: _"Should: Parameter set cannot be resolved using the specified named parameters. One or more parameters issued cannot be used together or an insufficient number of parameters were provided."_ – zett42 Jan 08 '21 at 11:54
  • Odd. What version of Pester are you using? It works for me with Pester 3.4 in Windows PowerShell 5.1 and with Pester 5 on PowerShell 7.1 – Mathias R. Jessen Jan 08 '21 at 12:29
  • It failed with Pester 5.0.4 but works with Pester 5.1.1 on both PS versions. – zett42 Jan 08 '21 at 12:52
  • 2
    There is an [overload of `ProxyCommand.Create()`](https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.proxycommand.create?view=powershellsdk-7.0.0#System_Management_Automation_ProxyCommand_Create_System_Management_Automation_CommandMetadata_System_String_System_Boolean_) that has a boolean parameter `generateDynamicParameters`, but doesn't appear to have any effect. Possibly a bug? – zett42 Jan 08 '21 at 13:22
  • 1
    That's my impression, I've never gotten that overload to work - hence the stichwork above :-) – Mathias R. Jessen Jan 08 '21 at 13:50
  • 2
    Nicely done. It is indeed a bug, and it's worth noting that it only affects (advanced) _functions and scripts_, not compiled cmdlets - see [GitHub issue #4792](https://github.com/PowerShell/PowerShell/issues/4792); I've added a comment there to point to the workaround here. /cc @zett42 – mklement0 Jan 08 '21 at 15:11
  • 1
    @zett42, you're right, the bug is still there, and the report should not have been closed. I've left a comment to that effect, with a simpler repro (the original repro was a bit unclear). – mklement0 Dec 23 '22 at 19:48