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..e532e32e085 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 @@ -24,9 +36,14 @@ 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 } + + 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" {