From 229598725742a6508b5248d292103b09cd202d43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Luthi?= Date: Fri, 29 Nov 2024 11:32:32 +0100 Subject: [PATCH] Add support for the [UnsupportedOSPlatform] attribute --- .../Sdk/TestMethodExtensions.cs | 45 ++++++++++++++++--- test/Xunit.SkippableFact.Tests/SampleTests.cs | 8 ++++ 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/Xunit.SkippableFact/Sdk/TestMethodExtensions.cs b/src/Xunit.SkippableFact/Sdk/TestMethodExtensions.cs index 2f316b6..1bb3fcd 100644 --- a/src/Xunit.SkippableFact/Sdk/TestMethodExtensions.cs +++ b/src/Xunit.SkippableFact/Sdk/TestMethodExtensions.cs @@ -21,16 +21,20 @@ internal static class TestMethodExtensions #if NET462 return null; #else - var platforms = new HashSet(StringComparer.OrdinalIgnoreCase); - AddPlatforms(platforms, testMethod.Method.GetCustomAttributes("System.Runtime.Versioning.SupportedOSPlatformAttribute")); - AddPlatforms(platforms, testMethod.Method.Type.GetCustomAttributes("System.Runtime.Versioning.SupportedOSPlatformAttribute")); + HashSet 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 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 } @@ -83,6 +87,37 @@ private static bool MatchesCurrentVersion(int major, int minor, int build, int r return currentRevision >= revision; } + /// + /// Returns the collection of platforms defined by the specified that decorate the test method and the test class. + /// + /// The . + /// Either SupportedOSPlatform or UnsupportedOSPlatform. + /// + /// + /// Calling GetPlatforms(testMethod, "SupportedOSPlatform") where represents MyTest returns ["Linux", "macOS"]. + /// + /// + /// [SupportedOSPlatform("macOS")] + /// public class MyTests + /// { + /// [SkippableFact] + /// [SupportedOSPlatform("Linux")] + /// public void MyTest() + /// { + /// } + /// } + /// + /// + /// The collection of platforms defined by the specified that decorate the test method and the test class. + private static HashSet GetPlatforms(ITestMethod testMethod, string platformAttributeName) + { + string platformAttribute = $"System.Runtime.Versioning.{platformAttributeName}Attribute"; + var platforms = new HashSet(StringComparer.OrdinalIgnoreCase); + AddPlatforms(platforms, testMethod.Method.GetCustomAttributes(platformAttribute)); + AddPlatforms(platforms, testMethod.Method.Type.GetCustomAttributes(platformAttribute)); + return platforms; + } + private static void AddPlatforms(HashSet platforms, IEnumerable supportedPlatformAttributes) { foreach (IAttributeInfo supportedPlatformAttribute in supportedPlatformAttributes) diff --git a/test/Xunit.SkippableFact.Tests/SampleTests.cs b/test/Xunit.SkippableFact.Tests/SampleTests.cs index 9bd28f8..8b91925 100644 --- a/test/Xunit.SkippableFact.Tests/SampleTests.cs +++ b/test/Xunit.SkippableFact.Tests/SampleTests.cs @@ -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 }