Skip to content

Commit

Permalink
Reorganize TestResuls-code into format-specific files (#2348)
Browse files Browse the repository at this point in the history
  • Loading branch information
fflaten committed Jun 7, 2023
1 parent 997002c commit 17157de
Show file tree
Hide file tree
Showing 13 changed files with 656 additions and 601 deletions.
8 changes: 4 additions & 4 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,17 @@ function Copy-Content ($Content) {

$content = @(
, ("$PSScriptRoot/src/en-US/*.txt", "$PSScriptRoot/bin/en-US/")
, ("$PSScriptRoot/src/schemas/NUnit3/*.xsd", "$PSScriptRoot/bin/schemas/NUnit3/")
, ("$PSScriptRoot/src/nunit_schema_2.5.xsd", "$PSScriptRoot/bin/")
, ("$PSScriptRoot/src/junit_schema_4.xsd", "$PSScriptRoot/bin/")
, ("$PSScriptRoot/src/report.dtd", "$PSScriptRoot/bin/")
, ("$PSScriptRoot/src/Pester.ps1", "$PSScriptRoot/bin/")
, ("$PSScriptRoot/src/Pester.psd1", "$PSScriptRoot/bin/")
, ("$PSScriptRoot/src/Pester.Format.ps1xml", "$PSScriptRoot/bin/")
)

if ($Clean) {
$content += @(
, ("$PSScriptRoot/src/schemas/JUnit4/*.xsd", "$PSScriptRoot/bin/schemas/JUnit4/")
, ("$PSScriptRoot/src/schemas/NUnit25/*.xsd", "$PSScriptRoot/bin/schemas/NUnit25/")
, ("$PSScriptRoot/src/schemas/NUnit3/*.xsd", "$PSScriptRoot/bin/schemas/NUnit3/")
, ("$PSScriptRoot/src/schemas/JaCoCo/*.dtd", "$PSScriptRoot/bin/schemas/JaCoCo/")
, ("$PSScriptRoot/src/csharp/Pester/bin/$Configuration/net452/Pester.dll", "$PSScriptRoot/bin/bin/net452/")
, ("$PSScriptRoot/src/csharp/Pester/bin/$Configuration/net452/Pester.pdb", "$PSScriptRoot/bin/bin/net452/")
, ("$PSScriptRoot/src/csharp/Pester/bin/$Configuration/netstandard2.0/Pester.dll", "$PSScriptRoot/bin/bin/netstandard2.0/")
Expand Down
47 changes: 26 additions & 21 deletions publish/release.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,27 @@ if ((Get-Item $bin/Pester.psm1).Length -lt 50KB) {

& "$PSScriptRoot/signModule.ps1" -Thumbprint $CertificateThumbprint -Path $bin


$files = @(
"nunit_schema_2.5.xsd"
"junit_schema_4.xsd"
"Pester.psd1"
"Pester.psm1"
"report.dtd"
"bin\net452\Pester.dll"
"bin\net452\Pester.pdb"
"bin\netstandard2.0\Pester.dll"
"bin\netstandard2.0\Pester.pdb"
"en-US\about_BeforeEach_AfterEach.help.txt"
"en-US\about_Mocking.help.txt"
"en-US\about_Pester.help.txt"
"en-US\about_Should.help.txt"
"en-US\about_TestDrive.help.txt"
'Pester.ps1'
'Pester.psd1'
'Pester.psm1'
'Pester.Format.ps1xml'
'PesterConfiguration.Format.ps1xml'
'bin/net452/Pester.dll'
'bin/net452/Pester.pdb'
'bin/netstandard2.0/Pester.dll'
'bin/netstandard2.0/Pester.pdb'
'en-US/about_BeforeEach_AfterEach.help.txt'
'en-US/about_Mocking.help.txt'
'en-US/about_Pester.help.txt'
'en-US/about_Should.help.txt'
'en-US/about_TestDrive.help.txt'
'schemas/JaCoCo/report.dtd'
'schemas/JUnit4/junit_schema_4.xsd'
'schemas/NUnit25/nunit_schema_2.5.xsd'
'schemas/NUnit3/TestDefinitions.xsd'
'schemas/NUnit3/TestFilterDefinitions.xsd'
'schemas/NUnit3/TestResult.xsd'
)

$notFound = @()
Expand All @@ -83,7 +88,7 @@ if (0 -lt $notFound.Count) {
throw "Did not find files:`n$($notFound -join "`n")"
}
else {
"Found all files!"
'Found all files!'
}

# build psgallery module
Expand All @@ -104,7 +109,7 @@ $null = New-Item -ItemType Directory -Path $nugetDir
Copy-Item "$PSScriptRoot/../bin/*" $nugetDir -Recurse
Copy-Item "$PSScriptRoot/../LICENSE" $nugetDir -Recurse

Out-File $nugetDir\VERIFICATION.txt -InputObject @"
Out-File "$nugetDir/VERIFICATION.txt" -InputObject @'
VERIFICATION
Verification is intended to assist the Chocolatey moderators and community
in verifying that this package's contents are trustworthy.
Expand All @@ -114,13 +119,13 @@ You can use one of the following methods to obtain the checksum
- Use chocolatey utility 'checksum.exe'
CHECKSUMS
"@
'@

Get-ChildItem -Path $bin -Filter *.dll -Recurse | Foreach-Object {
Get-ChildItem -Path $bin -Filter *.dll -Recurse | ForEach-Object {
$path = $_.FullName
$relativePath = ($path -replace [regex]::Escape($nugetDir.TrimEnd('/').TrimEnd('\'))).TrimStart('/').TrimStart('\')
$hash = Get-FileHash -Path $path -Algorithm SHA256 | Select-Object -ExpandProperty Hash
Out-File $nugetDir\VERIFICATION.txt -Append -InputObject @"
$hash = Get-FileHash -Path $path -Algorithm SHA256 | Select-Object -ExpandProperty Hash
Out-File "$nugetDir/VERIFICATION.txt" -Append -InputObject @"
file: $relativePath
hash: $hash
algorithm: sha256
Expand Down
14 changes: 14 additions & 0 deletions src/functions/Output.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,20 @@ function Write-BlockToScreen {
Write-PesterHostMessage "${margin}${Text}" -ForegroundColor $ReportTheme.$CommandUsed
}

function Get-HumanTime {
param( [TimeSpan] $TimeSpan)
if ($TimeSpan.Ticks -lt [timespan]::TicksPerSecond) {
$time = [int]($TimeSpan.TotalMilliseconds)
$unit = 'ms'
}
else {
$time = [math]::Round($TimeSpan.TotalSeconds, 2)
$unit = 's'
}

return "$time$unit"
}

# This is not a plugin-step due to Output-features being dependencies in Invoke-PluginStep etc for error/debug
# Output-options are also used for Write-PesterDebugMessage which is independent of WriteScreenPlugin
function Resolve-OutputConfiguration ([PesterConfiguration]$PesterPreference) {
Expand Down
152 changes: 152 additions & 0 deletions src/functions/TestResults.JUnit4.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
function Write-JUnitReport {
param($Result, [System.Xml.XmlWriter] $XmlWriter)
# Write the XML Declaration
$XmlWriter.WriteStartDocument($false)

# Write Root Element
$xmlWriter.WriteStartElement('testsuites')

Write-JUnitTestResultAttributes @PSBoundParameters

$testSuiteNumber = 0
foreach ($container in $Result.Containers) {
if (-not $container.ShouldRun) {
# skip containers that were discovered but none of their tests run
continue
}

Write-JUnitTestSuiteElements -XmlWriter $XmlWriter -Container $container -Id $testSuiteNumber
$testSuiteNumber++
}

$XmlWriter.WriteEndElement()
}

function Write-JUnitTestResultAttributes {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns','')]
param($Result, [System.Xml.XmlWriter] $XmlWriter)

$XmlWriter.WriteAttributeString('xmlns', 'xsi', $null, 'http://www.w3.org/2001/XMLSchema-instance')
$XmlWriter.WriteAttributeString('xsi', 'noNamespaceSchemaLocation', [Xml.Schema.XmlSchema]::InstanceNamespace , 'junit_schema_4.xsd')
$XmlWriter.WriteAttributeString('name', $Result.Configuration.TestResult.TestSuiteName.Value)
$XmlWriter.WriteAttributeString('tests', $Result.TotalCount)
$XmlWriter.WriteAttributeString('errors', $Result.FailedContainersCount + $Result.FailedBlocksCount)
$XmlWriter.WriteAttributeString('failures', $Result.FailedCount)
$XmlWriter.WriteAttributeString('disabled', $Result.NotRunCount + $Result.SkippedCount)
$XmlWriter.WriteAttributeString('time', ($Result.Duration.TotalSeconds.ToString('0.000', [System.Globalization.CultureInfo]::InvariantCulture)))
}

function Write-JUnitTestSuiteElements {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns','')]
param($Container, [System.Xml.XmlWriter] $XmlWriter, [uint16] $Id)

$XmlWriter.WriteStartElement('testsuite')

if ('File' -eq $Container.Type) {
$path = $Container.Item.FullName
}
elseif ('ScriptBlock' -eq $Container.Type) {
$path = "<ScriptBlock>$($Container.Item.File):$($Container.Item.StartPosition.StartLine)"
}
else {
throw "Container type '$($Container.Type)' is not supported."
}

Write-JUnitTestSuiteAttributes -Action $Container -XmlWriter $XmlWriter -Package $path -Id $Id


$testResults = [Pester.Factory]::CreateCollection()
Fold-Container -Container $Container -OnTest { param ($t) if ($t.ShouldRun) { $testResults.Add($t) } }
foreach ($t in $testResults) {
Write-JUnitTestCaseElements -TestResult $t -XmlWriter $XmlWriter -Package $path
}

$XmlWriter.WriteEndElement()
}

function Write-JUnitTestSuiteAttributes {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns','')]
param($Action, [System.Xml.XmlWriter] $XmlWriter, [string] $Package, [uint16] $Id)

$environment = Get-RunTimeEnvironment

$XmlWriter.WriteAttributeString('name', $Package)
$XmlWriter.WriteAttributeString('tests', $Action.TotalCount)
$XmlWriter.WriteAttributeString('errors', '0')
$XmlWriter.WriteAttributeString('failures', $Action.FailedCount)
$XmlWriter.WriteAttributeString('hostname', $environment.'machine-name')
$XmlWriter.WriteAttributeString('id', $Id)
$XmlWriter.WriteAttributeString('skipped', $Action.SkippedCount)
$XmlWriter.WriteAttributeString('disabled', $Action.NotRunCount)
$XmlWriter.WriteAttributeString('package', $Package)
$XmlWriter.WriteAttributeString('time', $Action.Duration.TotalSeconds.ToString('0.000', [System.Globalization.CultureInfo]::InvariantCulture))

$XmlWriter.WriteStartElement('properties')

foreach ($keyValuePair in $environment.GetEnumerator()) {
if ($keyValuePair.Name -eq 'nunit-version') {
continue
}

$XmlWriter.WriteStartElement('property')
$XmlWriter.WriteAttributeString('name', $keyValuePair.Name)
$XmlWriter.WriteAttributeString('value', $keyValuePair.Value)
$XmlWriter.WriteEndElement()
}

$XmlWriter.WriteEndElement()
}

function Write-JUnitTestCaseElements {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns','')]
param($TestResult, [System.Xml.XmlWriter] $XmlWriter, [string] $Package)

$XmlWriter.WriteStartElement('testcase')

Write-JUnitTestCaseAttributes -TestResult $TestResult -XmlWriter $XmlWriter -ClassName $Package

$XmlWriter.WriteEndElement()
}

function Write-JUnitTestCaseAttributes {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns','')]
param($TestResult,[System.Xml.XmlWriter] $XmlWriter, [string] $ClassName)

$XmlWriter.WriteAttributeString('name', $TestResult.ExpandedPath)

$statusElementName = switch ($TestResult.Result) {
Passed {
$null
}

Failed {
'failure'
}

default {
'skipped'
}
}

$XmlWriter.WriteAttributeString('status', $TestResult.Result)
$XmlWriter.WriteAttributeString('classname', $ClassName)
$XmlWriter.WriteAttributeString('assertions', '0')
$XmlWriter.WriteAttributeString('time', $TestResult.Duration.TotalSeconds.ToString('0.000', [System.Globalization.CultureInfo]::InvariantCulture))

if ($null -ne $statusElementName) {
Write-JUnitTestCaseMessageElements -TestResult $TestResult -XmlWriter $XmlWriter -StatusElementName $statusElementName
}
}

function Write-JUnitTestCaseMessageElements {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseSingularNouns','')]
param($TestResult,[System.Xml.XmlWriter] $XmlWriter, [string] $StatusElementName)

$XmlWriter.WriteStartElement($StatusElementName)

$result = Get-ErrorForXmlReport -TestResult $TestResult
$XmlWriter.WriteAttributeString('message', $result.FailureMessage)
$XmlWriter.WriteString($result.StackTrace)

$XmlWriter.WriteEndElement()
}

0 comments on commit 17157de

Please sign in to comment.