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

Add missing container-parameters to Data before BeforeDiscovery #2361

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,11 @@ function BeforeDiscovery {
)

if ($ExecutionContext.SessionState.PSVariable.Get('invokedViaInvokePester')) {
if ($state.CurrentBlock.IsRoot -and -not $state.CurrentBlock.FrameworkData.MissingParametersProcessed) {
# For undefined parameters in container, add parameter's default value to Data
Add-MissingContainerParameters -RootBlock $state.CurrentBlock -Container $container -CallingFunction $PSCmdlet
}

. $ScriptBlock
}
else {
Expand Down
2 changes: 2 additions & 0 deletions src/Pester.Runtime.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2702,4 +2702,6 @@ function Add-MissingContainerParameters ($RootBlock, $Container, $CallingFunctio
}
}
}

$RootBlock.FrameworkData.MissingParametersProcessed = $true
}
2 changes: 1 addition & 1 deletion src/functions/Context.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
}

if ($ExecutionContext.SessionState.PSVariable.Get('invokedViaInvokePester')) {
if ($state.CurrentBlock.IsRoot -and $state.CurrentBlock.Blocks.Count -eq 0) {
if ($state.CurrentBlock.IsRoot -and -not $state.CurrentBlock.FrameworkData.MissingParametersProcessed) {
# For undefined parameters in container, add parameter's default value to Data
Add-MissingContainerParameters -RootBlock $state.CurrentBlock -Container $container -CallingFunction $PSCmdlet
}
Expand Down
2 changes: 1 addition & 1 deletion src/functions/Describe.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
}

if ($ExecutionContext.SessionState.PSVariable.Get('invokedViaInvokePester')) {
if ($state.CurrentBlock.IsRoot -and $state.CurrentBlock.Blocks.Count -eq 0) {
if ($state.CurrentBlock.IsRoot -and -not $state.CurrentBlock.FrameworkData.MissingParametersProcessed) {
# For undefined parameters in container, add parameter's default value to Data
Add-MissingContainerParameters -RootBlock $state.CurrentBlock -Container $container -CallingFunction $PSCmdlet
}
Expand Down
42 changes: 42 additions & 0 deletions tst/Pester.RSpec.ts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,48 @@ i -PassThru:$PassThru {
}
}
}

t "Parameter default values aren't overriden by changes in BeforeDiscovery" {
# https://github.com/pester/Pester/issues/2359
$sb = {
param (
[int] $Value = 123
)

if ($Value -ne 123) {
throw "Expected `$Value to be 123, but it is, '$Value'"
}

# KNOWN ISSUE: Changes to $Value here, outside BeforeDiscovery, would be used as parameter default value in Data.
# Can't fix as we don't have a hook beetween script execution and first call to a Pester-function

BeforeDiscovery {
$Value = 456 # Should not override default value in Data
}

BeforeAll {
if ($Value -ne 123) {
throw "Expected `$Value to be 123 but it is, '$Value'"
}
}

Describe 'd1' {
It 't1' {
if ($Value -ne 123) {
throw "Expected `$Value to be 123 but it is, '$Value'"
}
}
}
}

$container = New-PesterContainer -ScriptBlock $sb -Data @{ }
$r = Invoke-Pester -Container $container -PassThru

$r.Containers[0].Data.ContainsKey('Value') | Verify-True
$r.Containers[0].Data['Value'] | Verify-Equal 123

$r.Containers[0].Blocks[0].Tests[0].Result | Verify-Equal 'Passed'
}
}

b "New-PesterContainer" {
Expand Down