Skip to content

Commit

Permalink
(cake-buildGH-4046) Add CakeTaskBuilder.Finally<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
devlead committed Nov 7, 2022
1 parent ffb63b0 commit 3cb5c1f
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Cake.Cli/Hosts/DryRunExecutionStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Task HandleErrorsAsync(Func<Exception, ICakeContext, Task> action, Except
return Task.CompletedTask;
}

public Task InvokeFinallyAsync(Func<Task> action)
public Task InvokeFinallyAsync(Func<ICakeContext, Task> action, ICakeContext context)
{
return Task.CompletedTask;
}
Expand Down
30 changes: 29 additions & 1 deletion src/Cake.Core.Tests/Unit/CakeTaskBuilderExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,21 @@ public void Should_Throw_If_Action_Is_Null()
var builder = new CakeTaskBuilder(task);

// When
var result = Record.Exception(() => CakeTaskBuilderExtensions.Finally(builder, null));
var result = Record.Exception(() => CakeTaskBuilderExtensions.Finally(builder, default(Action)));

// Then
AssertEx.IsArgumentNullException(result, "finallyHandler");
}

[Fact]
public void Should_Throw_If_Action_Context_Is_Null()
{
// Given
var task = new CakeTask("task");
var builder = new CakeTaskBuilder(task);

// When
var result = Record.Exception(() => CakeTaskBuilderExtensions.Finally(builder, default(Action<ICakeContext>)));

// Then
AssertEx.IsArgumentNullException(result, "finallyHandler");
Expand All @@ -682,6 +696,20 @@ public void Should_Set_The_Finally_Handler()
// Then
Assert.NotNull(builder.Target.FinallyHandler);
}

[Fact]
public void Should_Set_The_Finally_Context_Handler()
{
// Given
var task = new CakeTask("task");
var builder = new CakeTaskBuilder(task);

// When
builder.Finally(context => { });

// Then
Assert.NotNull(builder.Target.FinallyHandler);
}
}

public sealed class TheReportErrorMethod
Expand Down
28 changes: 27 additions & 1 deletion src/Cake.Core.Tests/Unit/CakeTaskTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,20 @@ public void Should_Throw_If_Finally_Handler_Is_Null()
var task = new CakeTask("task");

// When
var result = Record.Exception(() => task.SetFinallyHandler(null));
var result = Record.Exception(() => task.SetFinallyHandler(default(Action)));

// Then
AssertEx.IsArgumentNullException(result, "finallyHandler");
}

[Fact]
public void Should_Throw_If_Finally_Context_Handler_Is_Null()
{
// Given
var task = new CakeTask("task");

// When
var result = Record.Exception(() => task.SetFinallyHandler(default(Action<ICakeContext>)));

// Then
AssertEx.IsArgumentNullException(result, "finallyHandler");
Expand All @@ -206,6 +219,19 @@ public void Should_Set_Finally_Handler()
Assert.NotNull(task.FinallyHandler);
}

[Fact]
public void Should_Set_Finally_Context_Handler()
{
// Given
var task = new CakeTask("task");

// When
task.SetFinallyHandler(context => { });

// Then
Assert.NotNull(task.FinallyHandler);
}

[Fact]
public void Should_Throw_If_Setting_More_Than_One_Finally_Handler()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Cake.Core/CakeEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ private async Task ExecuteTaskAsync(ICakeContext context, IExecutionStrategy str
{
if (task.FinallyHandler != null)
{
await strategy.InvokeFinallyAsync(task.FinallyHandler);
await strategy.InvokeFinallyAsync(task.FinallyHandler, context);
}

PerformTaskTeardown(context, strategy, task, stopWatch.Elapsed, false, taskException);
Expand Down
2 changes: 1 addition & 1 deletion src/Cake.Core/CakeTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public sealed class CakeTask : ICakeTaskInfo
/// <summary>
/// Gets or sets the finally handler.
/// </summary>
public Func<Task> FinallyHandler { get; set; }
public Func<ICakeContext, Task> FinallyHandler { get; set; }

/// <summary>
/// Gets the task's actions.
Expand Down
48 changes: 47 additions & 1 deletion src/Cake.Core/CakeTaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public static void SetFinallyHandler(this CakeTask task, Action finallyHandler)
}

#pragma warning disable CS1998
task.FinallyHandler = async () => finallyHandler();
task.FinallyHandler = async context => finallyHandler();
#pragma warning restore CS1998
}

Expand All @@ -192,6 +192,52 @@ public static void SetFinallyHandler(this CakeTask task, Func<Task> finallyHandl
throw new ArgumentNullException(nameof(finallyHandler));
}

task.FinallyHandler = context => finallyHandler();
}

/// <summary>
/// Sets the task's finally handler.
/// </summary>
/// <param name="task">The task.</param>
/// <param name="finallyHandler">The finally handler.</param>
/// <exception cref="CakeException">There can only be one finally handler per task.</exception>
/// <exception cref="ArgumentNullException"><paramref name="finallyHandler"/> is null.</exception>
public static void SetFinallyHandler(this CakeTask task, Action<ICakeContext> finallyHandler)
{
if (task.FinallyHandler != null)
{
throw new CakeException("There can only be one finally handler per task.");
}

if (finallyHandler is null)
{
throw new ArgumentNullException(nameof(finallyHandler));
}

#pragma warning disable CS1998
task.FinallyHandler = async context => finallyHandler(context);
#pragma warning restore CS1998
}

/// <summary>
/// Sets the task's finally handler.
/// </summary>
/// <param name="task">The task.</param>
/// <param name="finallyHandler">The finally handler.</param>
/// <exception cref="CakeException">There can only be one finally handler per task.</exception>
/// <exception cref="ArgumentNullException"><paramref name="finallyHandler"/> is null.</exception>
public static void SetFinallyHandler(this CakeTask task, Func<ICakeContext, Task> finallyHandler)
{
if (task.FinallyHandler != null)
{
throw new CakeException("There can only be one finally handler per task.");
}

if (finallyHandler is null)
{
throw new ArgumentNullException(nameof(finallyHandler));
}

task.FinallyHandler = finallyHandler;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Cake.Core/DefaultExecutionStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ public async Task HandleErrorsAsync(Func<Exception, ICakeContext, Task> action,
}

/// <inheritdoc/>
public async Task InvokeFinallyAsync(Func<Task> action)
public async Task InvokeFinallyAsync(Func<ICakeContext, Task> action, ICakeContext context)
{
if (action is null)
{
return;
}

await action();
await action(context);
}

/// <inheritdoc/>
Expand Down
3 changes: 2 additions & 1 deletion src/Cake.Core/IExecutionStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ public interface IExecutionStrategy
/// Invokes the finally handler.
/// </summary>
/// <param name="action">The action.</param>
/// <param name="context">The context.</param>
/// <returns>The awaitable task.</returns>
Task InvokeFinallyAsync(Func<Task> action);
Task InvokeFinallyAsync(Func<ICakeContext, Task> action, ICakeContext context);

/// <summary>
/// Performs the specified setup action before each task is invoked.
Expand Down

0 comments on commit 3cb5c1f

Please sign in to comment.