This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Add OSVersion APIs to .NET Core #1999
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
78 changes: 0 additions & 78 deletions
78
src/System.Runtime.Environment/tests/CheckPlatformTests.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/System.Runtime.InteropServices.RuntimeInformation/src/OSPlatform.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace System.Runtime.InteropServices | ||
{ | ||
public struct OSPlatform : IEquatable<OSPlatform> | ||
{ | ||
private readonly string _osPlatform; | ||
|
||
private const string WindowsName = "WINDOWS"; | ||
private const string LinuxName = "LINUX"; | ||
private const string OSXName = "OSX"; | ||
|
||
private static readonly OSPlatform s_windows = new OSPlatform(WindowsName); | ||
private static readonly OSPlatform s_linux = new OSPlatform(LinuxName); | ||
private static readonly OSPlatform s_osx = new OSPlatform(OSXName); | ||
|
||
public static OSPlatform Windows | ||
{ | ||
get | ||
{ | ||
return s_windows; | ||
} | ||
} | ||
|
||
public static OSPlatform Linux | ||
{ | ||
get | ||
{ | ||
return s_linux; | ||
} | ||
} | ||
|
||
public static OSPlatform OSX | ||
{ | ||
get | ||
{ | ||
return s_osx; | ||
} | ||
} | ||
|
||
private OSPlatform(string osPlatform) | ||
{ | ||
if (osPlatform == null) throw new ArgumentNullException("name"); | ||
if (osPlatform.Length == 0) throw new ArgumentException(SR.Argument_EmptyValue, "name"); | ||
|
||
_osPlatform = osPlatform; | ||
} | ||
|
||
public static OSPlatform Create(string osPlatform) | ||
{ | ||
return new OSPlatform(osPlatform); | ||
} | ||
|
||
public bool Equals(OSPlatform other) | ||
{ | ||
return string.Equals(other._osPlatform, _osPlatform, StringComparison.Ordinal); | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
if (obj is OSPlatform) | ||
{ | ||
return Equals((OSPlatform)obj); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return _osPlatform == null ? 0 : _osPlatform.GetHashCode(); | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return _osPlatform ?? string.Empty; | ||
} | ||
|
||
public static bool operator ==(OSPlatform left, OSPlatform right) | ||
{ | ||
return left.Equals(right); | ||
} | ||
|
||
public static bool operator !=(OSPlatform left, OSPlatform right) | ||
{ | ||
return !(left == right); | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
80 changes: 80 additions & 0 deletions
80
src/System.Runtime.InteropServices.RuntimeInformation/tests/CheckPlatformTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System.Runtime.InteropServices; | ||
using Xunit; | ||
|
||
namespace System.Runtime.InteropServices.RuntimeInformationTests | ||
{ | ||
public class CheckPlatformTests | ||
{ | ||
[Fact, PlatformSpecific(PlatformID.Windows)] | ||
public void CheckWindows() | ||
{ | ||
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); | ||
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("WINDOWS"))); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("windows"))); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("Windows NT"))); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.OSX)); | ||
} | ||
|
||
[Fact, PlatformSpecific(PlatformID.Linux)] | ||
public void CheckLinux() | ||
{ | ||
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)); | ||
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("LINUX"))); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("linux"))); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("UNIX"))); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("DARWIN"))); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("ubuntu"))); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.OSX)); | ||
} | ||
|
||
[Fact, PlatformSpecific(PlatformID.OSX)] | ||
public void CheckOSX() | ||
{ | ||
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.OSX)); | ||
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("OSX"))); | ||
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"))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding a check for "Darwin" as well. |
||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); | ||
} | ||
|
||
[Fact] | ||
public void CheckOSPlatform() | ||
{ | ||
OSPlatform winObj = OSPlatform.Create("WINDOWS"); | ||
OSPlatform winProp = OSPlatform.Windows; | ||
OSPlatform randomObj = OSPlatform.Create("random"); | ||
OSPlatform defaultObj = default(OSPlatform); | ||
OSPlatform conObj = new OSPlatform(); | ||
Assert.Throws<ArgumentNullException>(() => { OSPlatform nullObj = OSPlatform.Create(null); }); | ||
Assert.Throws<ArgumentException>(() => { OSPlatform emptyObj = OSPlatform.Create(""); }); | ||
|
||
Assert.True(winObj == winProp); | ||
Assert.True(winObj != randomObj); | ||
Assert.True(defaultObj == conObj); | ||
Assert.False(winObj == defaultObj); | ||
Assert.False(winObj == randomObj); | ||
Assert.False(winObj != winProp); | ||
|
||
Assert.True(winObj.Equals(winProp)); | ||
Assert.True(conObj.Equals(defaultObj)); | ||
Assert.False(defaultObj.Equals(winProp)); | ||
Assert.False(winObj.Equals(null)); | ||
Assert.False(winObj.Equals("something")); | ||
|
||
Assert.Equal("WINDOWS", winObj.ToString()); | ||
Assert.Equal("WINDOWS", winProp.ToString()); | ||
Assert.Equal("", defaultObj.ToString()); | ||
Assert.Equal("", conObj.ToString()); | ||
Assert.Equal("random", randomObj.ToString()); | ||
|
||
Assert.Equal(winObj.GetHashCode(), winProp.GetHashCode()); | ||
Assert.Equal(0, defaultObj.GetHashCode()); | ||
Assert.Equal(defaultObj.GetHashCode(), conObj.GetHashCode()); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Priya91 sorry for not catching this earlier, but the parameter name for the exceptions is wrong here:
nameosPlatformThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed by commit 2f31aa9