Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Invoke-Pester and validate configuration early #2317

Merged
merged 18 commits into from Jun 7, 2023

Conversation

fflaten
Copy link
Collaborator

@fflaten fflaten commented Mar 4, 2023

PR Summary

The code for Invoke-Pester has become huge over the years and hard to read and maintain. This PR splits out parts of the code to plugin-steps and helper-functions.

It also moves configuration validation to the start to avoid longs runs being lost in the end due to a typo.

  • Extract parameter set migration for Simple and Legacy-set
  • Call TestResult-export through plugin End-step
  • Move CodeCoverage processing to plugin-steps Start, RunEnd and End
  • Validate configuration early to avoid running a long run just to loose it at the end when it throws due to invalid format for coverage, testresult etc. (ex. RuntimeExceptions for invalid output configuration doesn't kill the run #2304)
    • Also cleaned up the error message for invalid StringOption-value so they all look consistent.

Fix #2304

PR Checklist

  • PR has meaningful title
  • Summary describes changes
  • PR is ready to be merged
    • If not, use the arrow next to Create Pull Request to mark it as a draft. PR can be marked Ready for review when it's ready.
  • Tests are added/update (if required)
  • Documentation is updated/added (if required)

try {
$xmlWriter.Close()
}
catch {

Check warning

Code scanning / PSScriptAnalyzer

Empty catch block is used. Please use Write-Error or throw statements in catch blocks. Warning

Empty catch block is used. Please use Write-Error or throw statements in catch blocks.
try {
$stringWriter.Close()
}
catch {

Check warning

Code scanning / PSScriptAnalyzer

Empty catch block is used. Please use Write-Error or throw statements in catch blocks. Warning

Empty catch block is used. Please use Write-Error or throw statements in catch blocks.
src/functions/TestResults.ps1 Fixed Show fixed Hide fixed
@fflaten
Copy link
Collaborator Author

fflaten commented Mar 7, 2023

  • Throwing in Start plugin-step creates an ugly and duplicate error atm. Likely due to being thrown through Assert-Success. Might be better to use own function like Resolve-OutputConfiguration

The duplicate was because Assert-Success printed the error while also throwing it. Suggest removing the host message.

Thoughts on where to validate config, especially string-options? Options:

  1. Start plugin-step. Removed the duplicate message, but still a bit verbose due to Assert-Success
    image

  2. Validation-function, ex. Resolve-OutputConfiguration
    image

  3. Validate in assembly for StringOption. Would block third-party plugins from integrating with built-in options like TestResult.OutputFormat, but do we want to support that?
    image
    image

  4. A new ConfigurePlugin/SetupPlugin step invoked without Invoke-PluginStep or a different branch to simplify error. Maybe invoked before Find-File call which can be slow. This might also benefit public plugin API.

Comment on lines +1303 to +1311
$rc++
$ec = 0
if ($null -ne $r.ErrorRecord -and $r.ErrorRecord.Length -gt 0) {
$err += "Result $($rc++):"
$anyFailed = $true
foreach ($e in $r.ErrorRecord) {
$err += "Error $($ec++):"
$err += "$([Environment]::NewLine)Result $rc - Error $((++$ec)):"
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Previous message didn't print $ec and $rc (only incremented them) due to missing parantheses.

@fflaten
Copy link
Collaborator Author

fflaten commented Mar 25, 2023

/azp run

@azure-pipelines
Copy link
Contributor

Azure Pipelines successfully started running 1 pipeline(s).

@@ -1216,7 +1227,7 @@ function Invoke-PluginStep {
# this is end step, we should run all steps no matter if some failed, and we should run them in opposite direction
# only do this if there is more than 1, to avoid the "expensive" -like check and reverse
$isEndStep = 1 -lt $pluginsWithGivenStep.Count -and $Step -like "*End"
if (-not $isEndStep) {
if ($isEndStep) {
Copy link
Member

Choose a reason for hiding this comment

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

So this did not reverse the steps before, did you see any bugs linked to that?

Copy link
Collaborator Author

@fflaten fflaten Apr 6, 2023

Choose a reason for hiding this comment

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

No bugs I'm aware of. Just not behaving like comment says. We reverse on TearDown-steps as expected, but this currently reverses on everything else than *End.

Not a critical part of the PR, just testing it out while in draft. Will need to revert this or change plugin-order as it (PR-code) now runs CC and nunit-reports before writing test-report to screen. Either way it's confusing/inconsistent atm (pre-PR).

@fflaten
Copy link
Collaborator Author

fflaten commented Apr 15, 2023

  • Throwing in Start plugin-step creates an ugly and duplicate error atm. Likely due to being thrown through Assert-Success. Might be better to use own function like Resolve-OutputConfiguration

The duplicate was because Assert-Success printed the error while also throwing it. Suggest removing the host message.

Thoughts on where to validate config, especially string-options? Options:

  1. Start plugin-step. Removed the duplicate message, but still a bit verbose due to Assert-Success
    image
  2. Validation-function, ex. Resolve-OutputConfiguration
    image
  3. Validate in assembly for StringOption. Would block third-party plugins from integrating with built-in options like TestResult.OutputFormat, but do we want to support that?
    image
    image
  4. A new ConfigurePlugin/SetupPlugin step invoked without Invoke-PluginStep or a different branch to simplify error. Maybe invoked before Find-File call which can be slow. This might also benefit public plugin API.

Thoughts on this @nohwnd?

@fflaten
Copy link
Collaborator Author

fflaten commented May 14, 2023

Rebased and updated with validation-functions for now. Can revisit later when looking into public plugins API.

Also cleaned up the error message for invalid StringOption. It now list option and supported values just like CodeCoverage did.
image
image

$err += & $SafeCommands["Out-String"] -InputObject $e
$err += & $SafeCommands["Out-String"] -InputObject $e.ScriptStackTrace
}
}
}

if ($anyFailed) {
$Message = $Message + ":`n$err"
Write-PesterHostMessage -ForegroundColor Red $Message
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Revert this or good? Not validating configuration in plugin-step now, so PR is not affected by the duplicate error from Assert-Success. Not sure if it's useful somewhere else

Copy link
Member

Choose a reason for hiding this comment

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

good.

@fflaten fflaten changed the title WIP - Refactoring Invoke-Pester and early configuration-validation Refactor Invoke-Pester and validate configuration early May 14, 2023
@fflaten fflaten marked this pull request as ready for review May 14, 2023 13:21
@fflaten fflaten requested a review from nohwnd May 14, 2023 13:21
@nohwnd
Copy link
Member

nohwnd commented Jun 7, 2023

@fflaten Approved both this and your test output refactoring PRs, they seem like they might run into each other so I was not sure in which order you want them merged, please merge any way you like :)

@fflaten
Copy link
Collaborator Author

fflaten commented Jun 7, 2023

@nohwnd Thanks! Let's start with this. It will also cause a conflict in the last PR for container data. Can fix both later today. 🙂

@fflaten
Copy link
Collaborator Author

fflaten commented Jun 7, 2023

Btw. Don't have merge rights, so up to you :)

@nohwnd
Copy link
Member

nohwnd commented Jun 7, 2023

@fflaten you do now.

@fflaten fflaten merged commit 997002c into pester:main Jun 7, 2023
14 checks passed
@fflaten fflaten deleted the invokepester-refactor branch June 7, 2023 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RuntimeExceptions for invalid output configuration doesn't kill the run
2 participants