Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should -HaveParameter -DefaultValue does not work for boolean types or integer value of 0 (regression) #2388

Closed
3 tasks done
phoerr opened this issue Aug 30, 2023 · 2 comments · Fixed by #2398
Closed
3 tasks done
Assignees
Labels

Comments

@phoerr
Copy link
Contributor

phoerr commented Aug 30, 2023

Checklist

What is the issue?

Pester version 5.4.1 has a regression from 5.4.0 where Should -HaveParameter -DefaultValue no longer works for the following kinds of tests that previously passed:

  • Function parameter of type [int] with a default value of 0
  • Function parameter of type [bool] with any default value set (both $true and $false)

Sample test

Describe 'Parameter default value tests' {
    BeforeAll {
        function Test-Parameter {
            param (
                [Parameter()]
                [int]$intParam = 0,

                [Parameter()]
                [bool]$boolTrueParam = $true,

                [Parameter()]
                [bool]$boolFalseParam = $false
            )
        }
    }

    It 'Default value when integer is zero' {
        Get-Command Test-Parameter | Should -HaveParameter 'intParam' -Type 'int' -DefaultValue 0
    }

    It 'Default value for true boolean' {
        Get-Command Test-Parameter | Should -HaveParameter 'boolTrueParam' -Type 'bool' -DefaultValue $true
    }

    It 'Default value for false boolean' {
        Get-Command Test-Parameter | Should -HaveParameter 'boolFalseParam' -Type 'bool' -DefaultValue $false
    }
}

In Pester 5.4.0, these worked as expected:

Running tests from 'C:\source\PesterBug\Test-Parameter.Tests.ps1'
Describing Parameter default value tests
  [+] Default value when integer is zero 36ms (6ms|30ms)
  [+] Default value for true boolean 6ms (5ms|2ms)
  [+] Default value for false boolean 14ms (6ms|9ms)
Tests completed in 217ms
Tests Passed: 3, Failed: 0, Skipped: 0 NotRun: 0

In Pester 5.4.1, these fail with:

[-] Parameter default value tests.Default value for integer 8ms (7ms|1ms)
 Expected command Test-Parameter to have a parameter intParam, of type [System.Int32] and the default value to be '0', but the default value was <empty>.
 at Get-Command Test-Parameter | Should -HaveParameter 'intParam' -Type 'int' -DefaultValue 0, C:\source\PesterBug\Test-Parameter.Tests.ps1:21
 at <ScriptBlock>, C:\source\PesterBug\Test-Parameter.Tests.ps1:21
[-] Parameter default value tests.Default value for true boolean 17ms (16ms|1ms)
 Expected command Test-Parameter to have a parameter boolTrueParam, of type [System.Boolean] and the default value to be 'True', but the default value was '$true'.
 at Get-Command Test-Parameter | Should -HaveParameter 'boolTrueParam' -Type 'bool' -DefaultValue $true, C:\source\PesterBug\Test-Parameter.Tests.ps1:25
 at <ScriptBlock>, C:\source\PesterBug\Test-Parameter.Tests.ps1:25
[-] Parameter default value tests.Default value for false boolean 19ms (18ms|1ms)
 Expected command Test-Parameter to have a parameter boolFalseParam, of type [System.Boolean] and the default value to be 'False', but the default value was '$false'.
 at Get-Command Test-Parameter | Should -HaveParameter 'boolFalseParam' -Type 'bool' -DefaultValue $false, C:\source\PesterBug\Test-Parameter.Tests.ps1:29
 at <ScriptBlock>, C:\source\PesterBug\Test-Parameter.Tests.ps1:29

Regression

This appears to have been caused by ea70451. I used a debugger to check the value of $paramInfo in Get-ParameterInfo and it looks different when the function was changed to use the AST.

Boolean before and after

# Pester 5.4.0
Name      Type      DefaultValue DefaultValueType
----      ----      ------------ ----------------
boolParam [bool]    true         Variable

# Pester 5.4.1
Name      Type      DefaultValue DefaultValueType
----      ----      ------------ ----------------
boolParam [boolean] $true        Boolean

Integer before and after

# Pester 5.4.0
Name     Type    DefaultValue DefaultValueType
----     ----    ------------ ----------------
intParam [int]   0            Number

# Pester 5.4.1
Name     Type    DefaultValue DefaultValueType
----     ----    ------------ ----------------
intParam [int32] 0            Int32

Expected Behavior

These kind of tests with -DefaultValue should pass.

Steps To Reproduce

See working sample test above in issue description

Describe your environment

Pester version     : 5.4.1 C:\Users\phoerr\Documents\PowerShell\Modules\Pester\5.4.1\Pester.psm1
PowerShell version : 7.3.6
OS version         : Microsoft Windows NT 10.0.19045.0

Possible Solution?

No response

@fflaten
Copy link
Collaborator

fflaten commented Aug 30, 2023

Thanks for the detailed report and troubleshooting!

You're right about the cause. Looks like it broke in 5.4.1 due to #2288 which introduced the ast-logic. Will look into it later.

@fflaten fflaten added the Bug label Aug 30, 2023
@nohwnd
Copy link
Member

nohwnd commented Oct 16, 2023

The problem is at least this cast: https://github.com/pester/Pester/pull/2288/files#diff-48c3c91280455432daa49020cc6c75e8670271729e764a963b227d9c9168a1feR213

This would return false for any falsy default value. We need an additional bool flag, or a value descriptor as the original api has.

Investigating and writing some tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants