mirror of
https://github.com/Raphire/Win11Debloat.git
synced 2026-11-25 23:16:15 +01:00
fix: read boolean deployment values from a config file as booleans (#833)
This commit is contained in:
@@ -168,3 +168,145 @@ Describe 'Test-ConfigConsistency' {
|
||||
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'
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user