Replace custom TestMethodForPlatformAttribute with MSTest OSCondition (#1766)

* Initial plan

* Replace TestMethodForPlatformAttribute with OSCondition attribute

Co-authored-by: Rob-Hague <5132141+Rob-Hague@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Rob-Hague <5132141+Rob-Hague@users.noreply.github.com>
This commit is contained in:
Copilot
2026-03-01 18:02:08 +01:00
committed by GitHub
parent 085dead8f5
commit 41dde35d74
7 changed files with 28 additions and 65 deletions
@@ -3,14 +3,12 @@ using System.Diagnostics;
using System.Globalization;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Common;
using Renci.SshNet.Tests.Common;
namespace Renci.SshNet.Tests.Classes.Connection
{
@@ -72,7 +70,8 @@ namespace Renci.SshNet.Tests.Classes.Connection
}
}
[TestMethodForPlatform(nameof(OSPlatform.Windows))]
[TestMethod]
[OSCondition(OperatingSystems.Windows)]
public void ConnectShouldHaveThrownSshOperationTimeoutExceptionOnWindows()
{
Assert.IsNull(_actualException.InnerException);
@@ -80,7 +79,8 @@ namespace Renci.SshNet.Tests.Classes.Connection
Assert.AreEqual(string.Format(CultureInfo.InvariantCulture, "Connection failed to establish within {0} milliseconds.", _connectionInfo.Timeout.TotalMilliseconds), _actualException.Message);
}
[TestMethodForPlatform(nameof(OSPlatform.Linux))]
[TestMethod]
[OSCondition(OperatingSystems.Linux)]
public void ConnectShouldHaveThrownSocketExceptionOnLinux()
{
Assert.IsNull(_actualException.InnerException);
@@ -88,7 +88,8 @@ namespace Renci.SshNet.Tests.Classes.Connection
Assert.AreEqual("Connection refused", _actualException.Message);
}
[TestMethodForPlatform(nameof(OSPlatform.Windows))]
[TestMethod]
[OSCondition(OperatingSystems.Windows)]
public void ConnectShouldHaveRespectedTimeoutOnWindows()
{
var errorText = string.Format("Elapsed: {0}, Timeout: {1}",
@@ -3,14 +3,12 @@ using System.Diagnostics;
using System.Globalization;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Common;
using Renci.SshNet.Tests.Common;
namespace Renci.SshNet.Tests.Classes.Connection
{
@@ -82,7 +80,8 @@ namespace Renci.SshNet.Tests.Classes.Connection
}
}
[TestMethodForPlatform(nameof(OSPlatform.Windows))]
[TestMethod]
[OSCondition(OperatingSystems.Windows)]
public void ConnectShouldHaveThrownSshOperationTimeoutExceptionOnWindows()
{
Assert.IsNull(_actualException.InnerException);
@@ -90,7 +89,8 @@ namespace Renci.SshNet.Tests.Classes.Connection
Assert.AreEqual(string.Format(CultureInfo.InvariantCulture, "Connection failed to establish within {0} milliseconds.", _connectionInfo.Timeout.TotalMilliseconds), _actualException.Message);
}
[TestMethodForPlatform(nameof(OSPlatform.Linux))]
[TestMethod]
[OSCondition(OperatingSystems.Linux)]
public void ConnectShouldHaveThrownSshOperationTimeoutExceptionOnLinux()
{
Assert.IsNull(_actualException.InnerException);
@@ -98,7 +98,8 @@ namespace Renci.SshNet.Tests.Classes.Connection
Assert.AreEqual("Connection refused", _actualException.Message);
}
[TestMethodForPlatform(nameof(OSPlatform.Windows))]
[TestMethod]
[OSCondition(OperatingSystems.Windows)]
public void ConnectShouldHaveRespectedTimeoutOnWindows()
{
var errorText = string.Format("Elapsed: {0}, Timeout: {1}",
@@ -2,14 +2,12 @@
using System.Diagnostics;
using System.Globalization;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Common;
using Renci.SshNet.Tests.Common;
namespace Renci.SshNet.Tests.Classes.Connection
{
@@ -70,7 +68,8 @@ namespace Renci.SshNet.Tests.Classes.Connection
}
}
[TestMethodForPlatform(nameof(OSPlatform.Windows))]
[TestMethod]
[OSCondition(OperatingSystems.Windows)]
public void ConnectShouldHaveThrownSshOperationTimeoutExceptionOnWindows()
{
Assert.IsNull(_actualException.InnerException);
@@ -78,7 +77,8 @@ namespace Renci.SshNet.Tests.Classes.Connection
Assert.AreEqual(string.Format(CultureInfo.InvariantCulture, "Connection failed to establish within {0} milliseconds.", _connectionInfo.Timeout.TotalMilliseconds), _actualException.Message);
}
[TestMethodForPlatform(nameof(OSPlatform.Linux))]
[TestMethod]
[OSCondition(OperatingSystems.Linux)]
public void ConnectShouldHaveThrownSshOperationTimeoutExceptionOnLinux()
{
Assert.IsNull(_actualException.InnerException);
@@ -86,7 +86,8 @@ namespace Renci.SshNet.Tests.Classes.Connection
Assert.AreEqual("Connection refused", _actualException.Message);
}
[TestMethodForPlatform(nameof(OSPlatform.Windows))]
[TestMethod]
[OSCondition(OperatingSystems.Windows)]
public void ConnectShouldHaveRespectedTimeoutOnWindows()
{
var errorText = string.Format("Elapsed: {0}, Timeout: {1}",
@@ -2,14 +2,12 @@
using System.Diagnostics;
using System.Globalization;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Renci.SshNet.Common;
using Renci.SshNet.Tests.Common;
namespace Renci.SshNet.Tests.Classes.Connection
{
@@ -71,7 +69,8 @@ namespace Renci.SshNet.Tests.Classes.Connection
}
}
[TestMethodForPlatform(nameof(OSPlatform.Windows))]
[TestMethod]
[OSCondition(OperatingSystems.Windows)]
public void ConnectShouldHaveThrownSshOperationTimeoutExceptionOnWindows()
{
Assert.IsNull(_actualException.InnerException);
@@ -79,7 +78,8 @@ namespace Renci.SshNet.Tests.Classes.Connection
Assert.AreEqual(string.Format(CultureInfo.InvariantCulture, "Connection failed to establish within {0} milliseconds.", _connectionInfo.Timeout.TotalMilliseconds), _actualException.Message);
}
[TestMethodForPlatform(nameof(OSPlatform.Linux))]
[TestMethod]
[OSCondition(OperatingSystems.Linux)]
public void ConnectShouldHaveThrownSshOperationTimeoutExceptionOnLinux()
{
Assert.IsNull(_actualException.InnerException);
@@ -87,7 +87,8 @@ namespace Renci.SshNet.Tests.Classes.Connection
Assert.AreEqual("Connection refused", _actualException.Message);
}
[TestMethodForPlatform(nameof(OSPlatform.Windows))]
[TestMethod]
[OSCondition(OperatingSystems.Windows)]
public void ConnectShouldHaveRespectedTimeoutOnWindows()
{
var errorText = string.Format("Elapsed: {0}, Timeout: {1}",
@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.Extensions.Logging.Abstractions;
@@ -12,7 +11,6 @@ using Moq;
using Renci.SshNet.Channels;
using Renci.SshNet.Common;
using Renci.SshNet.Tests.Common;
namespace Renci.SshNet.Tests.Classes
{
@@ -108,7 +106,8 @@ namespace Renci.SshNet.Tests.Classes
}
// TODO We should investigate why this method doesn't work on Linux
[TestMethodForPlatform(nameof(OSPlatform.Windows))]
[TestMethod]
[OSCondition(OperatingSystems.Windows)]
public void ExistingConnectionShouldBeClosed()
{
try
@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.Extensions.Logging.Abstractions;
@@ -12,7 +11,6 @@ using Moq;
using Renci.SshNet.Channels;
using Renci.SshNet.Common;
using Renci.SshNet.Tests.Common;
namespace Renci.SshNet.Tests.Classes
{
@@ -112,7 +110,8 @@ namespace Renci.SshNet.Tests.Classes
}
// TODO We should investigate why this method doesn't work on Linux
[TestMethodForPlatform(nameof(OSPlatform.Windows))]
[TestMethod]
[OSCondition(OperatingSystems.Windows)]
public void ExistingConnectionShouldBeClosed()
{
try
@@ -1,39 +0,0 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Renci.SshNet.Tests.Common
{
[AttributeUsage(AttributeTargets.Method)]
public sealed class TestMethodForPlatformAttribute : TestMethodAttribute
{
public TestMethodForPlatformAttribute(string platform, [CallerFilePath] string callerFilePath = "", [CallerLineNumber] int callerLineNumber = -1) : base(callerFilePath, callerLineNumber)
{
Platform = platform;
}
public string Platform { get; }
public override async Task<TestResult[]> ExecuteAsync(ITestMethod testMethod)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Create(Platform)))
{
return await base.ExecuteAsync(testMethod);
}
var message = $"Test not executed. The test is intended for the '{Platform}' platform only.";
return new[]
{
new TestResult
{
Outcome = UnitTestOutcome.Inconclusive,
TestFailureException = new AssertInconclusiveException(message)
}
};
}
}
}