From e65210e3788746ab9aa3af18a45d47dece3e3119 Mon Sep 17 00:00:00 2001 From: jhoneill Date: Mon, 30 Dec 2019 00:34:40 +0000 Subject: [PATCH] Fix test not working with single digit dates. --- Examples/Pester-To-XLSx.ps1 | 96 ++++++++++++---------- __tests__/ConvertFrom-ExcelSheet.Tests.ps1 | 2 +- 2 files changed, 53 insertions(+), 45 deletions(-) diff --git a/Examples/Pester-To-XLSx.ps1 b/Examples/Pester-To-XLSx.ps1 index c4ec038..4c65e19 100644 --- a/Examples/Pester-To-XLSx.ps1 +++ b/Examples/Pester-To-XLSx.ps1 @@ -1,5 +1,4 @@  - [CmdletBinding(DefaultParameterSetName = 'Default')] param( [Parameter(Position=0)] @@ -8,11 +7,11 @@ param( [Parameter(ParameterSetName='Default',Position=1)] [Alias('Path', 'relative_path')] [object[]]$Script = '.', - + [Parameter(ParameterSetName='Existing',Mandatory=$true)] [switch] $UseExisting, - + [Parameter(ParameterSetName='Default', Position=2)] [Parameter(ParameterSetName='Existing',Position=2, Mandatory=$true)] [string]$OutputFile, @@ -20,38 +19,39 @@ param( [Parameter(ParameterSetName='Default')] [Alias("Name")] [string[]]$TestName, - + [Parameter(ParameterSetName='Default')] [switch]$EnableExit, - + [Parameter(ParameterSetName='Default')] [Alias('Tags')] [string[]]$Tag, [string[]]$ExcludeTag, - + [Parameter(ParameterSetName='Default')] - [Switch]$Strict, + [switch]$Strict, - [String]$WorkSheetName = 'PesterResults', - - [switch]$Show + [string]$WorkSheetName = 'PesterResults', + [switch]$append, + [switch]$Show ) $InvokePesterParams = @{OutputFormat = 'NUnitXml'} + $PSBoundParameters -if (-not $InvokePesterParams['OutputFile']) { +if (-not $InvokePesterParams['OutputFile']) { $InvokePesterParams['OutputFile'] = Join-Path -ChildPath 'Pester.xml'-Path ([environment]::GetFolderPath([System.Environment+SpecialFolder]::MyDocuments)) } if ($InvokePesterParams['Show'] ) {} if ($InvokePesterParams['XLFile']) {$InvokePesterParams.Remove('XLFile')} else {$XLFile = $InvokePesterParams['OutputFile'] -replace '.xml$','.xlsx'} if (-not $UseExisting) { + $InvokePesterParams.Remove('Append') $InvokePesterParams.Remove('UseExisting') $InvokePesterParams.Remove('Show') $InvokePesterParams.Remove('WorkSheetName') Invoke-Pester @InvokePesterParams } -if (-not (Test-Path -Path $InvokePesterParams['OutputFile'])) { - throw "Could not output file $($InvokePesterParams['OutputFile'])"; return +if (-not (Test-Path -Path $InvokePesterParams['OutputFile'])) { + throw "Could not output file $($InvokePesterParams['OutputFile'])"; return } $resultXML = ([xml](Get-Content $InvokePesterParams['OutputFile'])).'test-results' @@ -59,37 +59,44 @@ $startDate = [datetime]$resultXML.date $startTime = $resultXML.time $machine = $resultXML.environment.'machine-name' #$user = $resultXML.environment.'user-domain' + '\' + $resultXML.environment.user -$os = $resultXML.environment.platform -replace '\|.*$'," $($resultXML.environment.'os-version')" -<#hierarchy goes +$os = $resultXML.environment.platform -replace '\|.*$'," $($resultXML.environment.'os-version')" +<#hierarchy goes root, [date], start [time], [Name] (always "Pester"), test results broken down as [total],[errors],[failures],[not-run] etc. - Environment (user & machine info) + Environment (user & machine info) Culture-Info (current, and currentUi culture) - Test-Suite [name] = "Pester" [result], [time] to execute, etc. + Test-Suite [name] = "Pester" [result], [time] to execute, etc. Results - Test-Suite [name] = filename,[result], [Time] to Execute etc + Test-Suite [name] = filename,[result], [Time] to Execute etc Results Test-Suite [Name] = Describe block Name, [result], [Time] to execute etc.. Results Test-Suite [Name] = Context block name [result], [Time] to execute etc. - Results - Test-Case [name] = Describe.Context.It block names [description]= it block name, result], [Time] to execute etc - + Results + Test-Case [name] = Describe.Context.It block names [description]= it block name, result], [Time] to execute etc + or if the tests are parameterized + Test suite [description] - name in the the it block with not filled in + Results + Test-case [description] - name as rendered for display with filled in #> $testResults = foreach ($test in $resultXML.'test-suite'.results.'test-suite') { - $testPs1File = $test.name + $testPs1File = $test.name #Test if there are context blocks in the hierarchy OR if we go straight from Describe to test-case if ($test.results.'test-suite'.results.'test-suite' -ne $null) { foreach ($suite in $test.results.'test-suite') { - $Describe = $suite.description + $Describe = $suite.description foreach ($subsuite in $suite.results.'test-suite') { $Context = $subsuite.description - $subsuite.results.'test-case'| ForEach-Object { + if ($subsuite.results.'test-suite'.results.'test-case') { + $testCases = $subsuite.results.'test-suite'.results.'test-case' + } + else {$testCases = $subsuite.results.'test-case'} + $testCases | ForEach-Object { New-Object -TypeName psobject -Property ([ordered]@{ Machine = $machine ; OS = $os - Date = $startDate ; Time = $startTime - Executed = $_.executed ; Result = $_.result ; Duration = $_.time + Date = $startDate ; Time = $startTime + Executed = $_.executed ; Result = $_.result ; Duration = $_.time File = $testPs1File; Group = $Describe ; SubGroup = $Context - Test =($_.Description -replace '\s{2,}', ' ') + Test =($_.Description -replace '\s{2,}', ' ') FullDesc = '=Group&" "&SubGroup&" "&Test'}) } } @@ -97,37 +104,38 @@ $testResults = foreach ($test in $resultXML.'test-suite'.results.'test-suite') { } else { $test.results.'test-suite' | ForEach-Object { - $Describe = $_.description + $Describe = $_.description $_.results.'test-case'| ForEach-Object { New-Object -TypeName psobject -Property ([ordered]@{ Machine = $machine ; OS = $os - Date = $startDate ; Time = $startTime - Executed = $_.executed ; Result = $_.result ; Duration = $_.time - File = $testPs1File; Group = $Describe ; SubGroup = $null + Date = $startDate ; Time = $startTime + Executed = $_.executed ; Result = $_.result ; Duration = $_.time + File = $testPs1File; Group = $Describe ; SubGroup = $null Test =($_.Description -replace '\s{2,}', ' ') FullDesc = '=Group&" "&Test'}) } } } } +if (-not $testResults) {Write-Warning 'No Results found' ; return} +$clearSheet = -not $Append +$excel = $testResults | Export-Excel -Path $xlFile -WorkSheetname $WorkSheetName -ClearSheet:$clearSheet -Append:$append -PassThru -BoldTopRow -FreezeTopRow -AutoSize -AutoFilter -AutoNameRange +$ws = $excel.Workbook.Worksheets[$WorkSheetName] -$excel = $testResults | Export-Excel -Path $xlFile -WorkSheetname $WorkSheetName -ClearSheet -PassThru -BoldTopRow -FreezeTopRow -AutoSize -AutoFilter -AutoNameRange -$ws = $excel.Workbook.Worksheets["PesterResults"] - -<# Worksheet should look like ... - |A |B |C D |E |F |G |H |I |J |K |L - 1|Machine |OS |Date Time |Executed |Result |Duration |File |Group |SubGroup |Test |FullDescription - 2|Flatfish |Name_Version |[run started] |Boolean |Success |In seconds |xx.ps1 |Describe |Context |It |Desc_Context_It -#> +<# Worksheet should look like ... + |A |B |C D |E |F |G |H |I |J |K |L + 1|Machine |OS |Date Time |Executed |Result |Duration |File |Group |SubGroup |Test |FullDescription + 2|Flatfish |Name_Version |[run started] |Boolean |Success |In seconds |xx.ps1 |Describe |Context |It |Desc_Context_It +#> #Display Date as a date, not a date time Set-Column -Worksheet $ws -Column 3 -NumberFormat 'Short Date' # -AutoSize -#Hide columns G to K (the file and the parts of the description, and the duration)5 -(7..10) + 5 | ForEach-Object {Set-ExcelColumn -Worksheet $ws -Column $_ -Hide } +#Hide columns G to K (the file and the parts of the description, and the duration)5 +(7..10) + 5 | ForEach-Object {Set-ExcelColumn -Worksheet $ws -Column $_ -Hide } -#Use conditional formatting to make Failures red, and Successes green (skipped remains black ) ... and save +#Use conditional formatting to make Failures red, and Successes green (skipped remains black ) ... and save $endRow = $ws.Dimension.End.Row -Add-ConditionalFormatting -WorkSheet $ws -range "f2:f$endrow" -RuleType ContainsText -ConditionValue "Failure" -BackgroundPattern None -ForegroundColor Red -Bold -Add-ConditionalFormatting -WorkSheet $ws -range "f2:f$endRow" -RuleType ContainsText -ConditionValue "Success" -BackgroundPattern None -ForeGroundColor Green +Add-ConditionalFormatting -WorkSheet $ws -range "f2:f$endrow" -RuleType ContainsText -ConditionValue "Failure" -BackgroundPattern None -ForegroundColor Red -Bold +Add-ConditionalFormatting -WorkSheet $ws -range "f2:f$endRow" -RuleType ContainsText -ConditionValue "Success" -BackgroundPattern None -ForeGroundColor Green Close-ExcelPackage -ExcelPackage $excel -Show:$show diff --git a/__tests__/ConvertFrom-ExcelSheet.Tests.ps1 b/__tests__/ConvertFrom-ExcelSheet.Tests.ps1 index 3702d87..127882e 100644 --- a/__tests__/ConvertFrom-ExcelSheet.Tests.ps1 +++ b/__tests__/ConvertFrom-ExcelSheet.Tests.ps1 @@ -29,7 +29,7 @@ Describe 'ConvertFrom-ExcelSheet / Export-ExcelSheet' { $secondText[1] | Should -Belike "*$date*" $secondText[1] | Should -Match '"0\d","\w+","\d{1,2}"$' $ThirdText[1] | Should -Match '^"\w+ \w+","#\d\d/\d\d/\d{4}#","\d","0\d"$' - $FourthText[1] | Should -Match '^"\w+","[012]\d' + $FourthText[1] | Should -Match '^"\w+","([012]\d/|[1-9]/)' } } Context "Export aliased to ConvertFrom" {