Skip to content

Commit 6a68838

Browse files
TravisEz13daxian-dbw
authored andcommitted
For reliability, start using pester PassThru object to verify tests passed (PowerShell#4644)
* Add ability to verify a test run using the `-passthru` object from pester * update travis-ci to use `-passthru` object to verify test results * Address PR feedback * call show-pspestererror with a named parameter
1 parent c219066 commit 6a68838

2 files changed

Lines changed: 101 additions & 28 deletions

File tree

build.psm1

Lines changed: 95 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ function Publish-PSTestTools {
796796
}
797797

798798
function Start-PSPester {
799-
[CmdletBinding()]
799+
[CmdletBinding(DefaultParameterSetName='default')]
800800
param(
801801
[string]$OutputFormat = "NUnitXml",
802802
[string]$OutputFile = "pester-tests.xml",
@@ -807,8 +807,10 @@ function Start-PSPester {
807807
[string]$binDir = (Split-Path (New-PSOptions).Output),
808808
[string]$powershell = (Join-Path $binDir 'powershell'),
809809
[string]$Pester = ([IO.Path]::Combine($binDir, "Modules", "Pester")),
810+
[Parameter(ParameterSetName='Unelevate',Mandatory=$true)]
810811
[switch]$Unelevate,
811812
[switch]$Quiet,
813+
[Parameter(ParameterSetName='PassThru',Mandatory=$true)]
812814
[switch]$PassThru
813815
)
814816

@@ -839,7 +841,7 @@ function Start-PSPester {
839841
}
840842

841843
Write-Verbose "Running pester tests at '$path' with tag '$($Tag -join ''', ''')' and ExcludeTag '$($ExcludeTag -join ''', ''')'" -Verbose
842-
Publish-PSTestTools
844+
Publish-PSTestTools | ForEach-Object {Write-Host $_}
843845

844846
# All concatenated commands/arguments are suffixed with the delimiter (space)
845847
$Command = ""
@@ -914,7 +916,24 @@ function Start-PSPester {
914916
}
915917
else
916918
{
917-
& $powershell -noprofile -c $Command
919+
if ($PassThru.IsPresent)
920+
{
921+
$passThruFile = [System.IO.Path]::GetTempFileName()
922+
try
923+
{
924+
$Command += "|Export-Clixml -Path '$passThruFile' -Force"
925+
Start-NativeExecution -sb {& $powershell -noprofile -c $Command} | ForEach-Object { Write-Host $_}
926+
Import-Clixml -Path $passThruFile | Where-Object {$_.TotalCount -is [Int32]}
927+
}
928+
finally
929+
{
930+
Remove-Item $passThruFile -ErrorAction SilentlyContinue
931+
}
932+
}
933+
else
934+
{
935+
Start-NativeExecution -sb {& $powershell -noprofile -c $Command}
936+
}
918937
}
919938
} finally {
920939
$env:PSModulePath = $originalModulePath
@@ -946,46 +965,97 @@ function script:Start-UnelevatedProcess
946965

947966
function Show-PSPesterError
948967
{
949-
param ( [Xml.XmlElement]$testFailure )
950-
logerror ("Description: " + $testFailure.description)
951-
logerror ("Name: " + $testFailure.name)
968+
[CmdletBinding(DefaultParameterSetName='xml')]
969+
param (
970+
[Parameter(ParameterSetName='xml',Mandatory)]
971+
[Xml.XmlElement]$testFailure,
972+
[Parameter(ParameterSetName='object',Mandatory)]
973+
[PSCustomObject]$testFailureObject
974+
)
975+
976+
if ($PSCmdLet.ParameterSetName -eq 'xml')
977+
{
978+
$description = $testFailure.description
979+
$name = $testFailure.name
980+
$message = $testFailure.failure.message
981+
$stackTrace = $testFailure.failure."stack-trace"
982+
}
983+
elseif ($PSCmdLet.ParameterSetName -eq 'object')
984+
{
985+
$description = $testFailureObject.Describe + '/' + $testFailureObject.Context
986+
$name = $testFailureObject.Name
987+
$message = $testFailureObject.FailureMessage
988+
$stackTrace = $testFailureObject.StackTrace
989+
}
990+
else
991+
{
992+
throw 'Unknown Show-PSPester parameter set'
993+
}
994+
995+
logerror ("Description: " + $description)
996+
logerror ("Name: " + $name)
952997
logerror "message:"
953-
logerror $testFailure.failure.message
998+
logerror $message
954999
logerror "stack-trace:"
955-
logerror $testFailure.failure."stack-trace"
1000+
logerror $stackTrace
1001+
9561002
}
9571003

9581004
#
9591005
# Read the test result file and
9601006
# Throw if a test failed
9611007
function Test-PSPesterResults
9621008
{
1009+
[CmdletBinding(DefaultParameterSetName='file')]
9631010
param(
1011+
[Parameter(ParameterSetName='file')]
9641012
[string]$TestResultsFile = "pester-tests.xml",
965-
[string]$TestArea = 'test/powershell'
966-
)
1013+
[Parameter(ParameterSetName='file')]
1014+
[string]$TestArea = 'test/powershell',
1015+
[Parameter(ParameterSetName='PesterPassThruObject',Mandatory)]
1016+
[pscustomobject] $ResultObject
1017+
)
9671018

968-
if(!(Test-Path $TestResultsFile))
1019+
if($PSCmdLet.ParameterSetName -eq 'file')
9691020
{
970-
throw "Test result file '$testResultsFile' not found for $TestArea."
971-
}
1021+
if(!(Test-Path $TestResultsFile))
1022+
{
1023+
throw "Test result file '$testResultsFile' not found for $TestArea."
1024+
}
9721025

973-
$x = [xml](Get-Content -raw $testResultsFile)
974-
if ([int]$x.'test-results'.failures -gt 0)
975-
{
976-
logerror "TEST FAILURES"
977-
# switch between methods, SelectNode is not available on dotnet core
978-
if ( "System.Xml.XmlDocumentXPathExtensions" -as [Type] ) {
979-
$failures = [System.Xml.XmlDocumentXPathExtensions]::SelectNodes($x."test-results",'.//test-case[@result = "Failure"]')
1026+
$x = [xml](Get-Content -raw $testResultsFile)
1027+
if ([int]$x.'test-results'.failures -gt 0)
1028+
{
1029+
logerror "TEST FAILURES"
1030+
# switch between methods, SelectNode is not available on dotnet core
1031+
if ( "System.Xml.XmlDocumentXPathExtensions" -as [Type] )
1032+
{
1033+
$failures = [System.Xml.XmlDocumentXPathExtensions]::SelectNodes($x."test-results",'.//test-case[@result = "Failure"]')
1034+
}
1035+
else
1036+
{
1037+
$failures = $x.SelectNodes('.//test-case[@result = "Failure"]')
1038+
}
1039+
foreach ( $testfail in $failures )
1040+
{
1041+
Show-PSPesterError -testFailure $testfail
1042+
}
1043+
throw "$($x.'test-results'.failures) tests in $TestArea failed"
9801044
}
981-
else {
982-
$failures = $x.SelectNodes('.//test-case[@result = "Failure"]')
1045+
}
1046+
elseif ($PSCmdLet.ParameterSetName -eq 'PesterPassThruObject')
1047+
{
1048+
if ($ResultObject.TotalCount -le 0)
1049+
{
1050+
logerror 'NO TESTS RUN'
9831051
}
984-
foreach ( $testfail in $failures )
1052+
elseif ($ResultObject.FailedCount -gt 0)
9851053
{
986-
Show-PSPesterError $testfail
1054+
logerror 'TEST FAILURES'
1055+
$ResultObject.TestResult | Where-Object {$_.Passed -eq $false} | ForEach-Object {
1056+
Show-PSPesterError -testFailureObject $_
1057+
}
9871058
}
988-
throw "$($x.'test-results'.failures) tests in $TestArea failed"
9891059
}
9901060
}
9911061

tools/travis.ps1

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ else
149149
$ProgressPreference = $originalProgressPreference
150150
}
151151

152-
$pesterParam = @{ 'binDir' = $output }
152+
$pesterParam = @{
153+
'binDir' = $output
154+
'PassThru' = $true
155+
}
153156

154157
if ($isFullBuild) {
155158
$pesterParam['Tag'] = @('CI','Feature','Scenario')
@@ -165,7 +168,7 @@ else
165168
Remove-Item -force ${telemetrySemaphoreFilepath}
166169
}
167170

168-
Start-PSPester @pesterParam
171+
$pesterPassThruObject = Start-PSPester @pesterParam
169172

170173
if (-not $isPr) {
171174
# Run 'CrossGen' for push commit, so that we can generate package.
@@ -197,7 +200,7 @@ else
197200

198201
try {
199202
# this throws if there was an error
200-
Test-PSPesterResults
203+
Test-PSPesterResults -ResultObject $pesterPassThruObject
201204
$result = "PASS"
202205
}
203206
catch {

0 commit comments

Comments
 (0)