From 0fc21db411dfcfe97962e66c10bdfcfa01f6d359 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Tue, 9 Aug 2016 12:03:30 -0700 Subject: [PATCH] Fix Start-PSPester setup Start-PSPester must ensure that the environment variable `PSMODULEPATH` is not set before starting the new PowerShell process. If it is set, then the tests are not guaranteed to be correct, as they will be operating with the host process's `PSMODULEPATH`, thus ignoring their own modules. I also refactored the Start-PSPester command setup code. --- build.psm1 | 48 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/build.psm1 b/build.psm1 index 625996e4a2..4878a6d82f 100644 --- a/build.psm1 +++ b/build.psm1 @@ -554,23 +554,43 @@ function Start-PSPester { [string]$Path = "$PSScriptRoot/test/powershell" ) - $tagString = "-outputFormat ${OutputFormat} -outputFile ${outputFile} " - if ( ! $DisableExit ) { $tagString += " -EnableExit" } - if ( $ExcludeTag -and ($ExcludeTag -ne "")) { $tagString += " -ExcludeTag @('" + (${ExcludeTag} -join "','") + "')" } - if ( $Tag ) { $tagString += " -Tag @('" + (${Tag} -join "','") + "')" } + $powershell = Get-PSOutput - $powershell = get-psoutput - $psdir = [io.path]::GetDirectoryName($powershell) - $moduleDir = [io.path]::Combine($psdir,"Modules","Pester") + # All concatenated commands/arguments are suffixed with the delimiter (space) + $Command = "" - Write-Verbose "Import-Module '$moduleDir'; Invoke-Pester $tagString $Path" - $powershellexe = get-psoutput - $execPolicy = "" - if ($IsWindows) - { - $execPolicy = "Set-ExecutionPolicy -Scope Process Unrestricted; " + # Windows needs the execution policy adjusted + if ($IsWindows) { + $Command += "Set-ExecutionPolicy -Scope Process Unrestricted; " } - & $powershell -noprofile -c "$execPolicy Import-Module '$moduleDir'; Invoke-Pester $tagString $Path" + + $PesterModule = [IO.Path]::Combine((Split-Path $powershell), "Modules", "Pester") + $Command += "Import-Module $PesterModule; " + $Command += "Invoke-Pester " + + $Command += "-OutputFormat ${OutputFormat} -OutputFile ${OutputFile} " + if (!$DisableExit) { + $Command += "-EnableExit " + } + if ($ExcludeTag -and ($ExcludeTag -ne "")) { + $Command += "-ExcludeTag @('" + (${ExcludeTag} -join "','") + "') " + } + if ($Tag) { + $Command += "-Tag @('" + (${Tag} -join "','") + "') " + } + + $Command += $Path + + Write-Verbose $Command + # To ensure proper testing, the module path must not be inherited by the spawned process + try { + $originalModulePath = $env:PSMODULEPATH + $env:PSMODULEPATH = "" + & $powershell -noprofile -c $Command + } finally { + $env:PSMODULEPATH = $originalModulePath + } + if ($LASTEXITCODE -ne 0) { throw "$LASTEXITCODE Pester tests failed" }