microsoft/hve-core

Public

mirrored fromhttps://github.com/microsoft/hve-coreAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
feat/context-working

Branches

Tags

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

Clone

HTTPS

Download ZIP

scripts/tests/extension/Find-CollectionManifests.Tests.ps1

384lines · modecode

1#Requires -Modules Pester
2# Copyright (c) Microsoft Corporation.
3# SPDX-License-Identifier: MIT
4<#
5.SYNOPSIS
6 Pester tests for Find-CollectionManifests.ps1 script
7.DESCRIPTION
8 Tests for collection manifest discovery and matrix building:
9 - Empty collections directory returns empty matrix
10 - Single stable collection returns one matrix item
11 - Deprecated collections are always skipped
12 - Experimental collections skipped for Stable channel
13 - Experimental collections included for Preview channel
14 - Multiple collections produce correct matrix JSON
15 - Skipped collections tracked in Skipped property
16 - Missing name falls back to id
17 - Missing maturity defaults to stable
18#>
19
20BeforeAll {
21 $script:ScriptPath = Join-Path $PSScriptRoot '../../extension/Find-CollectionManifests.ps1'
22 $script:CIHelpersPath = Join-Path $PSScriptRoot '../../lib/Modules/CIHelpers.psm1'
23
24 # Import modules for mocking
25 Import-Module $script:CIHelpersPath -Force
26
27 # Dot-source the script to access Find-CollectionManifestsCore
28 . $script:ScriptPath
29}
30
31AfterAll {
32 Remove-Module CIHelpers -Force -ErrorAction SilentlyContinue
33}
34
35Describe 'Find-CollectionManifests' -Tag 'Unit' {
36
37 Context 'Empty collections directory' {
38 BeforeEach {
39 $script:TempDir = Join-Path ([System.IO.Path]::GetTempPath()) "pester-$([Guid]::NewGuid().ToString('N').Substring(0,8))"
40 New-Item -ItemType Directory -Path $script:TempDir -Force | Out-Null
41 }
42
43 AfterEach {
44 Remove-Item -Path $script:TempDir -Recurse -Force -ErrorAction SilentlyContinue
45 }
46
47 It 'Returns empty matrix JSON' {
48 $result = Find-CollectionManifestsCore -CollectionsDir $script:TempDir
49 $result.MatrixJson | Should -Be '{"include":[]}'
50 }
51
52 It 'Returns empty MatrixItems' {
53 $result = Find-CollectionManifestsCore -CollectionsDir $script:TempDir
54 $result.MatrixItems | Should -HaveCount 0
55 }
56 }
57
58 Context 'Single stable collection' {
59 BeforeEach {
60 $script:TempDir = Join-Path ([System.IO.Path]::GetTempPath()) "pester-$([Guid]::NewGuid().ToString('N').Substring(0,8))"
61 New-Item -ItemType Directory -Path $script:TempDir -Force | Out-Null
62
63 @"
64id: test-collection
65name: Test Collection
66maturity: stable
67"@ | Set-Content -Path (Join-Path $script:TempDir 'test.collection.yml')
68 }
69
70 AfterEach {
71 Remove-Item -Path $script:TempDir -Recurse -Force -ErrorAction SilentlyContinue
72 }
73
74 It 'Returns one matrix item' {
75 $result = Find-CollectionManifestsCore -CollectionsDir $script:TempDir
76 $result.MatrixItems | Should -HaveCount 1
77 }
78
79 It 'Includes correct id and name' {
80 $result = Find-CollectionManifestsCore -CollectionsDir $script:TempDir
81 $result.MatrixItems[0].id | Should -Be 'test-collection'
82 $result.MatrixItems[0].name | Should -Be 'Test Collection'
83 }
84
85 It 'Includes manifest path with forward slashes' {
86 $result = Find-CollectionManifestsCore -CollectionsDir $script:TempDir
87 $result.MatrixItems[0].manifest | Should -Not -BeLike '*\*'
88 }
89 }
90
91 Context 'Deprecated collections always skipped' {
92 BeforeEach {
93 $script:TempDir = Join-Path ([System.IO.Path]::GetTempPath()) "pester-$([Guid]::NewGuid().ToString('N').Substring(0,8))"
94 New-Item -ItemType Directory -Path $script:TempDir -Force | Out-Null
95
96 @"
97id: old-collection
98name: Old Collection
99maturity: deprecated
100"@ | Set-Content -Path (Join-Path $script:TempDir 'old.collection.yml')
101 }
102
103 AfterEach {
104 Remove-Item -Path $script:TempDir -Recurse -Force -ErrorAction SilentlyContinue
105 }
106
107 It 'Excludes deprecated from matrix' {
108 $result = Find-CollectionManifestsCore -CollectionsDir $script:TempDir
109 $result.MatrixItems | Should -HaveCount 0
110 }
111
112 It 'Tracks deprecated in Skipped' {
113 $result = Find-CollectionManifestsCore -CollectionsDir $script:TempDir
114 $result.Skipped | Should -HaveCount 1
115 $result.Skipped[0].Reason | Should -Be 'deprecated'
116 }
117 }
118
119 Context 'Removed collections always skipped' {
120 BeforeEach {
121 $script:TempDir = Join-Path ([System.IO.Path]::GetTempPath()) "pester-$([Guid]::NewGuid().ToString('N').Substring(0,8))"
122 New-Item -ItemType Directory -Path $script:TempDir -Force | Out-Null
123
124 @"
125id: gone-collection
126name: Gone Collection
127maturity: removed
128"@ | Set-Content -Path (Join-Path $script:TempDir 'gone.collection.yml')
129 }
130
131 AfterEach {
132 Remove-Item -Path $script:TempDir -Recurse -Force -ErrorAction SilentlyContinue
133 }
134
135 It 'Excludes removed from matrix on Stable channel' {
136 $result = Find-CollectionManifestsCore -CollectionsDir $script:TempDir -Channel 'Stable'
137 $result.MatrixItems | Should -HaveCount 0
138 }
139
140 It 'Excludes removed from matrix on Preview channel' {
141 $result = Find-CollectionManifestsCore -CollectionsDir $script:TempDir -Channel 'Preview'
142 $result.MatrixItems | Should -HaveCount 0
143 }
144
145 It 'Tracks removed in Skipped with reason removed' {
146 $result = Find-CollectionManifestsCore -CollectionsDir $script:TempDir
147 $result.Skipped | Should -HaveCount 1
148 $result.Skipped[0].Reason | Should -Be 'removed'
149 }
150 }
151
152 Context 'Experimental skipped for Stable channel' {
153 BeforeEach {
154 $script:TempDir = Join-Path ([System.IO.Path]::GetTempPath()) "pester-$([Guid]::NewGuid().ToString('N').Substring(0,8))"
155 New-Item -ItemType Directory -Path $script:TempDir -Force | Out-Null
156
157 @"
158id: exp-collection
159name: Experimental Collection
160maturity: experimental
161"@ | Set-Content -Path (Join-Path $script:TempDir 'exp.collection.yml')
162 }
163
164 AfterEach {
165 Remove-Item -Path $script:TempDir -Recurse -Force -ErrorAction SilentlyContinue
166 }
167
168 It 'Excludes experimental from Stable channel matrix' {
169 $result = Find-CollectionManifestsCore -Channel 'Stable' -CollectionsDir $script:TempDir
170 $result.MatrixItems | Should -HaveCount 0
171 }
172
173 It 'Tracks experimental in Skipped with reason' {
174 $result = Find-CollectionManifestsCore -Channel 'Stable' -CollectionsDir $script:TempDir
175 $result.Skipped | Should -HaveCount 1
176 $result.Skipped[0].Reason | Should -BeLike '*experimental*'
177 }
178 }
179
180 Context 'Experimental included for Preview channel' {
181 BeforeEach {
182 $script:TempDir = Join-Path ([System.IO.Path]::GetTempPath()) "pester-$([Guid]::NewGuid().ToString('N').Substring(0,8))"
183 New-Item -ItemType Directory -Path $script:TempDir -Force | Out-Null
184
185 @"
186id: exp-collection
187name: Experimental Collection
188maturity: experimental
189"@ | Set-Content -Path (Join-Path $script:TempDir 'exp.collection.yml')
190 }
191
192 AfterEach {
193 Remove-Item -Path $script:TempDir -Recurse -Force -ErrorAction SilentlyContinue
194 }
195
196 It 'Includes experimental for Preview channel' {
197 $result = Find-CollectionManifestsCore -Channel 'Preview' -CollectionsDir $script:TempDir
198 $result.MatrixItems | Should -HaveCount 1
199 $result.MatrixItems[0].id | Should -Be 'exp-collection'
200 }
201 }
202
203 Context 'Multiple collections produce correct matrix' {
204 BeforeEach {
205 $script:TempDir = Join-Path ([System.IO.Path]::GetTempPath()) "pester-$([Guid]::NewGuid().ToString('N').Substring(0,8))"
206 New-Item -ItemType Directory -Path $script:TempDir -Force | Out-Null
207
208 @"
209id: stable-one
210name: Stable One
211maturity: stable
212"@ | Set-Content -Path (Join-Path $script:TempDir 'stable-one.collection.yml')
213
214 @"
215id: stable-two
216name: Stable Two
217maturity: stable
218"@ | Set-Content -Path (Join-Path $script:TempDir 'stable-two.collection.yml')
219
220 @"
221id: deprecated-one
222name: Deprecated One
223maturity: deprecated
224"@ | Set-Content -Path (Join-Path $script:TempDir 'deprecated-one.collection.yml')
225 }
226
227 AfterEach {
228 Remove-Item -Path $script:TempDir -Recurse -Force -ErrorAction SilentlyContinue
229 }
230
231 It 'Includes only non-deprecated collections' {
232 $result = Find-CollectionManifestsCore -CollectionsDir $script:TempDir
233 $result.MatrixItems | Should -HaveCount 2
234 }
235
236 It 'Produces valid matrix JSON' {
237 $result = Find-CollectionManifestsCore -CollectionsDir $script:TempDir
238 { $result.MatrixJson | ConvertFrom-Json } | Should -Not -Throw
239 }
240
241 It 'Matrix JSON contains include array with correct count' {
242 $result = Find-CollectionManifestsCore -CollectionsDir $script:TempDir
243 $parsed = $result.MatrixJson | ConvertFrom-Json
244 $parsed.include | Should -HaveCount 2
245 }
246 }
247
248 Context 'Skipped collections tracked with reasons' {
249 BeforeEach {
250 $script:TempDir = Join-Path ([System.IO.Path]::GetTempPath()) "pester-$([Guid]::NewGuid().ToString('N').Substring(0,8))"
251 New-Item -ItemType Directory -Path $script:TempDir -Force | Out-Null
252
253 @"
254id: good-one
255name: Good One
256maturity: stable
257"@ | Set-Content -Path (Join-Path $script:TempDir 'good.collection.yml')
258
259 @"
260id: dep-one
261name: Deprecated One
262maturity: deprecated
263"@ | Set-Content -Path (Join-Path $script:TempDir 'dep.collection.yml')
264
265 @"
266id: exp-one
267name: Experimental One
268maturity: experimental
269"@ | Set-Content -Path (Join-Path $script:TempDir 'exp.collection.yml')
270 }
271
272 AfterEach {
273 Remove-Item -Path $script:TempDir -Recurse -Force -ErrorAction SilentlyContinue
274 }
275
276 It 'Tracks all skipped collections' {
277 $result = Find-CollectionManifestsCore -Channel 'Stable' -CollectionsDir $script:TempDir
278 $result.Skipped | Should -HaveCount 2
279 $result.Skipped.Id | Should -Contain 'dep-one'
280 $result.Skipped.Id | Should -Contain 'exp-one'
281 }
282
283 It 'Includes correct reason for deprecated' {
284 $result = Find-CollectionManifestsCore -Channel 'Stable' -CollectionsDir $script:TempDir
285 $depSkip = $result.Skipped | Where-Object { $_.Id -eq 'dep-one' }
286 $depSkip.Reason | Should -Be 'deprecated'
287 }
288
289 It 'Includes correct reason for experimental' {
290 $result = Find-CollectionManifestsCore -Channel 'Stable' -CollectionsDir $script:TempDir
291 $expSkip = $result.Skipped | Where-Object { $_.Id -eq 'exp-one' }
292 $expSkip.Reason | Should -BeLike '*experimental*'
293 }
294 }
295
296 Context 'Missing name falls back to id' {
297 BeforeEach {
298 $script:TempDir = Join-Path ([System.IO.Path]::GetTempPath()) "pester-$([Guid]::NewGuid().ToString('N').Substring(0,8))"
299 New-Item -ItemType Directory -Path $script:TempDir -Force | Out-Null
300
301 @"
302id: no-name-collection
303maturity: stable
304"@ | Set-Content -Path (Join-Path $script:TempDir 'noname.collection.yml')
305 }
306
307 AfterEach {
308 Remove-Item -Path $script:TempDir -Recurse -Force -ErrorAction SilentlyContinue
309 }
310
311 It 'Uses id as name when name field is missing' {
312 $result = Find-CollectionManifestsCore -CollectionsDir $script:TempDir
313 $result.MatrixItems[0].name | Should -Be 'no-name-collection'
314 }
315 }
316
317 Context 'Missing maturity defaults to stable' {
318 BeforeEach {
319 $script:TempDir = Join-Path ([System.IO.Path]::GetTempPath()) "pester-$([Guid]::NewGuid().ToString('N').Substring(0,8))"
320 New-Item -ItemType Directory -Path $script:TempDir -Force | Out-Null
321
322 @"
323id: no-maturity
324name: No Maturity
325"@ | Set-Content -Path (Join-Path $script:TempDir 'nomaturity.collection.yml')
326 }
327
328 AfterEach {
329 Remove-Item -Path $script:TempDir -Recurse -Force -ErrorAction SilentlyContinue
330 }
331
332 It 'Defaults maturity to stable when missing' {
333 $result = Find-CollectionManifestsCore -CollectionsDir $script:TempDir
334 $result.MatrixItems | Should -HaveCount 1
335 $result.MatrixItems[0].maturity | Should -Be 'stable'
336 }
337 }
338
339 Context 'Script guard execution with skipped collections' {
340 BeforeEach {
341 $script:TempDir = Join-Path ([System.IO.Path]::GetTempPath()) "pester-$([Guid]::NewGuid().ToString('N').Substring(0,8))"
342 New-Item -ItemType Directory -Path $script:TempDir -Force | Out-Null
343
344 @"
345id: stable-guard
346name: Stable Guard
347maturity: stable
348"@ | Set-Content -Path (Join-Path $script:TempDir 'stable.collection.yml')
349
350 @"
351id: dep-guard
352name: Deprecated Guard
353maturity: deprecated
354"@ | Set-Content -Path (Join-Path $script:TempDir 'dep.collection.yml')
355
356 $script:OutputFile = Join-Path $script:TempDir 'github_output'
357 New-Item -ItemType File -Path $script:OutputFile -Force | Out-Null
358 $env:GITHUB_OUTPUT = $script:OutputFile
359 $env:GITHUB_ACTIONS = 'true'
360 }
361
362 AfterEach {
363 $env:GITHUB_OUTPUT = $null
364 $env:GITHUB_ACTIONS = $null
365 Remove-Item -Path $script:TempDir -Recurse -Force -ErrorAction SilentlyContinue
366 }
367
368 It 'Writes matrix output to GITHUB_OUTPUT' {
369 & $script:ScriptPath -CollectionsDir $script:TempDir
370 $content = Get-Content $script:OutputFile -Raw
371 $content | Should -Match 'matrix='
372 }
373
374 It 'Emits notice for skipped collections' {
375 $output = & $script:ScriptPath -CollectionsDir $script:TempDir 6>&1 | Out-String
376 $output | Should -Match '::notice::Skipping Deprecated Guard'
377 }
378
379 It 'Outputs discovered collections JSON to host' {
380 $output = & $script:ScriptPath -CollectionsDir $script:TempDir 6>&1 | Out-String
381 $output | Should -Match 'Discovered collections:'
382 }
383 }
384}
385