PlatformHelper
PlatformHelper class is used by the PlatformAttribute 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 class PlatformHelper
{
private readonly OSPlatform _os;
private readonly RuntimeFramework _rt;
private const string CommonOSPlatforms = "Win,Win32,Win32S,Win32NT,Win32Windows,Win95,Win98,WinMe,NT3,NT4,NT5,NT6,Win2008Server,Win2008ServerR2,Win2012Server,Win2012ServerR2,Win2K,WinXP,Win2003Server,Vista,Win7,Windows7,Win8,Windows8,Win8.1,Windows8.1,Win10,Windows10,Win11,Windows11,WindowsServer10,Unix,Linux";
public const string OSPlatforms = "Win,Win32,Win32S,Win32NT,Win32Windows,Win95,Win98,WinMe,NT3,NT4,NT5,NT6,Win2008Server,Win2008ServerR2,Win2012Server,Win2012ServerR2,Win2K,WinXP,Win2003Server,Vista,Win7,Windows7,Win8,Windows8,Win8.1,Windows8.1,Win10,Windows10,Win11,Windows11,WindowsServer10,Unix,Linux,Xbox,MacOSX";
public static readonly string RuntimePlatforms = "Net,NetCore,SSCLI,Rotor,Mono,MonoTouch";
public string Reason { get; set; } = string.Empty;
public PlatformHelper()
{
_os = OSPlatform.CurrentPlatform;
_rt = RuntimeFramework.CurrentFramework;
}
public PlatformHelper(OSPlatform os, RuntimeFramework rt)
{
_os = os;
_rt = rt;
}
public bool IsPlatformSupported(string[] platforms)
{
return platforms.Any(IsPlatformSupported);
}
public bool IsPlatformSupported(PlatformAttribute platformAttribute)
{
string include = platformAttribute.Include;
string exclude = platformAttribute.Exclude;
return IsPlatformSupported(include, exclude);
}
public bool IsPlatformSupported(TestCaseAttribute testCaseAttribute)
{
string includePlatform = testCaseAttribute.IncludePlatform;
string excludePlatform = testCaseAttribute.ExcludePlatform;
return IsPlatformSupported(includePlatform, excludePlatform);
}
[System.Runtime.CompilerServices.NullableContext(2)]
private bool IsPlatformSupported(string include, string exclude)
{
if (include != null && !IsPlatformSupported(include)) {
Reason = "Only supported on " + include;
return false;
}
if (exclude != null && IsPlatformSupported(exclude)) {
Reason = "Not supported on " + exclude;
return false;
}
return true;
}
public bool IsPlatformSupported(string platform)
{
if (platform.IndexOf(',') >= 0)
return IsPlatformSupported(platform.Split(',', StringSplitOptions.None));
string text = platform.Trim();
string text2 = text.ToUpper();
if (text2 != null) {
switch (text2.Length) {
case 3:
break;
case 5:
goto IL_00a8;
case 6:
goto IL_011c;
case 13:
goto IL_0159;
case 15:
goto IL_0182;
case 4:
goto IL_01ab;
case 8:
goto IL_01d3;
case 9:
goto IL_01f3;
case 14:
goto IL_021c;
case 12:
goto IL_03db;
case 7:
goto IL_03f0;
case 10:
goto IL_04fc;
default:
goto IL_078a;
}
switch (text2[2]) {
case 'N':
break;
case '3':
goto IL_0251;
case '4':
goto IL_0266;
case '5':
goto IL_027b;
case '6':
goto IL_0290;
default:
goto IL_078a;
}
if (text2 == "WIN")
goto IL_058a;
}
goto IL_078a;
IL_027b:
if (!(text2 == "NT5"))
goto IL_078a;
bool flag = _os.IsNT5;
goto IL_0792;
IL_00a8:
switch (text2[4]) {
case '2':
break;
case '5':
goto IL_02ba;
case '8':
goto IL_02cf;
case 'E':
goto IL_02e4;
case 'K':
goto IL_02f9;
case 'P':
goto IL_030e;
case 'A':
goto IL_0323;
case '0':
goto IL_0338;
case '1':
goto IL_034d;
case 'X':
goto IL_0362;
default:
goto IL_078a;
}
if (text2 == "WIN32")
goto IL_058a;
goto IL_078a;
IL_02e4:
if (!(text2 == "WINME"))
goto IL_078a;
flag = _os.IsWinME;
goto IL_0792;
IL_0182:
switch (text2[5]) {
case '0':
break;
case '1':
goto IL_0459;
case 'W':
goto IL_046e;
default:
goto IL_078a;
}
if (!(text2 == "WIN2008SERVERR2"))
goto IL_078a;
flag = _os.IsWin2008ServerR2;
goto IL_0792;
IL_046e:
if (!(text2 == "WINDOWSSERVER10"))
goto IL_078a;
flag = _os.IsWindowsServer10;
goto IL_0792;
IL_0251:
if (!(text2 == "NT3"))
goto IL_078a;
flag = _os.IsNT3;
goto IL_0792;
IL_0459:
if (!(text2 == "WIN2012SERVERR2"))
goto IL_078a;
flag = _os.IsWin2012ServerR2;
goto IL_0792;
IL_078a:
flag = IsRuntimeSupported(text);
goto IL_0792;
IL_01ab:
switch (text2[0]) {
case 'W':
break;
case 'U':
goto IL_04a8;
case 'X':
goto IL_04bd;
default:
goto IL_078a;
}
if (text2 == "WIN7")
goto IL_06dd;
if (text2 == "WIN8")
goto IL_06ee;
goto IL_078a;
IL_04bd:
if (!(text2 == "XBOX"))
goto IL_078a;
flag = _os.IsXbox;
goto IL_0792;
IL_021c:
switch (text2[0]) {
case '6':
break;
case '3':
goto IL_0575;
default:
goto IL_078a;
}
if (text2 == "64-BIT-PROCESS")
goto IL_0764;
goto IL_078a;
IL_034d:
if (text2 == "WIN11")
goto IL_071e;
goto IL_078a;
IL_0575:
if (text2 == "32-BIT-PROCESS")
goto IL_076c;
goto IL_078a;
IL_0159:
switch (text2[6]) {
case '3':
break;
case '8':
goto IL_041a;
case '2':
goto IL_042f;
default:
goto IL_078a;
}
if (!(text2 == "WIN2003SERVER"))
goto IL_078a;
flag = _os.IsWin2003Server;
goto IL_0792;
IL_041a:
if (!(text2 == "WIN2008SERVER"))
goto IL_078a;
flag = _os.IsWin2008Server;
goto IL_0792;
IL_0323:
if (!(text2 == "VISTA"))
goto IL_078a;
flag = _os.IsVista;
goto IL_0792;
IL_02f9:
if (!(text2 == "WIN2K"))
goto IL_078a;
flag = _os.IsWin2K;
goto IL_0792;
IL_06ee:
flag = _os.IsWindows8;
goto IL_0792;
IL_042f:
if (!(text2 == "WIN2012SERVER"))
goto IL_078a;
flag = (_os.IsWin2012ServerR1 || _os.IsWin2012ServerR2);
goto IL_0792;
IL_06dd:
flag = _os.IsWindows7;
goto IL_0792;
IL_073a:
flag = _os.IsUnix;
goto IL_0792;
IL_02ba:
if (!(text2 == "WIN95"))
goto IL_078a;
flag = _os.IsWin95;
goto IL_0792;
IL_04a8:
if (text2 == "UNIX")
goto IL_073a;
goto IL_078a;
IL_03db:
if (!(text2 == "WIN32WINDOWS"))
goto IL_078a;
flag = _os.IsWin32Windows;
goto IL_0792;
IL_02cf:
if (!(text2 == "WIN98"))
goto IL_078a;
flag = _os.IsWin98;
goto IL_0792;
IL_04fc:
if (text2 == "WINDOWS8.1")
goto IL_06ff;
goto IL_078a;
IL_01f3:
switch (text2[0]) {
case 'W':
break;
case '6':
goto IL_0536;
case '3':
goto IL_054b;
default:
goto IL_078a;
}
if (text2 == "WINDOWS10")
goto IL_0710;
if (text2 == "WINDOWS11")
goto IL_071e;
goto IL_078a;
IL_071e:
flag = _os.IsWindows11;
goto IL_0792;
IL_0338:
if (text2 == "WIN10")
goto IL_0710;
goto IL_078a;
IL_0536:
if (!(text2 == "64-BIT-OS"))
goto IL_078a;
flag = Environment.Is64BitOperatingSystem;
goto IL_0792;
IL_0362:
if (text2 == "LINUX")
goto IL_073a;
goto IL_078a;
IL_054b:
if (!(text2 == "32-BIT-OS"))
goto IL_078a;
flag = !Environment.Is64BitOperatingSystem;
goto IL_0792;
IL_0710:
flag = _os.IsWindows10;
goto IL_0792;
IL_01d3:
switch (text2[7]) {
case '7':
break;
case '8':
goto IL_04e7;
default:
goto IL_078a;
}
if (text2 == "WINDOWS7")
goto IL_06dd;
goto IL_078a;
IL_04e7:
if (text2 == "WINDOWS8")
goto IL_06ee;
goto IL_078a;
IL_030e:
if (!(text2 == "WINXP"))
goto IL_078a;
flag = _os.IsWinXP;
goto IL_0792;
IL_03f0:
if (!(text2 == "WIN32NT"))
goto IL_078a;
flag = _os.IsWin32NT;
goto IL_0792;
IL_0792:
if (!flag)
Reason = "Only supported on " + platform;
return flag;
IL_011c:
switch (text2[0]) {
case 'W':
break;
case 'M':
goto IL_039c;
case '6':
goto IL_03b1;
case '3':
goto IL_03c6;
default:
goto IL_078a;
}
if (!(text2 == "WIN32S")) {
if (text2 == "WIN8.1")
goto IL_06ff;
goto IL_078a;
}
flag = _os.IsWin32S;
goto IL_0792;
IL_03b1:
if (text2 == "64-BIT")
goto IL_0764;
goto IL_078a;
IL_0764:
flag = Environment.Is64BitProcess;
goto IL_0792;
IL_03c6:
if (text2 == "32-BIT")
goto IL_076c;
goto IL_078a;
IL_076c:
flag = !Environment.Is64BitProcess;
goto IL_0792;
IL_058a:
flag = _os.IsWindows;
goto IL_0792;
IL_0290:
if (!(text2 == "NT6"))
goto IL_078a;
flag = _os.IsNT6;
goto IL_0792;
IL_06ff:
flag = _os.IsWindows81;
goto IL_0792;
IL_0266:
if (!(text2 == "NT4"))
goto IL_078a;
flag = _os.IsNT4;
goto IL_0792;
IL_039c:
if (!(text2 == "MACOSX"))
goto IL_078a;
flag = _os.IsMacOSX;
goto IL_0792;
}
private bool IsRuntimeSupported(string platformName)
{
string versionSpecification = null;
string[] array = platformName.Split('-', StringSplitOptions.None);
if (array.Length == 2) {
platformName = array[0];
versionSpecification = array[1];
}
switch (platformName.ToUpper()) {
case "NET":
return IsRuntimeSupported(RuntimeType.NetFramework, versionSpecification);
case "NETCORE":
return IsNetCoreRuntimeSupported(RuntimeType.NetCore, versionSpecification);
case "SSCLI":
case "ROTOR":
return IsRuntimeSupported(RuntimeType.SSCLI, versionSpecification);
case "MONO":
return IsRuntimeSupported(RuntimeType.Mono, versionSpecification);
case "MONOTOUCH":
return IsRuntimeSupported(RuntimeType.MonoTouch, versionSpecification);
default:
throw new InvalidPlatformException("Invalid platform name: " + platformName);
}
}
[System.Runtime.CompilerServices.NullableContext(2)]
private bool IsRuntimeSupported(RuntimeType runtime, string versionSpecification)
{
Version version = (versionSpecification == null) ? RuntimeFramework.DefaultVersion : new Version(versionSpecification);
RuntimeFramework target = new RuntimeFramework(runtime, version);
return _rt.Supports(target);
}
[System.Runtime.CompilerServices.NullableContext(2)]
private bool IsNetCoreRuntimeSupported(RuntimeType runtime, string versionSpecification)
{
if (versionSpecification != null) {
DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(52, 2);
defaultInterpolatedStringHandler.AppendLiteral("Detecting versions of .NET Core is not supported - ");
defaultInterpolatedStringHandler.AppendFormatted(runtime);
defaultInterpolatedStringHandler.AppendLiteral("-");
defaultInterpolatedStringHandler.AppendFormatted(versionSpecification);
throw new PlatformNotSupportedException(defaultInterpolatedStringHandler.ToStringAndClear());
}
RuntimeFramework target = new RuntimeFramework(runtime, RuntimeFramework.DefaultVersion);
return _rt.Supports(target);
}
}
}