From 6d906b2e224d2519ec33f577cf2ef4949ee764f0 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 9 Apr 2018 18:08:04 +0900 Subject: [PATCH 1/3] support importing module paths that end in trailing directory separator --- .../engine/Modules/ImportModuleCommand.cs | 5 +++++ .../Microsoft.PowerShell.Core/Import-Module.Tests.ps1 | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/src/System.Management.Automation/engine/Modules/ImportModuleCommand.cs b/src/System.Management.Automation/engine/Modules/ImportModuleCommand.cs index 9917f8c35d0..a5fd87629a6 100644 --- a/src/System.Management.Automation/engine/Modules/ImportModuleCommand.cs +++ b/src/System.Management.Automation/engine/Modules/ImportModuleCommand.cs @@ -638,6 +638,11 @@ private PSModuleInfo ImportModule_LocallyViaName(ImportModuleOptions importModul if (!found) { + // If the path ends with a directory separator, remove it + if (rootedPath.EndsWith(Path.DirectorySeparatorChar)) + { + rootedPath = Path.GetDirectoryName(rootedPath); + } // If the path is a directory, double up the end of the string // then try to load that using extensions... rootedPath = Path.Combine(rootedPath, Path.GetFileName(rootedPath)); 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 305053bb52e..ebbd1590ce6 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 @@ -30,6 +30,12 @@ Describe "Import-Module" -Tags "CI" { (Get-Module -Name $moduleName).Name | Should -BeExactly $moduleName } + It "should be able to load a module with a trailing directory separator" { + $modulePath = (Get-Module -ListAvailable $moduleName).ModuleBase + [System.IO.Path]::DirectorySeparatorChar + Import-Module -Name $modulePath + (Get-Module -Name $moduleName).Name | Should -BeExactly $moduleName + } + It "should be able to add a module with using ModuleInfo switch" { $a = Get-Module -ListAvailable $moduleName { Import-Module -ModuleInfo $a } | Should -Not -Throw From db797f46320b5917e575c76fd77e6b56a996a2a7 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 10 Apr 2018 07:40:38 +0900 Subject: [PATCH 2/3] address PR feedback --- .../Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 | 3 ++- 1 file changed, 2 insertions(+), 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 ebbd1590ce6..d23e297255f 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 @@ -32,7 +32,8 @@ Describe "Import-Module" -Tags "CI" { It "should be able to load a module with a trailing directory separator" { $modulePath = (Get-Module -ListAvailable $moduleName).ModuleBase + [System.IO.Path]::DirectorySeparatorChar - Import-Module -Name $modulePath + Remove-Module $moduleName + { Import-Module -Name $modulePath -ErrorAction Stop } | Should -Not -Throw (Get-Module -Name $moduleName).Name | Should -BeExactly $moduleName } From 26c72e26c1ef181125f12f9225fac04ad50e9273 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 12 Apr 2018 15:20:35 -0700 Subject: [PATCH 3/3] address Andrew's feedback --- .../engine/Modules/ImportModuleCommand.cs | 11 ++++++----- .../Microsoft.PowerShell.Core/Import-Module.Tests.ps1 | 11 +++++++---- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/System.Management.Automation/engine/Modules/ImportModuleCommand.cs b/src/System.Management.Automation/engine/Modules/ImportModuleCommand.cs index a5fd87629a6..4903fabed6e 100644 --- a/src/System.Management.Automation/engine/Modules/ImportModuleCommand.cs +++ b/src/System.Management.Automation/engine/Modules/ImportModuleCommand.cs @@ -629,6 +629,12 @@ private PSModuleInfo ImportModule_LocallyViaName(ImportModuleOptions importModul } else if (Directory.Exists(rootedPath)) { + // If the path ends with a directory separator, remove it + if (rootedPath.EndsWith(Path.DirectorySeparatorChar)) + { + rootedPath = Path.GetDirectoryName(rootedPath); + } + // Load the latest valid version if it is a multi-version module directory foundModule = LoadUsingMultiVersionModuleBase(rootedPath, ManifestProcessingFlags.LoadElements | @@ -638,11 +644,6 @@ private PSModuleInfo ImportModule_LocallyViaName(ImportModuleOptions importModul if (!found) { - // If the path ends with a directory separator, remove it - if (rootedPath.EndsWith(Path.DirectorySeparatorChar)) - { - rootedPath = Path.GetDirectoryName(rootedPath); - } // If the path is a directory, double up the end of the string // then try to load that using extensions... rootedPath = Path.Combine(rootedPath, Path.GetFileName(rootedPath)); 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 d23e297255f..04577e954f9 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 @@ -18,6 +18,7 @@ Describe "Import-Module" -Tags "CI" { BeforeEach { Remove-Module -Name $moduleName -Force (Get-Module -Name $moduleName).Name | Should -BeNullOrEmpty + Remove-Module -Name TestModule -Force -ErrorAction SilentlyContinue } AfterEach { @@ -30,11 +31,13 @@ Describe "Import-Module" -Tags "CI" { (Get-Module -Name $moduleName).Name | Should -BeExactly $moduleName } - It "should be able to load a module with a trailing directory separator" { - $modulePath = (Get-Module -ListAvailable $moduleName).ModuleBase + [System.IO.Path]::DirectorySeparatorChar - Remove-Module $moduleName + It "should be able to load a module with a trailing directory separator: " -TestCases @( + @{ modulePath = (Get-Module -ListAvailable $moduleName).ModuleBase + [System.IO.Path]::DirectorySeparatorChar; expectedName = $moduleName }, + @{ modulePath = Join-Path -Path $TestDrive -ChildPath "\Modules\TestModule\"; expectedName = "TestModule" } + ) { + param( $modulePath, $expectedName ) { Import-Module -Name $modulePath -ErrorAction Stop } | Should -Not -Throw - (Get-Module -Name $moduleName).Name | Should -BeExactly $moduleName + (Get-Module -Name $expectedName).Name | Should -BeExactly $expectedName } It "should be able to add a module with using ModuleInfo switch" {