Skip to content

Restore-TestEnvironment: New PSModulePath in target Machine is overwritten  #127

Closed
@johlju

Description

@johlju

The root cause is that

  1. Initialize-TestEnvironment saves the current machine environment variable PSModulePath and then updates it with the path to the built module.

$oldPSModulePath = $env:PSModulePath
if ($null -ne $oldPSModulePath)
{
$oldPSModulePathSplit = $oldPSModulePath.Split([io.path]::PathSeparator)
}
else
{
$oldPSModulePathSplit = $null
}
if ($oldPSModulePathSplit -ccontains $moduleParentFilePath)
{
# Remove the existing module path from the new PSModulePath
$newPSModulePathSplit = $oldPSModulePathSplit | Where-Object { $_ -ne $moduleParentFilePath }
}
else
{
$newPSModulePath = $oldPSModulePath
}
$RequiredModulesPath = Join-Path -Path $moduleParentFilePath 'RequiredModules'
if ($newPSModulePathSplit -cnotcontains $RequiredModulesPath)
{
$newPSModulePathSplit = @($RequiredModulesPath) + $newPSModulePathSplit
}
$newPSModulePathSplit = @($moduleParentFilePath) + $newPSModulePathSplit
$newPSModulePath = $newPSModulePathSplit -join [io.Path]::PathSeparator
Set-PSModulePath -Path $newPSModulePath

  1. The tests run for SqlSetup and the installation sets a new path to SQLPS in machine environment variable PSModulePath.

https://github.com/dsccommunity/SqlServerDsc/blob/f9bde8a61230754c078257d115103726f749570a/tests/Integration/DSC_SqlSetup.Integration.Tests.ps1#L171-L175

if ($TestEnvironment.OldPSModulePath -ne $env:PSModulePath)
{
Set-PSModulePath -Path $TestEnvironment.OldPSModulePath
if ($TestEnvironment.TestType -in ('Integration','All'))
{
# Restore the machine PSModulePath for integration tests.
Set-PSModulePath -Path $TestEnvironment.OldPSModulePath -Machine
}
}

  1. After the tests finishes the command Restore-TestEnvironment reverts the machine environment variable PSModulePath to the one saved in step 1 overwriting the new path that was set in step 2.
  2. All other tests no longer finds the module SQLPS and fails.

Originally posted by @johlju in dsccommunity/SqlServerDsc#1932 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementThe issue is an enhancement request.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions