Fix Import-Module to honor 'ScriptsToProcess' when '-Version' is specified (#3897)

This commit is contained in:
Andrew 2017-06-05 14:19:03 -07:00 committed by Dongbo Wang
parent 924f281c96
commit 28ec9a3511
2 changed files with 87 additions and 26 deletions

View file

@ -2768,38 +2768,58 @@ namespace Microsoft.PowerShell.Commands
}
if (scriptsToProcess != null)
{
foreach (string scriptFile in scriptsToProcess)
{
bool found = false;
PSModuleInfo module = LoadModule(scriptFile,
moduleBase,
string.Empty, // prefix (-Prefix shouldn't be applied to dot sourced scripts)
null,
ref options,
manifestProcessingFlags,
out found);
{
Version savedBaseMinimumVersion = BaseMinimumVersion;
Version savedBaseMaximumVersion = BaseMaximumVersion;
Version savedBaseRequiredVersion = BaseRequiredVersion;
Guid? savedBaseGuid = BaseGuid;
// If we're in analysis, add the detected exports to this module's
// exports
if (found && (ss == null))
try
{
BaseMinimumVersion = null;
BaseMaximumVersion = null;
BaseRequiredVersion = null;
BaseGuid = null;
foreach (string scriptFile in scriptsToProcess)
{
foreach (string detectedCmdlet in module.ExportedCmdlets.Keys)
{
manifestInfo.AddDetectedCmdletExport(detectedCmdlet);
}
bool found = false;
PSModuleInfo module = LoadModule(scriptFile,
moduleBase,
string.Empty, // prefix (-Prefix shouldn't be applied to dot sourced scripts)
null,
ref options,
manifestProcessingFlags,
out found);
foreach (string detectedFunction in module.ExportedFunctions.Keys)
// If we're in analysis, add the detected exports to this module's
// exports
if (found && (ss == null))
{
manifestInfo.AddDetectedFunctionExport(detectedFunction);
}
foreach (string detectedCmdlet in module.ExportedCmdlets.Keys)
{
manifestInfo.AddDetectedCmdletExport(detectedCmdlet);
}
foreach (string detectedAlias in module.ExportedAliases.Keys)
{
manifestInfo.AddDetectedAliasExport(detectedAlias,
module.ExportedAliases[detectedAlias].Definition);
foreach (string detectedFunction in module.ExportedFunctions.Keys)
{
manifestInfo.AddDetectedFunctionExport(detectedFunction);
}
foreach (string detectedAlias in module.ExportedAliases.Keys)
{
manifestInfo.AddDetectedAliasExport(detectedAlias,
module.ExportedAliases[detectedAlias].Definition);
}
}
}
}
}
finally
{
BaseMinimumVersion = savedBaseMinimumVersion;
BaseMaximumVersion = savedBaseMaximumVersion;
BaseRequiredVersion = savedBaseRequiredVersion;
BaseGuid = savedBaseGuid;
}
}

View file

@ -22,3 +22,44 @@
(Get-Module -Name $moduleName).Name | Should Be $moduleName
}
}
Describe "Import-Module with ScriptsToProcess" -Tags "CI" {
BeforeAll {
$moduleRootPath = Join-Path $TestDrive 'TestModules'
New-Item $moduleRootPath -ItemType Directory -Force | Out-Null
Push-Location $moduleRootPath
"1 | Out-File out.txt -Append -NoNewline" | Out-File script1.ps1
"2 | Out-File out.txt -Append -NoNewline" | Out-File script2.ps1
New-ModuleManifest module1.psd1 -ScriptsToProcess script1.ps1
New-ModuleManifest module2.psd1 -ScriptsToProcess script2.ps1 -NestedModules module1.psd1
}
AfterAll {
Pop-Location
}
BeforeEach {
New-Item out.txt -ItemType File -Force | Out-Null
}
AfterEach {
$m = @('module1','module2','script1','script2')
remove-module $m -Force -ErrorAction SilentlyContinue
Remove-Item out.txt -Force -ErrorAction SilentlyContinue
}
$testCases = @(
@{ TestNameSuffix = 'for top-level module'; ipmoParms = @{'Name'='.\module1.psd1'}; Expected = '1' }
@{ TestNameSuffix = 'for top-level and nested module'; ipmoParms = @{'Name'='.\module2.psd1'}; Expected = '21' }
@{ TestNameSuffix = 'for top-level module when -Version is specified'; ipmoParms = @{'Name'='.\module1.psd1'; 'Version'='1.0'}; Expected = '1' }
@{ TestNameSuffix = 'for top-level and nested module when -Version is specified'; ipmoParms = @{'Name'='.\module2.psd1'; 'Version'='1.0'}; Expected = '21' }
)
It "Verify ScriptsToProcess are executed <TestNameSuffix>" -TestCases $testCases {
param($TestNameSuffix,$ipmoParms,$Expected)
Import-Module @ipmoParms
Get-Content out.txt | Should Be $Expected
}
}