PageRenderTime 78ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/core/PlatformHelper.cs

#
C# | 243 lines | 173 code | 24 blank | 46 comment | 28 complexity | 549cb8dc75c27cbdeba3d31a9feebf76 MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // This is free software licensed under the NUnit license. You
  3. // may obtain a copy of the license as well as information regarding
  4. // copyright ownership at http://nunit.org.
  5. // ****************************************************************
  6. using System;
  7. using System.Reflection;
  8. namespace NUnit.Core
  9. {
  10. public class PlatformHelper
  11. {
  12. private OSPlatform os;
  13. private RuntimeFramework rt;
  14. // Set whenever we fail to support a list of platforms
  15. private string reason = string.Empty;
  16. /// <summary>
  17. /// Comma-delimited list of all supported OS platform constants
  18. /// </summary>
  19. public static readonly string OSPlatforms =
  20. "Win,Win32,Win32S,Win32NT,Win32Windows,WinCE,Win95,Win98,WinMe,NT3,NT4,NT5,NT6,Win2K,WinXP,Win2003Server,Vista,Win2008Server,Win2008ServerR2,Windows7,Unix,Linux";
  21. /// <summary>
  22. /// Comma-delimited list of all supported Runtime platform constants
  23. /// </summary>
  24. public static readonly string RuntimePlatforms =
  25. "Net,NetCF,SSCLI,Rotor,Mono";
  26. /// <summary>
  27. /// Default constructor uses the operating system and
  28. /// common language runtime of the system.
  29. /// </summary>
  30. public PlatformHelper()
  31. {
  32. this.os = OSPlatform.CurrentPlatform;
  33. this.rt = RuntimeFramework.CurrentFramework;
  34. }
  35. /// <summary>
  36. /// Contruct a PlatformHelper for a particular operating
  37. /// system and common language runtime. Used in testing.
  38. /// </summary>
  39. /// <param name="os">OperatingSystem to be used</param>
  40. public PlatformHelper( OSPlatform os, RuntimeFramework rt )
  41. {
  42. this.os = os;
  43. this.rt = rt;
  44. }
  45. /// <summary>
  46. /// Test to determine if one of a collection of platforms
  47. /// is being used currently.
  48. /// </summary>
  49. /// <param name="platforms"></param>
  50. /// <returns></returns>
  51. public bool IsPlatformSupported( string[] platforms )
  52. {
  53. foreach( string platform in platforms )
  54. if ( IsPlatformSupported( platform ) )
  55. return true;
  56. return false;
  57. }
  58. /// <summary>
  59. /// Tests to determine if the current platform is supported
  60. /// based on a platform attribute.
  61. /// </summary>
  62. /// <param name="platformAttribute">The attribute to examine</param>
  63. /// <returns></returns>
  64. public bool IsPlatformSupported( Attribute platformAttribute )
  65. {
  66. //Use reflection to avoid dependency on a particular framework version
  67. string include = (string)Reflect.GetPropertyValue(
  68. platformAttribute, "Include",
  69. BindingFlags.Public | BindingFlags.Instance );
  70. string exclude = (string)Reflect.GetPropertyValue(
  71. platformAttribute, "Exclude",
  72. BindingFlags.Public | BindingFlags.Instance );
  73. try
  74. {
  75. if (include != null && !IsPlatformSupported(include))
  76. {
  77. reason = string.Format("Only supported on {0}", include);
  78. return false;
  79. }
  80. if (exclude != null && IsPlatformSupported(exclude))
  81. {
  82. reason = string.Format("Not supported on {0}", exclude);
  83. return false;
  84. }
  85. }
  86. catch( ArgumentException ex )
  87. {
  88. reason = string.Format( "Invalid platform name: {0}", ex.ParamName );
  89. return false;
  90. }
  91. return true;
  92. }
  93. /// <summary>
  94. /// Test to determine if the a particular platform or comma-
  95. /// delimited set of platforms is in use.
  96. /// </summary>
  97. /// <param name="platform">Name of the platform or comma-separated list of platform names</param>
  98. /// <returns>True if the platform is in use on the system</returns>
  99. public bool IsPlatformSupported( string platform )
  100. {
  101. if ( platform.IndexOf( ',' ) >= 0 )
  102. return IsPlatformSupported( platform.Split( new char[] { ',' } ) );
  103. string platformName = platform.Trim();
  104. bool nameOK = false;
  105. string versionSpecification = null;
  106. string[] parts = platformName.Split( new char[] { '-' } );
  107. if ( parts.Length == 2 )
  108. {
  109. platformName = parts[0];
  110. versionSpecification = parts[1];
  111. }
  112. switch( platformName.ToUpper() )
  113. {
  114. case "WIN":
  115. case "WIN32":
  116. nameOK = os.IsWindows;
  117. break;
  118. case "WIN32S":
  119. nameOK = os.IsWin32S;
  120. break;
  121. case "WIN32WINDOWS":
  122. nameOK = os.IsWin32Windows;
  123. break;
  124. case "WIN32NT":
  125. nameOK = os.IsWin32NT;
  126. break;
  127. case "WINCE":
  128. nameOK = os.IsWinCE;
  129. break;
  130. case "WIN95":
  131. nameOK = os.IsWin95;
  132. break;
  133. case "WIN98":
  134. nameOK = os.IsWin98;
  135. break;
  136. case "WINME":
  137. nameOK = os.IsWinME;
  138. break;
  139. case "NT3":
  140. nameOK = os.IsNT3;
  141. break;
  142. case "NT4":
  143. nameOK = os.IsNT4;
  144. break;
  145. case "NT5":
  146. nameOK = os.IsNT5;
  147. break;
  148. case "WIN2K":
  149. nameOK = os.IsWin2K;
  150. break;
  151. case "WINXP":
  152. nameOK = os.IsWinXP;
  153. break;
  154. case "WIN2003SERVER":
  155. nameOK = os.IsWin2003Server;
  156. break;
  157. case "NT6":
  158. nameOK = os.IsNT6;
  159. break;
  160. case "VISTA":
  161. nameOK = os.IsVista;
  162. break;
  163. case "WIN2008SERVER":
  164. nameOK = os.IsWin2008Server;
  165. break;
  166. case "WIN2008SERVERR2":
  167. nameOK = os.IsWin2008ServerR2;
  168. break;
  169. case "WINDOWS7":
  170. nameOK = os.IsWindows7;
  171. break;
  172. case "UNIX":
  173. case "LINUX":
  174. nameOK = os.IsUnix;
  175. break;
  176. case "NET":
  177. nameOK = rt.Runtime == RuntimeType.Net;
  178. break;
  179. case "NETCF":
  180. nameOK = rt.Runtime == RuntimeType.NetCF;
  181. break;
  182. case "SSCLI":
  183. case "ROTOR":
  184. nameOK = rt.Runtime == RuntimeType.SSCLI;
  185. break;
  186. case "MONO":
  187. nameOK = rt.Runtime == RuntimeType.Mono;
  188. // Special handling because Mono 1.0 profile has version 1.1
  189. if ( versionSpecification == "1.0" )
  190. versionSpecification = "1.1";
  191. break;
  192. default:
  193. throw new ArgumentException( "Invalid platform name", platform.ToString() );
  194. }
  195. if ( nameOK )
  196. {
  197. if ( versionSpecification == null )
  198. return true;
  199. Version version = new Version( versionSpecification );
  200. if ( rt.ClrVersion.Major == version.Major &&
  201. rt.ClrVersion.Minor == version.Minor &&
  202. ( version.Build == -1 || rt.ClrVersion.Build == version.Build ) &&
  203. ( version.Revision == -1 || rt.ClrVersion.Revision == version.Revision ) )
  204. return true;
  205. }
  206. this.reason = "Only supported on " + platform;
  207. return false;
  208. }
  209. /// <summary>
  210. /// Return the last failure reason. Results are not
  211. /// defined if called before IsSupported( Attribute )
  212. /// is called.
  213. /// </summary>
  214. public string Reason
  215. {
  216. get { return reason; }
  217. }
  218. }
  219. }