config-central/tests/Output.Tests.ps1

48 lines
2.5 KiB
PowerShell

BeforeAll {
$script:entrypoints = @('install.ps1', 'bootstrap.ps1', 'update-configs.ps1', 'cc.ps1', 'tools/TemporarySsh.ps1', 'tools/Export-MpcHcSettings.ps1')
$script:formatters = foreach ($entry in $entrypoints) {
$tokens = $null; $errors = $null
$ast = [System.Management.Automation.Language.Parser]::ParseFile("$PSScriptRoot/../$entry", [ref]$tokens, [ref]$errors)
$fn = $ast.Find({ param($n) $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq 'Write-ConfigCentralMessage' }, $true)
if (-not $fn) { throw "Missing message formatter in $entry" }
$fn.Extent.Text
}
. ([scriptblock]::Create($formatters[0]))
}
Describe 'Consistent standalone output' {
It 'uses the same dependency-free formatter in every entrypoint' {
foreach ($formatter in $formatters) { $formatter | Should -BeExactly $formatters[0] }
}
It 'honors NO_COLOR for human status output' {
$previous = $env:NO_COLOR
try {
$env:NO_COLOR = '1'
Mock Write-Host {}
Write-ConfigCentralMessage -Level Warning -Message 'Try again'
Should -Invoke Write-Host -Times 1 -ParameterFilter { $Object -eq 'Try again' -and -not $PSBoundParameters.ContainsKey('ForegroundColor') }
}
finally { $env:NO_COLOR = $previous }
}
It 'writes errors to stderr without creating a second exception' {
$previous = [Console]::Error
$previousColor = $env:NO_COLOR
$capture = [IO.StringWriter]::new()
try {
$env:NO_COLOR = '1'
[Console]::SetError($capture)
{ Write-ConfigCentralMessage -Level Error -Message 'Could not read settings' } | Should -Not -Throw
$capture.ToString().Trim() | Should -BeExactly 'Could not read settings'
}
finally { [Console]::SetError($previous); $env:NO_COLOR = $previousColor; $capture.Dispose() }
}
It 'reports unsupported-platform or missing-registry errors cleanly as file entrypoints' -Skip:$IsWindows {
foreach ($entry in @('install.ps1', 'update-configs.ps1', 'tools/TemporarySsh.ps1', 'tools/Export-MpcHcSettings.ps1')) {
$output = & (Join-Path $PSHOME 'pwsh') -NoProfile -File "$PSScriptRoot/../$entry" 2>&1
$LASTEXITCODE | Should -BeGreaterThan 0
($output | Out-String) | Should -Match 'could not complete'
($output | Out-String) | Should -Not -Match 'CategoryInfo|FullyQualifiedErrorId|Write-Error:'
}
}
}