diff --git a/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs b/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs index 7c5857db3..b6aa270c9 100644 --- a/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs +++ b/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs @@ -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; } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 index c959d0d20..2da49f487 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 @@ -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 " -TestCases $testCases { + param($TestNameSuffix,$ipmoParms,$Expected) + Import-Module @ipmoParms + Get-Content out.txt | Should Be $Expected + } +}