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

Start interactive execution from BeforeDiscovery #2217

Merged
merged 2 commits into from
Jul 25, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1631,7 +1631,12 @@ function BeforeDiscovery {
[ScriptBlock]$ScriptBlock
)

. $ScriptBlock
if ($ExecutionContext.SessionState.PSVariable.Get('invokedViaInvokePester')) {
. $ScriptBlock
}
else {
Invoke-Interactively -CommandUsed 'BeforeDiscovery' -ScriptName $PSCmdlet.MyInvocation.ScriptName -SessionState $PSCmdlet.SessionState -BoundParameters $PSCmdlet.MyInvocation.BoundParameters
}
}

# Adding Add-ShouldOperator because it used to be an alias in v4, and so when we now import it will take precedence over
Expand Down
48 changes: 48 additions & 0 deletions tst/Pester.RSpec.InNewProcess.ts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,30 @@ i -PassThru:$PassThru {
}
}

t "Works with directly invoked testfile with root-level BeforeDiscovery" {
# Making sure variables in root-level BeforeDiscovery aren't set in session scope = available in Run-phase
# https://github.com/pester/Pester/issues/2092
$temp = [IO.Path]::GetTempPath()
$testpath = Join-Path $temp "$([Guid]::NewGuid().Guid).tests.ps1"

try {
$c = 'BeforeDiscovery { $myDiscoveryVar = 123 }; Describe "d" { It "i" { $myDiscoveryVar | Should -BeNullOrEmpty; 1 | Should -Be 1 } }'
Set-Content -Path $testpath -Value $c

$sb = [scriptblock]::Create("`$global:PesterPreference = [PesterConfiguration]@{Output=@{Verbosity='Detailed'}}; & $testpath")

$output = Invoke-InNewProcess -ScriptBlock $sb

$passedTests = $output | Select-String -SimpleMatch -Pattern '[+]' -Context 1, 0
$passedTests | Verify-NotNull
@($passedTests).Count | Verify-Equal 1
$passedTests.Context.PreContext | Verify-Equal "Describing d"
}
finally {
Remove-Item -Path $testpath
}
}

t "Works with directly invoked parameterized testfile using Describe" {
# https://github.com/pester/Pester/issues/1784

Expand Down Expand Up @@ -126,6 +150,30 @@ i -PassThru:$PassThru {
Remove-Item -Path $testpath
}
}

t "Works with directly invoked parameterized testfile with root-level BeforeDiscovery" {
# Also making sure variables in root-level BeforeDiscovery aren't set in session scope = available in Run-phase
# https://github.com/pester/Pester/issues/2092
$temp = [IO.Path]::GetTempPath()
$testpath = Join-Path $temp "$([Guid]::NewGuid().Guid).tests.ps1"

try {
$c = 'param([Parameter(Mandatory)]$File, $MyValue = 1) BeforeDiscovery { $myDiscoveryVar = 123 }; Describe "d - <File>" { It "i" { $myDiscoveryVar | Should -BeNullOrEmpty; $MyValue | Should -Be 1 } }'
Set-Content -Path $testpath -Value $c

$sb = [scriptblock]::Create("`$global:PesterPreference = [PesterConfiguration]@{Output=@{Verbosity='Detailed'}}; & $testpath -File 'demo.ps1'")

$output = Invoke-InNewProcess -ScriptBlock $sb

$passedTests = $output | Select-String -SimpleMatch -Pattern '[+]' -Context 1, 0
$passedTests | Verify-NotNull
@($passedTests).Count | Verify-Equal 1
$passedTests.Context.PreContext | Verify-Equal "Describing d - demo.ps1"
}
finally {
Remove-Item -Path $testpath
}
}
}

b "Exit codes" {
Expand Down