|
| 1 | +$ErrorActionPreference = 'Stop' |
| 2 | +Set-StrictMode -Version Latest |
| 3 | + |
| 4 | +BeforeAll { |
| 5 | + function Script:Invoke-GitForMigrationTests { |
| 6 | + [CmdletBinding()] |
| 7 | + param( |
| 8 | + [Parameter(Mandatory = $true)] |
| 9 | + [string]$RepoPath, |
| 10 | + [Parameter(Mandatory = $true)] |
| 11 | + [string[]]$Arguments, |
| 12 | + [switch]$AllowFail |
| 13 | + ) |
| 14 | + |
| 15 | + $previousPreference = $ErrorActionPreference |
| 16 | + $ErrorActionPreference = 'Continue' |
| 17 | + try { |
| 18 | + $output = & git -C $RepoPath @Arguments 2>&1 |
| 19 | + } finally { |
| 20 | + $ErrorActionPreference = $previousPreference |
| 21 | + } |
| 22 | + |
| 23 | + $exitCode = $LASTEXITCODE |
| 24 | + $text = ($output | Out-String).Trim() |
| 25 | + if (-not $AllowFail -and $exitCode -ne 0) { |
| 26 | + throw "git $($Arguments -join ' ') failed in '$RepoPath' (exit=$exitCode): $text" |
| 27 | + } |
| 28 | + |
| 29 | + [pscustomobject]@{ |
| 30 | + ExitCode = $exitCode |
| 31 | + Output = $text |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + function Script:New-MigrationTestRepository { |
| 36 | + [CmdletBinding()] |
| 37 | + param( |
| 38 | + [Parameter(Mandatory = $true)] |
| 39 | + [string]$Prefix |
| 40 | + ) |
| 41 | + |
| 42 | + $tempRoot = Join-Path ([IO.Path]::GetTempPath()) ($Prefix + '-' + [guid]::NewGuid().Guid) |
| 43 | + $repoPath = Join-Path $tempRoot 'repo' |
| 44 | + $toolsPath = Join-Path $repoPath 'tools' |
| 45 | + $modulePath = Join-Path $repoPath 'modules\GitAliases.Extras' |
| 46 | + |
| 47 | + New-Item -ItemType Directory -Path $toolsPath -Force | Out-Null |
| 48 | + New-Item -ItemType Directory -Path $modulePath -Force | Out-Null |
| 49 | + |
| 50 | + Copy-Item -Path $script:MigrationScriptSource -Destination (Join-Path $toolsPath 'migrate-gitaliases-extras.ps1') -Force |
| 51 | + |
| 52 | + Invoke-GitForMigrationTests -RepoPath $repoPath -Arguments @('init') | Out-Null |
| 53 | + Invoke-GitForMigrationTests -RepoPath $repoPath -Arguments @('config', 'user.email', 'test@example.com') | Out-Null |
| 54 | + Invoke-GitForMigrationTests -RepoPath $repoPath -Arguments @('config', 'user.name', 'Test User') | Out-Null |
| 55 | + Invoke-GitForMigrationTests -RepoPath $repoPath -Arguments @('config', 'commit.gpgsign', 'false') | Out-Null |
| 56 | + |
| 57 | + Set-Content -Path (Join-Path $modulePath 'GitAliases.Extras.psm1') -Value "function gsw { 'ok' }" -NoNewline -Encoding ascii |
| 58 | + Invoke-GitForMigrationTests -RepoPath $repoPath -Arguments @('add', '.') | Out-Null |
| 59 | + Invoke-GitForMigrationTests -RepoPath $repoPath -Arguments @('commit', '-m', 'init module') | Out-Null |
| 60 | + |
| 61 | + [pscustomobject]@{ |
| 62 | + TempRoot = $tempRoot |
| 63 | + RepoPath = $repoPath |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + [string]$script:RepoRoot = Resolve-Path -LiteralPath (Join-Path $PSScriptRoot '..') | |
| 68 | + Select-Object -ExpandProperty Path -First 1 |
| 69 | + $script:MigrationScriptSource = Join-Path $script:RepoRoot 'tools\migrate-gitaliases-extras.ps1' |
| 70 | +} |
| 71 | + |
| 72 | +Describe 'migrate-gitaliases-extras script' { |
| 73 | + It 'dry run succeeds and does not modify repository layout' -Skip:(-not (Get-Command git -ErrorAction SilentlyContinue)) { |
| 74 | + $context = New-MigrationTestRepository -Prefix 'migration-dry-run' |
| 75 | + try { |
| 76 | + Push-Location $context.RepoPath |
| 77 | + try { |
| 78 | + $output = & pwsh -NoProfile -File '.\tools\migrate-gitaliases-extras.ps1' -SubmoduleUrl 'git@github.com:example/GitAliases.Extras.git' 2>&1 |
| 79 | + $exitCode = $LASTEXITCODE |
| 80 | + } finally { |
| 81 | + Pop-Location |
| 82 | + } |
| 83 | + |
| 84 | + $exitCode | Should -Be 0 |
| 85 | + (($output | Out-String) -match 'Dry run complete') | Should -BeTrue |
| 86 | + (Test-Path -LiteralPath (Join-Path $context.RepoPath 'modules\GitAliases.Extras\GitAliases.Extras.psm1')) | Should -BeTrue |
| 87 | + (Test-Path -LiteralPath (Join-Path $context.RepoPath '.gitmodules')) | Should -BeFalse |
| 88 | + } finally { |
| 89 | + if (Test-Path -LiteralPath $context.TempRoot) { |
| 90 | + Remove-Item -Path $context.TempRoot -Recurse -Force -ErrorAction SilentlyContinue |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + It 'fails on dirty working tree without AllowDirty' -Skip:(-not (Get-Command git -ErrorAction SilentlyContinue)) { |
| 96 | + $context = New-MigrationTestRepository -Prefix 'migration-dirty' |
| 97 | + try { |
| 98 | + Set-Content -Path (Join-Path $context.RepoPath 'dirty.txt') -Value 'dirty' -NoNewline -Encoding ascii |
| 99 | + |
| 100 | + Push-Location $context.RepoPath |
| 101 | + try { |
| 102 | + $output = & pwsh -NoProfile -File '.\tools\migrate-gitaliases-extras.ps1' -SubmoduleUrl 'git@github.com:example/GitAliases.Extras.git' 2>&1 |
| 103 | + $exitCode = $LASTEXITCODE |
| 104 | + } finally { |
| 105 | + Pop-Location |
| 106 | + } |
| 107 | + |
| 108 | + $exitCode | Should -Not -Be 0 |
| 109 | + (($output | Out-String) -match 'Working tree is not clean') | Should -BeTrue |
| 110 | + } finally { |
| 111 | + if (Test-Path -LiteralPath $context.TempRoot) { |
| 112 | + Remove-Item -Path $context.TempRoot -Recurse -Force -ErrorAction SilentlyContinue |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments