Enable import-module to be case insensitive (#5097)

Fix #1621 

Enable import-module to be case insensitive as macOS is case insensitive.
This commit is contained in:
Steve Lee
2017-10-19 14:44:21 -07:00
committed by Aditya Patwardhan
parent 59b5d16139
commit e908b8a607
2 changed files with 66 additions and 20 deletions
@@ -293,34 +293,42 @@ namespace Microsoft.PowerShell.Commands
// Now search using the module path...
foreach (string path in modulePath)
{
// a name takes the form of .../moduleDir/moduleName.<ext>
// 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);
}
else if (Directory.Exists(qualifiedPath))
{
// if it points to a directory, add the basename back onto the path...
qualifiedPath = Path.Combine(qualifiedPath, Path.GetFileName(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);
module = LoadUsingExtensions(parentModule, name, qualifiedPath, extension, null, this.BasePrefix, ss, options, manifestProcessingFlags, out found);
}
#if UNIX
}
if (found)
{
break;
}
}
if (found)
{
break;
}
#endif
}
if (found)
@@ -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 '<modulePath>' and manifest:<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
}
}