config-central/tests/Repository.Tests.ps1

65 lines
3.1 KiB
PowerShell

BeforeAll {
Import-Module "$PSScriptRoot/../src/State.psm1" -Force
Import-Module "$PSScriptRoot/../src/Repository.psm1" -Force
}
Describe 'Repository synchronization' {
It 'fast-forwards the checkout to the resolved revision' {
InModuleScope Repository {
$script:revisionChecks = 0
Mock Test-RepositoryDirty { $false }
Mock Get-RepositoryRevision { $script:revisionChecks++; if ($script:revisionChecks -eq 1) { 'a' * 40 } else { 'b' * 40 } }
Mock Invoke-Git { [pscustomobject]@{ exitCode = 0; output = '' } }
$result = Update-RepositoryCheckout -RepositoryRoot 'C:\repo' -Revision ('b' * 40)
$result.status | Should -Be 'Succeeded'
Should -Invoke Invoke-Git -Times 1 -Exactly -ParameterFilter { $Arguments -join ' ' -eq ('merge --ff-only ' + ('b' * 40)) }
}
}
It 'does not touch an already current checkout' {
InModuleScope Repository {
Mock Test-RepositoryDirty { $false }
Mock Get-RepositoryRevision { 'a' * 40 }
Mock Invoke-Git { throw 'Git merge must not run' }
(Update-RepositoryCheckout -RepositoryRoot 'C:\repo' -Revision ('a' * 40)).status | Should -Be 'Unchanged'
Should -Invoke Invoke-Git -Times 0
}
}
It 'rejects unsafe branch names before fetching' {
{ Resolve-RemoteRevision -RepositoryRoot $TestDrive -Settings @{ branch = '../main' } } | Should -Throw '*Unsafe branch*'
}
It 'refuses a dirty checkout' {
InModuleScope Repository -Parameters @{ Working = $TestDrive } {
Mock Test-RepositoryDirty { $true }
{ Resolve-RemoteRevision -RepositoryRoot $Working -Settings @{ branch = 'main' } } | Should -Throw '*local changes*'
}
}
It 'reports fetch failures with recovery guidance' {
InModuleScope Repository -Parameters @{ Working = $TestDrive } {
Mock Test-RepositoryDirty { $false }
Mock Invoke-Git { throw 'credential rejected' }
{ Resolve-RemoteRevision -RepositoryRoot $Working -Settings @{ branch = 'main' } } | Should -Throw '*network access and Git credentials*'
}
}
It 'stages the selected commit without changing the checkout' -Skip:(-not $IsWindows) {
$repo = Join-Path $TestDrive 'repo'
New-Item -ItemType Directory $repo | Out-Null
& git.exe -C $repo init | Out-Null
& git.exe -C $repo config user.email 'test@example.invalid'
& git.exe -C $repo config user.name 'Config Central Test'
'first' | Set-Content "$repo/cc.ps1"
& git.exe -C $repo add cc.ps1
& git.exe -C $repo commit -m first | Out-Null
$revision = (& git.exe -C $repo rev-parse HEAD).Trim()
$paths = Get-ConfigCentralPaths -DataRoot (Join-Path $TestDrive 'data')
Initialize-ConfigCentralData $paths
$snapshot = New-RepositorySnapshot -RepositoryRoot $repo -Revision $revision -Paths $paths
(Get-Content "$snapshot/cc.ps1" -Raw).Trim() | Should -Be 'first'
(& git.exe -C $repo status --porcelain) | Should -BeNullOrEmpty
}
}