From ae892e2fe8c892c53f5bd69d3e62a64f04cf035c 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 2177f89dbb3..65b89a6f25e 100644 --- a/src/System.Management.Automation/utils/PathUtils.cs +++ b/src/System.Management.Automation/utils/PathUtils.cs @@ -293,7 +293,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 b438c30a372..c58b7874bdb 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 { @@ -91,6 +92,14 @@ Describe "Out-File" -Tags "CI" { $actual[11] | 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 90d6f52f57f2b647fe19be8790041203da90eefb 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 65b89a6f25e..59061902c3a 100644 --- a/src/System.Management.Automation/utils/PathUtils.cs +++ b/src/System.Management.Automation/utils/PathUtils.cs @@ -294,7 +294,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)) {