Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,26 @@ protected override void ProcessRecord()
{
foreach (PathInfo currentPath in result)
{
// When result path and base path is on different PSDrive
// (../)*path should not go beyond the root of base path
if (currentPath.Drive != SessionState.Path.CurrentLocation.Drive &&
SessionState.Path.CurrentLocation.Drive != null &&
!currentPath.ProviderPath.StartsWith(
SessionState.Path.CurrentLocation.Drive.Root, StringComparison.OrdinalIgnoreCase))
{
WriteObject(currentPath.Path, enumerateCollection: false);
continue;
}
string adjustedPath = SessionState.Path.NormalizeRelativePath(currentPath.Path,
SessionState.Path.CurrentLocation.ProviderPath);
if (!adjustedPath.StartsWith(".", StringComparison.OrdinalIgnoreCase))
// Do not insert './' if result path is not relative
if (!adjustedPath.StartsWith(
currentPath.Drive?.Root ?? currentPath.Path, StringComparison.OrdinalIgnoreCase) &&
!adjustedPath.StartsWith(".", StringComparison.OrdinalIgnoreCase))
{
adjustedPath = SessionState.Path.Combine(".", adjustedPath);
}
WriteObject(adjustedPath, false);
WriteObject(adjustedPath, enumerateCollection: false);
}
}
}
Expand Down Expand Up @@ -152,7 +165,7 @@ protected override void ProcessRecord()

if (!_relative)
{
WriteObject(result, true);
WriteObject(result, enumerateCollection: true);
}
}
} // ProcessRecord
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
Describe "Resolve-Path returns proper path" -Tag "CI" {
BeforeAll {
$driveName = "RvpaTest"
$root = Join-Path $TestDrive "fakeroot"
$file = Join-Path $root "file.txt"
$null = New-Item -Path $root -ItemType Directory -Force
$null = New-Item -Path $file -ItemType File -Force
$null = New-PSDrive -Name $driveName -PSProvider FileSystem -Root $root

$testRoot = Join-Path $TestDrive ""
$fakeRoot = Join-Path "$driveName`:" ""

$relCases = @(
@{ wd = $fakeRoot; target = $testRoot; expected = $testRoot }
@{ wd = $testRoot; target = Join-Path $fakeRoot "file.txt"; expected = Join-Path "." "fakeroot" "file.txt" }
)
}
AfterAll {
Remove-PSDrive -Name $driveName -Force
}
It "Resolve-Path returns resolved paths" {
Resolve-Path $TESTDRIVE | Should be "$TESTDRIVE"
}
Expand All @@ -23,4 +42,14 @@ Describe "Resolve-Path returns proper path" -Tag "CI" {
$result = Resolve-Path -LiteralPath "TestDrive:\\\\\"
($result.Path.TrimEnd('/\')) | Should Be "TestDrive:"
}
It "Resolve-Path -Relative '<target>' should return correct path on '<wd>'" -TestCases $relCases {
param($wd, $target, $expected)
try {
Push-Location -Path $wd
Resolve-Path -Path $target -Relative | Should -BeExactly $expected
}
finally {
Pop-Location
}
}
}