Skip to content

Commit

Permalink
Add support for the [UnsupportedOSPlatform] attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
0xced committed Nov 29, 2024
1 parent 00b459f commit 2295987
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
45 changes: 40 additions & 5 deletions src/Xunit.SkippableFact/Sdk/TestMethodExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ internal static class TestMethodExtensions
#if NET462
return null;
#else
var platforms = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
AddPlatforms(platforms, testMethod.Method.GetCustomAttributes("System.Runtime.Versioning.SupportedOSPlatformAttribute"));
AddPlatforms(platforms, testMethod.Method.Type.GetCustomAttributes("System.Runtime.Versioning.SupportedOSPlatformAttribute"));
HashSet<string> unsupportedPlatforms = GetPlatforms(testMethod, "UnsupportedOSPlatform");
string? unsupportedPlatform = unsupportedPlatforms.FirstOrDefault(MatchesCurrentPlatform);
if (unsupportedPlatform is not null)
{
return $"Unsupported on {unsupportedPlatform}";
}

if (platforms.Count == 0 || platforms.Any(MatchesCurrentPlatform))
HashSet<string> supportedPlatforms = GetPlatforms(testMethod, "SupportedOSPlatform");
if (supportedPlatforms.Count == 0 || supportedPlatforms.Any(MatchesCurrentPlatform))
{
return null;
}

string platformsDescription = platforms.Count == 1 ? platforms.First() : string.Join(", ", platforms.Reverse().Skip(1).Reverse()) + " and " + platforms.Last();
string platformsDescription = supportedPlatforms.Count == 1 ? supportedPlatforms.First() : string.Join(", ", supportedPlatforms.Reverse().Skip(1).Reverse()) + " and " + supportedPlatforms.Last();
return $"Only supported on {platformsDescription}";
#endif
}
Expand Down Expand Up @@ -83,6 +87,37 @@ private static bool MatchesCurrentVersion(int major, int minor, int build, int r
return currentRevision >= revision;
}

/// <summary>
/// Returns the collection of platforms defined by the specified <paramref name="platformAttributeName"/> that decorate the test method and the test class.
/// </summary>
/// <param name="testMethod">The <see cref="ITestMethod"/>.</param>
/// <param name="platformAttributeName">Either <c>SupportedOSPlatform</c> or <c>UnsupportedOSPlatform</c>.</param>
/// <example>
/// <para>
/// Calling GetPlatforms(testMethod, "SupportedOSPlatform") where <paramref name="testMethod"/> represents <c>MyTest</c> returns ["Linux", "macOS"].
/// </para>
/// <code>
/// [SupportedOSPlatform("macOS")]
/// public class MyTests
/// {
/// [SkippableFact]
/// [SupportedOSPlatform("Linux")]
/// public void MyTest()
/// {
/// }
/// }
/// </code>
/// </example>
/// <returns>The collection of platforms defined by the specified <paramref name="platformAttributeName"/> that decorate the test method and the test class.</returns>
private static HashSet<string> GetPlatforms(ITestMethod testMethod, string platformAttributeName)
{
string platformAttribute = $"System.Runtime.Versioning.{platformAttributeName}Attribute";
var platforms = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
AddPlatforms(platforms, testMethod.Method.GetCustomAttributes(platformAttribute));
AddPlatforms(platforms, testMethod.Method.Type.GetCustomAttributes(platformAttribute));
return platforms;
}

private static void AddPlatforms(HashSet<string> platforms, IEnumerable<IAttributeInfo> supportedPlatformAttributes)
{
foreach (IAttributeInfo supportedPlatformAttribute in supportedPlatformAttributes)
Expand Down
8 changes: 8 additions & 0 deletions test/Xunit.SkippableFact.Tests/SampleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,13 @@ public void AndroidAndBrowserAndWasiOnly()
{
Assert.True(OperatingSystem.IsAndroid() || OperatingSystem.IsBrowser() || OperatingSystem.IsWasi(), "This should only run on Android, Browser and Wasi");
}

[SkippableFact, UnsupportedOSPlatform("Linux"), UnsupportedOSPlatform("macOS"), UnsupportedOSPlatform("Windows")]
public void UnsupportedPlatforms()
{
Assert.False(OperatingSystem.IsLinux());
Assert.False(OperatingSystem.IsMacOS());
Assert.False(OperatingSystem.IsWindows());
}
#endif
}

0 comments on commit 2295987

Please sign in to comment.