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