diff --git a/windows/branding/lib/RegistryHelpers.ps1 b/windows/branding/lib/RegistryHelpers.ps1 new file mode 100644 index 0000000..943aed8 --- /dev/null +++ b/windows/branding/lib/RegistryHelpers.ps1 @@ -0,0 +1,16 @@ +Set-StrictMode -Version Latest + +# Write a registry value under an arbitrary hive root (a live HKLM:/HKCU: path +# OR a loaded offline hive exposed as a PSDrive). Creates intermediate keys. +function Set-SmRegValue { + param( + [Parameter(Mandatory)][string]$Root, + [Parameter(Mandatory)][string]$SubKey, + [Parameter(Mandatory)][string]$Name, + [Parameter(Mandatory)][ValidateSet('String','ExpandString','DWord','Binary')][string]$Type, + [Parameter(Mandatory)]$Value + ) + $key = Join-Path $Root $SubKey + if (-not (Test-Path $key)) { New-Item -Path $key -Force | Out-Null } + New-ItemProperty -Path $key -Name $Name -PropertyType $Type -Value $Value -Force | Out-Null +} diff --git a/windows/tests/Branding.Tests.ps1 b/windows/tests/Branding.Tests.ps1 new file mode 100644 index 0000000..f7cb680 --- /dev/null +++ b/windows/tests/Branding.Tests.ps1 @@ -0,0 +1,22 @@ +#Requires -Modules @{ ModuleName='Pester'; ModuleVersion='5.0.0' } + +Describe 'Set-SmRegValue' { + BeforeAll { + . "$PSScriptRoot\..\branding\lib\RegistryHelpers.ps1" + $script:root = 'HKCU:\Software\SilverMetalTest' + if (Test-Path $script:root) { Remove-Item $script:root -Recurse -Force } + } + AfterAll { + if (Test-Path $script:root) { Remove-Item $script:root -Recurse -Force } + } + + It 'creates the key path and writes a string value' { + Set-SmRegValue -Root $script:root -SubKey 'A\B' -Name 'Greeting' -Type String -Value 'hi' + (Get-ItemProperty "$script:root\A\B").Greeting | Should -Be 'hi' + } + + It 'writes a dword value' { + Set-SmRegValue -Root $script:root -SubKey 'A' -Name 'Flag' -Type DWord -Value 1 + (Get-ItemProperty "$script:root\A").Flag | Should -Be 1 + } +}