From 37366f15187cbb642578e5e3c523426f40591c70 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Mon, 13 Jul 2020 10:29:35 +0200 Subject: [PATCH 01/10] add OSPlatform.macOS, hide OSX --- .../ref/System.Runtime.InteropServices.RuntimeInformation.cs | 2 ++ .../Runtime/InteropServices/RuntimeInformation/OSPlatform.cs | 3 +++ 2 files changed, 5 insertions(+) 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 e9c675b61f2e8..46d2dd5159e22 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 @@ -23,6 +23,8 @@ public enum Architecture 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; } } 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 39f956e164bd0..71e58595f300a 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 @@ -15,6 +15,9 @@ namespace System.Runtime.InteropServices public static OSPlatform Linux { get; } = new OSPlatform("LINUX"); + public static OSPlatform macOS { get; } = new OSPlatform("MACOS"); + + [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] public static OSPlatform OSX { get; } = new OSPlatform("OSX"); public static OSPlatform iOS { get; } = new OSPlatform("IOS"); From fc4f747af5f7def0004051e10f187cd1c682cbbf Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Mon, 13 Jul 2020 10:57:42 +0200 Subject: [PATCH 02/10] use OrdinalIgnoreCase for platform name comparisons --- .../RuntimeInformation/OSPlatform.cs | 6 ++-- .../tests/CheckPlatformTests.cs | 29 +++++++++++++++---- 2 files changed, 27 insertions(+), 8 deletions(-) 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 71e58595f300a..796d85eae0af0 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 @@ -48,17 +48,17 @@ public bool Equals(OSPlatform other) internal bool Equals(string? other) { - return string.Equals(_osPlatform, other, StringComparison.Ordinal); + return string.Equals(_osPlatform, other, StringComparison.OrdinalIgnoreCase); } public override bool Equals(object? obj) { - return obj is OSPlatform && Equals((OSPlatform)obj); + return obj is OSPlatform oSPlatform && Equals(oSPlatform); } public override int GetHashCode() { - return _osPlatform == null ? 0 : _osPlatform.GetHashCode(); + return _osPlatform == null ? 0 : _osPlatform.GetHashCode(StringComparison.OrdinalIgnoreCase); } public override string ToString() diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/CheckPlatformTests.cs b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/CheckPlatformTests.cs index 1264c5c0d850a..e218506630ffe 100644 --- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/CheckPlatformTests.cs +++ b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/CheckPlatformTests.cs @@ -1,7 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Runtime.InteropServices; using Xunit; namespace System.Runtime.InteropServices.RuntimeInformationTests @@ -13,10 +12,10 @@ public void CheckLinux() { Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)); Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("LINUX"))); + Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("linux"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("DARWIN"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD"))); - Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("linux"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NETBSD"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NetBSD"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("netbsd"))); @@ -31,9 +30,9 @@ public void CheckLinux() public void CheckNetBSD() { Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NETBSD"))); + Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NetBSD"))); + Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("netbsd"))); - Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NetBSD"))); - Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("netbsd"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("DARWIN"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("LINUX"))); @@ -51,12 +50,12 @@ public void CheckOSX() { Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.OSX)); Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("OSX"))); + Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("osx"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NETBSD"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NetBSD"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("netbsd"))); - Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("osx"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("mac"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("DARWIN"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("MACOSX"))); @@ -70,6 +69,8 @@ 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"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NETBSD"))); @@ -89,6 +90,8 @@ 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"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NETBSD"))); @@ -108,6 +111,7 @@ public void CheckAndroid() { Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Android)); Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("ANDROID"))); + Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("android"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NETBSD"))); @@ -127,6 +131,7 @@ public void CheckBrowser() { Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Browser)); Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER"))); + Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("browser"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NETBSD"))); @@ -146,6 +151,7 @@ public void CheckWindows() { Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("WINDOWS"))); + Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("windows"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NETBSD"))); @@ -153,6 +159,7 @@ public void CheckWindows() Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("netbsd"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("windows"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("Windows NT"))); + Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("win"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.OSX)); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD)); @@ -213,5 +220,17 @@ public void CheckOSPlatform() Assert.Equal(0, defaultObj.GetHashCode()); Assert.Equal(defaultObj.GetHashCode(), conObj.GetHashCode()); } + + [Fact] + public void StringComparisonOrdinalIgnoreCaseIsUsed() + { + Assert.Equal(OSPlatform.Create("A"), OSPlatform.Create("a")); + Assert.Equal(OSPlatform.Create("A"), OSPlatform.Create("A")); + Assert.Equal(OSPlatform.Create("a"), OSPlatform.Create("a")); + + Assert.Equal(OSPlatform.Create("A").GetHashCode(), OSPlatform.Create("a").GetHashCode()); + Assert.Equal(OSPlatform.Create("A").GetHashCode(), OSPlatform.Create("A").GetHashCode()); + Assert.Equal(OSPlatform.Create("a").GetHashCode(), OSPlatform.Create("a").GetHashCode()); + } } } From caf43a28bce0370565363da53268f05fc6ff9dec Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Mon, 13 Jul 2020 10:58:16 +0200 Subject: [PATCH 03/10] RuntimeInformation.IsOSPlatform(OSPlatform.macOS) must return true on OSX --- .../InteropServices/RuntimeInformation/OSPlatform.cs | 7 +++++-- .../RuntimeInformation/RuntimeInformation.Unix.cs | 10 +++++++++- .../tests/CheckPlatformTests.cs | 5 ++++- 3 files changed, 18 insertions(+), 4 deletions(-) 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 796d85eae0af0..fc1b1ef2cee6c 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 @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Runtime.CompilerServices; + namespace System.Runtime.InteropServices { public readonly struct OSPlatform : IEquatable @@ -17,7 +19,7 @@ namespace System.Runtime.InteropServices public static OSPlatform macOS { get; } = new OSPlatform("MACOS"); - [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] + [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"); @@ -46,9 +48,10 @@ public bool Equals(OSPlatform other) return Equals(other._osPlatform); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal bool Equals(string? other) { - return string.Equals(_osPlatform, other, StringComparison.OrdinalIgnoreCase); + return other?.Length == _osPlatform?.Length && string.Equals(_osPlatform, other, StringComparison.OrdinalIgnoreCase); } public override bool Equals(object? obj) 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 c930aac925406..a8c0afb57d7bd 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 @@ -17,7 +17,15 @@ public static partial class RuntimeInformation public static bool IsOSPlatform(OSPlatform osPlatform) { string name = s_osPlatformName ??= Interop.Sys.GetUnixName(); - return osPlatform.Equals(name); + if (osPlatform.Equals(name)) + { + return true; + } + else if (name.Length == 3 && name[0] == 'O' && name[1] == 'S' && name[2] == 'X') + { + return osPlatform.Equals("MACOS"); + } + return false; } public static string OSDescription => s_osDescription ??= Interop.Sys.GetUnixVersion(); diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/CheckPlatformTests.cs b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/CheckPlatformTests.cs index e218506630ffe..e40f9f8b310af 100644 --- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/CheckPlatformTests.cs +++ b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/CheckPlatformTests.cs @@ -51,6 +51,10 @@ public void CheckOSX() 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"))); @@ -157,7 +161,6 @@ public void CheckWindows() Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NETBSD"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("NetBSD"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("netbsd"))); - Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("windows"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("Windows NT"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("win"))); Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)); From 2f4846229449b2794bab612796ee648ac46cd4e7 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Mon, 13 Jul 2020 15:03:25 +0200 Subject: [PATCH 04/10] add new test cases for IsOSPlatformEarlierThan and IsOSPlatformOrLater --- .../tests/PlatformVersionTests.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/PlatformVersionTests.cs b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/PlatformVersionTests.cs index c00ace3149033..b612d2e065bcb 100644 --- a/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/PlatformVersionTests.cs +++ b/src/libraries/System.Runtime.InteropServices.RuntimeInformation/tests/PlatformVersionTests.cs @@ -14,6 +14,11 @@ public static IEnumerable AllKnownOsPlatforms() 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] @@ -49,6 +54,8 @@ public void IsOSPlatformOrLater_ReturnsTrue_ForCurrentOS(OSPlatform 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)); @@ -78,6 +85,8 @@ public void IsOSPlatformOrLater_ReturnsFalse_ForNewerVersionOfCurrentOS(OSPlatfo 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); @@ -104,6 +113,8 @@ public void IsOSPlatformOrLater_ReturnsTrue_ForOlderVersionOfCurrentOS(OSPlatfor 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) @@ -160,6 +171,8 @@ public void IsOSPlatformEarlierThan_ReturnsFalse_ForCurrentOS(OSPlatform osPlatf 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)); @@ -190,6 +203,8 @@ public void IsOSPlatformEarlierThan_ReturnsTrue_ForNewerVersionOfCurrentOS(OSPla 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); @@ -215,6 +230,8 @@ public void IsOSPlatformEarlierThan_ReturnsFalse_ForOlderVersionOfCurrentOS(OSPl 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) From 911e62bbe9558abd45bd67482e3634c1532219bc Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Mon, 13 Jul 2020 15:45:03 +0200 Subject: [PATCH 05/10] it was not helping the perf for the HIT case --- .../Runtime/InteropServices/RuntimeInformation/OSPlatform.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 fc1b1ef2cee6c..f125c6cd47395 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 @@ -48,10 +48,9 @@ public bool Equals(OSPlatform other) return Equals(other._osPlatform); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] internal bool Equals(string? other) { - return other?.Length == _osPlatform?.Length && string.Equals(_osPlatform, other, StringComparison.OrdinalIgnoreCase); + return string.Equals(_osPlatform, other, StringComparison.OrdinalIgnoreCase); } public override bool Equals(object? obj) From 4ed7d530fcbff80457ee76f302a4b20d8c173e27 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Tue, 14 Jul 2020 10:02:42 +0200 Subject: [PATCH 06/10] cache the IsCurrent information --- .../InteropServices/RuntimeInformation/OSPlatform.cs | 9 +++++++-- .../RuntimeInformation/RuntimeInformation.Browser.cs | 2 +- .../RuntimeInformation/RuntimeInformation.Unix.cs | 6 +++--- .../RuntimeInformation/RuntimeInformation.Windows.cs | 5 +---- .../RuntimeInformation/RuntimeInformation.cs | 5 +++++ 5 files changed, 17 insertions(+), 10 deletions(-) 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 f125c6cd47395..667ab054e865f 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 @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Runtime.CompilerServices; - namespace System.Runtime.InteropServices { public readonly struct OSPlatform : IEquatable @@ -30,14 +28,21 @@ namespace System.Runtime.InteropServices public static OSPlatform Windows { get; } = new OSPlatform("WINDOWS"); + internal bool IsCurrent { 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); } + /// + /// Creates a new OSPlatform instance. + /// + /// If you plan to call this method frequently, please consider caching its result. public static OSPlatform Create(string osPlatform) { return new OSPlatform(osPlatform); 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 769d4ae38f30f..838aa49c2e76e 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,7 +5,7 @@ namespace System.Runtime.InteropServices { public static partial class RuntimeInformation { - public static bool IsOSPlatform(OSPlatform osPlatform) => osPlatform.Equals(OSPlatform.Browser); + internal static bool IsCurrentOSPlatform(string osPlatform) => osPlatform.Equals("BROWSER", StringComparison.OrdinalIgnoreCase); public static string OSDescription => "Browser"; 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 a8c0afb57d7bd..0e1239c43177b 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 @@ -14,16 +14,16 @@ public static partial class RuntimeInformation private static Architecture? s_osArch; private static Architecture? s_processArch; - public static bool IsOSPlatform(OSPlatform osPlatform) + internal static bool IsCurrentOSPlatform(string osPlatform) { string name = s_osPlatformName ??= Interop.Sys.GetUnixName(); - if (osPlatform.Equals(name)) + if (osPlatform.Equals(name, StringComparison.OrdinalIgnoreCase)) { return true; } else if (name.Length == 3 && name[0] == 'O' && name[1] == 'S' && name[2] == 'X') { - return osPlatform.Equals("MACOS"); + return osPlatform.Equals("MACOS", StringComparison.OrdinalIgnoreCase); } return false; } 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 0ccc9deb93004..7c71cc972aad5 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 @@ -13,10 +13,7 @@ public static partial class RuntimeInformation private static Architecture? s_osArch; private static Architecture? s_processArch; - public static bool IsOSPlatform(OSPlatform osPlatform) - { - return OSPlatform.Windows == osPlatform; - } + internal static bool IsCurrentOSPlatform(string osPlatform) => osPlatform.Equals("WINDOWS", StringComparison.OrdinalIgnoreCase); public static string OSDescription { 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 8e43d13ec4638..95b1b62b4cdbe 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 @@ -48,5 +48,10 @@ public static string FrameworkDescription /// public static string RuntimeIdentifier => s_runtimeIdentifier ??= AppContext.GetData("RUNTIME_IDENTIFIER") as string ?? "unknown"; + + /// + /// Indicates whether the current application is running on the specified platform. + /// + public static bool IsOSPlatform(OSPlatform osPlatform) => osPlatform.IsCurrent; } } From 46b3df88f0f7d05e9098ff67107e8adb6333cbdc Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Tue, 14 Jul 2020 10:03:45 +0200 Subject: [PATCH 07/10] apply code review suggestion Co-authored-by: Jan Kotas --- .../Runtime/InteropServices/RuntimeInformation/OSPlatform.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 667ab054e865f..ec5b34ee6500a 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 @@ -60,7 +60,7 @@ internal bool Equals(string? other) public override bool Equals(object? obj) { - return obj is OSPlatform oSPlatform && Equals(oSPlatform); + return obj is OSPlatform osPlatform && Equals(osPlatform); } public override int GetHashCode() From 6082d892870a9599417c491d9347d6c4389c3a59 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Tue, 14 Jul 2020 10:09:36 +0200 Subject: [PATCH 08/10] apply code review suggestion Co-authored-by: Jan Kotas --- .../RuntimeInformation/RuntimeInformation.Unix.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 0e1239c43177b..8284e06f38542 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 @@ -21,7 +21,7 @@ internal static bool IsCurrentOSPlatform(string osPlatform) { return true; } - else if (name.Length == 3 && name[0] == 'O' && name[1] == 'S' && name[2] == 'X') + else if (name == "OSX") { return osPlatform.Equals("MACOS", StringComparison.OrdinalIgnoreCase); } From dcb47862d35917702a6f865feca2357e502bc868 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Tue, 14 Jul 2020 10:12:31 +0200 Subject: [PATCH 09/10] simplify the source code and add a comment --- .../RuntimeInformation/RuntimeInformation.Unix.cs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) 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 8284e06f38542..ea7db57e016f8 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 @@ -17,15 +17,9 @@ public static partial class RuntimeInformation internal static bool IsCurrentOSPlatform(string osPlatform) { string name = s_osPlatformName ??= Interop.Sys.GetUnixName(); - if (osPlatform.Equals(name, StringComparison.OrdinalIgnoreCase)) - { - return true; - } - else if (name == "OSX") - { - return osPlatform.Equals("MACOS", StringComparison.OrdinalIgnoreCase); - } - return false; + + 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(); From 90f38099f77586dcc227947a63e14165a0b7fcbd Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Tue, 14 Jul 2020 10:14:40 +0200 Subject: [PATCH 10/10] add a comment --- .../Runtime/InteropServices/RuntimeInformation/OSPlatform.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 ec5b34ee6500a..18e45643d216f 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 @@ -28,7 +28,7 @@ namespace System.Runtime.InteropServices public static OSPlatform Windows { get; } = new OSPlatform("WINDOWS"); - internal bool IsCurrent { get; } + internal bool IsCurrent { get; } // this information is cached because it's frequently used private OSPlatform(string osPlatform) {