NetPlatformHelper
Class is used by the NetPlatformAttribute class to
determine whether a platform is supported.
using System;
using System.Linq;
using System.Runtime.CompilerServices;
namespace NUnit.Framework.Internal
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public static class NetPlatformHelper
{
[System.Runtime.CompilerServices.Nullable(0)]
private static class OperatingSystem
{
public static bool IsOSPlatform(string platform)
{
if (platform.Equals("Windows", StringComparison.OrdinalIgnoreCase))
return Environment.OSVersion.Platform == PlatformID.Win32NT;
if (platform.Equals("Linux", StringComparison.OrdinalIgnoreCase))
return Environment.OSVersion.Platform == PlatformID.Unix;
if (platform.Equals("macOS", StringComparison.OrdinalIgnoreCase))
return Environment.OSVersion.Platform == PlatformID.MacOSX;
return false;
}
public static bool IsOSPlatformVersionAtLeast(string os, int major, int minor, int build, int revision)
{
if (IsOSPlatform(os))
return IsOSVersionAtLeast(major, minor, build, revision);
return false;
}
private static bool IsOSVersionAtLeast(int major, int minor, int build, int revision)
{
Version version = Environment.OSVersion.Version;
if (version.Major != major)
return version.Major > major;
if (version.Minor != minor)
return version.Minor > minor;
if (version.Build != build)
return version.Build > build;
if (version.Revision < revision) {
if (version.Revision == -1)
return revision == 0;
return false;
}
return true;
}
}
private static readonly char[] Digits = new char[10] {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9'
};
public static bool IsPlatformSupported(string[] platforms)
{
return platforms.Any(IsPlatformSupported);
}
public static bool IsPlatformSupported(string platform)
{
if (Enumerable.Contains(platform, ','))
return IsPlatformSupported(platform.Split(new char[1] {
','
}));
string plaformName = platform.Trim();
ParseOSAndVersion(plaformName, out string os, out Version version);
if ((object)version != null && os.Equals("Windows", StringComparison.OrdinalIgnoreCase)) {
if (version.Major == 7)
version = new Version(6, 1);
else if (version.Major == 8) {
version = new Version(6, (version.Minor == 1) ? 3 : 2);
}
}
if ((object)version != null)
return OperatingSystem.IsOSPlatformVersionAtLeast(os, version.Major, version.Minor, version.Build, version.MajorRevision);
return OperatingSystem.IsOSPlatform(os);
}
private static void ParseOSAndVersion(string plaformName, out string os, [System.Runtime.CompilerServices.Nullable(2)] out Version version)
{
int num = plaformName.IndexOfAny(Digits);
if (num > 0) {
os = plaformName.Substring(0, num);
Version.TryParse(plaformName.Substring(num), out version);
} else {
os = plaformName;
version = null;
}
}
}
}