PageRenderTime 29ms CodeModel.GetById 13ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/core/NUnitConfiguration.cs

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