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(new char[1] {
','
}));
string text = platform.Trim();
string text2 = text.ToUpper();
if (text2 != null) {
switch (text2.Length) {
case 3:
break;
case 5:
goto IL_00b0;
case 6:
goto IL_0124;
case 13:
goto IL_0161;
case 15:
goto IL_018a;
case 4:
goto IL_01b3;
case 8:
goto IL_01db;
case 9:
goto IL_01fb;
case 14:
goto IL_0224;
case 12:
goto IL_03e3;
case 7:
goto IL_03f8;
case 10:
goto IL_0504;
default:
goto IL_0792;
}
switch (text2[2]) {
case 'N':
break;
case '3':
goto IL_0259;
case '4':
goto IL_026e;
case '5':
goto IL_0283;
case '6':
goto IL_0298;
default:
goto IL_0792;
}
if (text2 == "WIN")
goto IL_0592;
}
goto IL_0792;
IL_0283:
if (!(text2 == "NT5"))
goto IL_0792;
bool flag = _os.IsNT5;
goto IL_079a;
IL_00b0:
switch (text2[4]) {
case '2':
break;
case '5':
goto IL_02c2;
case '8':
goto IL_02d7;
case 'E':
goto IL_02ec;
case 'K':
goto IL_0301;
case 'P':
goto IL_0316;
case 'A':
goto IL_032b;
case '0':
goto IL_0340;
case '1':
goto IL_0355;
case 'X':
goto IL_036a;
default:
goto IL_0792;
}
if (text2 == "WIN32")
goto IL_0592;
goto IL_0792;
IL_02ec:
if (!(text2 == "WINME"))
goto IL_0792;
flag = _os.IsWinME;
goto IL_079a;
IL_018a:
switch (text2[5]) {
case '0':
break;
case '1':
goto IL_0461;
case 'W':
goto IL_0476;
default:
goto IL_0792;
}
if (!(text2 == "WIN2008SERVERR2"))
goto IL_0792;
flag = _os.IsWin2008ServerR2;
goto IL_079a;
IL_0476:
if (!(text2 == "WINDOWSSERVER10"))
goto IL_0792;
flag = _os.IsWindowsServer10;
goto IL_079a;
IL_0259:
if (!(text2 == "NT3"))
goto IL_0792;
flag = _os.IsNT3;
goto IL_079a;
IL_0461:
if (!(text2 == "WIN2012SERVERR2"))
goto IL_0792;
flag = _os.IsWin2012ServerR2;
goto IL_079a;
IL_0792:
flag = IsRuntimeSupported(text);
goto IL_079a;
IL_01b3:
switch (text2[0]) {
case 'W':
break;
case 'U':
goto IL_04b0;
case 'X':
goto IL_04c5;
default:
goto IL_0792;
}
if (text2 == "WIN7")
goto IL_06e5;
if (text2 == "WIN8")
goto IL_06f6;
goto IL_0792;
IL_04c5:
if (!(text2 == "XBOX"))
goto IL_0792;
flag = _os.IsXbox;
goto IL_079a;
IL_0224:
switch (text2[0]) {
case '6':
break;
case '3':
goto IL_057d;
default:
goto IL_0792;
}
if (text2 == "64-BIT-PROCESS")
goto IL_076c;
goto IL_0792;
IL_0355:
if (text2 == "WIN11")
goto IL_0726;
goto IL_0792;
IL_057d:
if (text2 == "32-BIT-PROCESS")
goto IL_0774;
goto IL_0792;
IL_0161:
switch (text2[6]) {
case '3':
break;
case '8':
goto IL_0422;
case '2':
goto IL_0437;
default:
goto IL_0792;
}
if (!(text2 == "WIN2003SERVER"))
goto IL_0792;
flag = _os.IsWin2003Server;
goto IL_079a;
IL_0422:
if (!(text2 == "WIN2008SERVER"))
goto IL_0792;
flag = _os.IsWin2008Server;
goto IL_079a;
IL_032b:
if (!(text2 == "VISTA"))
goto IL_0792;
flag = _os.IsVista;
goto IL_079a;
IL_0301:
if (!(text2 == "WIN2K"))
goto IL_0792;
flag = _os.IsWin2K;
goto IL_079a;
IL_06f6:
flag = _os.IsWindows8;
goto IL_079a;
IL_0437:
if (!(text2 == "WIN2012SERVER"))
goto IL_0792;
flag = (_os.IsWin2012ServerR1 || _os.IsWin2012ServerR2);
goto IL_079a;
IL_06e5:
flag = _os.IsWindows7;
goto IL_079a;
IL_0742:
flag = _os.IsUnix;
goto IL_079a;
IL_02c2:
if (!(text2 == "WIN95"))
goto IL_0792;
flag = _os.IsWin95;
goto IL_079a;
IL_04b0:
if (text2 == "UNIX")
goto IL_0742;
goto IL_0792;
IL_03e3:
if (!(text2 == "WIN32WINDOWS"))
goto IL_0792;
flag = _os.IsWin32Windows;
goto IL_079a;
IL_02d7:
if (!(text2 == "WIN98"))
goto IL_0792;
flag = _os.IsWin98;
goto IL_079a;
IL_0504:
if (text2 == "WINDOWS8.1")
goto IL_0707;
goto IL_0792;
IL_01fb:
switch (text2[0]) {
case 'W':
break;
case '6':
goto IL_053e;
case '3':
goto IL_0553;
default:
goto IL_0792;
}
if (text2 == "WINDOWS10")
goto IL_0718;
if (text2 == "WINDOWS11")
goto IL_0726;
goto IL_0792;
IL_0726:
flag = _os.IsWindows11;
goto IL_079a;
IL_0340:
if (text2 == "WIN10")
goto IL_0718;
goto IL_0792;
IL_053e:
if (!(text2 == "64-BIT-OS"))
goto IL_0792;
flag = Environment.Is64BitOperatingSystem;
goto IL_079a;
IL_036a:
if (text2 == "LINUX")
goto IL_0742;
goto IL_0792;
IL_0553:
if (!(text2 == "32-BIT-OS"))
goto IL_0792;
flag = !Environment.Is64BitOperatingSystem;
goto IL_079a;
IL_0718:
flag = _os.IsWindows10;
goto IL_079a;
IL_01db:
switch (text2[7]) {
case '7':
break;
case '8':
goto IL_04ef;
default:
goto IL_0792;
}
if (text2 == "WINDOWS7")
goto IL_06e5;
goto IL_0792;
IL_04ef:
if (text2 == "WINDOWS8")
goto IL_06f6;
goto IL_0792;
IL_0316:
if (!(text2 == "WINXP"))
goto IL_0792;
flag = _os.IsWinXP;
goto IL_079a;
IL_03f8:
if (!(text2 == "WIN32NT"))
goto IL_0792;
flag = _os.IsWin32NT;
goto IL_079a;
IL_079a:
if (!flag)
Reason = "Only supported on " + platform;
return flag;
IL_0124:
switch (text2[0]) {
case 'W':
break;
case 'M':
goto IL_03a4;
case '6':
goto IL_03b9;
case '3':
goto IL_03ce;
default:
goto IL_0792;
}
if (!(text2 == "WIN32S")) {
if (text2 == "WIN8.1")
goto IL_0707;
goto IL_0792;
}
flag = _os.IsWin32S;
goto IL_079a;
IL_03b9:
if (text2 == "64-BIT")
goto IL_076c;
goto IL_0792;
IL_076c:
flag = Environment.Is64BitProcess;
goto IL_079a;
IL_03ce:
if (text2 == "32-BIT")
goto IL_0774;
goto IL_0792;
IL_0774:
flag = !Environment.Is64BitProcess;
goto IL_079a;
IL_0592:
flag = _os.IsWindows;
goto IL_079a;
IL_0298:
if (!(text2 == "NT6"))
goto IL_0792;
flag = _os.IsNT6;
goto IL_079a;
IL_0707:
flag = _os.IsWindows81;
goto IL_079a;
IL_026e:
if (!(text2 == "NT4"))
goto IL_0792;
flag = _os.IsNT4;
goto IL_079a;
IL_03a4:
if (!(text2 == "MACOSX"))
goto IL_0792;
flag = _os.IsMacOSX;
goto IL_079a;
}
private bool IsRuntimeSupported(string platformName)
{
string versionSpecification = null;
string[] array = platformName.Split(new char[1] {
'-'
});
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)
throw new PlatformNotSupportedException($"""{runtime}""{versionSpecification}");
RuntimeFramework target = new RuntimeFramework(runtime, RuntimeFramework.DefaultVersion);
return _rt.Supports(target);
}
}
}