Skip to content

Commit

Permalink
(GH-1089) Add additional context information to Teardown
Browse files Browse the repository at this point in the history
  • Loading branch information
gep13 committed Jul 24, 2016
1 parent f3e53f5 commit 42527dc
Show file tree
Hide file tree
Showing 19 changed files with 311 additions and 44 deletions.
1 change: 1 addition & 0 deletions src/Cake.Core.Tests/Cake.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
<Compile Include="Unit\Scripting\ScriptTests.cs" />
<Compile Include="Unit\TaskSetupContextTests.cs" />
<Compile Include="Unit\TaskTeardownContextTests.cs" />
<Compile Include="Unit\TeardownContextTests.cs" />
<Compile Include="Unit\Text\TextTransformationTemplateTests.cs" />
<Compile Include="Unit\Tooling\ToolLocatorTests.cs" />
<Compile Include="Unit\Tooling\ToolResolutionStrategyTests.cs" />
Expand Down
4 changes: 2 additions & 2 deletions src/Cake.Core.Tests/Unit/Scripting/ScriptHostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void Should_Proxy_Call_To_Engine()
// Given
var fixture = new ScriptHostFixture();
var host = fixture.CreateHost();
Action<ICakeContext, ITaskSetupContext> action = (context, setupContext) => { };
Action<ICakeContext> action = context => { };

// When
host.TaskSetup(action);
Expand All @@ -86,7 +86,7 @@ public void Should_Proxy_Call_To_Engine()
// Given
var fixture = new ScriptHostFixture();
var host = fixture.CreateHost();
Action<ICakeContext, ITaskTeardownContext> action = (context, setupContext) => { };
Action<ICakeContext> action = context => { };

// When
host.TaskTeardown(action);
Expand Down
8 changes: 6 additions & 2 deletions src/Cake.Core.Tests/Unit/TaskSetupContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using NSubstitute;
using Xunit;

namespace Cake.Core.Tests.Unit
Expand All @@ -13,8 +14,11 @@ public sealed class TheConstructor
[Fact]
public void Should_Throw_If_Task_Is_Null()
{
// Given, When
var result = Record.Exception(() => new TaskTeardownContext(null,TimeSpan.Zero, false));
// Given
var context = Substitute.For<ICakeContext>();

// When
var result = Record.Exception(() => new TaskTeardownContext(context, null, TimeSpan.Zero, false));

// Then
Assert.IsArgumentNullException(result, "task");
Expand Down
9 changes: 7 additions & 2 deletions src/Cake.Core.Tests/Unit/TaskTeardownContextTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using NSubstitute;
using Xunit;

namespace Cake.Core.Tests.Unit
Expand All @@ -12,8 +14,11 @@ public sealed class TheConstructor
[Fact]
public void Should_Throw_If_Task_Is_Null()
{
// Given, When
var result = Record.Exception(() => new TaskSetupContext(null));
// Given
var context = Substitute.For<ICakeContext>();

// When
var result = Record.Exception(() => new TaskSetupContext(context, null));

// Then
Assert.IsArgumentNullException(result, "task");
Expand Down
15 changes: 15 additions & 0 deletions src/Cake.Core.Tests/Unit/TeardownContextTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Xunit;

namespace Cake.Core.Tests.Unit
{
public sealed class TeardownContextTests
{
public sealed class TheConstructor
{
// TODO: Put tests here
}
}
}
3 changes: 3 additions & 0 deletions src/Cake.Core/Cake.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="Annotations\CakeNamespaceImportAttribute.cs" />
<Compile Include="Annotations\CakePropertyAliasAttribute.cs" />
<Compile Include="Annotations\CakeModuleAttribute.cs" />
<Compile Include="CakeContextAdapter.cs" />
<Compile Include="CakePlatform.cs" />
<Compile Include="CakeRuntime.cs" />
<Compile Include="Composition\ICakeContainerRegistry.cs" />
Expand Down Expand Up @@ -91,6 +92,7 @@
<Compile Include="Diagnostics\Verbosity.cs" />
<Compile Include="Extensions\CakePlatformExtensions.cs" />
<Compile Include="ICakeRuntime.cs" />
<Compile Include="ITeardownContext.cs" />
<Compile Include="PlatformFamily.cs" />
<Compile Include="Extensions\ConsoleExtensions.cs" />
<Compile Include="Extensions\DirectoryExtensions.cs" />
Expand Down Expand Up @@ -221,6 +223,7 @@
<Compile Include="Scripting\ScriptRunner.cs" />
<Compile Include="TaskSetupContext.cs" />
<Compile Include="TaskTeardownContext.cs" />
<Compile Include="TeardownContext.cs" />
<Compile Include="Text\ITextTransformationTemplate.cs" />
<Compile Include="Text\TextTransformationTemplate.cs" />
<Compile Include="Tooling\IToolRepository.cs" />
Expand Down
105 changes: 105 additions & 0 deletions src/Cake.Core/CakeContextAdapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Cake.Core.Diagnostics;
using Cake.Core.IO;
using Cake.Core.Tooling;

namespace Cake.Core
{
public abstract class CakeContextAdapter
{
private readonly ICakeContext _cakeContext;

public CakeContextAdapter(ICakeContext cakeContext)
{
_cakeContext = cakeContext;
}

/// <summary>
/// Gets the file system.
/// </summary>
/// <value>
/// The file system.
/// </value>
public IFileSystem FileSystem
{
get { return _cakeContext.FileSystem; }
}

/// <summary>
/// Gets the environment.
/// </summary>
/// <value>
/// The environment.
/// </value>
public ICakeEnvironment Environment
{
get { return _cakeContext.Environment; }
}

/// <summary>
/// Gets the globber.
/// </summary>
/// <value>
/// The globber.
/// </value>
public IGlobber Globber
{
get { return _cakeContext.Globber; }
}

/// <summary>
/// Gets the log.
/// </summary>
/// <value>
/// The log.
/// </value>
public ICakeLog Log { get { return _cakeContext.Log; } }

/// <summary>
/// Gets the arguments.
/// </summary>
/// <value>
/// The arguments.
/// </value>
public ICakeArguments Arguments
{
get { return _cakeContext.Arguments; }
}

/// <summary>
/// Gets the process runner.
/// </summary>
/// <value>
/// The process runner.
/// </value>
public IProcessRunner ProcessRunner
{
get { return _cakeContext.ProcessRunner; }
}

/// <summary>
/// Gets the registry.
/// </summary>
/// <value>
/// The registry.
/// </value>
public IRegistry Registry
{
get { return _cakeContext.Registry; }
}

/// <summary>
/// Gets the tool locator.
/// </summary>
/// <value>
/// The tool locator.
/// </value>
public IToolLocator Tools
{
get { return _cakeContext.Tools; }
}
}
}
27 changes: 15 additions & 12 deletions src/Cake.Core/CakeEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public sealed class CakeEngine : ICakeEngine
private readonly List<CakeTask> _tasks;
private Action<ICakeContext> _setupAction;
private Action<ICakeContext> _teardownAction;
private Action<ICakeContext, ITaskSetupContext> _taskSetupAction;
private Action<ICakeContext, ITaskTeardownContext> _taskTeardownAction;
private Action<ICakeContext> _taskSetupAction;
private Action<ICakeContext> _taskTeardownAction;

/// <summary>
/// Gets all registered tasks.
Expand Down Expand Up @@ -116,6 +116,7 @@ public CakeReport RunTarget(ICakeContext context, IExecutionStrategy strategy, s
// while running a setup action, or a task. We do this since we don't
// want to throw teardown exceptions if an exception was thrown previously.
var exceptionWasThrown = false;
Exception thrownException = null;

try
{
Expand Down Expand Up @@ -146,14 +147,15 @@ public CakeReport RunTarget(ICakeContext context, IExecutionStrategy strategy, s

return report;
}
catch
catch (Exception ex)
{
exceptionWasThrown = true;
thrownException = ex;
throw;
}
finally
{
PerformTeardown(strategy, context, exceptionWasThrown);
PerformTeardown(strategy, context, exceptionWasThrown, thrownException);
}
}

Expand All @@ -162,7 +164,7 @@ public CakeReport RunTarget(ICakeContext context, IExecutionStrategy strategy, s
/// If the task setup fails, the task will not be executed but the task's teardown will be performed.
/// </summary>
/// <param name="action">The action to be executed.</param>
public void RegisterTaskSetupAction(Action<ICakeContext, ITaskSetupContext> action)
public void RegisterTaskSetupAction(Action<ICakeContext> action)
{
_taskSetupAction = action;
}
Expand All @@ -172,7 +174,7 @@ public void RegisterTaskSetupAction(Action<ICakeContext, ITaskSetupContext> acti
/// If a task setup action or a task fails with or without recovery, the specified task teardown action will still be executed.
/// </summary>
/// <param name="action">The action to be executed.</param>
public void RegisterTaskTeardownAction(Action<ICakeContext, ITaskTeardownContext> action)
public void RegisterTaskTeardownAction(Action<ICakeContext> action)
{
_taskTeardownAction = action;
}
Expand Down Expand Up @@ -271,8 +273,8 @@ private void PerformTaskSetup(ICakeContext context, IExecutionStrategy strategy,
{
try
{
var taskSetupContext = new TaskSetupContext(task);
strategy.PerformTaskSetup(_taskSetupAction, context, taskSetupContext);
var taskSetupContext = new TaskSetupContext(context, task);
strategy.PerformTaskSetup(_taskSetupAction, context);
}
catch
{
Expand All @@ -286,10 +288,10 @@ private void PerformTaskTeardown(ICakeContext context, IExecutionStrategy strate
{
if (_taskTeardownAction != null)
{
var taskTeardownContext = new TaskTeardownContext(task, duration, skipped);
var taskTeardownContext = new TaskTeardownContext(context, task, duration, skipped);
try
{
strategy.PerformTaskTeardown(_taskTeardownAction, context, taskTeardownContext);
strategy.PerformTaskTeardown(_taskTeardownAction, context);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -350,13 +352,14 @@ private void HandleErrors(IExecutionStrategy strategy, Action<Exception> errorHa
}
}

private void PerformTeardown(IExecutionStrategy strategy, ICakeContext context, bool exceptionWasThrown)
private void PerformTeardown(IExecutionStrategy strategy, ICakeContext context, bool exceptionWasThrown, Exception thrownException)
{
if (_teardownAction != null)
{
try
{
strategy.PerformTeardown(_teardownAction, context);
var teardownContext = new TeardownContext(context, !exceptionWasThrown, thrownException);
strategy.PerformTeardown(_teardownAction, teardownContext);
}
catch (Exception ex)
{
Expand Down
32 changes: 18 additions & 14 deletions src/Cake.Core/DefaultExecutionStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,19 @@ public void InvokeFinally(Action action)
/// </summary>
/// <param name="action">The action.</param>
/// <param name="context">The context.</param>
/// <param name="setupContext">The setup context.</param>
public void PerformTaskSetup(Action<ICakeContext, ITaskSetupContext> action, ICakeContext context, ITaskSetupContext setupContext)
public void PerformTaskSetup(Action<ICakeContext> action, ICakeContext context)
{
if (setupContext == null)
{
throw new ArgumentNullException("setupContext");
}
if (action != null)
{
var setupContext = context as ITaskSetupContext;

if (setupContext == null)
{
throw new ArgumentNullException("setupContext");
}

_log.Debug("Executing custom task setup action ({0})...", setupContext.Task.Name);
action(context, setupContext);
action(context);
}
}

Expand All @@ -159,17 +161,19 @@ public void PerformTaskSetup(Action<ICakeContext, ITaskSetupContext> action, ICa
/// </summary>
/// <param name="action">The action.</param>
/// <param name="context">The context.</param>
/// <param name="teardownContext">The teardown context.</param>
public void PerformTaskTeardown(Action<ICakeContext, ITaskTeardownContext> action, ICakeContext context, ITaskTeardownContext teardownContext)
public void PerformTaskTeardown(Action<ICakeContext> action, ICakeContext context)
{
if (teardownContext == null)
{
throw new ArgumentNullException("teardownContext");
}
if (action != null)
{
var teardownContext = context as ITaskTeardownContext;

if (teardownContext == null)
{
throw new ArgumentNullException("context");
}

_log.Debug("Executing custom task teardown action ({0})...", teardownContext.Task.Name);
action(context, teardownContext);
action(context);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Cake.Core/ICakeEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ public interface ICakeEngine
/// If the task setup fails, the task will not be executed but the task's teardown will be performed.
/// </summary>
/// <param name="action">The action to be executed.</param>
void RegisterTaskSetupAction(Action<ICakeContext, ITaskSetupContext> action);
void RegisterTaskSetupAction(Action<ICakeContext> action);

/// <summary>
/// Allows registration of an action that's executed after each task has been run.
/// If a task setup action or a task fails with or without recovery, the specified task teardown action will still be executed.
/// </summary>
/// <param name="action">The action to be executed.</param>
void RegisterTaskTeardownAction(Action<ICakeContext, ITaskTeardownContext> action);
void RegisterTaskTeardownAction(Action<ICakeContext> action);
}
}
Loading

0 comments on commit 42527dc

Please sign in to comment.