From 6cc21cb9bde2a0baa2db43b9d33c8faf342d4718 Mon Sep 17 00:00:00 2001 From: kwkam Date: Fri, 22 Dec 2017 23:27:47 +0800 Subject: [PATCH 01/11] commands: make rvpa -relative do not return ./absolute_path This happens on Windows when $pwd and -path is on different drive --- .../commands/management/ResolvePathCommand.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ResolvePathCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ResolvePathCommand.cs index d1222aa6596..c6717d43f41 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ResolvePathCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ResolvePathCommand.cs @@ -109,7 +109,8 @@ protected override void ProcessRecord() { string adjustedPath = SessionState.Path.NormalizeRelativePath(currentPath.Path, SessionState.Path.CurrentLocation.ProviderPath); - if (!adjustedPath.StartsWith(".", StringComparison.OrdinalIgnoreCase)) + if (currentPath.Drive == SessionState.Path.CurrentLocation.Drive && + !adjustedPath.StartsWith(".", StringComparison.OrdinalIgnoreCase)) { adjustedPath = SessionState.Path.Combine(".", adjustedPath); } From 8ee29ba081b6c72850ee636a261304d909e8d4fb Mon Sep 17 00:00:00 2001 From: kwkam Date: Tue, 13 Feb 2018 15:51:31 +0800 Subject: [PATCH 02/11] [Feature] add test --- .../Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 index e80671b7e3e..f3ecbc20454 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 @@ -23,4 +23,7 @@ Describe "Resolve-Path returns proper path" -Tag "CI" { $result = Resolve-Path -LiteralPath "TestDrive:\\\\\" ($result.Path.TrimEnd('/\')) | Should Be "TestDrive:" } + It "Resolve-Path -Relative should return correct path on different drive" -Skip:(!$IsWindows) { + Resolve-Path -Path "HKCU:\Software" -Relative | Should Be "HKCU:\Software" + } } From 2529663a006a39e74e3f026fc0b2a483ff0f7276 Mon Sep 17 00:00:00 2001 From: kwkam Date: Thu, 22 Feb 2018 12:58:47 +0800 Subject: [PATCH 03/11] fixup! [Feature] add test --- .../Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 index f3ecbc20454..9df152e7c07 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 @@ -23,7 +23,9 @@ Describe "Resolve-Path returns proper path" -Tag "CI" { $result = Resolve-Path -LiteralPath "TestDrive:\\\\\" ($result.Path.TrimEnd('/\')) | Should Be "TestDrive:" } - It "Resolve-Path -Relative should return correct path on different drive" -Skip:(!$IsWindows) { - Resolve-Path -Path "HKCU:\Software" -Relative | Should Be "HKCU:\Software" + It "Resolve-Path -Relative should return correct path on different drive" { + $item = Join-Path "TestDrive:" "ResolvePath.relative" + $null = New-Item -Path $item -ItemType File -Force + Resolve-Path -Path $item -Relative | Should Be $item } } From e30fbf52d9936eb4a33e52c3adb6133b77f44d9a Mon Sep 17 00:00:00 2001 From: kwkam Date: Fri, 23 Feb 2018 19:13:34 +0800 Subject: [PATCH 04/11] only return relative path inside current root --- .../commands/management/ResolvePathCommand.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ResolvePathCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ResolvePathCommand.cs index c6717d43f41..3f2bfebe88c 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ResolvePathCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ResolvePathCommand.cs @@ -107,9 +107,18 @@ 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 && + !currentPath.ProviderPath.StartsWith(SessionState.Path.CurrentLocation.Drive.Root)) + { + WriteObject(currentPath.Path, false); + continue; + } string adjustedPath = SessionState.Path.NormalizeRelativePath(currentPath.Path, SessionState.Path.CurrentLocation.ProviderPath); - if (currentPath.Drive == SessionState.Path.CurrentLocation.Drive && + // Do not insert './' if result path is not relative + if (!adjustedPath.StartsWith(currentPath.Drive.Root, StringComparison.OrdinalIgnoreCase) && !adjustedPath.StartsWith(".", StringComparison.OrdinalIgnoreCase)) { adjustedPath = SessionState.Path.Combine(".", adjustedPath); From ca6f9771ee10b97cbd514041e865cb6fea0b569e Mon Sep 17 00:00:00 2001 From: kwkam Date: Fri, 23 Feb 2018 22:55:42 +0800 Subject: [PATCH 05/11] change test --- .../Resolve-Path.Tests.ps1 | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 index 9df152e7c07..cc5215a02ae 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 @@ -24,8 +24,29 @@ Describe "Resolve-Path returns proper path" -Tag "CI" { ($result.Path.TrimEnd('/\')) | Should Be "TestDrive:" } It "Resolve-Path -Relative should return correct path on different drive" { - $item = Join-Path "TestDrive:" "ResolvePath.relative" - $null = New-Item -Path $item -ItemType File -Force - Resolve-Path -Path $item -Relative | Should Be $item + $base = Join-Path "TestDrive:" "ResolvePath.relative" + $root = Join-Path $base "fakeroot" + $file = Join-Path $root "file.txt" + $driveName = "RvpaTest" + $null = New-Item -Path $base -ItemType Directory -Force + $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 + $driveRoot = Join-Path "$driveName`:" "" + $driveFile = Join-Path "$driveName`:" "file.txt" + try { + Push-Location -Path $driveRoot + Resolve-Path -Path $base -Relative | Should Be $base + } + finally { + Pop-Location + } + try { + Push-Location -Path $base + Resolve-Path -Path $driveFile -Relative | Should Be $(Resolve-Path -Path $file -Relative) + } + finally { + Pop-Location + } } } From 095ccd5027ba239914253571a03ffd05e3cbf0ef Mon Sep 17 00:00:00 2001 From: kwkam Date: Sat, 3 Mar 2018 18:13:40 +0800 Subject: [PATCH 06/11] [Feature] update test --- .../Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 index cc5215a02ae..f54c06f9b62 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 @@ -24,9 +24,10 @@ Describe "Resolve-Path returns proper path" -Tag "CI" { ($result.Path.TrimEnd('/\')) | Should Be "TestDrive:" } It "Resolve-Path -Relative should return correct path on different drive" { - $base = Join-Path "TestDrive:" "ResolvePath.relative" + $base = Join-Path $TestDrive "ResolvePath.relative" $root = Join-Path $base "fakeroot" $file = Join-Path $root "file.txt" + $expectedFilePath = Join-Path "." "fakeroot" "file.txt" $driveName = "RvpaTest" $null = New-Item -Path $base -ItemType Directory -Force $null = New-Item -Path $root -ItemType Directory -Force @@ -36,14 +37,14 @@ Describe "Resolve-Path returns proper path" -Tag "CI" { $driveFile = Join-Path "$driveName`:" "file.txt" try { Push-Location -Path $driveRoot - Resolve-Path -Path $base -Relative | Should Be $base + Resolve-Path -Path $base -Relative | Should BeExactly $base } finally { Pop-Location } try { Push-Location -Path $base - Resolve-Path -Path $driveFile -Relative | Should Be $(Resolve-Path -Path $file -Relative) + Resolve-Path -Path $driveFile -Relative | Should BeExactly $expectedFilePath } finally { Pop-Location From d7d18d916c2542e8e45a5a8d5accf958a8be21eb Mon Sep 17 00:00:00 2001 From: kwkam Date: Sat, 10 Mar 2018 21:13:21 +0800 Subject: [PATCH 07/11] [feature] remove drive after test --- .../Resolve-Path.Tests.ps1 | 44 ++++++++++--------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 index f54c06f9b62..ceb7b6c2d9e 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 @@ -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 -PSProvider FileSystem + } It "Resolve-Path returns resolved paths" { Resolve-Path $TESTDRIVE | Should be "$TESTDRIVE" } @@ -23,28 +42,11 @@ Describe "Resolve-Path returns proper path" -Tag "CI" { $result = Resolve-Path -LiteralPath "TestDrive:\\\\\" ($result.Path.TrimEnd('/\')) | Should Be "TestDrive:" } - It "Resolve-Path -Relative should return correct path on different drive" { - $base = Join-Path $TestDrive "ResolvePath.relative" - $root = Join-Path $base "fakeroot" - $file = Join-Path $root "file.txt" - $expectedFilePath = Join-Path "." "fakeroot" "file.txt" - $driveName = "RvpaTest" - $null = New-Item -Path $base -ItemType Directory -Force - $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 - $driveRoot = Join-Path "$driveName`:" "" - $driveFile = Join-Path "$driveName`:" "file.txt" - try { - Push-Location -Path $driveRoot - Resolve-Path -Path $base -Relative | Should BeExactly $base - } - finally { - Pop-Location - } + It "Resolve-Path -Relative should return correct path on different drive" -TestCases $relCases { + param($wd, $target, $expected) try { - Push-Location -Path $base - Resolve-Path -Path $driveFile -Relative | Should BeExactly $expectedFilePath + Push-Location -Path $wd + Resolve-Path -Path $target -Relative | Should BeExactly $expected } finally { Pop-Location From ee4faa0264faf423e6bc9bb234c10d4c860cbb23 Mon Sep 17 00:00:00 2001 From: kwkam Date: Sat, 10 Mar 2018 21:14:24 +0800 Subject: [PATCH 08/11] [feature] use named parameters --- .../commands/management/ResolvePathCommand.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ResolvePathCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ResolvePathCommand.cs index 3f2bfebe88c..a7c453af783 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ResolvePathCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ResolvePathCommand.cs @@ -112,7 +112,7 @@ protected override void ProcessRecord() if (currentPath.Drive != SessionState.Path.CurrentLocation.Drive && !currentPath.ProviderPath.StartsWith(SessionState.Path.CurrentLocation.Drive.Root)) { - WriteObject(currentPath.Path, false); + WriteObject(sendToPipeline: currentPath.Path, enumerateCollection: false); continue; } string adjustedPath = SessionState.Path.NormalizeRelativePath(currentPath.Path, @@ -123,7 +123,7 @@ protected override void ProcessRecord() { adjustedPath = SessionState.Path.Combine(".", adjustedPath); } - WriteObject(adjustedPath, false); + WriteObject(sendToPipeline: adjustedPath, enumerateCollection: false); } } } @@ -162,7 +162,7 @@ protected override void ProcessRecord() if (!_relative) { - WriteObject(result, true); + WriteObject(sendToPipeline: result, enumerateCollection: true); } } } // ProcessRecord From 9d6c156a8c9c7e56771f79ee984a1d28f1c79b60 Mon Sep 17 00:00:00 2001 From: kwkam Date: Thu, 15 Mar 2018 12:45:15 +0800 Subject: [PATCH 09/11] [feature] fix parameter and test --- .../commands/management/ResolvePathCommand.cs | 6 +++--- .../Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ResolvePathCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ResolvePathCommand.cs index a7c453af783..51112362f98 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ResolvePathCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ResolvePathCommand.cs @@ -112,7 +112,7 @@ protected override void ProcessRecord() if (currentPath.Drive != SessionState.Path.CurrentLocation.Drive && !currentPath.ProviderPath.StartsWith(SessionState.Path.CurrentLocation.Drive.Root)) { - WriteObject(sendToPipeline: currentPath.Path, enumerateCollection: false); + WriteObject(currentPath.Path, enumerateCollection: false); continue; } string adjustedPath = SessionState.Path.NormalizeRelativePath(currentPath.Path, @@ -123,7 +123,7 @@ protected override void ProcessRecord() { adjustedPath = SessionState.Path.Combine(".", adjustedPath); } - WriteObject(sendToPipeline: adjustedPath, enumerateCollection: false); + WriteObject(adjustedPath, enumerateCollection: false); } } } @@ -162,7 +162,7 @@ protected override void ProcessRecord() if (!_relative) { - WriteObject(sendToPipeline: result, enumerateCollection: true); + WriteObject(result, enumerateCollection: true); } } } // ProcessRecord diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 index ceb7b6c2d9e..1068c030c69 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 @@ -16,7 +16,7 @@ Describe "Resolve-Path returns proper path" -Tag "CI" { ) } AfterAll { - Remove-PSDrive -Name $driveName -PSProvider FileSystem + Remove-PSDrive -Name $driveName } It "Resolve-Path returns resolved paths" { Resolve-Path $TESTDRIVE | Should be "$TESTDRIVE" @@ -42,7 +42,7 @@ Describe "Resolve-Path returns proper path" -Tag "CI" { $result = Resolve-Path -LiteralPath "TestDrive:\\\\\" ($result.Path.TrimEnd('/\')) | Should Be "TestDrive:" } - It "Resolve-Path -Relative should return correct path on different drive" -TestCases $relCases { + It "Resolve-Path -Relative '' should return correct path on ''" -TestCases $relCases { param($wd, $target, $expected) try { Push-Location -Path $wd From dd76ce8387f6034bee8363221913215ef0d487cf Mon Sep 17 00:00:00 2001 From: kwkam Date: Thu, 15 Mar 2018 13:44:15 +0800 Subject: [PATCH 10/11] [Feature] More fixes for test --- .../Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 index 1068c030c69..d651d5f0861 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Resolve-Path.Tests.ps1 @@ -16,7 +16,7 @@ Describe "Resolve-Path returns proper path" -Tag "CI" { ) } AfterAll { - Remove-PSDrive -Name $driveName + Remove-PSDrive -Name $driveName -Force } It "Resolve-Path returns resolved paths" { Resolve-Path $TESTDRIVE | Should be "$TESTDRIVE" @@ -46,7 +46,7 @@ Describe "Resolve-Path returns proper path" -Tag "CI" { param($wd, $target, $expected) try { Push-Location -Path $wd - Resolve-Path -Path $target -Relative | Should BeExactly $expected + Resolve-Path -Path $target -Relative | Should -BeExactly $expected } finally { Pop-Location From 4c5d4509693d7e21224625d0ecdabf1752544cd8 Mon Sep 17 00:00:00 2001 From: kwkam Date: Fri, 16 Mar 2018 09:41:08 +0800 Subject: [PATCH 11/11] [Feature] Check if Drive is null --- .../commands/management/ResolvePathCommand.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/ResolvePathCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/ResolvePathCommand.cs index 51112362f98..27f0147325f 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/ResolvePathCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/ResolvePathCommand.cs @@ -110,7 +110,9 @@ protected override void ProcessRecord() // 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 && - !currentPath.ProviderPath.StartsWith(SessionState.Path.CurrentLocation.Drive.Root)) + SessionState.Path.CurrentLocation.Drive != null && + !currentPath.ProviderPath.StartsWith( + SessionState.Path.CurrentLocation.Drive.Root, StringComparison.OrdinalIgnoreCase)) { WriteObject(currentPath.Path, enumerateCollection: false); continue; @@ -118,7 +120,8 @@ protected override void ProcessRecord() string adjustedPath = SessionState.Path.NormalizeRelativePath(currentPath.Path, SessionState.Path.CurrentLocation.ProviderPath); // Do not insert './' if result path is not relative - if (!adjustedPath.StartsWith(currentPath.Drive.Root, StringComparison.OrdinalIgnoreCase) && + if (!adjustedPath.StartsWith( + currentPath.Drive?.Root ?? currentPath.Path, StringComparison.OrdinalIgnoreCase) && !adjustedPath.StartsWith(".", StringComparison.OrdinalIgnoreCase)) { adjustedPath = SessionState.Path.Combine(".", adjustedPath);