added boolean conversion helper and replaced code in scripts to use it
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
#Requires -Version 5.0
|
||||
# Offline tests: load only the helper to avoid Tools.ps1 setup and logging.
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$BooleanToolsPath = Join-Path (Split-Path $PSScriptRoot -Parent) 'Tools.ps1'
|
||||
$BooleanTokens = $null
|
||||
$BooleanParseErrors = $null
|
||||
$BooleanAst = [Management.Automation.Language.Parser]::ParseFile($BooleanToolsPath, [ref]$BooleanTokens, [ref]$BooleanParseErrors)
|
||||
If ($BooleanParseErrors.Count) { Throw ($BooleanParseErrors | Out-String) }
|
||||
$BooleanDefinition = $BooleanAst.Find({
|
||||
param($Node)
|
||||
$Node -is [Management.Automation.Language.FunctionDefinitionAst] -and $Node.Name -eq 'ConvertTo-RmmBoolean'
|
||||
}, $true)
|
||||
If (!$BooleanDefinition) { Throw 'Missing helper: ConvertTo-RmmBoolean' }
|
||||
. ([scriptblock]::Create($BooleanDefinition.Extent.Text))
|
||||
|
||||
# Tools.ps1 overrides Write-Output with a string parameter. Boolean results must
|
||||
# bypass that wrapper so $false remains a Boolean, without log text or side effects.
|
||||
Function Write-Output { Throw 'Conversion must not use the string-based logging wrapper.' }
|
||||
|
||||
$BooleanCases = @(
|
||||
@{Values=@('true', '$true', 'yes', 'y', '1', $true, 1); Expected=$true}
|
||||
@{Values=@('false', '$false', 'no', 'n', '0', '', $null, $false, 0); Expected=$false}
|
||||
)
|
||||
Foreach ($Case in $BooleanCases) {
|
||||
Foreach ($Value in $Case.Values) {
|
||||
$Variants = @($Value)
|
||||
If ($Value -is [string]) { $Variants += " `t$($Value.ToUpperInvariant())`r`n " }
|
||||
Foreach ($Variant in $Variants) {
|
||||
$Result = @(ConvertTo-RmmBoolean -Value $Variant)
|
||||
If ($Result.Count -ne 1 -or $Result[0] -isnot [bool] -or $Result[0] -ne $Case.Expected) {
|
||||
Throw "Input '$Variant' must return exactly one Boolean with value $($Case.Expected)."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Write-Host 'PASS: accepted text, case, whitespace, null, native booleans, and numeric 0/1 return one Boolean.'
|
||||
|
||||
$RmmInput = ' no '
|
||||
$Result = ConvertTo-RmmBoolean $RmmInput
|
||||
If ($Result -isnot [bool] -or $Result -ne $false -or $RmmInput -cne ' no ') {
|
||||
Throw 'Positional conversion must return false without changing the original input.'
|
||||
}
|
||||
$Result = @(' yes ', 'false', '', $null, '$TRUE', '0' | ConvertTo-RmmBoolean)
|
||||
$Expected = @($true, $false, $false, $false, $true, $false)
|
||||
If ($Result.Count -ne $Expected.Count) { Throw 'Pipeline conversion must return one value for every input.' }
|
||||
For ($Index = 0; $Index -lt $Expected.Count; $Index++) {
|
||||
If ($Result[$Index] -isnot [bool] -or $Result[$Index] -ne $Expected[$Index]) {
|
||||
Throw "Pipeline conversion returned the wrong type or value at index $Index."
|
||||
}
|
||||
}
|
||||
Write-Host 'PASS: positional input, preserved caller value, and multiple pipeline inputs.'
|
||||
|
||||
# RMM scripts may use Continue and omit -ErrorAction. Invalid input must still stop.
|
||||
$ErrorActionPreference = 'Continue'
|
||||
Foreach ($Invalid in @('maybe', 'on', 'off', '2', 'true false', '$null')) {
|
||||
Foreach ($Parameters in @(@{}, @{VariableName='Restart'})) {
|
||||
$Caught = $null
|
||||
$Returned = @()
|
||||
Try { ConvertTo-RmmBoolean -Value $Invalid @Parameters | ForEach-Object { $Returned += $_ } }
|
||||
Catch { $Caught = $_ }
|
||||
$Name = If ($Parameters.ContainsKey('VariableName')) { 'Restart' } Else { 'Value' }
|
||||
If ($null -eq $Caught -or !$Caught.Exception.Message.StartsWith(('Invalid ${0} value.' -f $Name))) {
|
||||
Throw "Invalid input '$Invalid' must throw an error identifying $Name."
|
||||
}
|
||||
If ($Returned.Count -ne 0) { Throw 'Invalid input must not return a value.' }
|
||||
}
|
||||
}
|
||||
Write-Host 'PASS: invalid input throws with default or caller-supplied variable name.'
|
||||
Write-Host "All ConvertTo-RmmBoolean tests passed on PowerShell $($PSVersionTable.PSVersion), $([IntPtr]::Size * 8)-bit."
|
||||
@@ -8,6 +8,17 @@ $DsaErrors = $null
|
||||
$null = [Management.Automation.Language.Parser]::ParseFile($DsaScriptUnderTest, [ref]$DsaTokens, [ref]$DsaErrors)
|
||||
If ($DsaErrors.Count) { Throw ($DsaErrors | Out-String) }
|
||||
|
||||
# Load only the real conversion helper, without running Tools.ps1 setup.
|
||||
$DsaToolsPath = Join-Path (Split-Path $PSScriptRoot -Parent) 'Tools.ps1'
|
||||
$DsaToolsAst = [Management.Automation.Language.Parser]::ParseFile($DsaToolsPath, [ref]$DsaTokens, [ref]$DsaErrors)
|
||||
If ($DsaErrors.Count) { Throw ($DsaErrors | Out-String) }
|
||||
$DsaBooleanDefinition = $DsaToolsAst.Find({
|
||||
param($Node)
|
||||
$Node -is [Management.Automation.Language.FunctionDefinitionAst] -and $Node.Name -eq 'ConvertTo-RmmBoolean'
|
||||
}, $true)
|
||||
If (!$DsaBooleanDefinition) { Throw 'Missing helper: ConvertTo-RmmBoolean' }
|
||||
. ([scriptblock]::Create($DsaBooleanDefinition.Extent.Text))
|
||||
|
||||
Function Assert-Dsa { param([bool]$Condition, [string]$Description) If (!$Condition) { Throw $Description } }
|
||||
Function Download-File { Throw 'DSA must use Download-FileDirectly.' }
|
||||
Function Download-FileDirectly {
|
||||
|
||||
Reference in New Issue
Block a user