1 // ****************************************************************
\r
2 // This is free software licensed under the NUnit license. You
\r
3 // may obtain a copy of the license as well as information regarding
\r
4 // copyright ownership at http://nunit.org/?p=license&r=2.4.
\r
5 // ****************************************************************
\r
8 using System.Reflection;
\r
10 namespace NUnit.Core
\r
12 public class PlatformHelper
\r
14 private OperatingSystem os;
\r
15 private RuntimeFramework rt;
\r
17 // Set whenever we fail to support a list of platforms
\r
18 private string reason = string.Empty;
\r
20 // Defined here and used in tests. We can't use PlatformID.Unix
\r
21 // if we are building on .NET 1.0 or 1.1 and the values are different on Mono
\r
22 public static readonly PlatformID UnixPlatformID_Microsoft = (PlatformID) 4;
\r
23 public static readonly PlatformID UnixPlatformID_Mono = (PlatformID)128;
\r
26 /// Comma-delimited list of all supported OS platform constants
\r
28 public static readonly string OSPlatforms =
\r
29 "Win,Win32,Win32S,Win32NT,Win32Windows,WinCE,Win95,Win98,WinMe,NT3,NT4,NT5,NT6,Win2K,WinXP,Win2003Server,Vista,Win2008Server,Unix,Linux";
\r
32 /// Comma-delimited list of all supported Runtime platform constants
\r
34 public static readonly string RuntimePlatforms =
\r
35 "Net,NetCF,SSCLI,Rotor,Mono";
\r
38 /// Default constructor uses the operating system and
\r
39 /// common language runtime of the system.
\r
41 public PlatformHelper()
\r
43 this.os = Environment.OSVersion;
\r
44 this.rt = RuntimeFramework.CurrentFramework;
\r
48 /// Contruct a PlatformHelper for a particular operating
\r
49 /// system and common language runtime. Used in testing.
\r
51 /// <param name="os">OperatingSystem to be used</param>
\r
52 public PlatformHelper( OperatingSystem os, RuntimeFramework rt )
\r
59 /// Test to determine if one of a collection of platforms
\r
60 /// is being used currently.
\r
62 /// <param name="platforms"></param>
\r
63 /// <returns></returns>
\r
64 public bool IsPlatformSupported( string[] platforms )
\r
66 foreach( string platform in platforms )
\r
67 if ( IsPlatformSupported( platform ) )
\r
74 /// Tests to determine if the current platform is supported
\r
75 /// based on a platform attribute.
\r
77 /// <param name="platformAttribute">The attribute to examine</param>
\r
78 /// <returns></returns>
\r
79 public bool IsPlatformSupported( Attribute platformAttribute )
\r
81 //Use reflection to avoid dependency on a particular framework version
\r
82 string include = (string)Reflect.GetPropertyValue(
\r
83 platformAttribute, "Include",
\r
84 BindingFlags.Public | BindingFlags.Instance );
\r
86 string exclude = (string)Reflect.GetPropertyValue(
\r
87 platformAttribute, "Exclude",
\r
88 BindingFlags.Public | BindingFlags.Instance );
\r
92 if (include != null && !IsPlatformSupported(include))
\r
94 reason = string.Format("Only supported on {0}", include);
\r
98 if (exclude != null && IsPlatformSupported(exclude))
\r
100 reason = string.Format("Not supported on {0}", exclude);
\r
104 catch( ArgumentException ex )
\r
106 reason = string.Format( "Invalid platform name: {0}", ex.ParamName );
\r
114 /// Test to determine if the a particular platform or comma-
\r
115 /// delimited set of platforms is in use.
\r
117 /// <param name="platform">Name of the platform or comma-separated list of platform names</param>
\r
118 /// <returns>True if the platform is in use on the system</returns>
\r
119 public bool IsPlatformSupported( string platform )
\r
121 if ( platform.IndexOf( ',' ) >= 0 )
\r
122 return IsPlatformSupported( platform.Split( new char[] { ',' } ) );
\r
124 string platformName = platform.Trim();
\r
125 bool nameOK = false;
\r
127 string versionSpecification = null;
\r
129 string[] parts = platformName.Split( new char[] { '-' } );
\r
130 if ( parts.Length == 2 )
\r
132 platformName = parts[0];
\r
133 versionSpecification = parts[1];
\r
136 switch( platformName.ToUpper() )
\r
140 nameOK = os.Platform.ToString().StartsWith( "Win" );
\r
143 nameOK = os.Platform == PlatformID.Win32S;
\r
145 case "WIN32WINDOWS":
\r
146 nameOK = os.Platform == PlatformID.Win32Windows;
\r
149 nameOK = os.Platform == PlatformID.Win32NT;
\r
152 nameOK = (int)os.Platform == 3; // Not defined in .NET 1.0
\r
155 nameOK = os.Platform == PlatformID.Win32Windows && os.Version.Major == 4 && os.Version.Minor == 0;
\r
158 nameOK = os.Platform == PlatformID.Win32Windows && os.Version.Major == 4 && os.Version.Minor == 10;
\r
161 nameOK = os.Platform == PlatformID.Win32Windows && os.Version.Major == 4 && os.Version.Minor == 90;
\r
164 nameOK = os.Platform == PlatformID.Win32NT && os.Version.Major == 3;
\r
167 nameOK = os.Platform == PlatformID.Win32NT && os.Version.Major == 4;
\r
170 nameOK = os.Platform == PlatformID.Win32NT && os.Version.Major == 5;
\r
173 nameOK = os.Platform == PlatformID.Win32NT && os.Version.Major == 5 && os.Version.Minor == 0;
\r
176 nameOK = os.Platform == PlatformID.Win32NT && os.Version.Major == 5 && os.Version.Minor == 1;
\r
178 case "WIN2003SERVER":
\r
179 nameOK = os.Platform == PlatformID.Win32NT && os.Version.Major == 5 && os.Version.Minor == 2;
\r
182 nameOK = os.Platform == PlatformID.Win32NT && os.Version.Major == 6;
\r
184 // TODO: Distinguish Vista SP1 from Server 2008
\r
186 nameOK = os.Platform == PlatformID.Win32NT && os.Version.Major == 6 && os.Version.Minor == 0 && os.Version.Build != 6001;
\r
188 case "WIN2008SERVER":
\r
189 nameOK = os.Platform == PlatformID.Win32NT && os.Version.Major == 6 && os.Version.Minor == 0 && os.Version.Build == 6001;
\r
193 nameOK = os.Platform == UnixPlatformID_Microsoft
\r
194 || os.Platform == UnixPlatformID_Mono;
\r
197 nameOK = rt.Runtime == RuntimeType.Net;
\r
200 nameOK = rt.Runtime == RuntimeType.NetCF;
\r
204 nameOK = rt.Runtime == RuntimeType.SSCLI;
\r
207 nameOK = rt.Runtime == RuntimeType.Mono;
\r
208 // Special handling because Mono 1.0 profile has version 1.1
\r
209 if ( versionSpecification == "1.0" )
\r
210 versionSpecification = "1.1";
\r
213 throw new ArgumentException( "Invalid platform name", platform.ToString() );
\r
218 if ( versionSpecification == null )
\r
221 Version version = new Version( versionSpecification );
\r
223 if ( rt.Version.Major == version.Major &&
\r
224 rt.Version.Minor == version.Minor &&
\r
225 ( version.Build == -1 || rt.Version.Build == version.Build ) &&
\r
226 ( version.Revision == -1 || rt.Version.Revision == version.Revision ) )
\r
230 this.reason = "Only supported on " + platform;
\r
235 /// Return the last failure reason. Results are not
\r
236 /// defined if called before IsSupported( Attribute )
\r
239 public string Reason
\r
241 get { return reason; }
\r