Skip to content

Add Directory.Exists() check to SetInitialWorkingDirectoryAsync() #1851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2022
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));
}
}
}