fix: read boolean deployment values from a config file as booleans (#833)

This commit is contained in:
HetCreep
2026-10-18 03:21:50 +02:00
committed by GitHub
parent bcf44718f1
commit 94dd603da4
4 changed files with 406 additions and 14 deletions
+15 -11
View File
@@ -305,9 +305,11 @@ function Get-DeploymentCategoryDetailString {
} }
$options = @() $options = @()
if ($lookup.ContainsKey('CreateRestorePoint') -and [bool]$lookup['CreateRestorePoint']) { $options += Get-Translation -Key 'ImportExportOptionRestorePoint' } if ((ConvertTo-ConfigBoolean $lookup['CreateRestorePoint']) -eq $true) { $options += Get-Translation -Key 'ImportExportOptionRestorePoint' }
if (-not ($lookup.ContainsKey('SkipRegistryBackup') -and [bool]$lookup['SkipRegistryBackup'])) { $options += Get-Translation -Key 'ImportExportOptionRegistryBackup' } # Registry backup is on unless it is skipped; a value that is not a boolean is ignored by the import, so it is not listed
if ($lookup.ContainsKey('RestartExplorer') -and [bool]$lookup['RestartExplorer']) { $options += Get-Translation -Key 'ImportExportOptionRestartExplorer' } $skipRegistryBackup = ConvertTo-ConfigBoolean $lookup['SkipRegistryBackup']
if ($skipRegistryBackup -eq $false -or -not $lookup.ContainsKey('SkipRegistryBackup')) { $options += Get-Translation -Key 'ImportExportOptionRegistryBackup' }
if ((ConvertTo-ConfigBoolean $lookup['RestartExplorer']) -eq $true) { $options += Get-Translation -Key 'ImportExportOptionRestartExplorer' }
$lines = @() $lines = @()
if ($line1.Count -gt 0) { $lines += $line1 -join ', ' } if ($line1.Count -gt 0) { $lines += $line1 -join ', ' }
@@ -413,21 +415,23 @@ function Set-ImportedDeploymentSettings {
$appRemovalScopeCombo.SelectedIndex = [int]$lookup['AppRemovalScopeIndex'] $appRemovalScopeCombo.SelectedIndex = [int]$lookup['AppRemovalScopeIndex']
} }
# A value that is not a boolean (see ConvertTo-ConfigBoolean) leaves the checkbox as it is, after a warning
$restorePointCheckBox = $Owner.FindName('RestorePointCheckBox') $restorePointCheckBox = $Owner.FindName('RestorePointCheckBox')
if ($lookup.ContainsKey('CreateRestorePoint') -and $restorePointCheckBox) { $createRestorePoint = Get-ConfigDeploymentFlag -Lookup $lookup -Name 'CreateRestorePoint'
$restorePointCheckBox.IsChecked = [bool]$lookup['CreateRestorePoint'] if ($null -ne $createRestorePoint -and $restorePointCheckBox) {
$restorePointCheckBox.IsChecked = $createRestorePoint
} }
$registryBackupCheckBox = $Owner.FindName('RegistryBackupCheckBox') $registryBackupCheckBox = $Owner.FindName('RegistryBackupCheckBox')
if ($registryBackupCheckBox) { $skipRegistryBackup = Get-ConfigDeploymentFlag -Lookup $lookup -Name 'SkipRegistryBackup'
if ($lookup.ContainsKey('SkipRegistryBackup')) { if ($null -ne $skipRegistryBackup -and $registryBackupCheckBox) {
$registryBackupCheckBox.IsChecked = -not [bool]$lookup['SkipRegistryBackup'] $registryBackupCheckBox.IsChecked = -not $skipRegistryBackup
}
} }
$restartExplorerCheckBox = $Owner.FindName('RestartExplorerCheckBox') $restartExplorerCheckBox = $Owner.FindName('RestartExplorerCheckBox')
if ($lookup.ContainsKey('RestartExplorer') -and $restartExplorerCheckBox) { $restartExplorer = Get-ConfigDeploymentFlag -Lookup $lookup -Name 'RestartExplorer'
$restartExplorerCheckBox.IsChecked = [bool]$lookup['RestartExplorer'] if ($null -ne $restartExplorer -and $restartExplorerCheckBox) {
$restartExplorerCheckBox.IsChecked = $restartExplorer
} }
} }
+59 -3
View File
@@ -1,3 +1,53 @@
<#
.SYNOPSIS
Reads a boolean deployment value from a configuration file.
.DESCRIPTION
Returns $true or $false for a JSON boolean, for the text true or false (any case) and for the numbers 1 and 0
in any JSON notation (1, 1.0 and 1e0 are read as different number types by ConvertFrom-Json).
Returns $null for anything else. A plain [bool] cast cannot be used because it turns any non-empty string,
such as "false", into $true.
#>
function ConvertTo-ConfigBoolean {
param($Value)
if ($Value -is [bool]) { return $Value }
if ($Value -is [string]) {
$text = $Value.Trim()
if ($text -ieq 'true') { return $true }
if ($text -ieq 'false') { return $false }
return $null
}
if ($Value -is [int] -or $Value -is [long] -or $Value -is [decimal] -or $Value -is [double]) {
if ($Value -eq 1) { return $true }
if ($Value -eq 0) { return $false }
}
return $null
}
<#
.SYNOPSIS
Reads one boolean deployment setting from the deployment values of a configuration file.
.DESCRIPTION
Returns $null when the setting is missing. A setting that is present but is not a boolean (see ConvertTo-ConfigBoolean)
also gives $null, after a warning that names the setting and the value, so it is never silently dropped.
#>
function Get-ConfigDeploymentFlag {
param(
[hashtable]$Lookup,
[string]$Name
)
if (-not $Lookup.ContainsKey($Name)) { return $null }
$flagValue = ConvertTo-ConfigBoolean $Lookup[$Name]
if ($null -eq $flagValue) {
Write-Warning "Ignoring deployment setting '$Name' in the config file: expected true or false, found '$($Lookup[$Name])'."
}
return $flagValue
}
<# <#
.SYNOPSIS .SYNOPSIS
Imports valid application, tweak, and deployment selections from a configuration JSON file into active parameters. Imports valid application, tweak, and deployment selections from a configuration JSON file into active parameters.
@@ -81,17 +131,23 @@ function Import-ConfigToParams {
} }
} }
if ($deploymentLookup.ContainsKey('CreateRestorePoint') -and [bool]$deploymentLookup['CreateRestorePoint']) { $deploymentFlags = @{}
foreach ($flagName in 'CreateRestorePoint', 'SkipRegistryBackup', 'RestartExplorer') {
$flagValue = Get-ConfigDeploymentFlag -Lookup $deploymentLookup -Name $flagName
if ($null -ne $flagValue) { $deploymentFlags[$flagName] = $flagValue }
}
if ($deploymentFlags['CreateRestorePoint'] -eq $true) {
Add-Parameter 'CreateRestorePoint' Add-Parameter 'CreateRestorePoint'
$importedItems++ $importedItems++
} }
if ($deploymentLookup.ContainsKey('SkipRegistryBackup') -and [bool]$deploymentLookup['SkipRegistryBackup']) { if ($deploymentFlags['SkipRegistryBackup'] -eq $true) {
Add-Parameter 'SkipRegistryBackup' Add-Parameter 'SkipRegistryBackup'
$importedItems++ $importedItems++
} }
if ($deploymentLookup.ContainsKey('RestartExplorer') -and -not [bool]$deploymentLookup['RestartExplorer']) { if ($deploymentFlags['RestartExplorer'] -eq $false) {
Add-Parameter 'SkipExplorerRestart' Add-Parameter 'SkipExplorerRestart'
$importedItems++ $importedItems++
} }
+142
View File
@@ -168,3 +168,145 @@ Describe 'Test-ConfigConsistency' {
Test-ConfigConsistency -Config $config | Should -Match "requires an 'OtherUsername' value" Test-ConfigConsistency -Config $config | Should -Match "requires an 'OtherUsername' value"
} }
} }
Describe 'ConvertTo-ConfigBoolean' {
It 'returns <Expected> for the <Type> value <Text>' -ForEach @(
@{ Type = 'boolean'; Text = 'true'; Value = $true; Expected = $true }
@{ Type = 'boolean'; Text = 'false'; Value = $false; Expected = $false }
@{ Type = 'string'; Text = 'true'; Value = 'true'; Expected = $true }
@{ Type = 'string'; Text = 'True'; Value = 'True'; Expected = $true }
@{ Type = 'string'; Text = 'false'; Value = 'false'; Expected = $false }
@{ Type = 'string'; Text = 'FALSE'; Value = 'FALSE'; Expected = $false }
@{ Type = 'string'; Text = 'false with spaces'; Value = ' false '; Expected = $false }
@{ Type = 'number'; Text = '1'; Value = 1; Expected = $true }
@{ Type = 'number'; Text = '0'; Value = 0; Expected = $false }
@{ Type = 'decimal'; Text = '1.0'; Value = [decimal]1.0; Expected = $true }
@{ Type = 'decimal'; Text = '0.0'; Value = [decimal]0.0; Expected = $false }
@{ Type = 'double'; Text = '1e0'; Value = [double]1; Expected = $true }
@{ Type = 'double'; Text = '0e0'; Value = [double]0; Expected = $false }
) {
ConvertTo-ConfigBoolean $Value | Should -Be $Expected
}
It 'returns nothing for the <Type> value <Text>' -ForEach @(
@{ Type = 'string'; Text = '0'; Value = '0' }
@{ Type = 'string'; Text = 'yes'; Value = 'yes' }
@{ Type = 'string'; Text = 'empty'; Value = '' }
@{ Type = 'number'; Text = '2'; Value = 2 }
@{ Type = 'decimal'; Text = '1.5'; Value = [decimal]1.5 }
@{ Type = 'double'; Text = '0.5'; Value = [double]0.5 }
@{ Type = 'null'; Text = 'null'; Value = $null }
@{ Type = 'array'; Text = 'an array'; Value = @($true) }
) {
ConvertTo-ConfigBoolean $Value | Should -BeNullOrEmpty
}
}
Describe 'Import-ConfigToParams deployment flags' {
BeforeEach {
$script:Params = @{}
$script:ModernStandbySupported = $false
# A config that imports nothing is rejected, so every config below also selects one real tweak.
$script:Features = @{ DisableTelemetry = [PSCustomObject]@{ FeatureId = 'DisableTelemetry'; MinVersion = $null; MaxVersion = $null } }
$warnings = [System.Collections.Generic.List[string]]::new()
Mock Write-Warning { $warnings.Add([string]$Message) }
}
BeforeAll {
# Writes a config with the given JSON text for the three deployment flags and imports it.
function Import-DeploymentFlags {
param([string]$CreateRestorePoint, [string]$SkipRegistryBackup, [string]$RestartExplorer)
$entries = @()
if ($CreateRestorePoint) { $entries += '{ "Name": "CreateRestorePoint", "Value": ' + $CreateRestorePoint + ' }' }
if ($SkipRegistryBackup) { $entries += '{ "Name": "SkipRegistryBackup", "Value": ' + $SkipRegistryBackup + ' }' }
if ($RestartExplorer) { $entries += '{ "Name": "RestartExplorer", "Value": ' + $RestartExplorer + ' }' }
$path = Join-Path $TestDrive 'flags.json'
Set-Content -LiteralPath $path -Value ('{ "Version": "1.0", "Tweaks": [ { "Name": "DisableTelemetry", "Value": true } ], "Deployment": [ ' + ($entries -join ', ') + ' ] }') -Encoding UTF8
Import-ConfigToParams -ConfigPath $path -CurrentBuild 22631 | Out-Null
}
}
It 'does not turn on <Flag> when the value is the text false' -ForEach @(
@{ Flag = 'CreateRestorePoint'; Arguments = @{ CreateRestorePoint = '"false"' } }
@{ Flag = 'SkipRegistryBackup'; Arguments = @{ SkipRegistryBackup = '"false"' } }
@{ Flag = 'SkipRegistryBackup'; Arguments = @{ SkipRegistryBackup = '"False"' } }
) {
Import-DeploymentFlags @Arguments
$script:Params.ContainsKey($Flag) | Should -BeFalse
$warnings | Should -HaveCount 0
}
It 'turns on <Flag> for the text true and for a real boolean' -ForEach @(
@{ Flag = 'CreateRestorePoint'; Arguments = @{ CreateRestorePoint = '"true"' } }
@{ Flag = 'CreateRestorePoint'; Arguments = @{ CreateRestorePoint = 'true' } }
@{ Flag = 'CreateRestorePoint'; Arguments = @{ CreateRestorePoint = '1' } }
@{ Flag = 'CreateRestorePoint'; Arguments = @{ CreateRestorePoint = '1.0' } }
@{ Flag = 'SkipRegistryBackup'; Arguments = @{ SkipRegistryBackup = '1e0' } }
@{ Flag = 'SkipRegistryBackup'; Arguments = @{ SkipRegistryBackup = '"True"' } }
@{ Flag = 'SkipRegistryBackup'; Arguments = @{ SkipRegistryBackup = 'true' } }
) {
Import-DeploymentFlags @Arguments
$script:Params[$Flag] | Should -BeTrue
}
It 'skips the Explorer restart only when RestartExplorer is false (<Value>)' -ForEach @(
@{ Value = 'false'; Expected = $true }
@{ Value = '"false"'; Expected = $true }
@{ Value = '"False"'; Expected = $true }
@{ Value = '0'; Expected = $true }
@{ Value = '0.0'; Expected = $true }
@{ Value = '0e0'; Expected = $true }
@{ Value = '1.0'; Expected = $false }
@{ Value = 'true'; Expected = $false }
@{ Value = '"true"'; Expected = $false }
) {
Import-DeploymentFlags -RestartExplorer $Value
$script:Params.ContainsKey('SkipExplorerRestart') | Should -Be $Expected
}
It 'ignores an unrecognised <Flag> value and says so' -ForEach @(
@{ Flag = 'CreateRestorePoint'; Arguments = @{ CreateRestorePoint = '"yes"' } }
@{ Flag = 'SkipRegistryBackup'; Arguments = @{ SkipRegistryBackup = '"0"' } }
@{ Flag = 'RestartExplorer'; Arguments = @{ RestartExplorer = '"no"' } }
) {
Import-DeploymentFlags @Arguments
$script:Params.Keys | Where-Object { $_ -in 'CreateRestorePoint', 'SkipRegistryBackup', 'SkipExplorerRestart' } | Should -HaveCount 0
$warnings | Should -HaveCount 1
$warnings[0] | Should -BeLike "Ignoring deployment setting '$Flag'*expected true or false*"
}
}
Describe 'Get-ConfigDeploymentFlag' {
BeforeEach {
$warnings = [System.Collections.Generic.List[string]]::new()
Mock Write-Warning { $warnings.Add([string]$Message) }
}
It 'returns nothing and does not warn when the setting is missing' {
Get-ConfigDeploymentFlag -Lookup @{ Other = $true } -Name 'SkipRegistryBackup' | Should -BeNullOrEmpty
$warnings | Should -HaveCount 0
}
It 'returns <Expected> for the value <Text> without a warning' -ForEach @(
@{ Text = 'true'; Value = $true; Expected = $true }
@{ Text = 'false'; Value = $false; Expected = $false }
@{ Text = '"false"'; Value = 'false'; Expected = $false }
@{ Text = '1.0'; Value = [decimal]1.0; Expected = $true }
) {
Get-ConfigDeploymentFlag -Lookup @{ SkipRegistryBackup = $Value } -Name 'SkipRegistryBackup' | Should -Be $Expected
$warnings | Should -HaveCount 0
}
It 'returns nothing and warns with the name and the value for an unsupported value' {
Get-ConfigDeploymentFlag -Lookup @{ SkipRegistryBackup = 'yes' } -Name 'SkipRegistryBackup' | Should -BeNullOrEmpty
$warnings | Should -HaveCount 1
$warnings[0] | Should -BeLike "Ignoring deployment setting 'SkipRegistryBackup'*expected true or false, found 'yes'*"
}
}
@@ -0,0 +1,190 @@
BeforeAll {
Add-Type -AssemblyName PresentationFramework
. (Join-Path $PSScriptRoot '..\Scripts\Helpers\Import-ConfigToParams.ps1')
. (Join-Path $PSScriptRoot '..\Scripts\GUI\Show-ImportExportConfigWindow.ps1')
# Returns the key, followed by the format arguments, so tests can see which options a description lists.
function Get-Translation {
param($Key, $FormatArgs)
if ($FormatArgs) { return "$Key $($FormatArgs -join '|')" }
return $Key
}
function New-DeploymentTestControls {
$window = New-Object System.Windows.Window
[System.Windows.NameScope]::SetNameScope($window, [System.Windows.NameScope]::new())
$controls = @{ Window = $window }
# Start from the opposite of what the tests expect to be written, so a control that was left alone shows up.
foreach ($entry in @(
@{ Name = 'RestorePointCheckBox'; Initial = $true }
@{ Name = 'RegistryBackupCheckBox'; Initial = $false }
@{ Name = 'RestartExplorerCheckBox'; Initial = $true }
)) {
$checkBox = New-Object System.Windows.Controls.CheckBox
$checkBox.IsChecked = $entry.Initial
$window.RegisterName($entry.Name, $checkBox)
$controls[$entry.Name] = $checkBox
}
$combo = New-Object System.Windows.Controls.ComboBox
'Current User', 'Other User', 'Windows Default User (Sysprep)' | ForEach-Object {
$combo.Items.Add((New-Object System.Windows.Controls.ComboBoxItem -Property @{ Content = $_ })) | Out-Null
}
$combo.SelectedIndex = 0
$controls['UserCombo'] = $combo
$controls['OtherUserBox'] = New-Object System.Windows.Controls.TextBox
return $controls
}
function Invoke-ImportedDeploymentSettings {
param($Controls, [hashtable]$Values)
$settings = @($Values.GetEnumerator() | ForEach-Object { [PSCustomObject]@{ Name = $_.Key; Value = $_.Value } })
Set-ImportedDeploymentSettings -Owner $Controls.Window -UserSelectionCombo $Controls.UserCombo `
-OtherUsernameTextBox $Controls.OtherUserBox -DeploymentSettings $settings
}
}
Describe 'Set-ImportedDeploymentSettings' {
BeforeEach {
$warnings = [System.Collections.Generic.List[string]]::new()
Mock Write-Warning { $warnings.Add([string]$Message) }
}
It 'checks the boxes for real booleans' {
$controls = New-DeploymentTestControls
$controls.RestorePointCheckBox.IsChecked = $false
$controls.RegistryBackupCheckBox.IsChecked = $true
$controls.RestartExplorerCheckBox.IsChecked = $false
Invoke-ImportedDeploymentSettings -Controls $controls -Values @{ CreateRestorePoint = $true; SkipRegistryBackup = $false; RestartExplorer = $true }
$controls.RestorePointCheckBox.IsChecked | Should -BeTrue
$controls.RegistryBackupCheckBox.IsChecked | Should -BeTrue
$controls.RestartExplorerCheckBox.IsChecked | Should -BeTrue
}
It 'reads the text false as false, not as true' {
$controls = New-DeploymentTestControls
Invoke-ImportedDeploymentSettings -Controls $controls -Values @{ CreateRestorePoint = 'false'; SkipRegistryBackup = 'false'; RestartExplorer = 'False' }
$controls.RestorePointCheckBox.IsChecked | Should -BeFalse
$controls.RegistryBackupCheckBox.IsChecked | Should -BeTrue
$controls.RestartExplorerCheckBox.IsChecked | Should -BeFalse
}
It 'reads the text true as true' {
$controls = New-DeploymentTestControls
$controls.RestorePointCheckBox.IsChecked = $false
$controls.RegistryBackupCheckBox.IsChecked = $true
$controls.RestartExplorerCheckBox.IsChecked = $false
Invoke-ImportedDeploymentSettings -Controls $controls -Values @{ CreateRestorePoint = 'true'; SkipRegistryBackup = 'true'; RestartExplorer = 'TRUE' }
$controls.RestorePointCheckBox.IsChecked | Should -BeTrue
$controls.RegistryBackupCheckBox.IsChecked | Should -BeFalse
$controls.RestartExplorerCheckBox.IsChecked | Should -BeTrue
}
It 'leaves a box alone when the value is not a boolean (<Text>)' -ForEach @(
@{ Text = 'the text yes'; Value = 'yes' }
@{ Text = 'the text 0'; Value = '0' }
@{ Text = 'the number 2'; Value = 2 }
) {
$controls = New-DeploymentTestControls
Invoke-ImportedDeploymentSettings -Controls $controls -Values @{ CreateRestorePoint = $Value; SkipRegistryBackup = $Value; RestartExplorer = $Value }
$controls.RestorePointCheckBox.IsChecked | Should -BeTrue
$controls.RegistryBackupCheckBox.IsChecked | Should -BeFalse
$controls.RestartExplorerCheckBox.IsChecked | Should -BeTrue
$warnings | Should -HaveCount 3
foreach ($name in 'CreateRestorePoint', 'SkipRegistryBackup', 'RestartExplorer') {
$warnings | Where-Object { $_ -like "*'$name'*expected true or false, found '$Value'*" } | Should -HaveCount 1
}
}
It 'leaves a box alone when its setting is missing' {
$controls = New-DeploymentTestControls
Invoke-ImportedDeploymentSettings -Controls $controls -Values @{ UserSelectionIndex = 1 }
$controls.UserCombo.SelectedIndex | Should -Be 1
$controls.RestorePointCheckBox.IsChecked | Should -BeTrue
$controls.RegistryBackupCheckBox.IsChecked | Should -BeFalse
$controls.RestartExplorerCheckBox.IsChecked | Should -BeTrue
$warnings | Should -HaveCount 0
}
}
Describe 'Get-DeploymentCategoryDetailString' {
BeforeEach {
$warnings = [System.Collections.Generic.List[string]]::new()
Mock Write-Warning { $warnings.Add([string]$Message) }
}
It 'lists the options a config really turns on when values are written as text' {
$settings = @(
[PSCustomObject]@{ Name = 'CreateRestorePoint'; Value = 'false' }
[PSCustomObject]@{ Name = 'SkipRegistryBackup'; Value = 'false' }
[PSCustomObject]@{ Name = 'RestartExplorer'; Value = 'false' }
)
$text = Get-DeploymentCategoryDetailString -DeploymentSettings $settings
$text | Should -Match 'ImportExportOptionRegistryBackup'
$text | Should -Not -Match 'ImportExportOptionRestorePoint'
$text | Should -Not -Match 'ImportExportOptionRestartExplorer'
}
It 'lists every option for real booleans and the text true' -ForEach @(
@{ Create = $true; Skip = $false; Restart = $true }
@{ Create = 'true'; Skip = 'false'; Restart = 'True' }
) {
$settings = @(
[PSCustomObject]@{ Name = 'CreateRestorePoint'; Value = $Create }
[PSCustomObject]@{ Name = 'SkipRegistryBackup'; Value = $Skip }
[PSCustomObject]@{ Name = 'RestartExplorer'; Value = $Restart }
)
$text = Get-DeploymentCategoryDetailString -DeploymentSettings $settings
$text | Should -Match 'ImportExportOptionRestorePoint'
$text | Should -Match 'ImportExportOptionRegistryBackup'
$text | Should -Match 'ImportExportOptionRestartExplorer'
}
It 'leaves out the registry backup when it is skipped, including when written as text' -ForEach @(
@{ Skip = $true }
@{ Skip = 'true' }
) {
$settings = @([PSCustomObject]@{ Name = 'SkipRegistryBackup'; Value = $Skip })
Get-DeploymentCategoryDetailString -DeploymentSettings $settings | Should -Not -Match 'ImportExportOptionRegistryBackup'
}
}
Describe 'Get-DeploymentCategoryDetailString with unsupported values' {
BeforeEach {
$warnings = [System.Collections.Generic.List[string]]::new()
Mock Write-Warning { $warnings.Add([string]$Message) }
}
It 'does not list the registry backup for an unsupported SkipRegistryBackup value, and does not warn' {
$settings = @([PSCustomObject]@{ Name = 'SkipRegistryBackup'; Value = 'yes' })
Get-DeploymentCategoryDetailString -DeploymentSettings $settings | Should -Not -Match 'ImportExportOptionRegistryBackup'
$warnings | Should -HaveCount 0
}
It 'lists the registry backup when SkipRegistryBackup is false or missing' -ForEach @(
@{ Settings = @([PSCustomObject]@{ Name = 'SkipRegistryBackup'; Value = $false }) }
@{ Settings = @([PSCustomObject]@{ Name = 'SkipRegistryBackup'; Value = 'false' }) }
@{ Settings = @([PSCustomObject]@{ Name = 'RestartExplorer'; Value = $true }) }
) {
Get-DeploymentCategoryDetailString -DeploymentSettings $Settings | Should -Match 'ImportExportOptionRegistryBackup'
}
}