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.
This commit is contained in:
Andrew Schwartzmeyer 2016-08-09 12:03:30 -07:00
parent 031ba8795b
commit 0fc21db411

View file

@ -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"
}