return correct casing of filesystem path during normalization

This commit is contained in:
Steve Lee 2019-03-29 13:22:52 -07:00
parent df513bf7d8
commit 6bc2b55fdf
2 changed files with 46 additions and 1 deletions

View file

@ -98,7 +98,46 @@ namespace Microsoft.PowerShell.Commands
/// </returns>
private static string NormalizePath(string path)
{
return path.Replace(StringLiterals.AlternatePathSeparator, StringLiterals.DefaultPathSeparator);
return GetCorrectCasedPath(path.Replace(StringLiterals.AlternatePathSeparator, StringLiterals.DefaultPathSeparator));
}
/// <summary>
/// Get the correct casing for a path. This method assumes it's being called by NormalizePath()
/// so that the path is already normalized.
/// </summary>
/// <param name="path">
/// The path to retrieve.
/// </param>
/// <returns>
/// The path with accurate casing if item exists, otherwise it returns path that was passed in.
/// </returns>
private static string GetCorrectCasedPath(string path)
{
string exactPath = string.Empty;
if (File.Exists(path) || Directory.Exists(path))
{
foreach (string item in path.Split(StringLiterals.DefaultPathSeparator))
{
if (string.IsNullOrEmpty(exactPath))
{
exactPath = item + StringLiterals.DefaultPathSeparator;
}
else if (string.IsNullOrEmpty(item))
{
// This handles the trailing slash case
continue;
}
else
{
exactPath = Directory.GetFileSystemEntries(exactPath, item).First();
}
}
return exactPath;
}
else
{
return path;
}
}
/// <summary>

View file

@ -70,6 +70,12 @@ Describe "Set-Location" -Tags "CI" {
Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand"
}
It "Should use actual casing of folder on case-insensitive filesystem" -Skip:($IsLinux) {
$testPath = New-Item -ItemType Directory -Path testdrive:/teST
Set-Location $testPath.FullName.ToUpper()
$(Get-Location).Path | Should -BeExactly $testPath.FullName
}
Context 'Set-Location with no arguments' {
It 'Should go to $env:HOME when Set-Location run with no arguments from FileSystem provider' {