|
| 1 | +<# |
| 2 | +Helper test host objects and prompt transcript utilities for Pester tests. |
| 3 | +
|
| 4 | +Provides: |
| 5 | + - New-TestHostInteractive |
| 6 | + - New-TestHostNonInteractive |
| 7 | + - Start-TestPromptTranscript |
| 8 | + - Add-TestPromptEntry |
| 9 | + - Get-TestPromptTranscript |
| 10 | + - Clear-TestPromptTranscript |
| 11 | +
|
| 12 | +#> |
| 13 | + |
| 14 | +function New-TestHostInteractive { |
| 15 | + <# Creates a PSCustomObject that mimics $Host with UI.RawUI that indicates a TTY is available. #> |
| 16 | + $rawUI = [PSCustomObject]@{ |
| 17 | + KeyAvailable = $true |
| 18 | + CursorSize = 1 |
| 19 | + BackgroundColor = 'Black' |
| 20 | + ForegroundColor = 'White' |
| 21 | + WindowSize = [PSCustomObject]@{ Width = 120; Height = 30 } |
| 22 | + BufferSize = [PSCustomObject]@{ Width = 120; Height = 300 } |
| 23 | + CursorPosition = [PSCustomObject]@{ X = 0; Y = 0 } |
| 24 | + } |
| 25 | + |
| 26 | + $ui = [PSCustomObject]@{ RawUI = $rawUI } |
| 27 | + $testHostObj = [PSCustomObject]@{ Name = 'TestHost'; UI = $ui } |
| 28 | + return $testHostObj |
| 29 | +} |
| 30 | + |
| 31 | +function New-TestHostNonInteractive { |
| 32 | + <# Creates a PSCustomObject that mimics $Host without TTY support (KeyAvailable = $false). #> |
| 33 | + $rawUI = [PSCustomObject]@{ |
| 34 | + KeyAvailable = $false |
| 35 | + CursorSize = 1 |
| 36 | + BackgroundColor = 'Black' |
| 37 | + ForegroundColor = 'White' |
| 38 | + WindowSize = [PSCustomObject]@{ Width = 80; Height = 25 } |
| 39 | + BufferSize = [PSCustomObject]@{ Width = 80; Height = 200 } |
| 40 | + CursorPosition = [PSCustomObject]@{ X = 0; Y = 0 } |
| 41 | + } |
| 42 | + |
| 43 | + $ui = [PSCustomObject]@{ RawUI = $rawUI } |
| 44 | + $testHostObj = [PSCustomObject]@{ Name = 'TestHost'; UI = $ui } |
| 45 | + return $testHostObj |
| 46 | +} |
| 47 | + |
| 48 | +# Prompt transcript utilities (simple in-memory capture for tests) |
| 49 | +if (-not (Test-Path -LiteralPath variable:TestHostPromptTranscript -ErrorAction SilentlyContinue)) { |
| 50 | + Set-Variable -Name TestHostPromptTranscript -Scope Script -Value @() |
| 51 | +} |
| 52 | + |
| 53 | +function Start-TestPromptTranscript { |
| 54 | + Set-Variable -Name TestHostPromptTranscript -Scope Script -Value @() |
| 55 | +} |
| 56 | + |
| 57 | +function Add-TestPromptEntry { |
| 58 | + param( |
| 59 | + [Parameter(Mandatory=$true)] [string] $Prompt, |
| 60 | + [Parameter(Mandatory=$true)] [string] $Response |
| 61 | + ) |
| 62 | + $entry = [PSCustomObject]@{ |
| 63 | + Time = (Get-Date).ToString('o') |
| 64 | + Prompt = $Prompt |
| 65 | + Response = $Response |
| 66 | + } |
| 67 | + $script:TestHostPromptTranscript += $entry |
| 68 | +} |
| 69 | + |
| 70 | +function Get-TestPromptTranscript { |
| 71 | + return ,$script:TestHostPromptTranscript |
| 72 | +} |
| 73 | + |
| 74 | +function Clear-TestPromptTranscript { |
| 75 | + Set-Variable -Name TestHostPromptTranscript -Scope Script -Value @() |
| 76 | +} |
| 77 | + |
| 78 | +# Intentionally do not call Export-ModuleMember here so the file can be dot-sourced from tests. |
0 commit comments