mirror of
https://github.com/aphrody-labs/PowerShell.git
synced 2026-10-13 21:32:06 +00:00
Fix Resolve-Path -Relative output for dot-prefixed names (#28087)
This commit is contained in:
1 parent
9bc534b19b
commit
373f6a7b79
2 files changed
+39
-1
No files matched your search
+14
-1
@@ -117,6 +117,19 @@ namespace Microsoft.PowerShell.Commands
|
||||
|
||||
#endregion parameter data
|
||||
|
||||
private static bool StartsWithRelativePathToken(string path, ProviderInfo provider)
|
||||
{
|
||||
if (path.Length == 0 || path[0] != '.')
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int separatorIndex = path.Length > 1 && path[1] == '.' ? 2 : 1;
|
||||
return path.Length == separatorIndex ||
|
||||
path[separatorIndex] == provider.ItemSeparator ||
|
||||
path[separatorIndex] == provider.AltItemSeparator;
|
||||
}
|
||||
|
||||
#region Command code
|
||||
|
||||
/// <summary>
|
||||
@@ -233,7 +246,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
// Do not insert './' if result path is not relative
|
||||
if (!adjustedPath.StartsWith(
|
||||
currentPath.Drive?.Root ?? currentPath.Path, StringComparison.OrdinalIgnoreCase) &&
|
||||
!adjustedPath.StartsWith('.'))
|
||||
!StartsWithRelativePathToken(adjustedPath, currentPath.Provider))
|
||||
{
|
||||
adjustedPath = SessionState.Path.Combine(".", adjustedPath);
|
||||
}
|
||||
|
||||
@@ -67,6 +67,31 @@ Describe "Resolve-Path returns proper path" -Tag "CI" {
|
||||
Pop-Location
|
||||
}
|
||||
}
|
||||
|
||||
It "Resolve-Path -Relative prepends the current directory to every result" {
|
||||
$directoryNames = ".directory", "..directory", "directory"
|
||||
foreach ($directoryName in $directoryNames) {
|
||||
$directory = New-Item -Path (Join-Path $TestDrive $directoryName) -ItemType Directory
|
||||
$null = New-Item -Path $directory -Name "file1.txt" -ItemType File
|
||||
$null = New-Item -Path $directory -Name "file2.txt" -ItemType File
|
||||
}
|
||||
|
||||
try {
|
||||
Push-Location -Path $TestDrive
|
||||
foreach ($directoryName in $directoryNames) {
|
||||
$expected = @(
|
||||
Join-Path "." $directoryName "file1.txt"
|
||||
Join-Path "." $directoryName "file2.txt"
|
||||
)
|
||||
Resolve-Path -Path (Join-Path $directoryName "*.txt") -Relative |
|
||||
Should -BeExactly $expected
|
||||
}
|
||||
}
|
||||
finally {
|
||||
Pop-Location
|
||||
}
|
||||
}
|
||||
|
||||
It 'Resolve-Path RelativeBasePath should handle <Scenario>' -TestCases @(
|
||||
@{
|
||||
Scenario = "Absolute Path, Absolute ReleativeBasePath"
|
||||
|
||||
Reference in new issue
Block a user