From 8a069275ff2df5cdf888d5bcdd33af7d2f1258fc Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Wed, 30 Oct 2019 16:41:30 -0700 Subject: [PATCH 1/4] Fix `Start-Job -WorkingDirectory` with whitespace --- .../engine/hostifaces/PowerShellProcessInstance.cs | 6 +++--- test/powershell/engine/Job/Jobs.Tests.ps1 | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs b/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs index 2ec10fa7705..747c5c09e92 100644 --- a/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs +++ b/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs @@ -41,7 +41,7 @@ static PowerShellProcessInstance() } /// - /// Initializes a new instance of the class. Initializes the underlying dotnet process class. + /// Initializes a new instance of the class. Initializes the underlying dotnet process class. /// /// Specifies the version of powershell. /// Specifies a user account credentials. @@ -56,7 +56,7 @@ public PowerShellProcessInstance(Version powerShellVersion, PSCredential credent { processArguments = string.Format( CultureInfo.InvariantCulture, - "{0} -wd {1}", + "{0} -wd \"{1}\"", processArguments, workingDirectory); } @@ -105,7 +105,7 @@ public PowerShellProcessInstance(Version powerShellVersion, PSCredential credent } /// - /// Initializes a new instance of the class. Initializes the underlying dotnet process class. + /// Initializes a new instance of the class. Initializes the underlying dotnet process class. /// /// /// diff --git a/test/powershell/engine/Job/Jobs.Tests.ps1 b/test/powershell/engine/Job/Jobs.Tests.ps1 index 4e0332afdbb..abc0126ff50 100644 --- a/test/powershell/engine/Job/Jobs.Tests.ps1 +++ b/test/powershell/engine/Job/Jobs.Tests.ps1 @@ -78,9 +78,11 @@ Describe 'Basic Job Tests' -Tags 'CI' { } It 'Can use the user specified working directory parameter' { - $job = Start-Job -ScriptBlock { $pwd } -WorkingDirectory $TestDrive | Wait-Job + $path = Join-Path -Path $TestDrive -ChildPath "My Dir" + $null = New-Item -ItemType Directory -Path $path + $job = Start-Job -ScriptBlock { $pwd } -WorkingDirectory $path | Wait-Job $jobOutput = Receive-Job $job - $jobOutput | Should -BeExactly $TestDrive.ToString() + $jobOutput | Should -BeExactly $path.ToString() } It 'Throws an error when the working directory parameter is ' -TestCases $invalidPathTestCases { From ef8c4ed00e9dc928f68fabbe780536b312cba511 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 31 Oct 2019 09:15:26 -0700 Subject: [PATCH 2/4] escape workingdirectory that may contain double quotes --- .../engine/hostifaces/PowerShellProcessInstance.cs | 2 +- test/powershell/engine/Job/Jobs.Tests.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs b/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs index 747c5c09e92..83d5ce7a717 100644 --- a/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs +++ b/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs @@ -58,7 +58,7 @@ public PowerShellProcessInstance(Version powerShellVersion, PSCredential credent CultureInfo.InvariantCulture, "{0} -wd \"{1}\"", processArguments, - workingDirectory); + workingDirectory.Replace("\"","\"\"")); } if (initializationScript != null) diff --git a/test/powershell/engine/Job/Jobs.Tests.ps1 b/test/powershell/engine/Job/Jobs.Tests.ps1 index abc0126ff50..5516fb88bbc 100644 --- a/test/powershell/engine/Job/Jobs.Tests.ps1 +++ b/test/powershell/engine/Job/Jobs.Tests.ps1 @@ -78,7 +78,7 @@ Describe 'Basic Job Tests' -Tags 'CI' { } It 'Can use the user specified working directory parameter' { - $path = Join-Path -Path $TestDrive -ChildPath "My Dir" + $path = Join-Path -Path $TestDrive -ChildPath "My ""Dir" $null = New-Item -ItemType Directory -Path $path $job = Start-Job -ScriptBlock { $pwd } -WorkingDirectory $path | Wait-Job $jobOutput = Receive-Job $job From 5ec34c22c63ab6eb90c3fbd60f32f2d1848d50ef Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Thu, 31 Oct 2019 12:15:26 -0700 Subject: [PATCH 3/4] split whitespace and quote test since Windows doesn't allow quote in path --- test/powershell/engine/Job/Jobs.Tests.ps1 | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/test/powershell/engine/Job/Jobs.Tests.ps1 b/test/powershell/engine/Job/Jobs.Tests.ps1 index 5516fb88bbc..cdccbf6b253 100644 --- a/test/powershell/engine/Job/Jobs.Tests.ps1 +++ b/test/powershell/engine/Job/Jobs.Tests.ps1 @@ -77,9 +77,17 @@ Describe 'Basic Job Tests' -Tags 'CI' { $ProgressMsg[0].StatusDescription | Should -BeExactly 2 } - It 'Can use the user specified working directory parameter' { + It 'Can use the user specified working directory parameter with whitespace' { + $path = Join-Path -Path $TestDrive -ChildPath "My Dir" + $null = New-Item -ItemType Directory -Path "$path" + $job = Start-Job -ScriptBlock { $pwd } -WorkingDirectory $path | Wait-Job + $jobOutput = Receive-Job $job + $jobOutput | Should -BeExactly $path.ToString() + } + + It 'Can use the user specified working directory parameter with quote' -Skip:($IsWindows) { $path = Join-Path -Path $TestDrive -ChildPath "My ""Dir" - $null = New-Item -ItemType Directory -Path $path + $null = New-Item -ItemType Directory -Path "$path" $job = Start-Job -ScriptBlock { $pwd } -WorkingDirectory $path | Wait-Job $jobOutput = Receive-Job $job $jobOutput | Should -BeExactly $path.ToString() From e2ffc4232a3d841348538af709140e94ed7e4fb7 Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Thu, 31 Oct 2019 12:21:08 -0700 Subject: [PATCH 4/4] address CodeFactor --- .../engine/hostifaces/PowerShellProcessInstance.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs b/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs index 83d5ce7a717..f0f71454630 100644 --- a/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs +++ b/src/System.Management.Automation/engine/hostifaces/PowerShellProcessInstance.cs @@ -58,7 +58,7 @@ public PowerShellProcessInstance(Version powerShellVersion, PSCredential credent CultureInfo.InvariantCulture, "{0} -wd \"{1}\"", processArguments, - workingDirectory.Replace("\"","\"\"")); + workingDirectory.Replace("\"", "\"\"")); } if (initializationScript != null)