Pester icon indicating copy to clipboard operation
Pester copied to clipboard

`Should` throws exception for legacy syntax when parameters are broken across lines

Open snow-jlove opened this issue 3 years ago • 6 comments
trafficstars

General summary of the issue

When Should parameters (with dashes!) are split across lines, a runtime exception gets thrown, stating "RuntimeException: Legacy Should syntax (without dashes) is not supported in Pester 5. Please refer to migration guide at: https://pester.dev/docs/migrations/v3-to-v4".

Describe your environment

Pester version     : 5.3.1 C:\Users\jlove\Documents\WindowsPowerShell\Modules\Pester\5.3.1\Pester.psm1
PowerShell version : 5.1.19041.1320
OS version         : Microsoft Windows NT 10.0.19044.0

Steps to reproduce

Describe ShouldLegacySyntax {
    It "does not complain about syntax" {
        1 + 1 | Should -Be 2
    }

    It "complains about syntax" {
        1 + 1 | Should `
            -Be 2
    }
}

Expected Behavior

Both of the above tests should be treated equally and pass.

Current Behavior

The second above test fails with the syntax exception.

Possible Solution? (optional)

snow-jlove avatar Feb 09 '22 23:02 snow-jlove

I ran into this issue when I tried splitting a long command across lines. Consider this line - when indented, it overran my editor's vertical ruler (120 chars):

        Should -CommandName Install-Module -Exactly -Invoke -ModuleName $ModuleName -ParameterFilter $ParameterFilter1 -Times 2

# gets formatted to...

        Should `
            -CommandName Install-Module `
            -Exactly `
            -Invoke `
            -ModuleName $ModuleName `
            -ParameterFilter $ParameterFilter1 `
            -Times 2

snow-jlove avatar Feb 09 '22 23:02 snow-jlove

Yikes, that rule is nothing but trouble. :/

nohwnd avatar Apr 22 '22 19:04 nohwnd

Not parameters in general, just the operator (Invoke\Be...). I'd recommend always using the operator as your first parameter for readability. As a bonus that would let you keep it on the first line and avoid this issue. Try:

Should -Invoke `
    -CommandName Install-Module `
    -Exactly `
    -ModuleName $ModuleName `
    -ParameterFilter $ParameterFilter1 `
    -Times 2

fflaten avatar May 08 '22 16:05 fflaten

Upvote on what Frode said, that makes it way easier to understand what that Should does and avoids the issue as well.

nohwnd avatar Jun 30 '22 07:06 nohwnd

@nohwnd That seems like a bad workaround, it's much cleaner to only have a single parameter per line and either all on newlines or none.

A proper fix would be great, I don't this issue is 'solved', until the next version that removes them (and the messages) entirely lands.

ElvenSpellmaker avatar Jul 12 '22 13:07 ElvenSpellmaker

Ok let's just remove the warning.

nohwnd avatar Jul 13 '22 10:07 nohwnd