microsoft/hve-core
Publicmirrored fromhttps://github.com/microsoft/hve-coreAvailable
scripts/tests/dev-tools/Generate-PrReference.Tests.ps1
334lines · modecode
| 1 | #Requires -Modules Pester |
| 2 | |
| 3 | BeforeAll { |
| 4 | . $PSScriptRoot/../../dev-tools/Generate-PrReference.ps1 |
| 5 | } |
| 6 | |
| 7 | Describe 'Test-GitAvailability' { |
| 8 | It 'Does not throw when git is available' { |
| 9 | # This test assumes git is installed in the test environment |
| 10 | { Test-GitAvailability } | Should -Not -Throw |
| 11 | } |
| 12 | |
| 13 | It 'Should throw when git is not available' { |
| 14 | Mock Get-Command { $null } -ParameterFilter { $Name -eq 'git' } |
| 15 | { Test-GitAvailability } | Should -Throw '*Git is required*' |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | Describe 'Get-RepositoryRoot' { |
| 20 | It 'Returns a valid directory path' { |
| 21 | $result = Get-RepositoryRoot |
| 22 | $result | Should -Not -BeNullOrEmpty |
| 23 | Test-Path -Path $result -PathType Container | Should -BeTrue |
| 24 | } |
| 25 | |
| 26 | It 'Returns path containing .git directory' { |
| 27 | $result = Get-RepositoryRoot |
| 28 | Test-Path -Path (Join-Path $result '.git') | Should -BeTrue |
| 29 | } |
| 30 | |
| 31 | It 'Should throw when repository root cannot be determined' { |
| 32 | Mock git { $global:LASTEXITCODE = 0; return '' } |
| 33 | { Get-RepositoryRoot } | Should -Throw '*Unable to determine repository root*' |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | Describe 'New-PrDirectory' { |
| 38 | BeforeAll { |
| 39 | $script:tempRepo = Join-Path ([System.IO.Path]::GetTempPath()) ([System.Guid]::NewGuid().ToString()) |
| 40 | New-Item -ItemType Directory -Path $script:tempRepo -Force | Out-Null |
| 41 | } |
| 42 | |
| 43 | AfterAll { |
| 44 | Remove-Item -Path $script:tempRepo -Recurse -Force -ErrorAction SilentlyContinue |
| 45 | } |
| 46 | |
| 47 | It 'Creates .copilot-tracking/pr directory' { |
| 48 | $result = New-PrDirectory -RepoRoot $script:tempRepo |
| 49 | $result | Should -Not -BeNullOrEmpty |
| 50 | Test-Path -Path $result -PathType Container | Should -BeTrue |
| 51 | $result | Should -Match '\.copilot-tracking[\\/]pr$' |
| 52 | } |
| 53 | |
| 54 | It 'Returns existing directory without error' { |
| 55 | $firstCall = New-PrDirectory -RepoRoot $script:tempRepo |
| 56 | $secondCall = New-PrDirectory -RepoRoot $script:tempRepo |
| 57 | $secondCall | Should -Be $firstCall |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | Describe 'Resolve-ComparisonReference' { |
| 62 | It 'Returns PSCustomObject with Ref and Label properties' { |
| 63 | $result = Resolve-ComparisonReference -BaseBranch 'main' |
| 64 | $result | Should -BeOfType [PSCustomObject] |
| 65 | $result.PSObject.Properties.Name | Should -Contain 'Ref' |
| 66 | $result.PSObject.Properties.Name | Should -Contain 'Label' |
| 67 | } |
| 68 | |
| 69 | It 'Uses merge-base when remote branch exists' { |
| 70 | # This test assumes main branch exists |
| 71 | $result = Resolve-ComparisonReference -BaseBranch 'main' |
| 72 | $result.Ref | Should -Not -BeNullOrEmpty |
| 73 | } |
| 74 | |
| 75 | It 'Should throw when base branch does not exist' { |
| 76 | Mock git { $global:LASTEXITCODE = 1; return $null } |
| 77 | { Resolve-ComparisonReference -BaseBranch 'nonexistent-branch-xyz' } | Should -Throw '*does not exist*' |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | Describe 'Get-ShortCommitHash' { |
| 82 | It 'Returns 7-character hash for HEAD' { |
| 83 | $result = Get-ShortCommitHash -Ref 'HEAD' |
| 84 | $result | Should -Match '^[a-f0-9]{7,}$' |
| 85 | } |
| 86 | |
| 87 | It 'Returns consistent result for same ref' { |
| 88 | $first = Get-ShortCommitHash -Ref 'HEAD' |
| 89 | $second = Get-ShortCommitHash -Ref 'HEAD' |
| 90 | $first | Should -Be $second |
| 91 | } |
| 92 | |
| 93 | It 'Should throw when ref resolution fails' { |
| 94 | Mock git { $global:LASTEXITCODE = 128; return '' } |
| 95 | { Get-ShortCommitHash -Ref 'invalid-ref-xyz' } | Should -Throw "*Failed to resolve ref*" |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | Describe 'Get-CommitEntry' { |
| 100 | It 'Returns array of formatted commit entries' { |
| 101 | $result = Get-CommitEntry -ComparisonRef 'HEAD~1' |
| 102 | $result | Should -BeOfType [string] |
| 103 | } |
| 104 | |
| 105 | It 'Returns empty array when no commits in range' { |
| 106 | $result = Get-CommitEntry -ComparisonRef 'HEAD' |
| 107 | $result | Should -BeNullOrEmpty |
| 108 | } |
| 109 | |
| 110 | It 'Should throw when commit history retrieval fails' { |
| 111 | Mock git { $global:LASTEXITCODE = 128; return $null } |
| 112 | { Get-CommitEntry -ComparisonRef 'main' } | Should -Throw '*Failed to retrieve commit history*' |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | Describe 'Get-CommitCount' { |
| 117 | It 'Returns integer count' { |
| 118 | $result = Get-CommitCount -ComparisonRef 'HEAD~5' |
| 119 | $result | Should -BeOfType [int] |
| 120 | # Merge commits can inflate the count, so just verify it returns a positive integer |
| 121 | $result | Should -BeGreaterOrEqual 1 |
| 122 | } |
| 123 | |
| 124 | It 'Returns 0 when no commits in range' { |
| 125 | $result = Get-CommitCount -ComparisonRef 'HEAD' |
| 126 | $result | Should -Be 0 |
| 127 | } |
| 128 | |
| 129 | It 'Should throw when commit count fails' { |
| 130 | Mock git { $global:LASTEXITCODE = 128; return '' } |
| 131 | { Get-CommitCount -ComparisonRef 'main' } | Should -Throw '*Failed to count commits*' |
| 132 | } |
| 133 | |
| 134 | It 'Should return 0 when commit count text is empty' { |
| 135 | Mock git { $global:LASTEXITCODE = 0; return '' } |
| 136 | $result = Get-CommitCount -ComparisonRef 'main' |
| 137 | $result | Should -Be 0 |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | Describe 'Get-DiffOutput' { |
| 142 | It 'Returns array of diff lines' { |
| 143 | $result = Get-DiffOutput -ComparisonRef 'HEAD~1' |
| 144 | $result | Should -Not -BeNullOrEmpty |
| 145 | } |
| 146 | |
| 147 | It 'Excludes markdown when specified' { |
| 148 | # Verify the function executes without error when excluding markdown |
| 149 | # The result may be empty if only markdown files were changed |
| 150 | { Get-DiffOutput -ComparisonRef 'HEAD~1' -ExcludeMarkdownDiff } | Should -Not -Throw |
| 151 | } |
| 152 | |
| 153 | It 'Should throw when diff output fails' { |
| 154 | Mock git { $global:LASTEXITCODE = 128; return $null } |
| 155 | { Get-DiffOutput -ComparisonRef 'main' } | Should -Throw '*Failed to retrieve diff output*' |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | Describe 'Get-DiffSummary' { |
| 160 | It 'Returns shortstat summary string' { |
| 161 | $result = Get-DiffSummary -ComparisonRef 'HEAD~1' |
| 162 | $result | Should -BeOfType [string] |
| 163 | } |
| 164 | |
| 165 | It 'Should throw when diff summary fails' { |
| 166 | Mock git { $global:LASTEXITCODE = 128; return $null } |
| 167 | { Get-DiffSummary -ComparisonRef 'main' } | Should -Throw '*Failed to summarize diff output*' |
| 168 | } |
| 169 | |
| 170 | It 'Should return "0 files changed" when diff summary is empty' { |
| 171 | Mock git { $global:LASTEXITCODE = 0; return '' } |
| 172 | $result = Get-DiffSummary -ComparisonRef 'main' |
| 173 | $result | Should -Be '0 files changed' |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | Describe 'Get-PrXmlContent' { |
| 178 | It 'Returns valid XML string' { |
| 179 | $result = Get-PrXmlContent -CurrentBranch 'feature/test' -BaseBranch 'main' -CommitEntries @('commit 1', 'commit 2') -DiffOutput @('diff line 1', 'diff line 2') |
| 180 | $result | Should -Not -BeNullOrEmpty |
| 181 | $result | Should -Match '<commit_history>' |
| 182 | $result | Should -Match '</commit_history>' |
| 183 | } |
| 184 | |
| 185 | It 'Includes branch information' { |
| 186 | $result = Get-PrXmlContent -CurrentBranch 'feature/my-branch' -BaseBranch 'main' -CommitEntries @() -DiffOutput @() |
| 187 | $result | Should -Match 'feature/my-branch' |
| 188 | $result | Should -Match 'main' |
| 189 | } |
| 190 | |
| 191 | It 'Includes commit entries' { |
| 192 | $result = Get-PrXmlContent -CurrentBranch 'feature/test' -BaseBranch 'main' -CommitEntries @('abc123 Test commit') -DiffOutput @() |
| 193 | $result | Should -Match 'abc123 Test commit' |
| 194 | } |
| 195 | |
| 196 | It 'Handles empty inputs' { |
| 197 | $result = Get-PrXmlContent -CurrentBranch 'branch' -BaseBranch 'main' -CommitEntries @() -DiffOutput @() |
| 198 | $result | Should -Not -BeNullOrEmpty |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | Describe 'Get-LineImpact' { |
| 203 | It 'Parses insertions and deletions from shortstat' { |
| 204 | $result = Get-LineImpact -DiffSummary '5 files changed, 100 insertions(+), 50 deletions(-)' |
| 205 | $result | Should -Be 150 |
| 206 | } |
| 207 | |
| 208 | It 'Handles insertions only' { |
| 209 | $result = Get-LineImpact -DiffSummary '2 files changed, 25 insertions(+)' |
| 210 | $result | Should -Be 25 |
| 211 | } |
| 212 | |
| 213 | It 'Handles deletions only' { |
| 214 | $result = Get-LineImpact -DiffSummary '1 file changed, 10 deletions(-)' |
| 215 | $result | Should -Be 10 |
| 216 | } |
| 217 | |
| 218 | It 'Returns 0 for summary without insertions or deletions' { |
| 219 | $result = Get-LineImpact -DiffSummary 'no changes' |
| 220 | $result | Should -Be 0 |
| 221 | } |
| 222 | |
| 223 | It 'Returns 0 for no changes' { |
| 224 | $result = Get-LineImpact -DiffSummary '0 files changed' |
| 225 | $result | Should -Be 0 |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | Describe 'Get-CurrentBranchOrRef' { |
| 230 | BeforeAll { |
| 231 | . $PSScriptRoot/../../dev-tools/Generate-PrReference.ps1 |
| 232 | } |
| 233 | |
| 234 | It 'Returns branch name when on a branch' { |
| 235 | # This test runs in a real git repo, so it should return something |
| 236 | $result = Get-CurrentBranchOrRef |
| 237 | $result | Should -Not -BeNullOrEmpty |
| 238 | $result | Should -BeOfType [string] |
| 239 | } |
| 240 | |
| 241 | It 'Returns string starting with detached@ or branch name' { |
| 242 | $result = Get-CurrentBranchOrRef |
| 243 | # Either a branch name or detached@<sha> |
| 244 | ($result -match '^detached@' -or $result -notmatch '^detached@') | Should -BeTrue |
| 245 | } |
| 246 | |
| 247 | It 'Should return detached@sha when in detached HEAD state' { |
| 248 | # Use call sequence to distinguish git commands (cross-platform safe) |
| 249 | $script:gitCallCount = 0 |
| 250 | Mock git { |
| 251 | $script:gitCallCount++ |
| 252 | if ($script:gitCallCount -eq 1) { |
| 253 | # First call: git branch --show-current returns empty (detached) |
| 254 | $global:LASTEXITCODE = 0 |
| 255 | return '' |
| 256 | } |
| 257 | # Second call: git rev-parse --short HEAD returns SHA |
| 258 | $global:LASTEXITCODE = 0 |
| 259 | return 'abc1234' |
| 260 | } |
| 261 | $result = Get-CurrentBranchOrRef |
| 262 | $result | Should -Be 'detached@abc1234' |
| 263 | } |
| 264 | |
| 265 | It 'Should return unknown when both branch and rev-parse fail' { |
| 266 | Mock git { |
| 267 | $global:LASTEXITCODE = 128 |
| 268 | return $null |
| 269 | } |
| 270 | $result = Get-CurrentBranchOrRef |
| 271 | $result | Should -Be 'unknown' |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | Describe 'Invoke-PrReferenceGeneration' { |
| 276 | It 'Returns FileInfo object' { |
| 277 | # Skip if not in a git repo or no commits to compare |
| 278 | $commitCount = Get-CommitCount -ComparisonRef 'HEAD~1' |
| 279 | if ($commitCount -eq 0) { |
| 280 | Set-ItResult -Skipped -Because 'No commits available for comparison' |
| 281 | return |
| 282 | } |
| 283 | |
| 284 | # Determine available base branch - prefer origin/main, fall back to main, then HEAD~1 |
| 285 | $baseBranch = $null |
| 286 | foreach ($candidate in @('origin/main', 'main', 'HEAD~1')) { |
| 287 | & git rev-parse --verify $candidate 2>$null | Out-Null |
| 288 | if ($LASTEXITCODE -eq 0) { |
| 289 | $baseBranch = $candidate |
| 290 | break |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | if (-not $baseBranch) { |
| 295 | Set-ItResult -Skipped -Because 'No suitable base branch available for comparison' |
| 296 | return |
| 297 | } |
| 298 | |
| 299 | $result = Invoke-PrReferenceGeneration -BaseBranch $baseBranch |
| 300 | $result | Should -BeOfType [System.IO.FileInfo] |
| 301 | $result.Extension | Should -Be '.xml' |
| 302 | } |
| 303 | |
| 304 | It 'Should include markdown exclusion note when ExcludeMarkdownDiff is specified' { |
| 305 | # Skip if not in a git repo or no commits |
| 306 | $commitCount = Get-CommitCount -ComparisonRef 'HEAD~1' |
| 307 | if ($commitCount -eq 0) { |
| 308 | Set-ItResult -Skipped -Because 'No commits available for comparison' |
| 309 | return |
| 310 | } |
| 311 | |
| 312 | $baseBranch = $null |
| 313 | foreach ($candidate in @('origin/main', 'main', 'HEAD~1')) { |
| 314 | & git rev-parse --verify $candidate 2>$null | Out-Null |
| 315 | if ($LASTEXITCODE -eq 0) { |
| 316 | $baseBranch = $candidate |
| 317 | break |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | if (-not $baseBranch) { |
| 322 | Set-ItResult -Skipped -Because 'No suitable base branch available for comparison' |
| 323 | return |
| 324 | } |
| 325 | |
| 326 | Mock Write-Host {} |
| 327 | |
| 328 | $result = Invoke-PrReferenceGeneration -BaseBranch $baseBranch -ExcludeMarkdownDiff |
| 329 | $result | Should -BeOfType [System.IO.FileInfo] |
| 330 | |
| 331 | # Verify the markdown exclusion note was output |
| 332 | Should -Invoke Write-Host -ParameterFilter { $Object -eq 'Note: Markdown files were excluded from diff output' } |
| 333 | } |
| 334 | } |