PageRenderTime 27ms CodeModel.GetById 12ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

/src/NUnit/util/RuntimeFrameworkSelector.cs

#
C# | 73 lines | 50 code | 10 blank | 13 comment | 13 complexity | d721005a5b79b67f6cafab24de2035a4 MD5 | raw file
 1// ****************************************************************
 2// Copyright 2010, 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// ****************************************************************
 6using System;
 7using NUnit.Core;
 8
 9namespace NUnit.Util
10{
11    public class RuntimeFrameworkSelector : IRuntimeFrameworkSelector
12    {
13        static Logger log = InternalTrace.GetLogger(typeof(RuntimeFrameworkSelector));
14
15        /// <summary>
16        /// Selects a target runtime framework for a TestPackage based on
17        /// the settings in the package and the assemblies themselves.
18        /// The package RuntimeFramework setting may be updated as a 
19        /// result and the selected runtime is returned.
20        /// </summary>
21        /// <param name="package">A TestPackage</param>
22        /// <returns>The selected RuntimeFramework</returns>
23        public RuntimeFramework SelectRuntimeFramework(TestPackage package)
24        {
25            RuntimeFramework currentFramework = RuntimeFramework.CurrentFramework;
26            RuntimeFramework requestedFramework = package.Settings["RuntimeFramework"] as RuntimeFramework;
27
28            log.Debug("Current framework is {0}", currentFramework);
29            if (requestedFramework == null)
30                log.Debug("No specific framework requested");
31            else
32                log.Debug("Requested framework is {0}", requestedFramework);
33
34            RuntimeType targetRuntime = requestedFramework == null
35                ? RuntimeType.Any 
36                : requestedFramework.Runtime;
37            Version targetVersion = requestedFramework == null
38                ? RuntimeFramework.DefaultVersion
39                : requestedFramework.FrameworkVersion;
40
41            if (targetRuntime == RuntimeType.Any)
42                targetRuntime = currentFramework.Runtime;
43
44            if (targetVersion == RuntimeFramework.DefaultVersion)
45            {
46                foreach (string assembly in package.Assemblies)
47                {
48                    using (AssemblyReader reader = new AssemblyReader(assembly))
49                    {
50                        Version v = new Version(reader.ImageRuntimeVersion.Substring(1));
51                        log.Debug("Assembly {0} uses version {1}", assembly, v);
52                        if (v > targetVersion) targetVersion = v;
53                    }
54                }
55
56                RuntimeFramework checkFramework = new RuntimeFramework(targetRuntime, targetVersion);
57                if (!checkFramework.IsAvailable || NUnitConfiguration.GetTestAgentExePath(targetVersion) == null)
58                {
59                    log.Debug("Preferred version {0} is not installed or this NUnit installation does not support it", targetVersion);
60                    if (targetVersion < currentFramework.FrameworkVersion)
61                        targetVersion = currentFramework.FrameworkVersion;
62                }
63            }
64
65            RuntimeFramework targetFramework = new RuntimeFramework(targetRuntime, targetVersion);
66            package.Settings["RuntimeFramework"] = targetFramework;
67
68            log.Debug("Test will use {0} framework", targetFramework);
69
70            return targetFramework;
71        }
72    }
73}