diff --git a/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs b/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs index 1269b141a3a..bdea23d50d6 100644 --- a/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs +++ b/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs @@ -293,34 +293,42 @@ internal bool LoadUsingModulePath(PSModuleInfo parentModule, bool found, IEnumer // Now search using the module path... foreach (string path in modulePath) { - // a name takes the form of .../moduleDir/moduleName. - // if only one name has been specified, then the moduleDir - // and moduleName are identical and repeated... - // Also append th ename if the path currently points at a directory - string qualifiedPath = Path.Combine(path, fileBaseName); - - // Load the latest valid version if it is a multi-version module directory - module = LoadUsingMultiVersionModuleBase(qualifiedPath, manifestProcessingFlags, options, out found); - - if (!found) +#if UNIX + foreach (string folder in Directory.EnumerateDirectories(path)) { - if (name.IndexOfAny(Utils.Separators.Directory) == -1) + string moduleName = Path.GetFileName(folder); + if (String.Compare(moduleName, fileBaseName, StringComparison.OrdinalIgnoreCase) == 0) { - qualifiedPath = Path.Combine(qualifiedPath, fileBaseName); + fileBaseName = moduleName; +#endif + string qualifiedPath = Path.Combine(path, fileBaseName); + module = LoadUsingMultiVersionModuleBase(qualifiedPath, manifestProcessingFlags, options, out found); + if (!found) + { + if (name.IndexOfAny(Utils.Separators.Directory) == -1) + { + qualifiedPath = Path.Combine(qualifiedPath, fileBaseName); + } + else if (Utils.NativeDirectoryExists(qualifiedPath)) + { + // if it points to a directory, add the basename back onto the path... + qualifiedPath = Path.Combine(qualifiedPath, Path.GetFileName(fileBaseName)); + } + + module = LoadUsingExtensions(parentModule, name, qualifiedPath, extension, null, this.BasePrefix, ss, options, manifestProcessingFlags, out found); + } +#if UNIX } - else if (Directory.Exists(qualifiedPath)) + if (found) { - // if it points to a directory, add the basename back onto the path... - qualifiedPath = Path.Combine(qualifiedPath, Path.GetFileName(fileBaseName)); + break; } - - module = LoadUsingExtensions(parentModule, name, qualifiedPath, extension, null, this.BasePrefix, ss, options, manifestProcessingFlags, out found); } - if (found) { break; } +#endif } 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 911c74c8fae..48b216fc882 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 @@ -103,7 +103,7 @@ using System.Management.Automation; // Windows PowerShell namespace. namespace ModuleCmdlets { - [Cmdlet(VerbsDiagnostic.Test,"BinaryModuleCmdlet1")] + [Cmdlet(VerbsDiagnostic.Test,"BinaryModuleCmdlet1")] public class TestBinaryModuleCmdlet1Command : Cmdlet { protected override void BeginProcessing() @@ -124,3 +124,41 @@ namespace ModuleCmdlets } } +Describe "Import-Module should be case insensitive" -Tags 'CI' { + BeforeAll { + $defaultPSModuleAutoloadingPreference = $PSModuleAutoloadingPreference + $originalPSModulePath = $env:PSModulePath.Clone() + $modulesPath = "$TestDrive\Modules" + $env:PSModulePath += [System.IO.Path]::PathSeparator + $modulesPath + $PSModuleAutoloadingPreference = "none" + } + + AfterAll { + $global:PSModuleAutoloadingPreference = $defaultPSModuleAutoloadingPreference + $env:PSModulePath = $originalPSModulePath + } + + AfterEach { + Remove-Item -Recurse -Path $modulesPath -Force -ErrorAction SilentlyContinue + } + + It "Import-Module can import a module using different casing using '' and manifest:" -TestCases @( + @{modulePath="TESTMODULE/1.1"; manifest=$true}, + @{modulePath="TESTMODULE" ; manifest=$true}, + @{modulePath="TESTMODULE" ; manifest=$false} + ) { + param ($modulePath, $manifest) + New-Item -ItemType Directory -Path "$modulesPath/$modulePath" -Force > $null + if ($manifest) { + New-ModuleManifest -Path "$modulesPath/$modulePath/TESTMODULE.psd1" -RootModule "TESTMODULE.psm1" -ModuleVersion 1.1 + } + Set-Content -Path "$modulesPath/$modulePath/TESTMODULE.psm1" -Value "function mytest { 'hello' }" + Import-Module testMODULE + $m = Get-Module TESTmodule + $m | Should BeOfType "System.Management.Automation.PSModuleInfo" + $m.Name | Should Be "TESTMODULE" + mytest | Should BeExactly "hello" + Remove-Module TestModule + Get-Module tESTmODULE | Should BeNullOrEmpty + } +}