Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Resolve-Path -Relative with path starting with period #21154

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public SwitchParameter Relative
/// </summary>
[Parameter]
public string RelativeBasePath
{
{
get
{
return _relativeBasePath;
Expand Down Expand Up @@ -221,9 +221,9 @@ protected override void ProcessRecord()
string adjustedPath = SessionState.Path.NormalizeRelativePath(currentPath.Path, _relativeBasePath);

// Do not insert './' if result path is not relative
if (!adjustedPath.StartsWith(
currentPath.Drive?.Root ?? currentPath.Path, StringComparison.OrdinalIgnoreCase) &&
!adjustedPath.StartsWith('.'))
if (!adjustedPath.StartsWith(currentPath.Drive?.Root ?? currentPath.Path, StringComparison.OrdinalIgnoreCase) &&
!adjustedPath.StartsWith("." + currentPath.Provider.ItemSeparator, StringComparison.OrdinalIgnoreCase) &&
!adjustedPath.StartsWith(".." + currentPath.Provider.ItemSeparator, StringComparison.OrdinalIgnoreCase))
{
adjustedPath = SessionState.Path.Combine(".", adjustedPath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Describe "Resolve-Path returns proper path" -Tag "CI" {
}
) -Test {
param($Path, $BasePath, $Expected, $CD)

if ($null -eq $Expected)
{
{Resolve-Path -Path $Path -RelativeBasePath $BasePath -ErrorAction Stop} | Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.ResolvePathCommand"
Expand Down Expand Up @@ -114,4 +114,32 @@ Describe "Resolve-Path returns proper path" -Tag "CI" {
}
}
}

It 'returns filenames starting with period correctly' {
$testFile = Join-Path $TestDrive ".testfile"
$null = New-Item -Path $testFile -ItemType File -Force
try {
Push-Location $TestDrive
$result = Resolve-Path -Path ".testfile" -Relative
$result | Should -BeExactly (Join-Path '.' '.testfile')
}
finally {
Pop-Location
}
}

It 'does not return path containing both current and parent directory' {
$testDir = Join-Path $TestDrive "testDir"
$null = New-Item -Path $testDir -ItemType Directory -Force
$testFile = Join-Path $testDrive "testfile"
$null = New-Item -Path $testFile -ItemType File -Force
try {
Push-Location $testDir
$result = Resolve-Path -Path "..\testfile" -Relative
$result | Should -BeExactly (Join-Path '..' 'testfile')
}
finally {
Pop-Location
}
}
}