config-central/tests/Configs.Tests.ps1

61 lines
3.6 KiB
PowerShell

BeforeAll {
Import-Module "$PSScriptRoot/../src/State.psm1" -Force
Import-Module "$PSScriptRoot/../src/Configs.psm1" -Force
}
Describe 'Configuration deployment' {
BeforeEach {
$script:repo = Join-Path $TestDrive 'repo'
$script:data = Join-Path $TestDrive 'data'
$script:destination = Join-Path $TestDrive 'user/config.txt'
New-Item -ItemType Directory "$repo/configs" -Force | Out-Null
'version one' | Set-Content "$repo/configs/config.txt" -NoNewline
$script:settings = @{ userPaths = @{ APPDATA = $TestDrive; LOCALAPPDATA = $TestDrive; USERPROFILE = $TestDrive; PROGRAMDATA = $TestDrive } }
$script:state = @{ schemaVersion = 1; configs = @{} }
$script:paths = Get-ConfigCentralPaths -DataRoot $data
Initialize-ConfigCentralData $paths
$script:configs = @([pscustomobject]@{ name = 'sample'; source = 'configs/config.txt'; target = $destination; mode = 'copy' })
}
It 'deploys and becomes idempotent' {
$plan = Get-ConfigPlan -Configs $configs -RepositoryRoot $repo -State $state -Settings $settings
$plan[0].action | Should -Be 'Create'
(Invoke-ConfigOperation -Operation $plan[0] -Paths $paths -State $state).status | Should -Be 'Succeeded'
(Get-ConfigPlan -Configs $configs -RepositoryRoot $repo -State $state -Settings $settings)[0].action | Should -Be 'None'
}
It 'detects local edits and backs them up before force' {
$first = (Get-ConfigPlan -Configs $configs -RepositoryRoot $repo -State $state -Settings $settings)[0]
[void](Invoke-ConfigOperation -Operation $first -Paths $paths -State $state)
'local edit' | Set-Content $destination -NoNewline
$conflict = (Get-ConfigPlan -Configs $configs -RepositoryRoot $repo -State $state -Settings $settings)[0]
$conflict.action | Should -Be 'Conflict'
(Invoke-ConfigOperation -Operation $conflict -Paths $paths -State $state).status | Should -Be 'Conflict'
(Invoke-ConfigOperation -Operation $conflict -Paths $paths -State $state -Force).status | Should -Be 'Succeeded'
(Get-ChildItem "$data/backups/sample/*.bak").Count | Should -Be 1
}
It 'restores the newest backup and preserves the replaced file' {
$first = (Get-ConfigPlan -Configs $configs -RepositoryRoot $repo -State $state -Settings $settings)[0]
[void](Invoke-ConfigOperation -Operation $first -Paths $paths -State $state)
'local edit' | Set-Content $destination -NoNewline
$conflict = (Get-ConfigPlan -Configs $configs -RepositoryRoot $repo -State $state -Settings $settings)[0]
[void](Invoke-ConfigOperation -Operation $conflict -Paths $paths -State $state -Force)
(Restore-ConfigBackup -Name sample -Paths $paths -State $state).status | Should -Be 'Restored'
(Get-Content -LiteralPath $destination -Raw) | Should -Be 'local edit'
(Get-ChildItem "$data/backups/sample/*.bak").Count | Should -Be 2
}
It 'rejects sources outside configs' {
'secret' | Set-Content "$repo/outside.txt"
{ Resolve-ConfigSource -RepositoryRoot $repo -Source 'outside.txt' } | Should -Throw '*escapes configs*'
}
It 'handles spaces and non-ASCII target names' {
$configs[0].target = Join-Path $TestDrive 'Données utilisateur/a file.txt'
$plan = Get-ConfigPlan -Configs $configs -RepositoryRoot $repo -State $state -Settings $settings
(Invoke-ConfigOperation -Operation $plan[0] -Paths $paths -State $state).status | Should -Be 'Succeeded'
Test-Path -LiteralPath $configs[0].target | Should -BeTrue
}
}