Pass task detail including error into TaskSetup and TaskTearDown#276
Conversation
|
This PR introduces duplication in error formatting code between Recommend that this error code be either refactored into a common helper or possibly removed from The code in |
|
looks like that failing |
pass $task into the TaskSetup and TaskTearDown as an optional argument declare param($task) at the start of your TaskSetup or TaskTearDown and the optional $task argument is available, meaning no more need to wrangle it via $psake.context.Peek() to get the current task name as you can simply declare param($task) and then have $task.Name this also gives us a convenient place to hang current task status and error reporting without polluting the global state. can verify that these specs access the $task.Name by loading up this source code's psake module and invoking them directly: PS> remove-module psake; import-module .\src\psake.psm1 PS> invoke-psake .\specs\tasksetup_with_task_argument_should_pass.ps1 localize the task success/fail/error data - by hanging the success/fail/error data off the $task and making that optionally available to the TaskSetup and TaskTearDown blocks, we can avoid poluting global $psake scope and allow the TaskTearDown block to contain error handling logic with a richer and simpler context.
2a195b8 to
38d265f
Compare
|
rebased on top of |
src/public/Task.ps1
Outdated
| RequiredVariables = $requiredVariables | ||
| Alias = $alias | ||
| Success = $true # let's be optimistic | ||
| ErrorMessage = $nill |
There was a problem hiding this comment.
Please make these $null rather than $nill
|
|
||
| $psake.build_success = $false | ||
| $psake.error_message = $error_message | ||
| $psake.error_message = FormatErrorMessage $_ |
There was a problem hiding this comment.
@davidalpert By moving the error formatting logic out, we no longer have an $error_message local variable. This will cause line 330:WriteColoredOutput $error_message -foregroundcolor Red to produce nothing as that variable doesn't exist anymore. Can you please change that line to use $psake.error_message instead?
| if ($currentConfig.verboseError) { | ||
| $error_message = "{0}: An Error Occurred. See Error Details Below: $($script:nl)" -f (Get-Date) | ||
| $error_message += ("-" * 70) + $script:nl | ||
| $error_message += "Error: {0}$($script:nl)" -f (ResolveError $_ -Short) |
There was a problem hiding this comment.
There are a couple of references to $_ in this function. This made sense when it was in the catch block in the previous location, but by having it breaking it out into its own function, the error record is being passed in as $ErrorRecord. Can you please replace references to $_ with $ErrorRecord in this function?
Description
I have copied the exception handling from
Invoke-Psakeinto the try/catch block ofInvoke-Taskso that we can trap errors and capture error detail before invoking thefinallyclause which callsTaskTearDown; this allowsTaskTearDownto operate in the context of the current task's status including any error detail on failure.This PR also proposes passing the
$taskparameter in the heart ofInvoke-TaskintoTaskSetupandTaskTearDownas optional parameters so that instead of doing this:we can do this:
Using the same technique in
TaskTearDownand setting some additional properties on theTaskinstance we can now do this:Related Issue
#275 TaskTearDown doesn't have access to task_succeeded or task_error_message context
Motivation and Context
As explained in #275 I was looking for a way to refactor common plumbing out of my individual psake tasks and express them in common Setup and TearDown code, but wanted access to whether the current task passed or failed and if it failed, access to the error message.
How Has This Been Tested?
In the following environment,
invoke-pesteron the root folder shows that all existing specs pass and a few new ones demonstrate the additional opt-in syntax and behaviour.My Environment
Module version used:
psake 4.7.4Operating System:

PowerShell version:
Types of changes
Checklist: