config-central/tests/UpdateConfigs.Tests.ps1

63 lines
3.3 KiB
PowerShell

BeforeAll {
$messageTokens = $null; $messageErrors = $null
$messageAst = [System.Management.Automation.Language.Parser]::ParseFile("$PSScriptRoot/../update-configs.ps1", [ref]$messageTokens, [ref]$messageErrors)
$messageFn = $messageAst.Find({ param($n) $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq 'Write-ConfigCentralMessage' }, $true)
. ([scriptblock]::Create($messageFn.Extent.Text))
$tokens = $null; $errors = $null
$ast = [System.Management.Automation.Language.Parser]::ParseFile("$PSScriptRoot/../update-configs.ps1", [ref]$tokens, [ref]$errors)
if ($errors.Count) { throw $errors[0] }
$fn = $ast.Find({ param($node) $node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq 'Invoke-ConfigUpdate' }, $true)
. ([scriptblock]::Create($fn.Extent.Text))
function git.exe { }
function pwsh.exe { }
}
Describe 'Web config updater' {
BeforeEach {
$script:oldTemp = $env:TEMP
$env:TEMP = $TestDrive
$script:gitFailure = $false
$script:applyFailure = $false
$script:childArguments = @()
'{"machine":"desktop","branch":"main","repositoryUrl":"https://example.test/config.git"}' | Set-Content "$TestDrive/settings.json"
Mock Get-Command { [pscustomobject]@{ Source = $Name } } -ParameterFilter { $Name -in @('git.exe', 'pwsh.exe') }
Mock Test-Path { $true } -ParameterFilter { $LiteralPath -in @('git.exe', 'pwsh.exe') }
Mock git.exe {
$global:LASTEXITCODE = 0
if ($args[0] -eq 'clone') {
New-Item -ItemType Directory -Path $args[-1] -Force | Out-Null
if ($script:gitFailure) { $global:LASTEXITCODE = 1 }
}
if ($args -contains 'rev-parse') { 'a' * 40 }
}
Mock pwsh.exe {
$script:childArguments = $args
$global:LASTEXITCODE = if ($script:applyFailure) { 5 } else { 0 }
}
}
AfterEach { $env:TEMP = $script:oldTemp }
It 'applies only configs from the downloaded revision and removes its temporary checkout' {
Invoke-ConfigUpdate -DataRoot $TestDrive
$script:childArguments | Should -Contain '-ConfigsOnly'
$script:childArguments | Should -Contain ('a' * 40)
$script:childArguments | Should -Contain '-ForceConfigs'
@(Get-ChildItem $TestDrive -Directory -Filter 'config-central-update-*').Count | Should -Be 0
}
It 'allows explicitly preserving local edits' {
Invoke-ConfigUpdate -DataRoot $TestDrive -ForceConfigs:$false
$script:childArguments | Should -Not -Contain '-ForceConfigs'
}
It 'does not apply after a failed download' {
$script:gitFailure = $true
{ Invoke-ConfigUpdate -DataRoot $TestDrive } | Should -Throw '*download failed*'
Should -Invoke pwsh.exe -Times 0
@(Get-ChildItem $TestDrive -Directory -Filter 'config-central-update-*').Count | Should -Be 0
}
It 'surfaces partial failures while forcing config deployment' {
$script:applyFailure = $true
{ Invoke-ConfigUpdate -DataRoot $TestDrive -ForceConfigs } | Should -Throw '*exit 5*'
$script:childArguments | Should -Contain '-ForceConfigs'
@(Get-ChildItem $TestDrive -Directory -Filter 'config-central-update-*').Count | Should -Be 0
}
}