PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/core/NUnitConfiguration.cs

#
C# | 383 lines | 289 code | 47 blank | 47 comment | 58 complexity | 1fdb6d55b3601fa2ef66beaec22c260a MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // Copyright 2008, Charlie Poole
  3. // This is free software licensed under the NUnit license. You may
  4. // obtain a copy of the license at http://nunit.org.
  5. // ****************************************************************
  6. using System;
  7. using System.IO;
  8. using System.Reflection;
  9. using System.Configuration;
  10. using System.Collections.Specialized;
  11. using System.Threading;
  12. using Microsoft.Win32;
  13. namespace NUnit.Core
  14. {
  15. /// <summary>
  16. /// Provides static methods for accessing the NUnit config
  17. /// file
  18. /// </summary>
  19. public class NUnitConfiguration
  20. {
  21. #region Class Constructor
  22. /// <summary>
  23. /// Class constructor initializes fields from config file
  24. /// </summary>
  25. static NUnitConfiguration()
  26. {
  27. try
  28. {
  29. NameValueCollection settings = GetConfigSection("NUnit/TestCaseBuilder");
  30. if (settings != null)
  31. {
  32. string oldStyle = settings["OldStyleTestCases"];
  33. if (oldStyle != null)
  34. allowOldStyleTests = Boolean.Parse(oldStyle);
  35. }
  36. settings = GetConfigSection("NUnit/TestRunner");
  37. if (settings != null)
  38. {
  39. string apartment = settings["ApartmentState"];
  40. if (apartment != null)
  41. apartmentState = (ApartmentState)
  42. System.Enum.Parse(typeof(ApartmentState), apartment, true);
  43. string priority = settings["ThreadPriority"];
  44. if (priority != null)
  45. threadPriority = (ThreadPriority)
  46. System.Enum.Parse(typeof(ThreadPriority), priority, true);
  47. }
  48. }
  49. catch (Exception ex)
  50. {
  51. string msg = string.Format("Invalid configuration setting in {0}",
  52. AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
  53. throw new ApplicationException(msg, ex);
  54. }
  55. }
  56. private static NameValueCollection GetConfigSection( string name )
  57. {
  58. #if NET_2_0
  59. return (NameValueCollection)System.Configuration.ConfigurationManager.GetSection(name);
  60. #else
  61. return (NameValueCollection)System.Configuration.ConfigurationSettings.GetConfig(name);
  62. #endif
  63. }
  64. #endregion
  65. #region Public Properties
  66. #region AllowOldStyleTests
  67. private static bool allowOldStyleTests = false;
  68. public static bool AllowOldStyleTests
  69. {
  70. get { return allowOldStyleTests; }
  71. }
  72. #endregion
  73. #region ThreadPriority
  74. private static ThreadPriority threadPriority = ThreadPriority.Normal;
  75. public static ThreadPriority ThreadPriority
  76. {
  77. get { return threadPriority; }
  78. }
  79. #endregion
  80. #region ApartmentState
  81. private static ApartmentState apartmentState = ApartmentState.Unknown;
  82. public static ApartmentState ApartmentState
  83. {
  84. get { return apartmentState; }
  85. //set { apartmentState = value; }
  86. }
  87. #endregion
  88. #region BuildConfiguration
  89. public static string BuildConfiguration
  90. {
  91. get
  92. {
  93. #if DEBUG
  94. return "Debug";
  95. #else
  96. return "Release";
  97. #endif
  98. }
  99. }
  100. #endregion
  101. #region NUnitLibDirectory
  102. private static string nunitLibDirectory;
  103. /// <summary>
  104. /// Gets the path to the lib directory for the version and build
  105. /// of NUnit currently executing.
  106. /// </summary>
  107. public static string NUnitLibDirectory
  108. {
  109. get
  110. {
  111. if (nunitLibDirectory == null)
  112. {
  113. nunitLibDirectory =
  114. AssemblyHelper.GetDirectoryName(Assembly.GetExecutingAssembly());
  115. }
  116. return nunitLibDirectory;
  117. }
  118. }
  119. #endregion
  120. #region NUnitBinDirectory
  121. private static string nunitBinDirectory;
  122. public static string NUnitBinDirectory
  123. {
  124. get
  125. {
  126. if (nunitBinDirectory == null)
  127. {
  128. nunitBinDirectory = NUnitLibDirectory;
  129. if (Path.GetFileName(nunitBinDirectory).ToLower() == "lib")
  130. nunitBinDirectory = Path.GetDirectoryName(nunitBinDirectory);
  131. }
  132. return nunitBinDirectory;
  133. }
  134. }
  135. #endregion
  136. #region AddinDirectory
  137. private static string addinDirectory;
  138. public static string AddinDirectory
  139. {
  140. get
  141. {
  142. if (addinDirectory == null)
  143. {
  144. addinDirectory = Path.Combine(NUnitBinDirectory, "addins");
  145. }
  146. return addinDirectory;
  147. }
  148. }
  149. #endregion
  150. #region TestAgentExePath
  151. //private static string testAgentExePath;
  152. //private static string TestAgentExePath
  153. //{
  154. // get
  155. // {
  156. // if (testAgentExePath == null)
  157. // testAgentExePath = Path.Combine(NUnitBinDirectory, "nunit-agent.exe");
  158. // return testAgentExePath;
  159. // }
  160. //}
  161. #endregion
  162. #region MonoExePath
  163. private static string monoExePath;
  164. public static string MonoExePath
  165. {
  166. get
  167. {
  168. if (monoExePath == null)
  169. {
  170. string[] searchNames = IsWindows()
  171. ? new string[] { "mono.bat", "mono.cmd", "mono.exe" }
  172. : new string[] { "mono", "mono.exe" };
  173. monoExePath = FindOneOnPath(searchNames);
  174. if (monoExePath == null && IsWindows())
  175. {
  176. RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Novell\Mono");
  177. if (key != null)
  178. {
  179. string version = key.GetValue("DefaultCLR") as string;
  180. if (version != null)
  181. {
  182. key = key.OpenSubKey(version);
  183. if (key != null)
  184. {
  185. string installDir = key.GetValue("SdkInstallRoot") as string;
  186. if (installDir != null)
  187. monoExePath = Path.Combine(installDir, @"bin\mono.exe");
  188. }
  189. }
  190. }
  191. }
  192. if (monoExePath == null)
  193. return "mono";
  194. }
  195. return monoExePath;
  196. }
  197. }
  198. private static string FindOneOnPath(string[] names)
  199. {
  200. //foreach (string dir in Environment.GetEnvironmentVariable("path").Split(new char[] { Path.PathSeparator }))
  201. // foreach (string name in names)
  202. // {
  203. // string fullPath = Path.Combine(dir, name);
  204. // if (File.Exists(fullPath))
  205. // return name;
  206. // }
  207. return null;
  208. }
  209. private static bool IsWindows()
  210. {
  211. return Environment.OSVersion.Platform == PlatformID.Win32NT;
  212. }
  213. #endregion
  214. #region ApplicationDataDirectory
  215. private static string applicationDirectory;
  216. public static string ApplicationDirectory
  217. {
  218. get
  219. {
  220. if (applicationDirectory == null)
  221. {
  222. applicationDirectory = Path.Combine(
  223. Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
  224. "NUnit");
  225. }
  226. return applicationDirectory;
  227. }
  228. }
  229. #endregion
  230. #region HelpUrl
  231. public static string HelpUrl
  232. {
  233. get
  234. {
  235. #if NET_2_0
  236. string helpUrl = ConfigurationManager.AppSettings["helpUrl"];
  237. #else
  238. string helpUrl = ConfigurationSettings.AppSettings["helpUrl"];
  239. #endif
  240. if (helpUrl == null)
  241. {
  242. helpUrl = "http://nunit.org";
  243. string dir = Path.GetDirectoryName(NUnitBinDirectory);
  244. if ( dir != null )
  245. {
  246. dir = Path.GetDirectoryName(dir);
  247. if ( dir != null )
  248. {
  249. string localPath = Path.Combine(dir, @"doc/index.html");
  250. if (File.Exists(localPath))
  251. {
  252. UriBuilder uri = new UriBuilder();
  253. uri.Scheme = "file";
  254. uri.Host = "localhost";
  255. uri.Path = localPath;
  256. helpUrl = uri.ToString();
  257. }
  258. }
  259. }
  260. }
  261. return helpUrl;
  262. }
  263. }
  264. #endregion
  265. #endregion
  266. #region Public Methods
  267. /// <summary>
  268. /// Return the NUnit Bin Directory for a particular
  269. /// runtime version, or null if it's not installed.
  270. /// For normal installations, there are only 1.1 and
  271. /// 2.0 directories. However, this method accomodates
  272. /// 3.5 and 4.0 directories for the benefit of NUnit
  273. /// developers using those runtimes.
  274. /// </summary>
  275. public static string GetNUnitBinDirectory(Version v)
  276. {
  277. string dir = NUnitBinDirectory;
  278. if ((Environment.Version.Major >= 2) == (v.Major >= 2))
  279. return dir;
  280. string[] v1Strings = new string[] { "1.0", "1.1" };
  281. string[] v2Strings = new string[] { "2.0", "3.0", "3.5", "4.0" };
  282. string[] search;
  283. string[] replace;
  284. if (Environment.Version.Major == 1)
  285. {
  286. search = v1Strings;
  287. replace = v2Strings;
  288. }
  289. else
  290. {
  291. search = v2Strings;
  292. replace = v1Strings;
  293. }
  294. // Look for current value in path so it can be replaced
  295. string current = null;
  296. foreach (string s in search)
  297. if (dir.IndexOf(s) >= 0)
  298. {
  299. current = s;
  300. break;
  301. }
  302. // If nothing found, we can't do it
  303. if (current == null)
  304. return null;
  305. // First try exact replacement
  306. string altDir = dir.Replace(current, v.ToString(2));
  307. if (Directory.Exists(altDir))
  308. return altDir;
  309. // Now try all the alternatives
  310. foreach (string target in replace)
  311. {
  312. altDir = dir.Replace(current, target);
  313. if (Directory.Exists(altDir))
  314. return altDir;
  315. }
  316. // Nothing was found
  317. return null;
  318. }
  319. public static string GetTestAgentExePath(Version v)
  320. {
  321. string binDir = GetNUnitBinDirectory(v);
  322. if ( binDir == null ) return null;
  323. #if NET_2_0
  324. Assembly a = System.Reflection.Assembly.GetEntryAssembly();
  325. string agentName = v.Major > 1 && a != null && a.GetName().ProcessorArchitecture == ProcessorArchitecture.X86
  326. ? "nunit-agent-x86.exe"
  327. : "nunit-agent.exe";
  328. #else
  329. string agentName = "nunit-agent.exe";
  330. #endif
  331. string agentExePath = Path.Combine(binDir, agentName);
  332. return File.Exists(agentExePath) ? agentExePath : null;
  333. }
  334. #endregion
  335. }
  336. }