Skip to content

Commit

Permalink
Enable import-module to be case insensitive (PowerShell#5097)
Browse files Browse the repository at this point in the history
Fix PowerShell#1621 

Enable import-module to be case insensitive as macOS is case insensitive.
  • Loading branch information
SteveL-MSFT authored and adityapatwardhan committed Oct 19, 2017
1 parent 59b5d16 commit e908b8a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 18 deletions.
44 changes: 26 additions & 18 deletions src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.<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);
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit e908b8a

Please sign in to comment.