|
| 1 | +$scriptDir = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path) |
| 2 | + |
| 3 | +function New-TempGitRepo { |
| 4 | + $dir = Join-Path $env:TEMP "pester-git-$(Get-Random)" |
| 5 | + New-Item -Path $dir -ItemType Directory -Force | Out-Null |
| 6 | + Push-Location $dir |
| 7 | + git init . 2>$null | Out-Null |
| 8 | + git config user.email "test@test.com" 2>$null |
| 9 | + git config user.name "Test" 2>$null |
| 10 | + git commit --allow-empty -m "init" 2>$null | Out-Null |
| 11 | + Pop-Location |
| 12 | + return $dir |
| 13 | +} |
| 14 | + |
| 15 | +Describe "Get-GitQuick" { |
| 16 | + It "Should return correct JSON schema for clean repo" { |
| 17 | + $tempDir = New-TempGitRepo |
| 18 | + Push-Location $tempDir |
| 19 | + try { |
| 20 | + $raw = & "$scriptDir\Get-GitQuick.ps1" -AsJson 2>$null |
| 21 | + $result = $raw | ConvertFrom-Json |
| 22 | + ($null -ne $result.branch) | Should Be $true |
| 23 | + ($null -ne $result.ahead) | Should Be $true |
| 24 | + ($null -ne $result.behind) | Should Be $true |
| 25 | + ($null -ne $result.staged) | Should Be $true |
| 26 | + ($null -ne $result.modified) | Should Be $true |
| 27 | + ($null -ne $result.untracked) | Should Be $true |
| 28 | + ($null -ne $result.clean) | Should Be $true |
| 29 | + ($null -ne $result.files) | Should Be $true |
| 30 | + } finally { |
| 31 | + Pop-Location |
| 32 | + Remove-Item $tempDir -Recurse -Force -ErrorAction SilentlyContinue |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + It "Should report clean = true on clean repo" { |
| 37 | + $tempDir = New-TempGitRepo |
| 38 | + Push-Location $tempDir |
| 39 | + try { |
| 40 | + $raw = & "$scriptDir\Get-GitQuick.ps1" -AsJson 2>$null |
| 41 | + $result = $raw | ConvertFrom-Json |
| 42 | + $result.clean | Should Be $true |
| 43 | + $result.staged | Should Be 0 |
| 44 | + $result.modified | Should Be 0 |
| 45 | + $result.untracked | Should Be 0 |
| 46 | + } finally { |
| 47 | + Pop-Location |
| 48 | + Remove-Item $tempDir -Recurse -Force -ErrorAction SilentlyContinue |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + It "Should detect untracked files" { |
| 53 | + $tempDir = New-TempGitRepo |
| 54 | + Push-Location $tempDir |
| 55 | + try { |
| 56 | + Set-Content "newfile.txt" "hello" |
| 57 | + $raw = & "$scriptDir\Get-GitQuick.ps1" -AsJson 2>$null |
| 58 | + $result = $raw | ConvertFrom-Json |
| 59 | + $result.clean | Should Be $false |
| 60 | + $result.untracked | Should Be 1 |
| 61 | + } finally { |
| 62 | + Pop-Location |
| 63 | + Remove-Item $tempDir -Recurse -Force -ErrorAction SilentlyContinue |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + It "Should detect staged files" { |
| 68 | + $tempDir = New-TempGitRepo |
| 69 | + Push-Location $tempDir |
| 70 | + try { |
| 71 | + Set-Content "staged.txt" "content" |
| 72 | + git add staged.txt 2>$null |
| 73 | + $raw = & "$scriptDir\Get-GitQuick.ps1" -AsJson 2>$null |
| 74 | + $result = $raw | ConvertFrom-Json |
| 75 | + $result.staged | Should Be 1 |
| 76 | + $result.clean | Should Be $false |
| 77 | + } finally { |
| 78 | + Pop-Location |
| 79 | + Remove-Item $tempDir -Recurse -Force -ErrorAction SilentlyContinue |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + It "Should detect modified files" { |
| 84 | + $tempDir = New-TempGitRepo |
| 85 | + Push-Location $tempDir |
| 86 | + try { |
| 87 | + Set-Content "tracked.txt" "original" |
| 88 | + git add tracked.txt 2>$null |
| 89 | + git commit -m "add tracked" 2>$null | Out-Null |
| 90 | + Set-Content "tracked.txt" "modified" |
| 91 | + $raw = & "$scriptDir\Get-GitQuick.ps1" -AsJson 2>$null |
| 92 | + $result = $raw | ConvertFrom-Json |
| 93 | + $result.modified | Should Be 1 |
| 94 | + } finally { |
| 95 | + Pop-Location |
| 96 | + Remove-Item $tempDir -Recurse -Force -ErrorAction SilentlyContinue |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + It "Should report correct branch name" { |
| 101 | + $tempDir = New-TempGitRepo |
| 102 | + Push-Location $tempDir |
| 103 | + try { |
| 104 | + $raw = & "$scriptDir\Get-GitQuick.ps1" -AsJson 2>$null |
| 105 | + $result = $raw | ConvertFrom-Json |
| 106 | + ($result.branch -eq 'main' -or $result.branch -eq 'master') | Should Be $true |
| 107 | + } finally { |
| 108 | + Pop-Location |
| 109 | + Remove-Item $tempDir -Recurse -Force -ErrorAction SilentlyContinue |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + It "Should report stash count" { |
| 114 | + $tempDir = New-TempGitRepo |
| 115 | + Push-Location $tempDir |
| 116 | + try { |
| 117 | + $raw = & "$scriptDir\Get-GitQuick.ps1" -AsJson 2>$null |
| 118 | + $result = $raw | ConvertFrom-Json |
| 119 | + $result.stashes | Should Be 0 |
| 120 | + } finally { |
| 121 | + Pop-Location |
| 122 | + Remove-Item $tempDir -Recurse -Force -ErrorAction SilentlyContinue |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + It "Should have files object with sub-arrays" { |
| 127 | + $tempDir = New-TempGitRepo |
| 128 | + Push-Location $tempDir |
| 129 | + try { |
| 130 | + $raw = & "$scriptDir\Get-GitQuick.ps1" -AsJson 2>$null |
| 131 | + $result = $raw | ConvertFrom-Json |
| 132 | + ($null -ne $result.files.staged) | Should Be $true |
| 133 | + ($null -ne $result.files.modified) | Should Be $true |
| 134 | + ($null -ne $result.files.deleted) | Should Be $true |
| 135 | + ($null -ne $result.files.untracked) | Should Be $true |
| 136 | + ($null -ne $result.files.conflicts) | Should Be $true |
| 137 | + } finally { |
| 138 | + Pop-Location |
| 139 | + Remove-Item $tempDir -Recurse -Force -ErrorAction SilentlyContinue |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments