From 48884150f3d74667a71a3414527dbf223388ac8d Mon Sep 17 00:00:00 2001 From: kwkam Date: Mon, 30 Jul 2018 20:41:53 +0800 Subject: [PATCH 1/2] [Feature] PathUtils: handle literal -Path correctly Unescape non-literal, non-glob path before calling GetUnresolvedProviderPathFromPSPath since it only accepts literal path. This solve the issue where some commands depending on that method will treat -Path as literal unintentionally. For example, > Out-File -Path './`[out`].txt' -Append will create a new file named "`[out`].txt" instead of writing to "[out].txt". --- src/System.Management.Automation/utils/PathUtils.cs | 3 ++- .../Microsoft.PowerShell.Utility/Out-File.Tests.ps1 | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/utils/PathUtils.cs b/src/System.Management.Automation/utils/PathUtils.cs index 6c398af7494..f164d991102 100644 --- a/src/System.Management.Automation/utils/PathUtils.cs +++ b/src/System.Management.Automation/utils/PathUtils.cs @@ -295,7 +295,8 @@ internal static string ResolveFilePath(string filePath, PSCmdlet command, bool i PSDriveInfo drive = null; path = command.SessionState.Path.GetUnresolvedProviderPathFromPSPath( - filePath, cmdletProviderContext, out provider, out drive); + isLiteralPath ? filePath : WildcardPattern.Unescape(filePath), + cmdletProviderContext, out provider, out drive); cmdletProviderContext.ThrowFirstErrorOrDoNothing(); if (!provider.NameEquals(command.Context.ProviderNames.FileSystem)) { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Out-File.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Out-File.Tests.ps1 index 7a97528b574..e60a7767c6d 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Out-File.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Out-File.Tests.ps1 @@ -24,6 +24,7 @@ Describe "Out-File" -Tags "CI" { $expectedContent = "some test text" $inObject = New-Object psobject -Property @{text=$expectedContent} $testfile = Join-Path -Path $TestDrive -ChildPath outfileTest.txt + $testfileSp = Join-Path -Path $TestDrive -ChildPath "``[outfileTest``].txt" } AfterEach { @@ -90,6 +91,14 @@ Describe "Out-File" -Tags "CI" { $actual[10] | Should -BeNullOrEmpty } + It "Should create the file with correct name when FilePath contains special char" { + Out-File -FilePath $testfile + Out-File -FilePath $testfileSp + + $testfile | Should -Exist + $testfileSp | Should -Exist + } + It "Should limit each line to the specified number of characters when the width switch is used on objects" { Out-File -FilePath $testfile -Width 10 -InputObject $inObject From 0227f7ef1226c9da87d81422cad10843a19df58e Mon Sep 17 00:00:00 2001 From: kwkam Date: Mon, 30 Jul 2018 23:38:17 +0800 Subject: [PATCH 2/2] [Feature] Fix code format --- src/System.Management.Automation/utils/PathUtils.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/utils/PathUtils.cs b/src/System.Management.Automation/utils/PathUtils.cs index f164d991102..7fd044396aa 100644 --- a/src/System.Management.Automation/utils/PathUtils.cs +++ b/src/System.Management.Automation/utils/PathUtils.cs @@ -296,7 +296,9 @@ internal static string ResolveFilePath(string filePath, PSCmdlet command, bool i path = command.SessionState.Path.GetUnresolvedProviderPathFromPSPath( isLiteralPath ? filePath : WildcardPattern.Unescape(filePath), - cmdletProviderContext, out provider, out drive); + cmdletProviderContext, + out provider, + out drive); cmdletProviderContext.ThrowFirstErrorOrDoNothing(); if (!provider.NameEquals(command.Context.ProviderNames.FileSystem)) {