Skip to content

Commit

Permalink
Allow access CurrentFramework from IRuntimeService
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisMaddock committed Jul 1, 2017
1 parent 707505b commit 607ac28
Show file tree
Hide file tree
Showing 18 changed files with 87 additions and 42 deletions.
8 changes: 5 additions & 3 deletions src/NUnitEngine/nunit-agent/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ public static int Main(string[] args)
InternalTrace.Initialize(logname, traceLevel);

log.Info("Agent process {0} starting", pid);
log.Info("Running under version {0}, {1}",
Environment.Version,
RuntimeFrameworkService.CurrentFramework.DisplayName);

// Restore the COMPLUS_Version env variable if it's been overridden by TestAgency::LaunchAgentProcess
try
Expand Down Expand Up @@ -125,6 +122,11 @@ public static int Main(string[] args)
log.Info("Initializing Services");
engine.Initialize();

var frameworkService = engine.Services.GetService<IRuntimeFrameworkService>();
log.Info("Running under version {0}, {1}",
Environment.Version,
frameworkService.CurrentFramework.DisplayName);

// Owns the channel used for communications with the agency and with clients
var testAgencyServer = engine.Services.GetService<TestAgency>();

Expand Down
33 changes: 31 additions & 2 deletions src/NUnitEngine/nunit.engine.api/IRuntimeFramework.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ***********************************************************************
// Copyright (c) 2016 Charlie Poole
// Copyright (c) 2017 Charlie Poole
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
Expand Down Expand Up @@ -33,7 +33,7 @@ public interface IRuntimeFramework
/// <summary>
/// Gets the inique Id for this runtime, such as "net-4.5"
/// </summary>
string Id { get; }
string Id { get; }

/// <summary>
/// Gets the display name of the framework, such as ".NET 4.5"
Expand All @@ -57,5 +57,34 @@ public interface IRuntimeFramework
/// values are Full and Client.
/// </summary>
string Profile { get; }

/// <summary>
/// The type of this runtime framework
/// </summary>
RuntimeType Runtime { get; }

/// <summary>
/// Returns true if the current framework matches the
/// one supplied as an argument. Two frameworks match
/// if their runtime types are the same or either one
/// is RuntimeType.Any and all specified version components
/// are equal. Negative (i.e. unspecified) version
/// components are ignored.
/// </summary>
/// <param name="target">The IRuntimeFramework to be matched.</param>
/// <returns><c>true</c> on match, otherwise <c>false</c></returns>
bool Supports(IRuntimeFramework target);

/// <summary>
/// Return true if any CLR version may be used in
/// matching this IRuntimeFramework object.
/// </summary>
bool AllowAnyVersion { get; }

/// <summary>
/// Returns true if the current IRuntimeFramework is available.
/// </summary>
/// <returns><c>true</c> if available, otherwise <c>false</c></returns>
bool IsAvailable { get; }
}
}
5 changes: 5 additions & 0 deletions src/NUnitEngine/nunit.engine.api/IRuntimeFrameworkService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,10 @@ public interface IRuntimeFrameworkService
/// <param name="package">A TestPackage</param>
/// <returns>The selected RuntimeFramework</returns>
string SelectRuntimeFramework(TestPackage package);

/// <summary>
/// Returns the framework that is currently in use.
/// </summary>
IRuntimeFramework CurrentFramework { get; }
}
}
File renamed without changes.
3 changes: 2 additions & 1 deletion src/NUnitEngine/nunit.engine.api/nunit.engine.api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<Compile Include="ITestRunner.cs" />
<Compile Include="NUnitEngineException.cs" />
<Compile Include="IRecentFiles.cs" />
<Compile Include="RuntimeType.cs" />
<Compile Include="TestEngineActivator.cs" />
<Compile Include="TestFilter.cs" />
<Compile Include="TestPackage.cs" />
Expand All @@ -106,4 +107,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void Initialize()
_services.Add(new Services.ProjectService());
_services.Add(new Services.DefaultTestRunnerFactory());
_services.Add(new Services.RuntimeFrameworkService());
_services.Add(new Services.TestAgency("ProcessRunnerTests", 0));
_services.Add(new Services.TestAgency("ProcessRunnerTests", 0, _services));
_services.ServiceManager.StartServices();

_runner = new MasterTestRunner(_services, _package);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public void Initialize()
_services = new ServiceContext();
_services.Add(new Services.DriverService());
_services.Add(new Services.DomainManager());
_services.Add(new Services.TestAgency("ProcessRunnerTests", 0));
_services.Add(new Services.RuntimeFrameworkService());
_services.Add(new Services.TestAgency("ProcessRunnerTests", 0, _services));
_services.ServiceManager.StartServices();

var assemblies = new List<string>();
Expand Down
7 changes: 3 additions & 4 deletions src/NUnitEngine/nunit.engine.tests/RuntimeFrameworkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public class RuntimeFrameworkTests
[Test]
public void AvailableFrameworksList()
{
RuntimeFramework[] available = RuntimeFramework.AvailableFrameworks;
Assert.That(RuntimeFramework.AvailableFrameworks.Length, Is.GreaterThan(0) );
foreach (var framework in RuntimeFramework.AvailableFrameworks)
Console.WriteLine("Available: {0}", framework.DisplayName);
Expand All @@ -44,7 +43,7 @@ public void AvailableFrameworksList()
public void AvailableFrameworksList_IncludesCurrentFramework()
{
foreach (var framework in RuntimeFramework.AvailableFrameworks)
if (RuntimeFrameworkService.CurrentFramework.Supports(framework))
if (new RuntimeFrameworkService().CurrentFramework.Supports(framework))
return;

Assert.Fail("CurrentFramework not listed as available");
Expand Down Expand Up @@ -91,8 +90,8 @@ public void CanParseRuntimeFramework(FrameworkData data)
public void CanDisplayFrameworkAsString(FrameworkData data)
{
RuntimeFramework framework = new RuntimeFramework(data.runtime, data.frameworkVersion);
Assert.AreEqual(data.representation, framework.ToString());
Assert.AreEqual(data.displayName, framework.DisplayName);
Assert.AreEqual(data.representation, framework.ToString(), "Test RuntimeFramework ToString()");
Assert.AreEqual(data.displayName, framework.DisplayName, "Test RuntimeFramework DisplayName");
}

[TestCaseSource("matchData")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,7 @@ string IRuntimeFrameworkService.SelectRuntimeFramework(TestPackage package)
{
throw new NotImplementedException();
}

public IRuntimeFramework CurrentFramework { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void EngineOptionPreferredOverImageTarget(string framework, int majorVers
[Test]
public void CanGetCurrentFramework()
{
RuntimeFramework framework = RuntimeFrameworkService.CurrentFramework;
IRuntimeFramework framework = _runtimeService.CurrentFramework;

Assert.That(framework.Runtime, Is.EqualTo(CurrentRuntime));
Assert.That(framework.ClrVersion, Is.EqualTo(Environment.Version));
Expand All @@ -109,13 +109,13 @@ public void CanGetCurrentFramework()
[Test]
public void CurrentFrameworkHasBuildSpecified()
{
Assert.That(RuntimeFrameworkService.CurrentFramework.ClrVersion.Build, Is.GreaterThan(0));
Assert.That(_runtimeService.CurrentFramework.ClrVersion.Build, Is.GreaterThan(0));
}

[Test]
public void CurrentFrameworkMustBeAvailable()
{
var current = RuntimeFrameworkService.CurrentFramework;
var current = _runtimeService.CurrentFramework;
Console.WriteLine("Current framework is {0} ({1})", current.DisplayName, current.Id);
Assert.That(current.IsAvailable, "{0} not available", current);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void CreateServiceContext()
var services = new ServiceContext();
services.Add(new FakeRuntimeService());
// Use a different URI to avoid conflicting with the "real" TestAgency
_testAgency = new TestAgency("TestAgencyTest", 0);
_testAgency = new TestAgency("TestAgencyTest", 0, services);
services.Add(_testAgency);
services.ServiceManager.StartServices();
}
Expand Down
2 changes: 1 addition & 1 deletion src/NUnitEngine/nunit.engine/Runners/MasterTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ private void ValidatePackageSettings()
var processModel = TestPackage.GetSetting(EnginePackageSettings.ProcessModel, "Default").ToLower();
if (processModel == "single" || processModel == "inprocess")
{
var currentFramework = RuntimeFrameworkService.CurrentFramework;
var currentFramework = _runtimeService.CurrentFramework;
var requestedFramework = RuntimeFramework.Parse(frameworkSetting);
if (!currentFramework.Supports(requestedFramework))
throw new NUnitEngineException(string.Format(
Expand Down
23 changes: 12 additions & 11 deletions src/NUnitEngine/nunit.engine/RuntimeFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public sealed class RuntimeFramework : IRuntimeFramework
/// </summary>
public static readonly Version DefaultVersion = new Version(0, 0);

private static List<RuntimeFramework> _availableFrameworks;
private static List<IRuntimeFramework> _availableFrameworks;

private static readonly string DEFAULT_WINDOWS_MONO_DIR =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "Mono");
Expand Down Expand Up @@ -180,7 +180,7 @@ private void InitFromClrVersion(Version version)
/// Gets an array of all available frameworks
/// </summary>
// TODO: Special handling for netcf
public static RuntimeFramework[] AvailableFrameworks
public static IRuntimeFramework[] AvailableFrameworks
{
get
{
Expand Down Expand Up @@ -349,9 +349,9 @@ public static RuntimeFramework Parse(string s)
/// </summary>
/// <param name="target"></param>
/// <returns></returns>
public static RuntimeFramework GetBestAvailableFramework(RuntimeFramework target)
public static IRuntimeFramework GetBestAvailableFramework(IRuntimeFramework target)
{
RuntimeFramework result = target;
IRuntimeFramework result = target;

foreach (RuntimeFramework framework in AvailableFrameworks)
if (framework.Supports(target))
Expand Down Expand Up @@ -382,7 +382,7 @@ public override string ToString()
/// </summary>
/// <param name="target">The RuntimeFramework to be matched.</param>
/// <returns><c>true</c> on match, otherwise <c>false</c></returns>
public bool Supports(RuntimeFramework target)
public bool Supports(IRuntimeFramework target)
{
if (this.Runtime != RuntimeType.Any
&& target.Runtime != RuntimeType.Any
Expand Down Expand Up @@ -467,7 +467,7 @@ private static bool VersionsMatch(Version v1, Version v2)

private static void FindAvailableFrameworks()
{
_availableFrameworks = new List<RuntimeFramework>();
_availableFrameworks = new List<IRuntimeFramework>();

if (Environment.OSVersion.Platform == PlatformID.Win32NT)
_availableFrameworks.AddRange(DotNetFrameworkLocator.FindAvailableDotNetFrameworks());
Expand All @@ -481,17 +481,18 @@ private static void FindAvailableFrameworks()

private static void FindDefaultMonoFramework()
{
if (RuntimeFrameworkService.CurrentFramework.Runtime == RuntimeType.Mono)
UseCurrentMonoFramework();
//TODO: This is a temporary hack until #173 moves this functionality to RuntimeFrameworkService
var currentFramework = CurrentFrameworkLocator.GetCurrentFramework();

if (currentFramework.Runtime == RuntimeType.Mono)
UseCurrentMonoFramework(currentFramework);
else
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
FindBestMonoFrameworkOnWindows();
}

private static void UseCurrentMonoFramework()
private static void UseCurrentMonoFramework(IRuntimeFramework currentFramework)
{
var currentFramework = RuntimeFrameworkService.CurrentFramework;

Debug.Assert(currentFramework.Runtime == RuntimeType.Mono && MonoPrefix != null && MonoVersion != null);

// Multiple profiles are no longer supported with Mono 4.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ namespace NUnit.Engine.Services.FrameworkUtilities
{
internal static class DotNetFrameworkLocator
{
internal static ICollection<RuntimeFramework> FindAvailableDotNetFrameworks()
internal static ICollection<IRuntimeFramework> FindAvailableDotNetFrameworks()
{
var availableFrameworks = new List<RuntimeFramework>();
var availableFrameworks = new List<IRuntimeFramework>();

// Handle Version 1.0, using a different registry key
availableFrameworks.AddRange(FindExtremelyOldDotNetFrameworkVersions());
Expand Down Expand Up @@ -62,7 +62,7 @@ internal static ICollection<RuntimeFramework> FindAvailableDotNetFrameworks()
return availableFrameworks;
}

private static IEnumerable<RuntimeFramework> FindExtremelyOldDotNetFrameworkVersions()
private static IEnumerable<IRuntimeFramework> FindExtremelyOldDotNetFrameworkVersions()
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\.NETFramework\policy\v1.0");
if (key == null) yield break;
Expand All @@ -74,7 +74,7 @@ private static IEnumerable<RuntimeFramework> FindExtremelyOldDotNetFrameworkVers
// Note: this method cannot be generalized past V4, because (a) it has
// specific code for detecting .NET 4.5 and (b) we don't know what
// Microsoft will do in the future
private static IEnumerable<RuntimeFramework> FindDotNetFourFrameworkVersions(RegistryKey versionKey)
private static IEnumerable<IRuntimeFramework> FindDotNetFourFrameworkVersions(RegistryKey versionKey)
{
foreach (string profile in new[] { "Full", "Client" })
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public class RuntimeFrameworkService : Service, IRuntimeFrameworkService, IAvail

private static readonly Version AnyVersion = new Version(0, 0);

static readonly RuntimeFramework[] _availableRuntimes;
private readonly IRuntimeFramework[] _availableRuntimes;

static RuntimeFrameworkService()
public RuntimeFrameworkService()
{
CurrentFramework = CurrentFrameworkLocator.GetCurrentFramework();
_availableRuntimes = RuntimeFramework.AvailableFrameworks;
Expand All @@ -57,7 +57,7 @@ public IList<IRuntimeFramework> AvailableRuntimes
/// <summary>
/// Returns the framework that is currently in use.
/// </summary>
public static RuntimeFramework CurrentFramework { get; }
public IRuntimeFramework CurrentFramework { get; }

#endregion

Expand Down Expand Up @@ -238,7 +238,7 @@ private static void ApplyImageData(TestPackage package)
package.Settings[InternalEnginePackageSettings.ImageRequiresDefaultAppDomainAssemblyResolver] = requiresAssemblyResolver;
}

private static bool FrameworksMatch(RuntimeFramework f1, RuntimeFramework f2)
private static bool FrameworksMatch(IRuntimeFramework f1, IRuntimeFramework f2)
{
var rt1 = f1.Runtime;
var rt2 = f2.Runtime;
Expand Down
14 changes: 10 additions & 4 deletions src/NUnitEngine/nunit.engine/Services/TestAgency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public enum AgentStatus
/// </summary>
public class TestAgency : ServerBase, ITestAgency, IService
{
private readonly IServiceLocator _serviceLocator;
private static readonly Logger log = InternalTrace.GetLogger(typeof(TestAgency));

#region Private Fields
Expand All @@ -61,9 +62,12 @@ public class TestAgency : ServerBase, ITestAgency, IService
#endregion

#region Constructors
public TestAgency() : this( "TestAgency", 0 ) { }
public TestAgency(IServiceLocator serviceLocator) : this("TestAgency", 0, serviceLocator) { }

public TestAgency( string uri, int port ) : base( uri, port ) { }
public TestAgency(string uri, int port, IServiceLocator serviceLocator) : base( uri, port )
{
_serviceLocator = serviceLocator;
}
#endregion

#region ServerBase Overrides
Expand Down Expand Up @@ -130,13 +134,15 @@ public void ReleaseAgent( ITestAgent agent )
#region Helper Methods
private Guid LaunchAgentProcess(TestPackage package)
{
RuntimeFramework targetRuntime = RuntimeFrameworkService.CurrentFramework;
var frameworkService = _serviceLocator.GetService<IRuntimeFrameworkService>();

IRuntimeFramework targetRuntime = frameworkService.CurrentFramework;
string runtimeSetting = package.GetSetting(EnginePackageSettings.RuntimeFramework, "");
if (runtimeSetting != "")
targetRuntime = RuntimeFramework.Parse(runtimeSetting);

if (targetRuntime.Runtime == RuntimeType.Any)
targetRuntime = new RuntimeFramework(RuntimeFrameworkService.CurrentFramework.Runtime, targetRuntime.ClrVersion);
targetRuntime = new RuntimeFramework(frameworkService.CurrentFramework.Runtime, targetRuntime.ClrVersion);

bool useX86Agent = package.GetSetting(EnginePackageSettings.RunAsX86, false);
bool debugTests = package.GetSetting(EnginePackageSettings.DebugTests, false);
Expand Down
2 changes: 1 addition & 1 deletion src/NUnitEngine/nunit.engine/TestEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void Initialize()
Services.Add(new ProjectService());
Services.Add(new RuntimeFrameworkService());
Services.Add(new DefaultTestRunnerFactory());
Services.Add(new TestAgency());
Services.Add(new TestAgency(Services));
Services.Add(new ResultService());
Services.Add(new TestFilterService());

Expand Down
1 change: 0 additions & 1 deletion src/NUnitEngine/nunit.engine/nunit.engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@
<Compile Include="Runners\ParallelTaskWorkerPool.cs" />
<Compile Include="Runners\TestEventDispatcher.cs" />
<Compile Include="Runners\TestExecutionTask.cs" />
<Compile Include="RuntimeType.cs" />
<Compile Include="Services\AgentDataBase.cs" />
<Compile Include="Services\ExtensionAssembly.cs" />
<Compile Include="Services\ExtensionService.cs" />
Expand Down

0 comments on commit 607ac28

Please sign in to comment.