Tests/Remove-StaleGitBranches.Tests.ps1
|
BeforeAll { Import-Module "$PSScriptRoot\..\GittHelpers.psd1" -Force } Describe 'Remove-StaleGitBranches' { BeforeEach { Mock Get-GitRoot { return $TestDrive } -ModuleName GittHelpers Mock Get-WorktreeList { return @( [PSCustomObject]@{ Path = $TestDrive; Branch = $null; IsBare = $true }, [PSCustomObject]@{ Path = 'C:\wt\main'; Branch = 'main'; IsBare = $false } ) } -ModuleName GittHelpers Mock git { $global:LASTEXITCODE = 0 if ($args -contains 'branch') { return @('main','stale-branch','feature/done') } } -ModuleName GittHelpers } It 'Does not delete protected branches' { $deleted = [System.Collections.Generic.List[string]]::new() Mock git { $global:LASTEXITCODE = 0 if ($args -contains 'branch' -and ($args -join ' ') -notmatch '\-D') { return @('main','stale-branch') } if ($args -contains '-D') { $deleted.Add($args[-1]) } } -ModuleName GittHelpers Remove-StaleGitBranches -Confirm:$false $deleted | Should -Not -Contain 'main' } It 'Does not delete branches that have a worktree' { $deleted = [System.Collections.Generic.List[string]]::new() Mock git { $global:LASTEXITCODE = 0 if ($args -contains 'branch' -and ($args -join ' ') -notmatch '\-D') { return @('main','stale-branch') } if ($args -contains '-D') { $deleted.Add($args[-1]) } } -ModuleName GittHelpers Remove-StaleGitBranches -Confirm:$false $deleted | Should -Not -Contain 'main' } It 'Reports nothing deleted in -DryRun mode' { $deleted = [System.Collections.Generic.List[string]]::new() Mock git { $global:LASTEXITCODE = 0 if ($args -contains '-D') { $deleted.Add($args[-1]) } return @('main','stale-branch') } -ModuleName GittHelpers Remove-StaleGitBranches -DryRun $deleted.Count | Should -Be 0 } It 'Reports no stale branches when all are checked out' { Mock Get-WorktreeList { return @( [PSCustomObject]@{ Path = $TestDrive; Branch = $null; IsBare = $true }, [PSCustomObject]@{ Path = 'C:\wt\main'; Branch = 'main'; IsBare = $false }, [PSCustomObject]@{ Path = 'C:\wt\stale'; Branch = 'stale-branch'; IsBare = $false } ) } -ModuleName GittHelpers Mock git { $global:LASTEXITCODE = 0 if ($args -contains 'branch') { return @('main','stale-branch') } } -ModuleName GittHelpers { Remove-StaleGitBranches -Confirm:$false } | Should -Not -Throw } } |