Fix Import-Module to honor 'ScriptsToProcess' when '-Version' is specified (#3897)
This commit is contained in:
parent
924f281c96
commit
28ec9a3511
|
@ -2768,38 +2768,58 @@ namespace Microsoft.PowerShell.Commands
|
||||||
}
|
}
|
||||||
|
|
||||||
if (scriptsToProcess != null)
|
if (scriptsToProcess != null)
|
||||||
{
|
{
|
||||||
foreach (string scriptFile in scriptsToProcess)
|
Version savedBaseMinimumVersion = BaseMinimumVersion;
|
||||||
{
|
Version savedBaseMaximumVersion = BaseMaximumVersion;
|
||||||
bool found = false;
|
Version savedBaseRequiredVersion = BaseRequiredVersion;
|
||||||
PSModuleInfo module = LoadModule(scriptFile,
|
Guid? savedBaseGuid = BaseGuid;
|
||||||
moduleBase,
|
|
||||||
string.Empty, // prefix (-Prefix shouldn't be applied to dot sourced scripts)
|
|
||||||
null,
|
|
||||||
ref options,
|
|
||||||
manifestProcessingFlags,
|
|
||||||
out found);
|
|
||||||
|
|
||||||
// If we're in analysis, add the detected exports to this module's
|
try
|
||||||
// exports
|
{
|
||||||
if (found && (ss == null))
|
BaseMinimumVersion = null;
|
||||||
|
BaseMaximumVersion = null;
|
||||||
|
BaseRequiredVersion = null;
|
||||||
|
BaseGuid = null;
|
||||||
|
|
||||||
|
foreach (string scriptFile in scriptsToProcess)
|
||||||
{
|
{
|
||||||
foreach (string detectedCmdlet in module.ExportedCmdlets.Keys)
|
bool found = false;
|
||||||
{
|
PSModuleInfo module = LoadModule(scriptFile,
|
||||||
manifestInfo.AddDetectedCmdletExport(detectedCmdlet);
|
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)
|
foreach (string detectedFunction in module.ExportedFunctions.Keys)
|
||||||
{
|
{
|
||||||
manifestInfo.AddDetectedAliasExport(detectedAlias,
|
manifestInfo.AddDetectedFunctionExport(detectedFunction);
|
||||||
module.ExportedAliases[detectedAlias].Definition);
|
}
|
||||||
|
|
||||||
|
foreach (string detectedAlias in module.ExportedAliases.Keys)
|
||||||
|
{
|
||||||
|
manifestInfo.AddDetectedAliasExport(detectedAlias,
|
||||||
|
module.ExportedAliases[detectedAlias].Definition);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
BaseMinimumVersion = savedBaseMinimumVersion;
|
||||||
|
BaseMaximumVersion = savedBaseMaximumVersion;
|
||||||
|
BaseRequiredVersion = savedBaseRequiredVersion;
|
||||||
|
BaseGuid = savedBaseGuid;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,3 +22,44 @@
|
||||||
(Get-Module -Name $moduleName).Name | Should Be $moduleName
|
(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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue