Visitar URL original
Fix `Resolve-Path -Relative` output for dot-prefixed names by daxian-dbw · Pull Request #28087 · PowerShell/PowerShell · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ public override SwitchParameter Force

#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;
}
Comment thread
daxian-dbw marked this conversation as resolved.

#region Command code

/// <summary>
Expand Down Expand Up @@ -233,7 +246,7 @@ protected override void ProcessRecord()
// 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Comment thread
daxian-dbw marked this conversation as resolved.
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"
Expand Down
Loading