Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,11 @@ internal Task LoadHostProfilesAsync(CancellationToken cancellationToken)

public Task SetInitialWorkingDirectoryAsync(string path, CancellationToken cancellationToken)
{
return ExecutePSCommandAsync(
new PSCommand().AddCommand("Set-Location").AddParameter("LiteralPath", path),
cancellationToken);
return Directory.Exists(path)
? ExecutePSCommandAsync(
new PSCommand().AddCommand("Set-Location").AddParameter("LiteralPath", path),
cancellationToken)
: Task.CompletedTask;
}

private void Run()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,20 @@ await psesHost.ExecutePSCommandAsync(
new PSCommand().AddScript("\"protocol=https`nhost=myhost.com`nusername=john`npassword=doe`n`n\" | git.exe credential approve; if ($LastExitCode) { throw }"),
CancellationToken.None).ConfigureAwait(true);
}

[Theory]
[InlineData("")] // Regression test for "unset" path.
[InlineData("C:\\Some\\Bad\\Directory")] // Non-existent directory.
[InlineData("testhost.dll")] // Existent file.
public async Task CanHandleBadInitialWorkingDirectory(string path)
{
string cwd = Environment.CurrentDirectory;
await psesHost.SetInitialWorkingDirectoryAsync(path, CancellationToken.None).ConfigureAwait(true);

IReadOnlyList<string> getLocation = await psesHost.ExecutePSCommandAsync<string>(
new PSCommand().AddCommand("Get-Location"),
CancellationToken.None).ConfigureAwait(true);
Assert.Collection(getLocation, (d) => Assert.Equal(cwd, d, ignoreCase: true));
}
}
}