Skip to content

Commit d03a555

Browse files
authored
Fix Start-Job to check the existence of working directory using the PowerShell way (PowerShell#18675)
* Fix `Start-Job` to check the existence of working directory using the PowerShell way * Add a test * Update * Fix the test
1 parent 8686612 commit d03a555

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

src/System.Management.Automation/engine/remoting/commands/StartJob.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ public virtual ScriptBlock InitializationScript
484484
/// Gets or sets an initial working directory for the powershell background job.
485485
/// </summary>
486486
[Parameter]
487-
[ValidateNotNullOrEmpty]
487+
[ValidateNotNullOrWhiteSpace]
488488
public string WorkingDirectory { get; set; }
489489

490490
/// <summary>
@@ -601,7 +601,7 @@ protected override void BeginProcessing()
601601
ThrowTerminatingError(errorRecord);
602602
}
603603

604-
if (WorkingDirectory != null && !Directory.Exists(WorkingDirectory))
604+
if (WorkingDirectory != null && !InvokeProvider.Item.IsContainer(WorkingDirectory))
605605
{
606606
string message = StringUtil.Format(RemotingErrorIdStrings.StartJobWorkingDirectoryNotFound, WorkingDirectory);
607607
var errorRecord = new ErrorRecord(

test/powershell/engine/Job/Jobs.Tests.ps1

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ Describe 'Basic Job Tests' -Tags 'Feature' {
8686
$jobOutput | Should -BeExactly $path.ToString()
8787
}
8888

89+
It 'Can specify the working directory with a PSPath' {
90+
try {
91+
Push-Location 'Temp:\'
92+
$job = Start-Job -ScriptBlock { $PWD } -WorkingDirectory $pwd.Path | Wait-Job
93+
$jobOutput = Receive-Job $job
94+
$jobOutput | Should -BeExactly $pwd.Path
95+
} finally {
96+
Pop-Location
97+
}
98+
}
99+
89100
It 'Can use the user specified working directory parameter with quote' -Skip:($IsWindows) {
90101
$path = Join-Path -Path $TestDrive -ChildPath "My ""Dir"
91102
$null = New-Item -ItemType Directory -Path "$path"
@@ -100,9 +111,9 @@ Describe 'Basic Job Tests' -Tags 'Feature' {
100111
}
101112

102113
It 'Throws an error when the working directory parameter is <case>' -TestCases $invalidPathTestCases {
103-
param($path, $case, $expectedErrorId)
114+
param($path, $case, $errorId)
104115

105-
{Start-Job -ScriptBlock { 1 + 1 } -WorkingDirectory $path} | Should -Throw -ErrorId $expectedErrorId
116+
{Start-Job -ScriptBlock { 1 + 1 } -WorkingDirectory $path} | Should -Throw -ErrorId $errorId
106117
}
107118

108119
It 'Verifies that the current working directory is preserved' {

0 commit comments

Comments
 (0)