Skip to content

Commit

Permalink
RemoteProcess: change ReadToEndAsStringAsync nullability. (#323)
Browse files Browse the repository at this point in the history
The nullable return value force users to deal with nullability.
Rather than return null for stdout/stderr that gets skipped,
change to return an empty string.
  • Loading branch information
tmds authored Jan 17, 2025
1 parent 91c9cdd commit d3b7cf2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class RemoteProcess : IDisposable
{
// Read from the remote process.
ValueTask<(bool isError, string? line)> ReadLineAsync(bool readStdout = true, bool readStderr = true, CancellationToken cancellationToken = default);
ValueTask<(string? stdout, string? stderr)> ReadToEndAsStringAsync(bool readStdout = true, bool readStderr = true, CancellationToken cancellationToken = default);
ValueTask<(string stdout, string stderr)> ReadToEndAsStringAsync(bool readStdout = true, bool readStderr = true, CancellationToken cancellationToken = default);
IAsyncEnumerable<(bool isError, string line)> ReadAllLinesAsync(bool readStdout = true, bool readStderr = true, CancellationToken cancellationToken = default);
ValueTask ReadToEndAsync(Stream? stdoutStream, Stream? stderrStream, CancellationToken? cancellationToken);
ValueTask<(bool isError, int bytesRead)> ReadAsync(Memory<byte>? stdoutBuffer, Memory<byte>? stderrBuffer, CancellationToken cancellationToken = default);
Expand Down
6 changes: 3 additions & 3 deletions src/Tmds.Ssh/RemoteProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public ValueTask WaitForExitAsync(CancellationToken cancellationToken = default)
return ReadToEndAsync(null, null, null, null, cancellationToken);
}

public async ValueTask<(string? stdout, string? stderr)> ReadToEndAsStringAsync(bool readStdout = true, bool readStderr = true, CancellationToken cancellationToken = default)
public async ValueTask<(string stdout, string stderr)> ReadToEndAsStringAsync(bool readStdout = true, bool readStderr = true, CancellationToken cancellationToken = default)
{
CheckReadMode(ReadMode.ReadChars);

Expand All @@ -367,8 +367,8 @@ public ValueTask WaitForExitAsync(CancellationToken cancellationToken = default)
if (readType == ProcessReadType.ProcessExit)
{
_readMode = ReadMode.Exited;
string? stdout = readStdout ? _stdoutBuffer.BuildString() : null;
string? stderr = readStderr ? _stderrBuffer.BuildString() : null;
string stdout = readStdout ? _stdoutBuffer.BuildString()! : "";
string stderr = readStderr ? _stderrBuffer.BuildString()! : "";
return (stdout, stderr);
}
}
Expand Down
10 changes: 5 additions & 5 deletions test/Tmds.Ssh.Tests/KerberosTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ await _kerberosExecutor.RunAsync(

{
using var process = await client.ExecuteAsync("whoami");
(string? stdout, string? stderr) = await process.ReadToEndAsStringAsync();
(string stdout, string stderr) = await process.ReadToEndAsStringAsync();
Assert.Equal(0, process.ExitCode);
Assert.Equal(userName, stdout?.Trim());
Assert.Equal(userName, stdout.Trim());
}
},
[ connectionName, targetName ?? string.Empty, _sshServer.KnownHostsFilePath, userName, _sshServer.TestKerberosCredential.UserName, _sshServer.TestKerberosCredential.Password ]
Expand Down Expand Up @@ -163,14 +163,14 @@ await _kerberosExecutor.RunAsync(

{
using var process = await client.ExecuteAsync("whoami");
(string? stdout, string? stderr) = await process.ReadToEndAsStringAsync();
(string stdout, string stderr) = await process.ReadToEndAsStringAsync();
Assert.Equal(0, process.ExitCode);
Assert.Equal(userName, stdout?.Trim());
Assert.Equal(userName, stdout.Trim());
}

{
using var process = await client.ExecuteAsync("klist -f");
(string? stdout, string? stderr) = await process.ReadToEndAsStringAsync();
(string stdout, string stderr) = await process.ReadToEndAsStringAsync();

if (requestDelegate)
{
Expand Down
6 changes: 3 additions & 3 deletions test/Tmds.Ssh.Tests/RemoteProcessTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,9 @@ public async Task ReadToEndAsStringAsync(bool readStdout, bool readStderr)
await process.WriteLineAsync("echo -n 'hello stderr2' >&2");
await process.WriteLineAsync("exit 0");

(string? stdout, string? stderr) = await process.ReadToEndAsStringAsync(readStdout, readStderr);
Assert.Equal(readStdout ? "hello stdout1hello stdout2" : null, stdout);
Assert.Equal(readStderr ? "hello stderr1hello stderr2" : null, stderr);
(string stdout, string stderr) = await process.ReadToEndAsStringAsync(readStdout, readStderr);
Assert.Equal(readStdout ? "hello stdout1hello stdout2" : "", stdout);
Assert.Equal(readStderr ? "hello stderr1hello stderr2" : "", stderr);
}

[Theory]
Expand Down

0 comments on commit d3b7cf2

Please sign in to comment.