From 00b459f3ad8baf7c509a8568fbc9aa7b49ee98b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Luthi?= Date: Fri, 29 Nov 2024 11:46:01 +0100 Subject: [PATCH] Add support for platform versions --- .../Sdk/TestMethodExtensions.cs | 49 ++++++++++++++++++- test/Xunit.SkippableFact.Tests/SampleTests.cs | 24 +++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/src/Xunit.SkippableFact/Sdk/TestMethodExtensions.cs b/src/Xunit.SkippableFact/Sdk/TestMethodExtensions.cs index e17efcf..2f316b6 100644 --- a/src/Xunit.SkippableFact/Sdk/TestMethodExtensions.cs +++ b/src/Xunit.SkippableFact/Sdk/TestMethodExtensions.cs @@ -25,7 +25,7 @@ internal static class TestMethodExtensions AddPlatforms(platforms, testMethod.Method.GetCustomAttributes("System.Runtime.Versioning.SupportedOSPlatformAttribute")); AddPlatforms(platforms, testMethod.Method.Type.GetCustomAttributes("System.Runtime.Versioning.SupportedOSPlatformAttribute")); - if (platforms.Count == 0 || platforms.Any(platform => RuntimeInformation.IsOSPlatform(OSPlatform.Create(platform)))) + if (platforms.Count == 0 || platforms.Any(MatchesCurrentPlatform)) { return null; } @@ -36,6 +36,53 @@ internal static class TestMethodExtensions } #if !NET462 + private static bool MatchesCurrentPlatform(string platform) + { + int versionIndex = platform.IndexOfAny(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']); + bool matchesVersion; + if (versionIndex >= 0 && Version.TryParse(platform[versionIndex..], out Version version)) + { + platform = platform[..versionIndex]; + matchesVersion = MatchesCurrentVersion(version.Major, version.Minor, version.Build, version.Revision); + } + else + { + matchesVersion = true; + } + + return matchesVersion && RuntimeInformation.IsOSPlatform(OSPlatform.Create(platform)); + } + + // Adapted from OperatingSystem.IsOSVersionAtLeast() which is private, see https://github.com/dotnet/runtime/blob/d6eb35426ebdb09ee5c754aa9afb9ad6e96a3dec/src/libraries/System.Private.CoreLib/src/System/OperatingSystem.cs#L326-L351 + private static bool MatchesCurrentVersion(int major, int minor, int build, int revision) + { + Version current = Environment.OSVersion.Version; + + if (current.Major != major) + { + return current.Major > major; + } + + if (current.Minor != minor) + { + return current.Minor > minor; + } + + // Unspecified build component is to be treated as zero + int currentBuild = current.Build < 0 ? 0 : current.Build; + build = build < 0 ? 0 : build; + if (currentBuild != build) + { + return currentBuild > build; + } + + // Unspecified revision component is to be treated as zero + int currentRevision = current.Revision < 0 ? 0 : current.Revision; + revision = revision < 0 ? 0 : revision; + + return currentRevision >= revision; + } + 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 5598c88..9bd28f8 100644 --- a/test/Xunit.SkippableFact.Tests/SampleTests.cs +++ b/test/Xunit.SkippableFact.Tests/SampleTests.cs @@ -90,12 +90,36 @@ public void MacOsOnly() Assert.True(OperatingSystem.IsMacOS(), "This should only run on macOS"); } + [SkippableFact, SupportedOSPlatform("macOS10.6")] + public void MacOs10_6Minimum() + { + Assert.True(OperatingSystem.IsMacOSVersionAtLeast(10, 6), "This should only run on macOS 10.6 onwards"); + } + + [SkippableFact, SupportedOSPlatform("macOS77.7")] + public void MacOs77_7Minimum() + { + Assert.True(OperatingSystem.IsMacOSVersionAtLeast(77, 7), "This should only run on macOS 77.7 onwards"); + } + [SkippableFact, SupportedOSPlatform("Windows")] public void WindowsOnly() { Assert.True(OperatingSystem.IsWindows(), "This should only run on Windows"); } + [SkippableFact, SupportedOSPlatform("Windows10.0")] + public void Windows10Minimum() + { + Assert.True(OperatingSystem.IsWindowsVersionAtLeast(10), "This should only run on Windows 10.0 onwards"); + } + + [SkippableFact, SupportedOSPlatform("Windows77.7")] + public void Windows77_7Minimum() + { + Assert.True(OperatingSystem.IsWindowsVersionAtLeast(77, 7), "This should only run on Windows 77.7 onwards"); + } + [SkippableFact, SupportedOSPlatform("Android"), SupportedOSPlatform("Browser")] public void AndroidAndBrowserFact() {