Skip to content

Pass task detail including error into TaskSetup and TaskTearDown#276

Merged
devblackops merged 3 commits intopsake:masterfrom
davidalpert:teardown-with-error-detail
Apr 5, 2019
Merged

Pass task detail including error into TaskSetup and TaskTearDown#276
devblackops merged 3 commits intopsake:masterfrom
davidalpert:teardown-with-error-detail

Conversation

@davidalpert
Copy link
Copy Markdown
Contributor

Description

I have copied the exception handling from Invoke-Psake into the try/catch block of Invoke-Task so that we can trap errors and capture error detail before invoking the finally clause which calls TaskTearDown; this allows TaskTearDown to operate in the context of the current task's status including any error detail on failure.

This PR also proposes passing the $task parameter in the heart of Invoke-Task into TaskSetup and TaskTearDown as optional parameters so that instead of doing this:

taskSetup {
    "Begin task: '$($psake.context.Peek().currentTaskName)'"
}

we can do this:

taskSetup {
    "Begin task: '$($psake.context.Peek().currentTaskName)'"
}

Using the same technique in TaskTearDown and setting some additional properties on the Task instance we can now do this:

taskTearDown {
    param($task)

    if ($task.Success) {
        "'$($task.Name)' Tear Down: task passed!"
    } else {
        "'$($task.Name)' Tear Down: task failed with '$($task.ErrorMessage)'"
    }
}

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-pester on 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.4

  • Operating System:
    image

  • PowerShell version:

PS C:\dev\psake-test> get-host

Name             : ConsoleHost
Version          : 5.1.17763.316
InstanceId       : cf5e4b3b-1d00-42e4-9bc7-3a84c02aa881
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@davidalpert
Copy link
Copy Markdown
Contributor Author

This PR introduces duplication in error formatting code between Invoke-Task and Invoke-Psake;

Recommend that this error code be either refactored into a common helper or possibly removed from Invoke-Psake altogether, should it be deemed that it is no longer necessary.

The code in Invoke-Psake looks like a fairly basic runner/wrapper while the actual work seems to get done in Invoke-Task so if we are trapping and wrapping and exposing errors in Invoke-Task perhaps it is redundant to continue handling them the same way in Invoke-Psake and that original catch block could be simplified by someone with more familiarity in the codebase.

@devblackops devblackops self-requested a review March 20, 2019 06:26
@devblackops devblackops self-assigned this Mar 20, 2019
@davidalpert
Copy link
Copy Markdown
Contributor Author

looks like that failing travis-ci build is a known issue, failing on environment setup

./download.sh: line 150: pwsh: command not found
ERROR: PowerShell failed to install!
The command "./download.sh" failed and exited with 127 during .
Your build has been stopped.

davidalpert and others added 3 commits March 24, 2019 12:41
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.
@davidalpert davidalpert force-pushed the teardown-with-error-detail branch from 2a195b8 to 38d265f Compare March 24, 2019 18:07
@davidalpert
Copy link
Copy Markdown
Contributor Author

rebased on top of upstream/master

RequiredVariables = $requiredVariables
Alias = $alias
Success = $true # let's be optimistic
ErrorMessage = $nill
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make these $null rather than $nill


$psake.build_success = $false
$psake.error_message = $error_message
$psake.error_message = FormatErrorMessage $_
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@devblackops devblackops merged commit 6c2ea1c into psake:master Apr 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants