From 2b90ac5c9e962c795fb646542b4f4f72f51abb77 Mon Sep 17 00:00:00 2001 From: Gilbert Sanchez Date: Mon, 2 Jun 2025 19:14:49 -0700 Subject: [PATCH 1/2] =?UTF-8?q?feat(tests):=20=E2=9C=A8=20add=20example=20?= =?UTF-8?q?tests=20and=20improve=20rendering=20functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Introduced `Example.ps1` with basic test cases for validation. * Enhanced `Get-RenderedText` function with detailed parameter descriptions. * Updated `Format-PesterObjectName` to use `darkorange` for inconclusive results. * Simplified rendering options in `Get-PreviewPanel` and `Get-TitlePanel` tests. --- .../Private/Format-PesterObjectName.ps1 | 2 +- tests/Get-PreviewPanel.tests.ps1 | 18 +---- tests/Get-TitlePanel.tests.ps1 | 69 +++++++++++-------- tests/Helpers.ps1 | 50 ++++++++++++-- tests/fixtures/Example.ps1 | 25 +++++++ 5 files changed, 115 insertions(+), 49 deletions(-) create mode 100644 tests/fixtures/Example.ps1 diff --git a/PesterExplorer/Private/Format-PesterObjectName.ps1 b/PesterExplorer/Private/Format-PesterObjectName.ps1 index d1beaf7..fccf6f2 100644 --- a/PesterExplorer/Private/Format-PesterObjectName.ps1 +++ b/PesterExplorer/Private/Format-PesterObjectName.ps1 @@ -61,7 +61,7 @@ function Format-PesterObjectName { 'Passed' { 'green' } 'Failed' { 'red' } 'Skipped' { 'yellow' } - 'Inconclusive' { 'orange' } + 'Inconclusive' { 'darkorange' } default { 'white' } } $finalName = if ($NoColor) { diff --git a/tests/Get-PreviewPanel.tests.ps1 b/tests/Get-PreviewPanel.tests.ps1 index 70d8412..fdfae7c 100644 --- a/tests/Get-PreviewPanel.tests.ps1 +++ b/tests/Get-PreviewPanel.tests.ps1 @@ -15,13 +15,6 @@ Describe 'Get-PreviewPanel' { InModuleScope $env:BHProjectName { $script:ContainerWidth = 80 $script:ContainerHeight = 200 - $size = [Spectre.Console.Size]::new($containerWidth, $containerHeight) - $script:renderOptions = [Spectre.Console.Rendering.RenderOptions]::new( - [Spectre.Console.AnsiConsole]::Console.Profile.Capabilities, - $size - ) - $script:renderOptions.Justification = $null - $script:renderOptions.Height = $null $container = New-PesterContainer -Scriptblock { Describe 'Demo Tests' { Context 'Contextualize It' { @@ -69,7 +62,7 @@ Describe 'Get-PreviewPanel' { PreviewWidth = $script:ContainerWidth } $panel = Get-PreviewPanel @getPreviewPanelSplat - global:Get-RenderedText -panel $panel -renderOptions $script:renderOptions -containerWidth $script:ContainerWidth | + global:Get-RenderedText -Panel $panel | Should -BeLike "*Please select an item.*" } } @@ -78,11 +71,6 @@ Describe 'Get-PreviewPanel' { InModuleScope $env:BHProjectName { $Items = Get-ListFromObject -Object $script:run.Containers[0].Blocks[0].Order[0] $height = 5 - $size = [Spectre.Console.Size]::new(80, $height) - $renderOptions = [Spectre.Console.Rendering.RenderOptions]::new( - [Spectre.Console.AnsiConsole]::Console.Profile.Capabilities, - $size - ) $getPreviewPanelSplat = @{ Items = $Items SelectedItem = 'Test1' @@ -91,7 +79,7 @@ Describe 'Get-PreviewPanel' { PreviewWidth = $script:ContainerWidth } $panel = Get-PreviewPanel @getPreviewPanelSplat - global:Get-RenderedText -panel $panel -renderOptions $renderOptions -containerWidth $script:ContainerWidth | + global:Get-RenderedText -Panel $panel | Should -BeLike "*resize your terminal*" } } @@ -106,7 +94,7 @@ Describe 'Get-PreviewPanel' { PreviewWidth = $script:ContainerWidth } $panel = Get-PreviewPanel @getPreviewPanelSplat - global:Get-RenderedText -panel $panel -renderOptions $script:renderOptions -containerWidth $script:ContainerWidth | + global:Get-RenderedText -panel $panel | Should -BeLike '*$true | Should -Be $true*' } } diff --git a/tests/Get-TitlePanel.tests.ps1 b/tests/Get-TitlePanel.tests.ps1 index a7cfb5d..eac6fc5 100644 --- a/tests/Get-TitlePanel.tests.ps1 +++ b/tests/Get-TitlePanel.tests.ps1 @@ -1,5 +1,6 @@ Describe 'Get-TitlePanel' { BeforeAll { + . (Join-Path $PSScriptRoot 'Helpers.ps1') $manifest = Import-PowerShellDataFile -Path $env:BHPSModuleManifest $outputDir = Join-Path -Path $env:BHProjectPath -ChildPath 'Output' $outputModDir = Join-Path -Path $outputDir -ChildPath $env:BHProjectName @@ -11,16 +12,8 @@ Describe 'Get-TitlePanel' { Get-Module $env:BHProjectName | Remove-Module -Force -ErrorAction Ignore Import-Module -Name $outputModVerManifest -Verbose:$false -ErrorAction Stop - InModuleScope $env:BHProjectName { - $script:ContainerWidth = 80 - $script:ContainerHeight = 5 - $size = [Spectre.Console.Size]::new($containerWidth, $containerHeight) - $script:renderOptions = [Spectre.Console.Rendering.RenderOptions]::new( - [Spectre.Console.AnsiConsole]::Console.Profile.Capabilities, - $size - ) - $script:renderOptions.Justification = $null - $script:renderOptions.Height = $null + InModuleScope $env:BHProjectname { + $script:pesterResult = Invoke-Pester -PassThru -Path "$PSScriptRoot\fixtures\Example.ps1" -Output 'None' } } It 'should return a Spectre.Console.Panel object' { @@ -34,28 +27,48 @@ Describe 'Get-TitlePanel' { InModuleScope $env:BHProjectName { Mock -CommandName 'Get-Date' -MockWith { '2025-01-10 12:00:00' } $title = Get-TitlePanel - $render = $title.Render($script:renderOptions, $script:ContainerWidth) - # These are rendered segments. - ( - 'Pester', - 'Explorer', - '2025-01-10', - '12:00:00' - ) | ForEach-Object { - $render.Text | Should -Contain $_ - } + global:Get-RenderedText -Panel $title | + Should -Contain 'Pester Explorer - 2025-01-10 12:00:00' + } + } + + It 'should print pester run name and type' { + InModuleScope $env:BHProjectName { + $titleWithItem = Get-TitlePanel -Item $script:pesterResult + $renderWithItem = global:Get-RenderedText -Panel $titleWithItem + $renderWithItem | Should -Match 'Run: . Pester.Run' + } + } + + It 'should print container name and type' { + InModuleScope $env:BHProjectName { + $titleWithItem = Get-TitlePanel -Item $script:pesterResult.Containers[0] + $renderWithItem = global:Get-RenderedText -Panel $titleWithItem + $renderWithItem | Should -BeLike '*Container:*Example.ps1' } } - It 'should include the Pester object type and name if provided' { + It 'should print block name and type' { InModuleScope $env:BHProjectName { - $pesterBlock = [Pester.Block]::Create() - $pesterBlock.Name = 'Blockhead' - $pesterBlock.Result = 'Failed' - $titleWithItem = Get-TitlePanel -Item $pesterBlock - $renderWithItem = $titleWithItem.Render($script:renderOptions, $script:ContainerWidth) - $renderWithItem.Text | Should -Contain 'Block:' - $renderWithItem.Text | Should -Contain 'Blockhead' + $titleWithItem = Get-TitlePanel -Item $script:pesterResult.Containers[0].Blocks[0] + $renderWithItem = global:Get-RenderedText -Panel $titleWithItem + $renderWithItem | Should -Match 'Block: . Example Tests' + } + } + + Context 'Tests' { + BeforeDiscovery { + $pesterResult = Invoke-Pester -PassThru -Path "$PSScriptRoot\fixtures\Example.ps1" -Output 'None' + $tests = $pesterResult.Containers[0].Blocks[0].Tests + } + + It 'should print test name <_>' -ForEach $tests { + InModuleScope $env:BHProjectName -ArgumentList $_ -ScriptBlock { + param($test) + $titleWithItem = Get-TitlePanel -Item $test + $renderWithItem = global:Get-RenderedText -Panel $titleWithItem + $renderWithItem | Should -Match "Test: .*$($test.Name)" + } } } } diff --git a/tests/Helpers.ps1 b/tests/Helpers.ps1 index 9adda9a..888c105 100644 --- a/tests/Helpers.ps1 +++ b/tests/Helpers.ps1 @@ -1,11 +1,51 @@ - function global:Get-RenderedText { + <# + .SYNOPSIS + Returns the rendered text from a panel object. + + .DESCRIPTION + This function processes a panel object to extract and return the rendered + text. It filters out control codes and specific characters, joining the + remaining text segments into a single string. + + .PARAMETER Panel + The panel object to be processed. It should have a Render method that + returns a collection of text segments. + + .PARAMETER RenderOptions + Options to control the rendering of the panel. This is passed to the Render method of the panel. + + .PARAMETER ContainerWidth + The width of the container in which the panel is rendered. This is also passed to the Render method of the panel. + + .EXAMPLE + $panel = Get-PanelObject -Name "ExamplePanel" + $renderOptions = Get-RenderOptions -SomeOption "Value" + $containerWidth = 80 + $renderedText = global:Get-RenderedText -panel $panel -renderOptions $renderOptions -containerWidth $containerWidth + + This example retrieves a panel object, specifies rendering options and + container width, and then calls the function to get the rendered text. + .NOTES + This is a helper function we can use for our tests. + #> param ( - $panel, - $renderOptions, - $containerWidth + [Parameter(Mandatory = $true)] + #[Spectre.Console.Panel] + $Panel, + [Parameter()] + [int] + $ContainerHeight = 200, + [Parameter()] + [int] + $ContainerWidth = 100 + ) + $size = [Spectre.Console.Size]::new($ContainerWidth, $ContainerHeight) + $renderOptions = [Spectre.Console.Rendering.RenderOptions]::new( + [Spectre.Console.AnsiConsole]::Console.Profile.Capabilities, + $size ) - $render = $panel.Render($renderOptions, $ContainerWidth) + $render = $Panel.Render($RenderOptions, $ContainerWidth) # These are rendered segments. $onlyText = $render | diff --git a/tests/fixtures/Example.ps1 b/tests/fixtures/Example.ps1 new file mode 100644 index 0000000..64ee815 --- /dev/null +++ b/tests/fixtures/Example.ps1 @@ -0,0 +1,25 @@ +Describe 'Example Tests' { + BeforeAll { + $script:TestVariable = "Initial Value" + } + + It 'should pass' { + $script:TestVariable | Should -Be "Initial Value" + } + + It 'should skip this' { + Set-ItResult -Skipped 'This test is skipped intentionally.' + } + + It 'should be inconclusive' { + Set-ItResult -Inconclusive 'This test is inconclusive.' + } + + It 'should be pending' { + Set-ItResult -Pending 'This test is pending.' + } + + AfterAll { + $script:TestVariable = $null + } +} From 40e84d2a767786d8e0fb8e865ee2ddb925f5ee82 Mon Sep 17 00:00:00 2001 From: Gilbert Sanchez Date: Tue, 3 Jun 2025 07:04:07 -0700 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=F0=9F=90=9B=20update=20module=20ver?= =?UTF-8?q?sion=20to=200.3.3=20and=20document=20inconclusive=20test=20colo?= =?UTF-8?q?r=20change?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Updated `ModuleVersion` in the module manifest to `0.3.3`. * Documented that inconclusive tests are now displayed in darkorange as orange isn't a valid Spectre color. --- CHANGELOG.md | 7 +++++++ PesterExplorer/PesterExplorer.psd1 | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 437bfde..0e6999e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.3.3] 2025-06-03 + +### Fixed + +- Inconclusive tests are now darkorange because orange isn't a valid Spectre + color. + ## [0.3.2] 2025-05-31 ### Added diff --git a/PesterExplorer/PesterExplorer.psd1 b/PesterExplorer/PesterExplorer.psd1 index 246e872..cfb9bcb 100644 --- a/PesterExplorer/PesterExplorer.psd1 +++ b/PesterExplorer/PesterExplorer.psd1 @@ -12,7 +12,7 @@ RootModule = 'PesterExplorer.psm1' # Version number of this module. - ModuleVersion = '0.3.2' + ModuleVersion = '0.3.3' # Supported PSEditions # CompatiblePSEditions = @()