From 3ea278dae3906826fe85a032a45e04d9c1975d94 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Fri, 7 Aug 2020 12:18:34 +0200 Subject: [PATCH] Remove new platform members from OSPlatform (#40373) * Remove new platform members from OSPlatform * remove IsOSPlatformEarlierThan and IsOSPlatformOrLater methods * remove unused resource and dependency * delegate RuntimeInformation.IsOSPlatform to call OperatingSystem.IsOSPlatform. it won't compile now as the new guards have not been merged yet (#40457) * remove OSPlatform.Browser and it's usage --- .../tests/MemoryMappedFilesTestsBase.Unix.cs | 2 +- .../tests/System/EnvironmentTests.cs | 4 +- ...m.Runtime.InteropServices.RuntimeInformation.cs | 17 -- .../src/Resources/Strings.resx | 3 - ...ntime.InteropServices.RuntimeInformation.csproj | 4 - .../RuntimeInformation/OSPlatform.cs | 28 +-- .../RuntimeInformation.Browser.cs | 2 - .../RuntimeInformation.PlatformVersion.cs | 131 ----------- .../RuntimeInformation/RuntimeInformation.Unix.cs | 9 - .../RuntimeInformation.Windows.cs | 2 - .../RuntimeInformation/RuntimeInformation.cs | 2 +- .../tests/CheckPlatformTests.cs | 8 - .../tests/PlatformVersionTests.cs | 259 --------------------- ...InteropServices.RuntimeInformation.Tests.csproj | 1 - 14 files changed, 10 insertions(+), 462 deletions(-) delete mode 100644 src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.PlatformVersion.cs delete mode 100644 src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/PlatformVersionTests.cs diff --git a/src/libraries/System.IO.MemoryMappedFiles/tests/MemoryMappedFilesTestsBase.Unix.cs b/src/libraries/System.IO.MemoryMappedFiles/tests/MemoryMappedFilesTestsBase.Unix.cs index c754fcb..86cc494 100644 --- a/src/libraries/System.IO.MemoryMappedFiles/tests/MemoryMappedFilesTestsBase.Unix.cs +++ b/src/libraries/System.IO.MemoryMappedFiles/tests/MemoryMappedFilesTestsBase.Unix.cs @@ -14,7 +14,7 @@ namespace System.IO.MemoryMappedFiles.Tests /// Gets the system's page size. protected static Lazy s_pageSize = new Lazy(() => { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Browser)) + if (OperatingSystem.IsBrowser()) return Environment.SystemPageSize; int pageSize; diff --git a/src/libraries/System.Runtime.Extensions/tests/System/EnvironmentTests.cs b/src/libraries/System.Runtime.Extensions/tests/System/EnvironmentTests.cs index 136e7a9..9b65740 100644 --- a/src/libraries/System.Runtime.Extensions/tests/System/EnvironmentTests.cs +++ b/src/libraries/System.Runtime.Extensions/tests/System/EnvironmentTests.cs @@ -127,7 +127,7 @@ namespace System.Tests public void OSVersion_MatchesPlatform() { PlatformID id = Environment.OSVersion.Platform; - PlatformID expected = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? PlatformID.Win32NT : RuntimeInformation.IsOSPlatform(OSPlatform.Browser) ? PlatformID.Other : PlatformID.Unix; + PlatformID expected = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? PlatformID.Win32NT : OperatingSystem.IsBrowser() ? PlatformID.Other : PlatformID.Unix; Assert.Equal(expected, id); } @@ -142,7 +142,7 @@ namespace System.Tests Assert.Contains(version.ToString(2), versionString); - string expectedOS = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "Windows " : RuntimeInformation.IsOSPlatform(OSPlatform.Browser) ? "Other " : "Unix "; + string expectedOS = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "Windows " : OperatingSystem.IsBrowser() ? "Other " : "Unix "; Assert.Contains(expectedOS, versionString); } diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/ref/System.Runtime.InteropServices.RuntimeInformation.cs b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/ref/System.Runtime.InteropServices.RuntimeInformation.cs index 59b347a..eb1abb4 100644 --- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/ref/System.Runtime.InteropServices.RuntimeInformation.cs +++ b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/ref/System.Runtime.InteropServices.RuntimeInformation.cs @@ -18,16 +18,9 @@ namespace System.Runtime.InteropServices { private readonly object _dummy; private readonly int _dummyPrimitive; - public static System.Runtime.InteropServices.OSPlatform Android { get { throw null; } } - public static System.Runtime.InteropServices.OSPlatform Browser { get { throw null; } } public static System.Runtime.InteropServices.OSPlatform FreeBSD { get { throw null; } } - public static System.Runtime.InteropServices.OSPlatform iOS { get { throw null; } } public static System.Runtime.InteropServices.OSPlatform Linux { get { throw null; } } - public static System.Runtime.InteropServices.OSPlatform macOS { get { throw null; } } - [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] public static System.Runtime.InteropServices.OSPlatform OSX { get { throw null; } } - public static System.Runtime.InteropServices.OSPlatform tvOS { get { throw null; } } - public static System.Runtime.InteropServices.OSPlatform watchOS { get { throw null; } } public static System.Runtime.InteropServices.OSPlatform Windows { get { throw null; } } public static System.Runtime.InteropServices.OSPlatform Create(string osPlatform) { throw null; } public override bool Equals(object? obj) { throw null; } @@ -45,15 +38,5 @@ namespace System.Runtime.InteropServices public static System.Runtime.InteropServices.Architecture ProcessArchitecture { get { throw null; } } public static string RuntimeIdentifier { get { throw null; } } public static bool IsOSPlatform(System.Runtime.InteropServices.OSPlatform osPlatform) { throw null; } - public static bool IsOSPlatformEarlierThan(System.Runtime.InteropServices.OSPlatform osPlatform, int major) { throw null; } - public static bool IsOSPlatformEarlierThan(System.Runtime.InteropServices.OSPlatform osPlatform, int major, int minor) { throw null; } - public static bool IsOSPlatformEarlierThan(System.Runtime.InteropServices.OSPlatform osPlatform, int major, int minor, int build) { throw null; } - public static bool IsOSPlatformEarlierThan(System.Runtime.InteropServices.OSPlatform osPlatform, int major, int minor, int build, int revision) { throw null; } - public static bool IsOSPlatformEarlierThan(string platformName) { throw null; } - public static bool IsOSPlatformOrLater(System.Runtime.InteropServices.OSPlatform osPlatform, int major) { throw null; } - public static bool IsOSPlatformOrLater(System.Runtime.InteropServices.OSPlatform osPlatform, int major, int minor) { throw null; } - public static bool IsOSPlatformOrLater(System.Runtime.InteropServices.OSPlatform osPlatform, int major, int minor, int build) { throw null; } - public static bool IsOSPlatformOrLater(System.Runtime.InteropServices.OSPlatform osPlatform, int major, int minor, int build, int revision) { throw null; } - public static bool IsOSPlatformOrLater(string platformName) { throw null; } } } diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/Resources/Strings.resx b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/Resources/Strings.resx index c7f9079..e554cfe 100644 --- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/Resources/Strings.resx +++ b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/Resources/Strings.resx @@ -60,9 +60,6 @@ Value cannot be empty. - - "{0}" is not a valid platform name. It must contain both name and version (with at least major and minor number specified). Example: "ios14.0". - RuntimeInformation is not supported for Portable Class Libraries. diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System.Runtime.InteropServices.RuntimeInformation.csproj b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System.Runtime.InteropServices.RuntimeInformation.csproj index 02fa495..12aab18e 100644 --- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System.Runtime.InteropServices.RuntimeInformation.csproj +++ b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System.Runtime.InteropServices.RuntimeInformation.csproj @@ -7,7 +7,6 @@ - @@ -16,8 +15,6 @@ - - diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/OSPlatform.cs b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/OSPlatform.cs index 18e4564..91dcbad 100644 --- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/OSPlatform.cs +++ b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/OSPlatform.cs @@ -5,38 +5,22 @@ namespace System.Runtime.InteropServices { public readonly struct OSPlatform : IEquatable { - private readonly string _osPlatform; - - public static OSPlatform Android { get; } = new OSPlatform("ANDROID"); - - public static OSPlatform Browser { get; } = new OSPlatform("BROWSER"); - public static OSPlatform FreeBSD { get; } = new OSPlatform("FREEBSD"); public static OSPlatform Linux { get; } = new OSPlatform("LINUX"); - public static OSPlatform macOS { get; } = new OSPlatform("MACOS"); - - [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] // superseded by macOS public static OSPlatform OSX { get; } = new OSPlatform("OSX"); - public static OSPlatform iOS { get; } = new OSPlatform("IOS"); - - public static OSPlatform tvOS { get; } = new OSPlatform("TVOS"); - - public static OSPlatform watchOS { get; } = new OSPlatform("WATCHOS"); - public static OSPlatform Windows { get; } = new OSPlatform("WINDOWS"); - internal bool IsCurrent { get; } // this information is cached because it's frequently used + internal string Name { get; } private OSPlatform(string osPlatform) { if (osPlatform == null) throw new ArgumentNullException(nameof(osPlatform)); if (osPlatform.Length == 0) throw new ArgumentException(SR.Argument_EmptyValue, nameof(osPlatform)); - _osPlatform = osPlatform; - IsCurrent = RuntimeInformation.IsCurrentOSPlatform(osPlatform); + Name = osPlatform; } /// @@ -50,12 +34,12 @@ namespace System.Runtime.InteropServices public bool Equals(OSPlatform other) { - return Equals(other._osPlatform); + return Equals(other.Name); } internal bool Equals(string? other) { - return string.Equals(_osPlatform, other, StringComparison.OrdinalIgnoreCase); + return string.Equals(Name, other, StringComparison.OrdinalIgnoreCase); } public override bool Equals(object? obj) @@ -65,12 +49,12 @@ namespace System.Runtime.InteropServices public override int GetHashCode() { - return _osPlatform == null ? 0 : _osPlatform.GetHashCode(StringComparison.OrdinalIgnoreCase); + return Name == null ? 0 : Name.GetHashCode(StringComparison.OrdinalIgnoreCase); } public override string ToString() { - return _osPlatform ?? string.Empty; + return Name ?? string.Empty; } public static bool operator ==(OSPlatform left, OSPlatform right) diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Browser.cs b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Browser.cs index 838aa49..c702ef8 100644 --- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Browser.cs +++ b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Browser.cs @@ -5,8 +5,6 @@ namespace System.Runtime.InteropServices { public static partial class RuntimeInformation { - internal static bool IsCurrentOSPlatform(string osPlatform) => osPlatform.Equals("BROWSER", StringComparison.OrdinalIgnoreCase); - public static string OSDescription => "Browser"; public static Architecture OSArchitecture => Architecture.Wasm; diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.PlatformVersion.cs b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.PlatformVersion.cs deleted file mode 100644 index 0eec219..0000000 --- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.PlatformVersion.cs +++ /dev/null @@ -1,131 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -namespace System.Runtime.InteropServices -{ - public static partial class RuntimeInformation - { - /// - /// Check for the OS with a >= version comparison. Used to guard APIs that were added in the given OS release. - /// - /// OS name concatenated with a version number. - /// The version number must contain at least major and minor numbers separated with a dot. - /// Example: "ios14.0" is OK, "ios14" is NOT OK. - public static bool IsOSPlatformOrLater(string platformName) - { - (OSPlatform platform, Version version) = Parse(platformName); - - return IsOSPlatformOrLater(platform, version.Major, version.Minor, version.Build, version.Revision); - } - - /// - /// Check for the OS with a >= version comparison. Used to guard APIs that were added in the given OS release. - /// - public static bool IsOSPlatformOrLater(OSPlatform osPlatform, int major) - => IsOSPlatform(osPlatform) && Environment.OSVersion.Version.Major >= major; - - /// - /// Check for the OS with a >= version comparison. Used to guard APIs that were added in the given OS release. - /// - public static bool IsOSPlatformOrLater(OSPlatform osPlatform, int major, int minor) - => IsOSPlatform(osPlatform) && IsOSVersionOrLater(major, minor, int.MinValue, int.MinValue); - - /// - /// Check for the OS with a >= version comparison. Used to guard APIs that were added in the given OS release. - /// - public static bool IsOSPlatformOrLater(OSPlatform osPlatform, int major, int minor, int build) - => IsOSPlatform(osPlatform) && IsOSVersionOrLater(major, minor, build, int.MinValue); - - /// - /// Check for the OS with a >= version comparison. Used to guard APIs that were added in the given OS release. - /// - public static bool IsOSPlatformOrLater(OSPlatform osPlatform, int major, int minor, int build, int revision) - => IsOSPlatform(osPlatform) && IsOSVersionOrLater(major, minor, build, revision); - - /// - /// Check for the OS with a < version comparison. Used to guard APIs that were obsoleted or removed in the given OS release. - /// - /// OS name concatenated with a version number. - /// The version number must contain at least major and minor numbers separated with a dot. - /// Example: "ios14.0" is OK, "ios14" is NOT OK. - public static bool IsOSPlatformEarlierThan(string platformName) - { - (OSPlatform platform, Version version) = Parse(platformName); - - return IsOSPlatformEarlierThan(platform, version.Major, version.Minor, version.Build, version.Revision); - } - - /// - /// Check for the OS with a < version comparison. Used to guard APIs that were obsoleted or removed in the given OS release. - /// - public static bool IsOSPlatformEarlierThan(OSPlatform osPlatform, int major) - => IsOSPlatform(osPlatform) && Environment.OSVersion.Version.Major < major; - - /// - /// Check for the OS with a < version comparison. Used to guard APIs that were obsoleted or removed in the given OS release. - /// - public static bool IsOSPlatformEarlierThan(OSPlatform osPlatform, int major, int minor) - => IsOSPlatform(osPlatform) && !IsOSVersionOrLater(major, minor, int.MinValue, int.MinValue); - - /// - /// Check for the OS with a < version comparison. Used to guard APIs that were obsoleted or removed in the given OS release. - /// - public static bool IsOSPlatformEarlierThan(OSPlatform osPlatform, int major, int minor, int build) - => IsOSPlatform(osPlatform) && !IsOSVersionOrLater(major, minor, build, int.MinValue); - - /// - /// Check for the OS with a < version comparison. Used to guard APIs that were obsoleted or removed in the given OS release. - /// - public static bool IsOSPlatformEarlierThan(OSPlatform osPlatform, int major, int minor, int build, int revision) - => IsOSPlatform(osPlatform) && !IsOSVersionOrLater(major, minor, build, revision); - - private static bool IsOSVersionOrLater(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; - } - if (current.Build != build) - { - return current.Build > build; - } - - return current.Revision >= revision; - } - - private static (OSPlatform, Version) Parse(string platformName) - { - if (platformName == null) - { - throw new ArgumentNullException(nameof(platformName)); - } - if (platformName.Length == 0) - { - throw new ArgumentException(SR.Argument_EmptyValue, nameof(platformName)); - } - - // iterate from the begining, as digits in the middle of the names are not supported by design - for (int i = 0; i < platformName.Length; i++) - { - if (char.IsDigit(platformName[i])) - { - if (i > 0 && Version.TryParse(platformName.AsSpan(i), out Version? version)) - { - return (OSPlatform.Create(platformName.Substring(0, i)), version); - } - else - { - break; - } - } - } - - throw new ArgumentException(SR.Format(SR.Argument_InvalidPlatfromName, platformName), nameof(platformName)); - } - } -} diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Unix.cs b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Unix.cs index 232c7fc..0a8147d 100644 --- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Unix.cs +++ b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Unix.cs @@ -7,17 +7,8 @@ namespace System.Runtime.InteropServices { public static partial class RuntimeInformation { - private static string? s_osPlatformName; private static string? s_osDescription; - internal static bool IsCurrentOSPlatform(string osPlatform) - { - string name = s_osPlatformName ??= Interop.Sys.GetUnixName(); - - return osPlatform.Equals(name, StringComparison.OrdinalIgnoreCase) - || (name == "OSX" && osPlatform.Equals("MACOS", StringComparison.OrdinalIgnoreCase)); // GetUnixName returns OSX on macOS - } - public static string OSDescription => s_osDescription ??= Interop.Sys.GetUnixVersion(); public static Architecture OSArchitecture { get; } = Map((Interop.Sys.ProcessorArchitecture)Interop.Sys.GetOSArchitecture()); diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Windows.cs b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Windows.cs index 668b843..0177eec 100644 --- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Windows.cs +++ b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.Windows.cs @@ -11,8 +11,6 @@ namespace System.Runtime.InteropServices private static volatile int s_osArch = -1; private static volatile int s_processArch = -1; - internal static bool IsCurrentOSPlatform(string osPlatform) => osPlatform.Equals("WINDOWS", StringComparison.OrdinalIgnoreCase); - public static string OSDescription { get diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.cs b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.cs index 95b1b62..df0fb53 100644 --- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.cs +++ b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/src/System/Runtime/InteropServices/RuntimeInformation/RuntimeInformation.cs @@ -52,6 +52,6 @@ namespace System.Runtime.InteropServices /// /// Indicates whether the current application is running on the specified platform. /// - public static bool IsOSPlatform(OSPlatform osPlatform) => osPlatform.IsCurrent; + public static bool IsOSPlatform(OSPlatform osPlatform) => OperatingSystem.IsOSPlatform(osPlatform.Name); } } diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/CheckPlatformTests.cs b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/CheckPlatformTests.cs index e40f9f8b..76ea344 100644 --- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/CheckPlatformTests.cs +++ b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/CheckPlatformTests.cs @@ -51,10 +51,6 @@ namespace System.Runtime.InteropServices.RuntimeInformationTests Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.OSX)); Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("OSX"))); Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("osx"))); - Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.macOS)); - Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("MACOS"))); - Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("macOS"))); - Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("macos"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NETBSD"))); @@ -71,7 +67,6 @@ namespace System.Runtime.InteropServices.RuntimeInformationTests [Fact, PlatformSpecific(TestPlatforms.iOS)] // Tests RuntimeInformation OS platform public void CheckiOS() { - Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.iOS)); Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("IOS"))); Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("iOS"))); Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("ios"))); @@ -92,7 +87,6 @@ namespace System.Runtime.InteropServices.RuntimeInformationTests [Fact, PlatformSpecific(TestPlatforms.tvOS)] // Tests RuntimeInformation OS platform public void ChecktvOS() { - Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.tvOS)); Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("TVOS"))); Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("tvOS"))); Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("tvos"))); @@ -113,7 +107,6 @@ namespace System.Runtime.InteropServices.RuntimeInformationTests [Fact, PlatformSpecific(TestPlatforms.Android)] // Tests RuntimeInformation OS platform public void CheckAndroid() { - Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Android)); Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("ANDROID"))); Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("android"))); @@ -133,7 +126,6 @@ namespace System.Runtime.InteropServices.RuntimeInformationTests [Fact, PlatformSpecific(TestPlatforms.Browser)] // Tests RuntimeInformation OS platform public void CheckBrowser() { - Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Browser)); Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER"))); Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("browser"))); diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/PlatformVersionTests.cs b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/PlatformVersionTests.cs deleted file mode 100644 index b612d2e..0000000 --- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/PlatformVersionTests.cs +++ /dev/null @@ -1,259 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Collections.Generic; -using Xunit; - -namespace System.Runtime.InteropServices.RuntimeInformationTests -{ - public class PlatformVersionTests - { - public static IEnumerable AllKnownOsPlatforms() - { - yield return new object[] { OSPlatform.Windows }; - yield return new object[] { OSPlatform.Linux }; - yield return new object[] { OSPlatform.OSX }; - yield return new object[] { OSPlatform.Browser }; - yield return new object[] { OSPlatform.macOS }; - yield return new object[] { OSPlatform.iOS }; - yield return new object[] { OSPlatform.tvOS }; - yield return new object[] { OSPlatform.watchOS }; - yield return new object[] { OSPlatform.Android }; - } - - [Fact] - public void IsOSPlatformOrLater_Null_ThrowsArgumentNullExceptionWithArgumentName() - { - Assert.Throws("platformName", () => RuntimeInformation.IsOSPlatformOrLater(null)); - } - - [Fact] - public void IsOSPlatformOrLater_Empty_ThrowsArgumentNullExceptionWithArgumentName() - { - Assert.Throws("platformName", () => RuntimeInformation.IsOSPlatformOrLater(string.Empty)); - } - - [Theory] - [InlineData("ios")] // missing version number - [InlineData("ios14")] // ios14.0 is fine, ios14 is not: https://github.com/dotnet/runtime/pull/39005#discussion_r452541491 - [InlineData("ios14.0.0.0.0.0")] // too many numbers - [InlineData("ios14.0.")] // version should not end with dot (but OS name could potentially end with dot, imagine "NET.") - [InlineData("numbers1.2inplatformname1.2")] // numbers in platform names are not supported https://github.com/dotnet/runtime/pull/39005#discussion_r452644601 - public void IsOSPlatformOrLater_InvalidVersionNumber_ThrowsArgumentExceptionWithArgumentName(string platformName) - { - Assert.Throws("platformName", () => RuntimeInformation.IsOSPlatformOrLater(platformName)); - } - - [Theory] - [MemberData(nameof(AllKnownOsPlatforms))] - public void IsOSPlatformOrLater_ReturnsTrue_ForCurrentOS(OSPlatform osPlatform) - { - // IsOSPlatformOrLater("xyz1.2.3.4") running as "xyz1.2.3.4" should return true - - bool isCurrentPlatfom = RuntimeInformation.IsOSPlatform(osPlatform); - Version current = Environment.OSVersion.Version; - - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater($"{osPlatform}{current}")); - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater($"{osPlatform.ToString().ToLower()}{current}")); - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater($"{osPlatform.ToString().ToUpper()}{current}")); - - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater(osPlatform, current.Major)); - - if (current.Minor >= 0) - { - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater(osPlatform, current.Major, current.Minor)); - - if (current.Build >= 0) - { - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater(osPlatform, current.Major, current.Minor, current.Build)); - - if (current.Revision >= 0) - { - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater(osPlatform, current.Major, current.Minor, current.Build, current.Revision)); - } - } - } - } - - [Theory] - [MemberData(nameof(AllKnownOsPlatforms))] - public void IsOSPlatformOrLater_ReturnsFalse_ForNewerVersionOfCurrentOS(OSPlatform osPlatform) - { - // IsOSPlatformOrLater("xyz11.0") running as "xyz10.0" should return false - - Version currentVersion = Environment.OSVersion.Version; - - Version newer = new Version(currentVersion.Major + 1, 0); - Assert.False(RuntimeInformation.IsOSPlatformOrLater($"{osPlatform}{newer}")); - Assert.False(RuntimeInformation.IsOSPlatformOrLater($"{osPlatform.ToString().ToLower()}{newer}")); - Assert.False(RuntimeInformation.IsOSPlatformOrLater($"{osPlatform.ToString().ToUpper()}{newer}")); - Assert.False(RuntimeInformation.IsOSPlatformOrLater(osPlatform, newer.Major)); - - newer = new Version(currentVersion.Major, currentVersion.Minor + 1); - Assert.False(RuntimeInformation.IsOSPlatformOrLater($"{osPlatform}{newer}")); - Assert.False(RuntimeInformation.IsOSPlatformOrLater(osPlatform, newer.Major, newer.Minor)); - - newer = new Version(currentVersion.Major, currentVersion.Minor, currentVersion.Build + 1); - Assert.False(RuntimeInformation.IsOSPlatformOrLater($"{osPlatform}{newer}")); - Assert.False(RuntimeInformation.IsOSPlatformOrLater(osPlatform, newer.Major, newer.Minor, newer.Build)); - - newer = new Version(currentVersion.Major, currentVersion.Minor, currentVersion.Build, currentVersion.Revision + 1); - Assert.False(RuntimeInformation.IsOSPlatformOrLater($"{osPlatform}{newer}")); - Assert.False(RuntimeInformation.IsOSPlatformOrLater(osPlatform, newer.Major, newer.Minor, newer.Build, newer.Revision)); - } - - [Theory] - [MemberData(nameof(AllKnownOsPlatforms))] - public void IsOSPlatformOrLater_ReturnsTrue_ForOlderVersionOfCurrentOS(OSPlatform osPlatform) - { - // IsOSPlatformOrLater("xyz10.0") running as "xyz11.0" should return true - - bool isCurrentPlatfom = RuntimeInformation.IsOSPlatform(osPlatform); - Version current = Environment.OSVersion.Version; - - Version older = new Version(current.Major - 1, 0); - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater($"{osPlatform}{older}")); - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater($"{osPlatform.ToString().ToLower()}{older}")); - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater($"{osPlatform.ToString().ToUpper()}{older}")); - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater(osPlatform, older.Major)); - - if (current.Minor > 0) - { - older = new Version(current.Major, current.Minor - 1); - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater($"{osPlatform}{older}")); - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater(osPlatform, older.Major, older.Minor)); - } - - if (current.Build > 0) - { - older = new Version(current.Major, current.Minor, current.Build - 1); - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater($"{osPlatform}{older}")); - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater(osPlatform, older.Major, older.Minor, older.Build)); - } - - if (current.Revision > 0) - { - older = new Version(current.Major, current.Minor, current.Build, current.Revision - 1); - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater($"{osPlatform}{older}")); - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformOrLater(osPlatform, older.Major, older.Minor, older.Build, older.Revision)); - } - } - - [Fact] - public void IsOSPlatformEarlierThan_Null_ThrowsArgumentNullExceptionWithArgumentName() - { - Assert.Throws("platformName", () => RuntimeInformation.IsOSPlatformEarlierThan(null)); - } - - [Fact] - public void IsOSPlatformEarlierThan_Empty_ThrowsArgumentNullExceptionWithArgumentName() - { - Assert.Throws("platformName", () => RuntimeInformation.IsOSPlatformEarlierThan(string.Empty)); - } - - [Theory] - [InlineData("ios")] // missing version number - [InlineData("ios14")] // ios14.0 is fine, ios14 is not: https://github.com/dotnet/runtime/pull/39005#discussion_r452541491 - [InlineData("ios14.0.0.0.0.0")] // too many numbers - [InlineData("ios14.0.")] // version should not end with dot (but OS name could potentially end with dot, imagine "NET.") - [InlineData("numbers1.2inplatformname1.2")] // numbers in platform names are not supported https://github.com/dotnet/runtime/pull/39005#discussion_r452644601 - public void IsOSPlatformEarlierThan_InvalidVersionNumber_ThrowsArgumentExceptionWithArgumentName(string platformName) - { - Assert.Throws("platformName", () => RuntimeInformation.IsOSPlatformEarlierThan(platformName)); - } - - [Theory] - [MemberData(nameof(AllKnownOsPlatforms))] - public void IsOSPlatformEarlierThan_ReturnsFalse_ForCurrentOS(OSPlatform osPlatform) - { - // IsOSPlatformEarlierThan("xyz1.2.3.4") running as "xyz1.2.3.4" should return false - - Version current = Environment.OSVersion.Version; - - Assert.False(RuntimeInformation.IsOSPlatformEarlierThan($"{osPlatform}{current}")); - Assert.False(RuntimeInformation.IsOSPlatformEarlierThan($"{osPlatform.ToString().ToLower()}{current}")); - Assert.False(RuntimeInformation.IsOSPlatformEarlierThan($"{osPlatform.ToString().ToUpper()}{current}")); - - Assert.False(RuntimeInformation.IsOSPlatformEarlierThan(osPlatform, current.Major)); - - if (current.Minor >= 0) - { - Assert.False(RuntimeInformation.IsOSPlatformEarlierThan(osPlatform, current.Major, current.Minor)); - - if (current.Build >= 0) - { - Assert.False(RuntimeInformation.IsOSPlatformEarlierThan(osPlatform, current.Major, current.Minor, current.Build)); - - if (current.Revision >= 0) - { - Assert.False(RuntimeInformation.IsOSPlatformEarlierThan(osPlatform, current.Major, current.Minor, current.Build, current.Revision)); - } - } - } - } - - [Theory] - [MemberData(nameof(AllKnownOsPlatforms))] - public void IsOSPlatformEarlierThan_ReturnsTrue_ForNewerVersionOfCurrentOS(OSPlatform osPlatform) - { - // IsOSPlatformEarlierThan("xyz11.0") running as "xyz10.0" should return true - - bool isCurrentPlatfom = RuntimeInformation.IsOSPlatform(osPlatform); - Version current = Environment.OSVersion.Version; - - Version newer = new Version(current.Major + 1, 0); - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformEarlierThan($"{osPlatform}{newer}")); - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformEarlierThan($"{osPlatform.ToString().ToLower()}{newer}")); - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformEarlierThan($"{osPlatform.ToString().ToUpper()}{newer}")); - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformEarlierThan(osPlatform, newer.Major)); - - newer = new Version(current.Major, current.Minor + 1); - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformEarlierThan($"{osPlatform}{newer}")); - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformEarlierThan(osPlatform, newer.Major, newer.Minor)); - - newer = new Version(current.Major, current.Minor, current.Build + 1); - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformEarlierThan($"{osPlatform}{newer}")); - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformEarlierThan(osPlatform, newer.Major, newer.Minor, newer.Build)); - - newer = new Version(current.Major, current.Minor, current.Build, current.Revision + 1); - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformEarlierThan($"{osPlatform}{newer}")); - Assert.Equal(isCurrentPlatfom, RuntimeInformation.IsOSPlatformEarlierThan(osPlatform, newer.Major, newer.Minor, newer.Build, newer.Revision)); - } - - [Theory] - [MemberData(nameof(AllKnownOsPlatforms))] - public void IsOSPlatformEarlierThan_ReturnsFalse_ForOlderVersionOfCurrentOS(OSPlatform osPlatform) - { - // IsOSPlatformEarlierThan("xyz10.0") running as "xyz11.0" should return false - - Version current = Environment.OSVersion.Version; - - Version older = new Version(current.Major - 1, 0); - Assert.False(RuntimeInformation.IsOSPlatformEarlierThan($"{osPlatform}{older}")); - Assert.False(RuntimeInformation.IsOSPlatformEarlierThan($"{osPlatform.ToString().ToLower()}{older}")); - Assert.False(RuntimeInformation.IsOSPlatformEarlierThan($"{osPlatform.ToString().ToUpper()}{older}")); - Assert.False(RuntimeInformation.IsOSPlatformEarlierThan(osPlatform, older.Major)); - - if (current.Minor > 0) - { - older = new Version(current.Major, current.Minor - 1); - Assert.False(RuntimeInformation.IsOSPlatformEarlierThan($"{osPlatform}{older}")); - Assert.False(RuntimeInformation.IsOSPlatformEarlierThan(osPlatform, older.Major, older.Minor)); - } - - if (current.Build > 0) - { - older = new Version(current.Major, current.Minor, current.Build - 1); - Assert.False(RuntimeInformation.IsOSPlatformEarlierThan($"{osPlatform}{older}")); - Assert.False(RuntimeInformation.IsOSPlatformEarlierThan(osPlatform, older.Major, older.Minor, older.Build)); - } - - if (current.Revision > 0) - { - older = new Version(current.Major, current.Minor, current.Build, current.Revision - 1); - Assert.False(RuntimeInformation.IsOSPlatformEarlierThan($"{osPlatform}{older}")); - Assert.False(RuntimeInformation.IsOSPlatformEarlierThan(osPlatform, older.Major, older.Minor, older.Build, older.Revision)); - } - } - } -} diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/System.Runtime.InteropServices.RuntimeInformation.Tests.csproj b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/System.Runtime.InteropServices.RuntimeInformation.Tests.csproj index e374551..de6d696 100644 --- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/System.Runtime.InteropServices.RuntimeInformation.Tests.csproj +++ b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/System.Runtime.InteropServices.RuntimeInformation.Tests.csproj @@ -8,7 +8,6 @@ - -- 2.7.4