Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Ensuring IAssemblyContexts are executed only once, even when running …

…several contexts using one instance of the AssemblyRunner. Closes #17
  • Loading branch information...
commit fba25c2f3ff9a195f53449ab9fad7eebdb269142 1 parent 9ac0839
@agross agross authored
View
52 Source/Machine.Specifications.ReSharperRunner.6.1/Runners/RecursiveMSpecTaskRunner.cs
@@ -12,6 +12,18 @@
namespace Machine.Specifications.ReSharperRunner.Runners
{
+ class RunScope
+ {
+ public Action<Assembly> StartRun { get; set; }
+ public Action<Assembly> EndRun { get; set; }
+
+ public RunScope()
+ {
+ StartRun = x => { };
+ EndRun = x => { };
+ }
+ }
+
internal class RecursiveMSpecTaskRunner : RecursiveRemoteTaskRunner
{
readonly RemoteTaskNotificationFactory _taskNotificationFactory = new RemoteTaskNotificationFactory();
@@ -19,6 +31,7 @@ internal class RecursiveMSpecTaskRunner : RecursiveRemoteTaskRunner
Type _contextClass;
PerAssemblyRunListener _listener;
DefaultRunner _runner;
+ RunScope _runScope;
public RecursiveMSpecTaskRunner(IRemoteTaskServer server) : base(server)
{
@@ -46,10 +59,32 @@ public override TaskResult Start(TaskExecutionNode node)
_listener = new PerAssemblyRunListener(Server, task);
_runner = new DefaultRunner(_listener, RunOptions.Default);
+ _runScope = GetRunScope(_runner);
return TaskResult.Success;
}
+ static RunScope GetRunScope(DefaultRunner runner)
+ {
+ var scope = new RunScope();
+
+ var runnerType = runner.GetType();
+
+ var startRun = runnerType.GetMethod("StartRun", new[]{typeof(Assembly)});
+ if (startRun != null)
+ {
+ scope.StartRun = asm => startRun.Invoke(runner, new object[] { asm });
+ }
+
+ var endRun = runnerType.GetMethod("EndRun", new[] {typeof(Assembly)});
+ if (endRun != null)
+ {
+ scope.EndRun = asm => endRun.Invoke(runner, new object[] { asm });
+ }
+
+ return scope;
+ }
+
public override TaskResult Execute(TaskExecutionNode node)
{
// This method is never called.
@@ -63,12 +98,21 @@ public override void ExecuteRecursive(TaskExecutionNode node)
public override TaskResult Finish(TaskExecutionNode node)
{
- foreach (var child in node.Children)
+ try
{
- RunContext(child);
- }
+ _runScope.StartRun(_contextAssembly);
- return TaskResult.Success;
+ foreach (var child in node.Children)
+ {
+ RunContext(child);
+ }
+
+ return TaskResult.Success;
+ }
+ finally
+ {
+ _runScope.EndRun(_contextAssembly);
+ }
}
void RunContext(TaskExecutionNode node)
View
69 Source/Machine.Specifications/Runner/Impl/AssemblyRunner.cs
@@ -5,18 +5,29 @@
using Machine.Specifications.Explorers;
using Machine.Specifications.Model;
+using Machine.Specifications.Runner.Impl.Listener;
namespace Machine.Specifications.Runner.Impl
{
- public class AssemblyRunner
+ class AssemblyRunner
{
readonly ISpecificationRunListener _listener;
readonly RunOptions _options;
+ Action<Assembly> _assemblyStart;
+ Action<Assembly> _assemblyEnd;
public AssemblyRunner(ISpecificationRunListener listener, RunOptions options)
{
- _listener = listener;
+ _listener = new AggregateRunListener(new[]
+ {
+ new AssemblyLocationAwareListener(),
+ new AssemblyContextRunListener(),
+ listener
+ });
_options = options;
+
+ _assemblyStart = OnAssemblyStart;
+ _assemblyEnd = OnAssemblyEnd;
}
public void Run(Assembly assembly, IEnumerable<Context> contexts)
@@ -24,14 +35,14 @@ public void Run(Assembly assembly, IEnumerable<Context> contexts)
var hasExecutableSpecifications = contexts.Any(x => x.HasExecutableSpecifications);
var explorer = new AssemblyExplorer();
- var globalCleanups = new List<ICleanupAfterEveryContextInAssembly>(explorer.FindAssemblyWideContextCleanupsIn(assembly));
- var specificationSupplements = new List<ISupplementSpecificationResults>(explorer.FindSpecificationSupplementsIn(assembly));
+ var globalCleanups = explorer.FindAssemblyWideContextCleanupsIn(assembly).ToList();
+ var specificationSupplements = explorer.FindSpecificationSupplementsIn(assembly).ToList();
try
{
if (hasExecutableSpecifications)
{
- _listener.OnAssemblyStart(assembly.GetInfo());
+ _assemblyStart(assembly);
}
foreach (var context in contexts)
@@ -45,20 +56,50 @@ public void Run(Assembly assembly, IEnumerable<Context> contexts)
}
finally
{
- try
- {
- if (hasExecutableSpecifications)
- {
- _listener.OnAssemblyEnd(assembly.GetInfo());
- }
- }
- catch (Exception err)
+ if (hasExecutableSpecifications)
{
- _listener.OnFatalError(new ExceptionResult(err));
+ _assemblyEnd(assembly);
}
}
}
+ void OnAssemblyStart(Assembly assembly)
+ {
+ try
+ {
+ _listener.OnAssemblyStart(assembly.GetInfo());
+ }
+ catch (Exception err)
+ {
+ _listener.OnFatalError(new ExceptionResult(err));
+ }
+ }
+
+ void OnAssemblyEnd(Assembly assembly)
+ {
+ try
+ {
+ _listener.OnAssemblyEnd(assembly.GetInfo());
+ }
+ catch (Exception err)
+ {
+ _listener.OnFatalError(new ExceptionResult(err));
+ }
+ }
+
+ public void StartExplicitRunScope(Assembly assembly)
+ {
+ _assemblyStart = x => {};
+ _assemblyEnd = x => {};
+
+ OnAssemblyStart(assembly);
+ }
+
+ public void EndExplicitRunScope(Assembly assembly)
+ {
+ OnAssemblyEnd(assembly);
+ }
+
void RunContext(Context context,
IEnumerable<ICleanupAfterEveryContextInAssembly> globalCleanups,
IEnumerable<ISupplementSpecificationResults> supplements)
View
44 Source/Machine.Specifications/Runner/Impl/DefaultRunner.cs
@@ -5,7 +5,6 @@
using Machine.Specifications.Explorers;
using Machine.Specifications.Model;
-using Machine.Specifications.Runner.Impl.Listener;
using Machine.Specifications.Utility;
namespace Machine.Specifications.Runner.Impl
@@ -15,19 +14,20 @@ public class DefaultRunner : ISpecificationRunner
readonly ISpecificationRunListener _listener;
readonly RunOptions _options;
readonly AssemblyExplorer _explorer;
+ readonly AssemblyRunner _assemblyRunner;
+ Action _runStart;
+ Action _runEnd;
public DefaultRunner(ISpecificationRunListener listener, RunOptions options)
{
- _listener = new AggregateRunListener(
- new ISpecificationRunListener[]
- {
- new AssemblyLocationAwareListener(),
- new AssemblyContextRunListener(),
- }
- .Concat(new[] {listener}));
-
+ _listener = listener;
_options = options;
+ _assemblyRunner = new AssemblyRunner(_listener, _options);
+
_explorer = new AssemblyExplorer();
+
+ _runStart = () => _listener.OnRunStart();
+ _runEnd = () => _listener.OnRunEnd();
}
public void RunAssembly(Assembly assembly)
@@ -69,7 +69,7 @@ public void RunMember(Assembly assembly, MemberInfo member)
void RunField(MemberInfo member, Assembly assembly)
{
- FieldInfo fieldInfo = (FieldInfo)member;
+ var fieldInfo = (FieldInfo)member;
var context = _explorer.FindContexts(fieldInfo);
StartRun(CreateMap(assembly, new[] {context}));
@@ -77,7 +77,7 @@ void RunField(MemberInfo member, Assembly assembly)
void RunClass(MemberInfo member, Assembly assembly)
{
- Type type = (Type)member;
+ var type = (Type)member;
var context = _explorer.FindContexts(type);
if (context == null)
@@ -97,7 +97,7 @@ void RunClass(MemberInfo member, Assembly assembly)
void StartRun(IDictionary<Assembly, IEnumerable<Context>> contextMap)
{
- _listener.OnRunStart();
+ _runStart();
foreach (var pair in contextMap)
{
@@ -111,13 +111,27 @@ void StartRun(IDictionary<Assembly, IEnumerable<Context>> contextMap)
}
}
- _listener.OnRunEnd();
+ _runEnd();
}
void StartAssemblyRun(Assembly assembly, IEnumerable<Context> contexts)
{
- var runner = new AssemblyRunner(_listener, _options);
- runner.Run(assembly, contexts.FilteredBy(_options));
+ _assemblyRunner.Run(assembly, contexts.FilteredBy(_options));
+ }
+
+ public void StartRun(Assembly assembly)
+ {
+ _runStart = () => {};
+ _runEnd = () => {};
+
+ _listener.OnRunStart();
+ _assemblyRunner.StartExplicitRunScope(assembly);
+ }
+
+ public void EndRun(Assembly assembly)
+ {
+ _assemblyRunner.EndExplicitRunScope(assembly);
+ _listener.OnRunEnd();
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.