From f45491c07d98224638dc6f38fafb48ade38a1193 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 25 Oct 2017 14:23:22 -0700 Subject: [PATCH 1/2] import-module incorrectly reporting a loaded module was not found missing break after module was loaded successfully so loop continues but module isn't "found" and thus reports error even though loaded --- .../engine/Modules/ModuleCmdletBase.cs | 8 ++++---- .../Import-Module.Tests.ps1 | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs b/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs index cbce9e2c3f3..6b01b8c023b 100644 --- a/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs +++ b/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs @@ -317,12 +317,12 @@ internal bool LoadUsingModulePath(PSModuleInfo parentModule, bool found, IEnumer module = LoadUsingExtensions(parentModule, name, qualifiedPath, extension, null, this.BasePrefix, ss, options, manifestProcessingFlags, out found); } + if (found) + { + break; + } #if UNIX } - if (found) - { - break; - } } if (found) { 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 4dc287ab881..12475fe8b5f 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 @@ -1,5 +1,17 @@ Describe "Import-Module" -Tags "CI" { $moduleName = "Microsoft.PowerShell.Security" + BeforeAll { + $originalPSModulePath = $env:PSModulePath + New-Item -ItemType Directory -Path "$testdrive\Modules\TestModule\1.1" -Force > $null + New-Item -ItemType Directory -Path "$testdrive\Modules\TestModule\2.0" -Force > $null + $env:PSModulePath += [System.IO.Path]::PathSeparator + "$testdrive\Modules" + New-ModuleManifest -Path "$testdrive\Modules\TestModule\1.1\TestModule.psd1" -ModuleVersion 1.1 + New-ModuleManifest -Path "$testdrive\Modules\TestModule\2.0\TestModule.psd1" -ModuleVersion 2.0 + } + + AfterAll { + $env:PSModulePath = $originalPSModulePath + } BeforeEach { Remove-Module -Name $moduleName -Force @@ -27,6 +39,11 @@ { $script:module = Import-Module $moduleName -PassThru } | Should Not Throw Get-Module -Name $moduleName | Should Be $script:module } + + It "should only load the specified version" { + Import-Module TestModule -RequiredVersion 1.1 + (Get-Module TestModule).Version | Should Be "1.1" + } } Describe "Import-Module with ScriptsToProcess" -Tags "CI" { From daa1ce3250932f13e73c8bb7bdabc0480e4f7c80 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 25 Oct 2017 14:31:04 -0700 Subject: [PATCH 2/2] [feature] fix older test --- .../Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 12475fe8b5f..e532e32e085 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 @@ -36,7 +36,7 @@ It "should be able to load an already loaded module" { Import-Module $moduleName - { $script:module = Import-Module $moduleName -PassThru } | Should Not Throw + { $script:module = Import-Module $moduleName -PassThru -ErrorAction Stop } | Should Not Throw Get-Module -Name $moduleName | Should Be $script:module }