microsoft/hve-core
Publicmirrored fromhttps://github.com/microsoft/hve-coreAvailable
scripts/tests/dev-tools/Generate-PrReference.Tests.ps1
224lines · 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 | |
| 14 | Describe 'Get-RepositoryRoot' { |
| 15 | It 'Returns a valid directory path' { |
| 16 | $result = Get-RepositoryRoot |
| 17 | $result | Should -Not -BeNullOrEmpty |
| 18 | Test-Path -Path $result -PathType Container | Should -BeTrue |
| 19 | } |
| 20 | |
| 21 | It 'Returns path containing .git directory' { |
| 22 | $result = Get-RepositoryRoot |
| 23 | Test-Path -Path (Join-Path $result '.git') | Should -BeTrue |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | Describe 'New-PrDirectory' { |
| 28 | BeforeAll { |
| 29 | $script:tempRepo = Join-Path ([System.IO.Path]::GetTempPath()) ([System.Guid]::NewGuid().ToString()) |
| 30 | New-Item -ItemType Directory -Path $script:tempRepo -Force | Out-Null |
| 31 | } |
| 32 | |
| 33 | AfterAll { |
| 34 | Remove-Item -Path $script:tempRepo -Recurse -Force -ErrorAction SilentlyContinue |
| 35 | } |
| 36 | |
| 37 | It 'Creates .copilot-tracking/pr directory' { |
| 38 | $result = New-PrDirectory -RepoRoot $script:tempRepo |
| 39 | $result | Should -Not -BeNullOrEmpty |
| 40 | Test-Path -Path $result -PathType Container | Should -BeTrue |
| 41 | $result | Should -Match '\.copilot-tracking[\\/]pr$' |
| 42 | } |
| 43 | |
| 44 | It 'Returns existing directory without error' { |
| 45 | $firstCall = New-PrDirectory -RepoRoot $script:tempRepo |
| 46 | $secondCall = New-PrDirectory -RepoRoot $script:tempRepo |
| 47 | $secondCall | Should -Be $firstCall |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | Describe 'Resolve-ComparisonReference' { |
| 52 | It 'Returns PSCustomObject with Ref and Label properties' { |
| 53 | $result = Resolve-ComparisonReference -BaseBranch 'main' |
| 54 | $result | Should -BeOfType [PSCustomObject] |
| 55 | $result.PSObject.Properties.Name | Should -Contain 'Ref' |
| 56 | $result.PSObject.Properties.Name | Should -Contain 'Label' |
| 57 | } |
| 58 | |
| 59 | It 'Uses merge-base when remote branch exists' { |
| 60 | # This test assumes main branch exists |
| 61 | $result = Resolve-ComparisonReference -BaseBranch 'main' |
| 62 | $result.Ref | Should -Not -BeNullOrEmpty |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | Describe 'Get-ShortCommitHash' { |
| 67 | It 'Returns 7-character hash for HEAD' { |
| 68 | $result = Get-ShortCommitHash -Ref 'HEAD' |
| 69 | $result | Should -Match '^[a-f0-9]{7,}$' |
| 70 | } |
| 71 | |
| 72 | It 'Returns consistent result for same ref' { |
| 73 | $first = Get-ShortCommitHash -Ref 'HEAD' |
| 74 | $second = Get-ShortCommitHash -Ref 'HEAD' |
| 75 | $first | Should -Be $second |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | Describe 'Get-CommitEntry' { |
| 80 | It 'Returns array of formatted commit entries' { |
| 81 | $result = Get-CommitEntry -ComparisonRef 'HEAD~1' |
| 82 | $result | Should -BeOfType [string] |
| 83 | } |
| 84 | |
| 85 | It 'Returns empty array when no commits in range' { |
| 86 | $result = Get-CommitEntry -ComparisonRef 'HEAD' |
| 87 | $result | Should -BeNullOrEmpty |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | Describe 'Get-CommitCount' { |
| 92 | It 'Returns integer count' { |
| 93 | $result = Get-CommitCount -ComparisonRef 'HEAD~5' |
| 94 | $result | Should -BeOfType [int] |
| 95 | # Merge commits can inflate the count, so just verify it returns a positive integer |
| 96 | $result | Should -BeGreaterOrEqual 1 |
| 97 | } |
| 98 | |
| 99 | It 'Returns 0 when no commits in range' { |
| 100 | $result = Get-CommitCount -ComparisonRef 'HEAD' |
| 101 | $result | Should -Be 0 |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | Describe 'Get-DiffOutput' { |
| 106 | It 'Returns array of diff lines' { |
| 107 | $result = Get-DiffOutput -ComparisonRef 'HEAD~1' |
| 108 | $result | Should -Not -BeNullOrEmpty |
| 109 | } |
| 110 | |
| 111 | It 'Excludes markdown when specified' { |
| 112 | # Verify the function executes without error when excluding markdown |
| 113 | # The result may be empty if only markdown files were changed |
| 114 | { Get-DiffOutput -ComparisonRef 'HEAD~1' -ExcludeMarkdownDiff } | Should -Not -Throw |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | Describe 'Get-DiffSummary' { |
| 119 | It 'Returns shortstat summary string' { |
| 120 | $result = Get-DiffSummary -ComparisonRef 'HEAD~1' |
| 121 | $result | Should -BeOfType [string] |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | Describe 'Get-PrXmlContent' { |
| 126 | It 'Returns valid XML string' { |
| 127 | $result = Get-PrXmlContent -CurrentBranch 'feature/test' -BaseBranch 'main' -CommitEntries @('commit 1', 'commit 2') -DiffOutput @('diff line 1', 'diff line 2') |
| 128 | $result | Should -Not -BeNullOrEmpty |
| 129 | $result | Should -Match '<commit_history>' |
| 130 | $result | Should -Match '</commit_history>' |
| 131 | } |
| 132 | |
| 133 | It 'Includes branch information' { |
| 134 | $result = Get-PrXmlContent -CurrentBranch 'feature/my-branch' -BaseBranch 'main' -CommitEntries @() -DiffOutput @() |
| 135 | $result | Should -Match 'feature/my-branch' |
| 136 | $result | Should -Match 'main' |
| 137 | } |
| 138 | |
| 139 | It 'Includes commit entries' { |
| 140 | $result = Get-PrXmlContent -CurrentBranch 'feature/test' -BaseBranch 'main' -CommitEntries @('abc123 Test commit') -DiffOutput @() |
| 141 | $result | Should -Match 'abc123 Test commit' |
| 142 | } |
| 143 | |
| 144 | It 'Handles empty inputs' { |
| 145 | $result = Get-PrXmlContent -CurrentBranch 'branch' -BaseBranch 'main' -CommitEntries @() -DiffOutput @() |
| 146 | $result | Should -Not -BeNullOrEmpty |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | Describe 'Get-LineImpact' { |
| 151 | It 'Parses insertions and deletions from shortstat' { |
| 152 | $result = Get-LineImpact -DiffSummary '5 files changed, 100 insertions(+), 50 deletions(-)' |
| 153 | $result | Should -Be 150 |
| 154 | } |
| 155 | |
| 156 | It 'Handles insertions only' { |
| 157 | $result = Get-LineImpact -DiffSummary '2 files changed, 25 insertions(+)' |
| 158 | $result | Should -Be 25 |
| 159 | } |
| 160 | |
| 161 | It 'Handles deletions only' { |
| 162 | $result = Get-LineImpact -DiffSummary '1 file changed, 10 deletions(-)' |
| 163 | $result | Should -Be 10 |
| 164 | } |
| 165 | |
| 166 | It 'Returns 0 for summary without insertions or deletions' { |
| 167 | $result = Get-LineImpact -DiffSummary 'no changes' |
| 168 | $result | Should -Be 0 |
| 169 | } |
| 170 | |
| 171 | It 'Returns 0 for no changes' { |
| 172 | $result = Get-LineImpact -DiffSummary '0 files changed' |
| 173 | $result | Should -Be 0 |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | Describe 'Get-CurrentBranchOrRef' { |
| 178 | BeforeAll { |
| 179 | . $PSScriptRoot/../../dev-tools/Generate-PrReference.ps1 |
| 180 | } |
| 181 | |
| 182 | It 'Returns branch name when on a branch' { |
| 183 | # This test runs in a real git repo, so it should return something |
| 184 | $result = Get-CurrentBranchOrRef |
| 185 | $result | Should -Not -BeNullOrEmpty |
| 186 | $result | Should -BeOfType [string] |
| 187 | } |
| 188 | |
| 189 | It 'Returns string starting with detached@ or branch name' { |
| 190 | $result = Get-CurrentBranchOrRef |
| 191 | # Either a branch name or detached@<sha> |
| 192 | ($result -match '^detached@' -or $result -notmatch '^detached@') | Should -BeTrue |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | Describe 'Invoke-PrReferenceGeneration' { |
| 197 | It 'Returns FileInfo object' { |
| 198 | # Skip if not in a git repo or no commits to compare |
| 199 | $commitCount = Get-CommitCount -ComparisonRef 'HEAD~1' |
| 200 | if ($commitCount -eq 0) { |
| 201 | Set-ItResult -Skipped -Because 'No commits available for comparison' |
| 202 | return |
| 203 | } |
| 204 | |
| 205 | # Determine available base branch - prefer origin/main, fall back to main, then HEAD~1 |
| 206 | $baseBranch = $null |
| 207 | foreach ($candidate in @('origin/main', 'main', 'HEAD~1')) { |
| 208 | & git rev-parse --verify $candidate 2>$null | Out-Null |
| 209 | if ($LASTEXITCODE -eq 0) { |
| 210 | $baseBranch = $candidate |
| 211 | break |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | if (-not $baseBranch) { |
| 216 | Set-ItResult -Skipped -Because 'No suitable base branch available for comparison' |
| 217 | return |
| 218 | } |
| 219 | |
| 220 | $result = Invoke-PrReferenceGeneration -BaseBranch $baseBranch |
| 221 | $result | Should -BeOfType [System.IO.FileInfo] |
| 222 | $result.Extension | Should -Be '.xml' |
| 223 | } |
| 224 | } |