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

PSBoundParameters is empty in v5 at runtime of tests #1828

Closed
simonsabin opened this issue Jan 18, 2021 · 11 comments · Fixed by pester/docs#205
Closed

PSBoundParameters is empty in v5 at runtime of tests #1828

simonsabin opened this issue Jan 18, 2021 · 11 comments · Fixed by pester/docs#205
Milestone

Comments

@simonsabin
Copy link

simonsabin commented Jan 18, 2021

PsBoundParamIssue.ps1.txt

General summary of the issue

PSBoundParameters isn't populated as the scripts are now run in two phases. I believe this is a breaking change in v5.

Rather than checking PSBoundParameters on has to check the variables PSDrive (or other variable checking solution) to check if the variable has been defined, and has a value, i.e. passed in using a PesterContainer

Describe your environment

Pester version : 5.1.1 C:\Users\SimonSabin\OneDrive\Documents2\PowerShell\Modules\Pester\5.1.1\Pester.psm1
PowerShell version : 7.0.1
OS version : Microsoft Windows NT 10.0.19041.0

Steps to reproduce

See Attached file

Expected Behavior

PSBoundParameters is populated with parameters at runtime.

Current Behavior

PSBoundParameters is only available at discovery not at runtime, this is different to the behaviour in v4.

Possible Solution? (optional)

Not sure if a change is required, except to document the change in behaviour.

@nohwnd nohwnd added this to the 5.2 milestone Mar 4, 2021
@nohwnd
Copy link
Member

nohwnd commented Mar 22, 2021

Note to self: The Data should be merged with the splat to allow this in It and BeforeAll etc..

Get-Module Pester  | remove-module;
/p/pester/build.ps1
Import-Module  /p/pester/bin/pester.psd1

$c = [pesterconfiguration]::Default

$c.Run.PassThru = $true
$c.output.Verbosity = "Diagnostic"
$c.Run.Container = new-Pestercontainer -scriptblock {

  param ($a, $b)

  BeforeAll {
    param ($a)

    $g = $a
  }

  Describe 'test' {


    it 'it' {
        param ($a)
        Write-Host -ForegroundColor cyan  $PSBoundParameters
    }
 }
} -Data @{ a = "aaa"; b = "bbb" }

$r = Invoke-Pester -configuration $c

@nohwnd
Copy link
Member

nohwnd commented Apr 23, 2021

This is way more difficult than #1542 and has little benefit at the moment. Moving it out of the milestone.

To make this work consistently we would have to merge all parent data with the internal scriptBlock contexts (like $Pester object) when invoking every block. Which seems costly for almost no benefit because the variables nicely flow down using variable inheritance.

@nohwnd nohwnd modified the milestones: 5.2, 5.3 Apr 23, 2021
@simonsabin
Copy link
Author

I’ll look to put it in the docs somewhere that one shouldn’t use it

@fflaten
Copy link
Collaborator

fflaten commented Jun 19, 2021

Was about to add it to breaking changes from v4 to v5, but I can't really reproduce it in Pester 4.10.1.
PSBoundParameters isn't listed inside It.. Am I doing something wrong?

Pester v4.10.1
Executing all tests in '.\git\pester\samples\demoIssue1828v4.tests.ps1'

Executing script .\git\pester\samples\demoIssue1828v4.tests.ps1
VERBOSE: Pester version 4.10.1
VERBOSE: Before Test

Key Value
--- -----
foo A new value for foo


Foo=A new value for foo

  Describing Test
VERBOSE: During  Test

Foo=A new value for foo
    [+] Test psboundparameters 0ms
Tests completed in 36ms
Tests Passed: 1, Failed: 0, Skipped: 0, Pending: 0, Inconclusive: 0

@nohwnd
Copy link
Member

nohwnd commented Jun 25, 2021

@simonsabin friendly ping on this :)

@simonsabin
Copy link
Author

The repro is a little confusing. It shows that whilst PSBoundParameters is not set, the variable $foo has been set.
This means you can't use PSBoundParamaters in BeforeAll in v5.0

@fflaten
Copy link
Collaborator

fflaten commented Jun 25, 2021

The repro is a little confusing. It shows that whilst PSBoundParameters is not set, the variable $foo has been set.

This means you can't use PSBoundParamaters in BeforeAll in v5.0

The issue mentions this behavior as a v5-regression, but I can't even get it to work in v4. Can you?

This affects whether it should be documented in breaking changes or just some general doc.

@simonsabin
Copy link
Author

When you run this code, for the v 4.10.1 The output from the script block outside of the describe runs and has PSBoundparameters assigned see my results below.

get-module pester | remove-module 
import-module pester -MaximumVersion 4.10.1

{
param($foo)  

write-verbose "Pester version $((get-module pester).Version)" -verbose
Write-verbose "Before Test Not in any code block"  -Verbose
Write-verbose "Bound Parameters is "  -Verbose
Write-verbose ($PSBoundParameters| Convertto-json |out-string)  -Verbose
Write-verbose "The Variable is Foo is set to =($foo)" -Verbose

Describe "Test"{
    It "Test psboundparameters"{
        Write-verbose "During  Test" -Verbose
        Write-verbose "Bound Parameters is "  -Verbose
        Write-verbose ($PSBoundParameters| Convertto-json |out-string)  -Verbose
        Write-verbose "The Variable is Foo is set to =($foo)" -Verbose
        }
    }
} |out-file ".\my.tests.ps1";

invoke-pester -Script @{Path=".\my.tests.ps1";Parameters=@{foo="A new value for foo"}}

remove-module Pester
import-module pester -MinimumVersion 5.0.0

{
param($foo)  
BeforeDiscovery{
    write-verbose "Pester version $((get-module pester).Version)" -verbose
    Write-verbose "Before Test in BeforeDiscovery" -Verbose
    Write-verbose ($PSBoundParameters| convertto-json|out-string) -Verbose
    Write-verbose "The Variable is Foo is set to =($foo)" -Verbose
}
BeforeAll{
    write-verbose "Pester version $((get-module pester).Version)" -verbose
    Write-verbose "Before Test in BeforeAll" -Verbose
    Write-verbose ($PSBoundParameters| convertto-json|out-string) -Verbose
    Write-verbose "The Variable is Foo is set to =($foo)" -Verbose
}
Describe "Test"{
    It "Test psboundparameters"{
        Write-verbose "During  Test" -Verbose
        Write-verbose "Bound Parameters is "  -Verbose
        Write-verbose ($PSBoundParameters| Convertto-json |out-string)  -Verbose
        Write-verbose "The Variable is Foo is set to =($foo)" -Verbose
        }
}
}|out-file ".\my.tests2.ps1";
invoke-pester -Container (New-PesterContainer -Path ".\my.tests2.ps1" -Data @{foo="A new value for foo"})

My results are as below

Pester v4.10.1
Executing all tests in '.\my.tests.ps1'

Executing script .\my.tests.ps1
VERBOSE: Pester version 4.10.1
VERBOSE: Before Test Not in any code block
VERBOSE: Bound Parameters is
VERBOSE: {
"foo": "A new value for foo"
}

VERBOSE: The Variable is Foo is set to =(A new value for foo)

Describing Test
VERBOSE: During Test
VERBOSE: Bound Parameters is
VERBOSE: {}

VERBOSE: The Variable is Foo is set to =(A new value for foo)
[+] Test psboundparameters 6ms
Tests completed in 130ms
Tests Passed: 1, Failed: 0, Skipped: 0, Pending: 0, Inconclusive: 0

Starting discovery in 1 files.
VERBOSE: Pester version 5.2.2
VERBOSE: Before Test in BeforeDiscovery
VERBOSE: {}

VERBOSE: The Variable is Foo is set to =(A new value for foo)
Discovery finished in 49ms.
Running tests.
VERBOSE: Pester version 5.2.2
VERBOSE: Before Test in BeforeAll
VERBOSE: {}

VERBOSE: The Variable is Foo is set to =(A new value for foo)
VERBOSE: During Test
VERBOSE: Bound Parameters is
VERBOSE: {}

VERBOSE: The Variable is Foo is set to =(A new value for foo)
[+] C:\Users\SimonSabin\source\repos\template.sql\src\TemplateProject.tests\my.tests2.ps1 249ms (20ms|185ms)
Tests completed in 253ms
Tests Passed: 1, Failed: 0, Skipped: 0 NotRun: 0

@simonsabin
Copy link
Author

I should add this is in relation to PSBound paramaters in an undefined block outside of describe in V4 compared to a BeforeAll or BeforeDiscovery in V5.

Its not in relation to use within an It block

@fflaten
Copy link
Collaborator

fflaten commented Jun 26, 2021

Aha, thanks! So it's only missing in the discovery phase in v5, It's never been available during test execution (It-blocks in v4, Run-phase in general in v5).

Thoughts on where this should be documented on https://pester.dev? A note-block at the end of https://pester.dev/docs/usage/data-driven-tests#providing-external-data-to-tests maybe saying that $PSBoundParameters are unavailable in general? Thinking it might be mostly relevant for parameterized scripts.

@simonsabin
Copy link
Author

simonsabin commented Jun 26, 2021 via email

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

Successfully merging a pull request may close this issue.

3 participants