Skip to content

Commit

Permalink
Merge pull request #59 from pester/fail-on-stferr
Browse files Browse the repository at this point in the history
Add flag to fail on or not on stderr - Fixes #56
  • Loading branch information
ChrisLGardner authored Jan 9, 2021
2 parents 9ee5894 + 1e05e76 commit d606005
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 4 deletions.
14 changes: 12 additions & 2 deletions Extension/PesterTask/PesterV10/Pester.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ param

[string]$ScriptBlock,

[string]$TargetPesterVersion = "latest"
[string]$TargetPesterVersion = "latest",

[string]$FailOnStdErr
)

if ($TargetPesterVersion -match '^4') {
Write-Host "##vso[task.logissue type=error]This version of the task does not support Pester V4, please use task version 9."
exit 1
}

$FailStdErr = [Boolean]::Parse($FailOnStdErr)

Write-Host "TestFolder $TestFolder"
Write-Host "resultsFile $resultsFile"
Write-Host "run32Bit $run32Bit"
Expand All @@ -57,6 +61,7 @@ Write-Host "ExcludeTag $ExcludeTag"
Write-Host "CodeCoverageOutputFile $CodeCoverageOutputFile"
Write-Host "CodeCoverageFolder $CodeCoverageFolder"
Write-Host "ScriptBlock $ScriptBlock"
Write-Host "FailOnStdErr $FailOnStdErr"

Import-Module -Name (Join-Path $PSScriptRoot "HelperModule.psm1") -Force
Import-Pester -Version $TargetPesterVersion
Expand Down Expand Up @@ -142,7 +147,12 @@ if (-not([String]::IsNullOrWhiteSpace($ScriptBlock))) {
$ScriptBlockObject.Invoke()
}

$result = Invoke-Pester -Configuration ([PesterConfiguration]$PesterConfig)
if ($FailStdErr) {
$result = Invoke-Pester -Configuration ([PesterConfiguration]$PesterConfig)
}
else {
$result = Invoke-Pester -Configuration ([PesterConfiguration]$PesterConfig) 2> $null
}

if ($Result.Failed.Count -gt 0) {
Write-Error "Pester Failed at least one test. Please see results for details."
Expand Down
7 changes: 7 additions & 0 deletions Extension/PesterTask/PesterV10/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@
"required": false,
"helpMarkDown": "A scriptblock to run before tests are executed. This will be run in the scope of the task where Invoke-Pester is run.",
"groupName": "advanced"
},
{
"name": "failOnStdErr",
"type": "boolean",
"defaultValue": true,
"label": "Fail on StdErr",
"helpMarkDown": "Sets whether the task should fail on any output to StdErr from within the tests or not. Default true"
}
],
"instanceNameFormat": "Pester Test Runner",
Expand Down
15 changes: 13 additions & 2 deletions Extension/PesterTask/PesterV9/Pester.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,18 @@ param

[string]$ScriptBlock,

[string]$TargetPesterVersion = "latest"
[string]$TargetPesterVersion = "latest",

[string]$FailOnStdErr
)

if ($TargetPesterVersion -match '^5') {
Write-Host "##vso[task.logissue type=error]This version of the task does not support Pester V5, please use task version 10 or higher."
exit 1
}

$FailStdErr = [Boolean]::Parse($FailOnStdErr)

Write-Host "scriptFolder $scriptFolder"
Write-Host "resultsFile $resultsFile"
Write-Host "run32Bit $run32Bit"
Expand All @@ -56,6 +61,7 @@ Write-Host "ExcludeTag $ExcludeTag"
Write-Host "CodeCoverageOutputFile $CodeCoverageOutputFile"
Write-Host "CodeCoverageFolder $CodeCoverageFolder"
Write-Host "ScriptBlock $ScriptBlock"
Write-Host "FailOnStdErr $FailOnStdErr"

Import-Module -Name (Join-Path $PSScriptRoot "HelperModule.psm1") -Force
Import-Pester -Version $TargetPesterVersion
Expand Down Expand Up @@ -145,7 +151,12 @@ if (-not([String]::IsNullOrWhiteSpace($ScriptBlock))) {
$ScriptBlockObject.Invoke()
}

$result = Invoke-Pester @Parameters
if ($FailStdErr) {
$result = Invoke-Pester @Parameters
}
else {
$result = Invoke-Pester @Parameters 2> $null
}

if ($result.failedCount -ne 0) {
Write-Error "Pester returned errors"
Expand Down
7 changes: 7 additions & 0 deletions Extension/PesterTask/PesterV9/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@
"required": false,
"helpMarkDown": "A scriptblock to run before tests are executed. This will be run in the scope of the task where Invoke-Pester is run.",
"groupName": "advanced"
},
{
"name": "failOnStdErr",
"type": "boolean",
"defaultValue": true,
"label": "Fail on StdErr",
"helpMarkDown": "Sets whether the task should fail on any output to StdErr from within the tests or not. Default true"
}
],
"instanceNameFormat": "Pester Test Runner",
Expand Down
2 changes: 2 additions & 0 deletions Extension/PesterTask/src/pester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export async function run() {
let CodeCoverageFolder = tl.getInput("CodeCoverageFolder");
let ScriptBlock = tl.getInput("ScriptBlock");
let PesterVersion = tl.getInput("PesterVersion");
let FailOnStdErr = tl.getInput("failOnStdErr");

let TargetPesterVersion = "0.0.0";
if (PesterVersion === "OtherVersion") {
Expand Down Expand Up @@ -48,6 +49,7 @@ export async function run() {
"-scriptFolder", scriptFolder,
"-resultsFile", resultsFile,
"-run32Bit", run32Bit,
"-FailOnStdErr", FailOnStdErr,
];

if (additionalModulePath) {
Expand Down
2 changes: 2 additions & 0 deletions Extension/PesterTask/src/pesterv10.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export async function run() {
let CodeCoverageFolder = tl.getInput("CodeCoverageFolder");
let ScriptBlock = tl.getInput("ScriptBlock");
let PesterVersion = tl.getInput("PesterVersion");
let FailOnStdErr = tl.getInput("failOnStdErr");

let TargetPesterVersion = "0.0.0";
if (PesterVersion === "OtherVersion") {
Expand Down Expand Up @@ -48,6 +49,7 @@ export async function run() {
"-TestFolder", TestFolder,
"-resultsFile", resultsFile,
"-run32Bit", run32Bit,
"-FailOnStdErr", FailOnStdErr,
];

if (additionalModulePath) {
Expand Down

0 comments on commit d606005

Please sign in to comment.