mirror of
https://github.com/sshnet/SSH.NET.git
synced 2026-10-29 07:07:02 +08:00
fix S6966: Awaitable method should be used
Introduced abstractions for CancellationTokenSource.CancelAsync() and Stream.DisposeAsync() to avoid #ifdef. Supressed pipeStream.WriteAsync because it deadlocks the test. I assume because PipeStream doesn't override WriteAsync. https://rules.sonarsource.com/csharp/RSPEC-6966/ temp
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Renci.SshNet.Abstractions
|
||||
{
|
||||
internal static class CancellationTokenSourceExtensions
|
||||
{
|
||||
#if !NET8_OR_GREATER
|
||||
public static Task CancelAsync(this CancellationTokenSource cancellationTokenSource)
|
||||
{
|
||||
cancellationTokenSource.Cancel();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#if !NET && !NETSTANDARD2_1_OR_GREATER
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
#endif
|
||||
|
||||
namespace Renci.SshNet.Abstractions
|
||||
{
|
||||
internal static class StreamExtensions
|
||||
{
|
||||
#if !NET && !NETSTANDARD2_1_OR_GREATER
|
||||
public static ValueTask DisposeAsync(this Stream stream)
|
||||
{
|
||||
stream.Dispose();
|
||||
return default;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
#if !NET8_0_OR_GREATER
|
||||
using Renci.SshNet.Abstractions;
|
||||
#endif
|
||||
using Renci.SshNet.Common;
|
||||
|
||||
namespace Renci.SshNet.IntegrationTests.OldIntegrationTests
|
||||
@@ -130,7 +133,7 @@ namespace Renci.SshNet.IntegrationTests.OldIntegrationTests
|
||||
|
||||
Task executeTask = cmd.ExecuteAsync(cts.Token);
|
||||
|
||||
cts.Cancel();
|
||||
await cts.CancelAsync();
|
||||
|
||||
var tce = await Assert.ThrowsExceptionAsync<TaskCanceledException>(() => executeTask);
|
||||
Assert.AreSame(executeTask, tce.Task);
|
||||
|
||||
@@ -318,7 +318,7 @@ namespace Renci.SshNet.IntegrationTests
|
||||
{
|
||||
using (var cmd = sshClient.CreateCommand("chmod 777 " + remoteFile))
|
||||
{
|
||||
cmd.Execute();
|
||||
await cmd.ExecuteAsync();
|
||||
|
||||
Assert.AreEqual(0, cmd.ExitStatus, cmd.Error);
|
||||
}
|
||||
@@ -333,7 +333,7 @@ namespace Renci.SshNet.IntegrationTests
|
||||
{
|
||||
var lines = new List<string>();
|
||||
string line = null;
|
||||
while ((line = reader.ReadLine()) != null)
|
||||
while ((line = await reader.ReadLineAsync()) != null)
|
||||
{
|
||||
lines.Add(line);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,10 @@
|
||||
using DotNet.Testcontainers.Containers;
|
||||
using DotNet.Testcontainers.Images;
|
||||
|
||||
#if !NET && !NETSTANDARD2_1_OR_GREATER
|
||||
using Renci.SshNet.Abstractions;
|
||||
#endif
|
||||
|
||||
namespace Renci.SshNet.IntegrationTests.TestsFixtures
|
||||
{
|
||||
public sealed class InfrastructureFixture : IDisposable
|
||||
@@ -80,8 +84,8 @@ namespace Renci.SshNet.IntegrationTests.TestsFixtures
|
||||
await _sshServerImage.DisposeAsync();
|
||||
}
|
||||
|
||||
_fsOut.Dispose();
|
||||
_fsErr.Dispose();
|
||||
await _fsOut.DisposeAsync();
|
||||
await _fsErr.DisposeAsync();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
||||
@@ -4,6 +4,9 @@ using System.Threading.Tasks;
|
||||
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
#if !NET && !NETSTANDARD2_1_OR_GREATER
|
||||
using Renci.SshNet.Abstractions;
|
||||
#endif
|
||||
using Renci.SshNet.Common;
|
||||
using Renci.SshNet.Tests.Common;
|
||||
|
||||
@@ -93,7 +96,7 @@ namespace Renci.SshNet.Tests.Classes.Common
|
||||
|
||||
Assert.IsFalse(readTask.IsCompleted);
|
||||
|
||||
pipeStream.Dispose();
|
||||
await pipeStream.DisposeAsync();
|
||||
|
||||
Assert.AreEqual(0, await readTask);
|
||||
}
|
||||
@@ -111,7 +114,7 @@ namespace Renci.SshNet.Tests.Classes.Common
|
||||
|
||||
Assert.IsFalse(readTask.IsCompleted);
|
||||
|
||||
pipeStream.Dispose();
|
||||
await pipeStream.DisposeAsync();
|
||||
|
||||
Assert.AreEqual(0, await readTask);
|
||||
}
|
||||
@@ -130,7 +133,10 @@ namespace Renci.SshNet.Tests.Classes.Common
|
||||
|
||||
Assert.IsFalse(readTask.IsCompleted);
|
||||
|
||||
// not using WriteAsync here because it deadlocks the test
|
||||
#pragma warning disable S6966 // Awaitable method should be used
|
||||
pipeStream.Write(new byte[] { 1, 2, 3, 4 }, 0, 4);
|
||||
#pragma warning restore S6966 // Awaitable method should be used
|
||||
|
||||
Assert.AreEqual(0, await readTask);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user