Skip to content

Commit

Permalink
Fix false errors and broken CI-logs from P-tests (#2345)
Browse files Browse the repository at this point in the history
  • Loading branch information
fflaten committed Jun 7, 2023
1 parent 96da478 commit 89a3f8e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 40 deletions.
22 changes: 22 additions & 0 deletions tst/PTestHelpers.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function Invoke-InNewProcess ([ScriptBlock] $ScriptBlock) {
# get the path of the currently loaded Pester to re-import it in the child process
$pesterPath = Get-Module Pester | Select-Object -ExpandProperty Path
$powershell = Get-Process -Id $pid | Select-Object -ExpandProperty Path
# run any scriptblock in a separate process to be able to grab all the output
# doesn't enforce Invoke-Pester usage so we can test other public functions directly
$command = {
param ($PesterPath, [ScriptBlock] $ScriptBlock)
Import-Module $PesterPath

. $ScriptBlock
}.ToString()

if ($PSVersionTable.PSVersion -ge '7.3' -and $PSNativeCommandArgumentPassing -ne 'Legacy') {
$cmd = "& { $command } -PesterPath ""$PesterPath"" -ScriptBlock { $ScriptBlock }"
}
else {
$cmd = "& { $command } -PesterPath ""$PesterPath"" -ScriptBlock { $($ScriptBlock -replace '"','\"') }"
}

& $powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Command $cmd
}
22 changes: 2 additions & 20 deletions tst/Pester.RSpec.InNewProcess.ts.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
param ([switch] $PassThru, [switch] $NoBuild)

Get-Module Pester.Runtime, Pester.Utility, P, Pester, Axiom, Stack | Remove-Module
Get-Module P, PTestHelpers, Pester, Axiom | Remove-Module

Import-Module $PSScriptRoot\p.psm1 -DisableNameChecking
Import-Module $PSScriptRoot\axiom\Axiom.psm1 -DisableNameChecking
Import-Module $PSScriptRoot\PTestHelpers.psm1 -DisableNameChecking

if (-not $NoBuild) { & "$PSScriptRoot\..\build.ps1" }
Import-Module $PSScriptRoot\..\bin\Pester.psd1
Expand All @@ -15,25 +16,6 @@ $global:PesterPreference = @{
}
$PSDefaultParameterValues = @{}

function Invoke-InNewProcess ([ScriptBlock] $ScriptBlock) {
# get the path of the currently loaded Pester to re-import it in the child process
$pesterPath = Get-Module Pester | Select-Object -ExpandProperty Path
$powershell = Get-Process -Id $pid | Select-Object -ExpandProperty Path
# run any scriptblock in a separate process to be able to grab all the output
# doesn't enforce Invoke-Pester usage so we can test other public functions directly
$command = {
param ($PesterPath, [ScriptBlock] $ScriptBlock)
Import-Module $PesterPath

. $ScriptBlock
}.ToString()

# using base64 because we need to escape quotes in $ScriptBlock and previous method using \" stopped working in PS7.3
$cmd = "& { $command } -PesterPath ""$PesterPath"" -ScriptBlock { $ScriptBlock }"
$encodedcommand = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($cmd))
& $powershell -NoProfile -ExecutionPolicy Bypass -encodedCommand $encodedcommand
}

i -PassThru:$PassThru {
b "Interactive execution" {
t "Works with directly invoked testfile using Describe" {
Expand Down
22 changes: 2 additions & 20 deletions tst/Pester.RSpec.Output.ts.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
param ([switch] $PassThru, [switch] $NoBuild)

Get-Module Pester.Runtime, Pester.Utility, P, Pester, Axiom, Stack | Remove-Module
Get-Module P, PTestHelpers, Pester, Axiom | Remove-Module

Import-Module $PSScriptRoot\p.psm1 -DisableNameChecking
Import-Module $PSScriptRoot\axiom\Axiom.psm1 -DisableNameChecking
Import-Module $PSScriptRoot\PTestHelpers.psm1 -DisableNameChecking

if (-not $NoBuild) { & "$PSScriptRoot\..\build.ps1" }
Import-Module $PSScriptRoot\..\bin\Pester.psd1
Expand All @@ -21,25 +22,6 @@ $global:PesterPreference = @{
}
$PSDefaultParameterValues = @{}

function Invoke-InNewProcess ([ScriptBlock] $ScriptBlock) {
# get the path of the currently loaded Pester to re-import it in the child process
$pesterPath = Get-Module Pester | Select-Object -ExpandProperty Path
$powershell = Get-Process -Id $pid | Select-Object -ExpandProperty Path
# run any scriptblock in a separate process to be able to grab all the output
# doesn't enforce Invoke-Pester usage so we can test other public functions directly
$command = {
param ($PesterPath, [ScriptBlock] $ScriptBlock)
Import-Module $PesterPath

. $ScriptBlock
}.ToString()

# using base64 because we need to escape quotes in $ScriptBlock and previous method using \" stopped working in PS7.3
$cmd = "& { $command } -PesterPath ""$PesterPath"" -ScriptBlock { $ScriptBlock }"
$encodedcommand = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($cmd))
& $powershell -NoProfile -ExecutionPolicy Bypass -encodedCommand $encodedcommand
}

i -PassThru:$PassThru {
b 'Output in VSCode mode' {
# VSCode-powershell Problem Matcher pattern
Expand Down

0 comments on commit 89a3f8e

Please sign in to comment.