Skip to content

Commit

Permalink
Merge branch 'wozzo-GH-2005' into develop
Browse files Browse the repository at this point in the history
* wozzo-GH-2005:
  (GH-2005) Add setup and teardown to report if run. * fixes #2005
  • Loading branch information
devlead committed Mar 13, 2018
2 parents 4f72974 + a6559b9 commit c6b8a18
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
36 changes: 36 additions & 0 deletions src/Cake.Core.Tests/Unit/CakeEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,42 @@ public async Task Should_Log_Task_Teardown_Exception_If_Both_Task_And_Task_Teard
Assert.Contains(fixture.Log.Entries, x => x.Message.StartsWith("Task Teardown error (A):"));
}

[Fact]
public async Task Should_Return_Report_That_Contains_Entry_For_Setup_First()
{
// Given
var fixture = new CakeEngineFixture();
var engine = fixture.CreateEngine();
engine.RegisterSetupAction(x => { });
engine.RegisterTask("A");

// When
var report = await engine.RunTargetAsync(fixture.Context, fixture.ExecutionStrategy, "A");

// Then
Assert.Equal(2, report.Count());
Assert.Equal("**Setup**", report.ElementAt(0).TaskName);
Assert.Equal("A", report.ElementAt(1).TaskName);
}

[Fact]
public async Task Should_Return_Report_That_Contains_Entry_For_Teardown_Last()
{
// Given
var fixture = new CakeEngineFixture();
var engine = fixture.CreateEngine();
engine.RegisterTeardownAction(x => { });
engine.RegisterTask("A");

// When
var report = await engine.RunTargetAsync(fixture.Context, fixture.ExecutionStrategy, "A");

// Then
Assert.Equal(2, report.Count());
Assert.Equal("A", report.ElementAt(0).TaskName);
Assert.Equal("**Teardown**", report.ElementAt(1).TaskName);
}

[Fact]
public async Task Should_Return_Report_That_Contains_Executed_Tasks_In_Order()
{
Expand Down
28 changes: 18 additions & 10 deletions src/Cake.Core/CakeEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ public async Task<CakeReport> RunTargetAsync(ICakeContext context, IExecutionStr
var exceptionWasThrown = false;
Exception thrownException = null;

var stopWatch = new Stopwatch();
var report = new CakeReport();

try
{
PerformSetup(strategy, context);

var stopWatch = new Stopwatch();
var report = new CakeReport();
PerformSetup(strategy, context, stopWatch, report);

foreach (var taskNode in graph.Traverse(target))
{
Expand Down Expand Up @@ -165,7 +165,7 @@ public async Task<CakeReport> RunTargetAsync(ICakeContext context, IExecutionStr
}
finally
{
PerformTeardown(strategy, context, exceptionWasThrown, thrownException);
PerformTeardown(strategy, context, stopWatch, report, exceptionWasThrown, thrownException);
}
}

Expand Down Expand Up @@ -199,12 +199,15 @@ public void RegisterTaskTeardownAction(Action<ITaskTeardownContext> action)
/// </summary>
public event EventHandler<TaskTeardownEventArgs> TaskTeardown;

private void PerformSetup(IExecutionStrategy strategy, ICakeContext context)
private void PerformSetup(IExecutionStrategy strategy, ICakeContext context, Stopwatch stopWatch, CakeReport report)
{
stopWatch.Restart();

PublishEvent(Setup, new SetupEventArgs(context));
if (_setupAction != null)
{
strategy.PerformSetup(_setupAction, context);
report.Add("**Setup**", stopWatch.Elapsed);
}
}

Expand All @@ -230,9 +233,7 @@ private static bool ShouldTaskExecute(ICakeContext context, CakeTask task, bool

private async Task ExecuteTaskAsync(ICakeContext context, IExecutionStrategy strategy, Stopwatch stopWatch, CakeTask task, CakeReport report)
{
// Reset the stop watch.
stopWatch.Reset();
stopWatch.Start();
stopWatch.Restart();

PerformTaskSetup(context, strategy, task, false);

Expand Down Expand Up @@ -375,8 +376,10 @@ private void HandleErrors(IExecutionStrategy strategy, Action<Exception> errorHa
}
}

private void PerformTeardown(IExecutionStrategy strategy, ICakeContext context, bool exceptionWasThrown, Exception thrownException)
private void PerformTeardown(IExecutionStrategy strategy, ICakeContext context, Stopwatch stopWatch, CakeReport report, bool exceptionWasThrown, Exception thrownException)
{
stopWatch.Restart();

var teardownContext = new TeardownContext(context, thrownException);
PublishEvent(Teardown, new TeardownEventArgs(teardownContext));
if (_teardownAction != null)
Expand All @@ -393,8 +396,13 @@ private void PerformTeardown(IExecutionStrategy strategy, ICakeContext context,
// If no other exception was thrown, we throw this one.
throw;
}

_log.Error("Teardown error: {0}", ex.ToString());
}
finally
{
report.Add("**Teardown**", stopWatch.Elapsed);
}
}
}

Expand Down

0 comments on commit c6b8a18

Please sign in to comment.