Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Add OSVersion APIs to .NET Core
Browse files Browse the repository at this point in the history
The contract System.Runtime.InteropServices.RuntimeInformation provides
RuntimeInformation type that has methods to query for underlying OS
information at runtime. The OS information is expressed by the OSPlatform type.
Reference issue: dotnet\corefx#1017, API Review done on 2015\06\09.
  • Loading branch information
Lakshmi Priya Sekar committed Jun 11, 2015
1 parent 8d8d3bc commit e84ac58
Show file tree
Hide file tree
Showing 15 changed files with 185 additions and 154 deletions.
61 changes: 0 additions & 61 deletions src/System.Runtime.Environment/src/OSName.cs

This file was deleted.

78 changes: 0 additions & 78 deletions src/System.Runtime.Environment/tests/CheckPlatformTests.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.22609.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Runtime.Environment", "src\System.Runtime.Environment.csproj", "{F9DF2357-81B4-4317-908E-512DA9395583}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Runtime.InteropServices.RuntimeInformation", "src\System.Runtime.InteropServices.RuntimeInformation.csproj", "{F9DF2357-81B4-4317-908E-512DA9395583}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Runtime.Environment.Tests", "tests\System.Runtime.Environment.Tests.csproj", "{9B4D1DA9-AA4C-428F-9F66-D45C924025A5}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Runtime.InteropServices.RuntimeInformation.Tests", "tests\System.Runtime.InteropServices.RuntimeInformation.Tests.csproj", "{9B4D1DA9-AA4C-428F-9F66-D45C924025A5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace System.Runtime.InteropServices
{
public static class RuntimeInformation
{
public static bool IsOperatingSystem(OSName osName)
public static bool IsOSPlatform(OSPlatform osPlatform)
{
return OSName.Linux == osName;
return OSPlatform.Linux == osPlatform;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace System.Runtime.InteropServices
{
public static class RuntimeInformation
{
public static bool IsOperatingSystem(OSName osName)
public static bool IsOSPlatform(OSPlatform osPlatform)
{
return OSName.OSX == osName;
return OSPlatform.OSX == osPlatform;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace System.Runtime.InteropServices
{
public static class RuntimeInformation
{
public static bool IsOperatingSystem(OSName osName)
public static bool IsOSPlatform(OSPlatform osPlatform)
{
return OSName.Windows == osName;
return OSPlatform.Windows == osPlatform;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<OutputType>Library</OutputType>
<RootNamespace>System.Runtime.InteropServices</RootNamespace>
<AssemblyName>System.Runtime.Environment</AssemblyName>
<AssemblyName>System.Runtime.InteropServices.RuntimeInformation</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<ProjectGuid>{F9DF2357-81B4-4317-908E-512DA9395583}</ProjectGuid>
</PropertyGroup>
Expand All @@ -30,7 +30,7 @@
<None Include="project.json" />
</ItemGroup>
<ItemGroup>
<Compile Include="OSName.cs" />
<Compile Include="OSPlatform.cs" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>
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")));
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());
}
}
}
Loading

0 comments on commit e84ac58

Please sign in to comment.