microsoft/hve-core

Public

mirrored from https://github.com/microsoft/hve-coreAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
feat/add-yaml-linting

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

scripts/tests/dev-tools/Generate-PrReference.Tests.ps1

225lines · modecode

1#Requires -Modules Pester
2
3BeforeAll {
4 . $PSScriptRoot/../../dev-tools/Generate-PrReference.ps1
5}
6
7Describe '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
14Describe '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
27Describe '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
51Describe '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
66Describe '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
79Describe '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
91Describe '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
105Describe '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 $withMarkdown = Get-DiffOutput -ComparisonRef 'HEAD~1'
113 # Results may differ if there are .md file changes - just verify both calls work
114 $withMarkdown | Should -Not -BeNullOrEmpty
115 Get-DiffOutput -ComparisonRef 'HEAD~1' -ExcludeMarkdownDiff | Should -Not -BeNullOrEmpty
116 }
117}
118
119Describe 'Get-DiffSummary' {
120 It 'Returns shortstat summary string' {
121 $result = Get-DiffSummary -ComparisonRef 'HEAD~1'
122 $result | Should -BeOfType [string]
123 }
124}
125
126Describe 'Get-PrXmlContent' {
127 It 'Returns valid XML string' {
128 $result = Get-PrXmlContent -CurrentBranch 'feature/test' -BaseBranch 'main' -CommitEntries @('commit 1', 'commit 2') -DiffOutput @('diff line 1', 'diff line 2')
129 $result | Should -Not -BeNullOrEmpty
130 $result | Should -Match '<commit_history>'
131 $result | Should -Match '</commit_history>'
132 }
133
134 It 'Includes branch information' {
135 $result = Get-PrXmlContent -CurrentBranch 'feature/my-branch' -BaseBranch 'main' -CommitEntries @() -DiffOutput @()
136 $result | Should -Match 'feature/my-branch'
137 $result | Should -Match 'main'
138 }
139
140 It 'Includes commit entries' {
141 $result = Get-PrXmlContent -CurrentBranch 'feature/test' -BaseBranch 'main' -CommitEntries @('abc123 Test commit') -DiffOutput @()
142 $result | Should -Match 'abc123 Test commit'
143 }
144
145 It 'Handles empty inputs' {
146 $result = Get-PrXmlContent -CurrentBranch 'branch' -BaseBranch 'main' -CommitEntries @() -DiffOutput @()
147 $result | Should -Not -BeNullOrEmpty
148 }
149}
150
151Describe 'Get-LineImpact' {
152 It 'Parses insertions and deletions from shortstat' {
153 $result = Get-LineImpact -DiffSummary '5 files changed, 100 insertions(+), 50 deletions(-)'
154 $result | Should -Be 150
155 }
156
157 It 'Handles insertions only' {
158 $result = Get-LineImpact -DiffSummary '2 files changed, 25 insertions(+)'
159 $result | Should -Be 25
160 }
161
162 It 'Handles deletions only' {
163 $result = Get-LineImpact -DiffSummary '1 file changed, 10 deletions(-)'
164 $result | Should -Be 10
165 }
166
167 It 'Returns 0 for summary without insertions or deletions' {
168 $result = Get-LineImpact -DiffSummary 'no changes'
169 $result | Should -Be 0
170 }
171
172 It 'Returns 0 for no changes' {
173 $result = Get-LineImpact -DiffSummary '0 files changed'
174 $result | Should -Be 0
175 }
176}
177
178Describe 'Get-CurrentBranchOrRef' {
179 BeforeAll {
180 . $PSScriptRoot/../../dev-tools/Generate-PrReference.ps1
181 }
182
183 It 'Returns branch name when on a branch' {
184 # This test runs in a real git repo, so it should return something
185 $result = Get-CurrentBranchOrRef
186 $result | Should -Not -BeNullOrEmpty
187 $result | Should -BeOfType [string]
188 }
189
190 It 'Returns string starting with detached@ or branch name' {
191 $result = Get-CurrentBranchOrRef
192 # Either a branch name or detached@<sha>
193 ($result -match '^detached@' -or $result -notmatch '^detached@') | Should -BeTrue
194 }
195}
196
197Describe 'Invoke-PrReferenceGeneration' {
198 It 'Returns FileInfo object' {
199 # Skip if not in a git repo or no commits to compare
200 $commitCount = Get-CommitCount -ComparisonRef 'HEAD~1'
201 if ($commitCount -eq 0) {
202 Set-ItResult -Skipped -Because 'No commits available for comparison'
203 return
204 }
205
206 # Determine available base branch - prefer origin/main, fall back to main, then HEAD~1
207 $baseBranch = $null
208 foreach ($candidate in @('origin/main', 'main', 'HEAD~1')) {
209 & git rev-parse --verify $candidate 2>$null | Out-Null
210 if ($LASTEXITCODE -eq 0) {
211 $baseBranch = $candidate
212 break
213 }
214 }
215
216 if (-not $baseBranch) {
217 Set-ItResult -Skipped -Because 'No suitable base branch available for comparison'
218 return
219 }
220
221 $result = Invoke-PrReferenceGeneration -BaseBranch $baseBranch
222 $result | Should -BeOfType [System.IO.FileInfo]
223 $result.Extension | Should -Be '.xml'
224 }
225}