microsoft/hve-core
Publicmirrored fromhttps://github.com/microsoft/hve-coreAvailable
scripts/tests/extension/Find-CollectionManifests.Tests.ps1
351lines · 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 | |
| 20 | BeforeAll { |
| 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 | |
| 31 | AfterAll { |
| 32 | Remove-Module CIHelpers -Force -ErrorAction SilentlyContinue |
| 33 | } |
| 34 | |
| 35 | Describe '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 | @" |
| 64 | id: test-collection |
| 65 | name: Test Collection |
| 66 | maturity: 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 | @" |
| 97 | id: old-collection |
| 98 | name: Old Collection |
| 99 | maturity: 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 'Experimental skipped for Stable channel' { |
| 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 | @" |
| 125 | id: exp-collection |
| 126 | name: Experimental Collection |
| 127 | maturity: experimental |
| 128 | "@ | Set-Content -Path (Join-Path $script:TempDir 'exp.collection.yml') |
| 129 | } |
| 130 | |
| 131 | AfterEach { |
| 132 | Remove-Item -Path $script:TempDir -Recurse -Force -ErrorAction SilentlyContinue |
| 133 | } |
| 134 | |
| 135 | It 'Excludes experimental from Stable channel matrix' { |
| 136 | $result = Find-CollectionManifestsCore -Channel 'Stable' -CollectionsDir $script:TempDir |
| 137 | $result.MatrixItems | Should -HaveCount 0 |
| 138 | } |
| 139 | |
| 140 | It 'Tracks experimental in Skipped with reason' { |
| 141 | $result = Find-CollectionManifestsCore -Channel 'Stable' -CollectionsDir $script:TempDir |
| 142 | $result.Skipped | Should -HaveCount 1 |
| 143 | $result.Skipped[0].Reason | Should -BeLike '*experimental*' |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | Context 'Experimental included for Preview channel' { |
| 148 | BeforeEach { |
| 149 | $script:TempDir = Join-Path ([System.IO.Path]::GetTempPath()) "pester-$([Guid]::NewGuid().ToString('N').Substring(0,8))" |
| 150 | New-Item -ItemType Directory -Path $script:TempDir -Force | Out-Null |
| 151 | |
| 152 | @" |
| 153 | id: exp-collection |
| 154 | name: Experimental Collection |
| 155 | maturity: experimental |
| 156 | "@ | Set-Content -Path (Join-Path $script:TempDir 'exp.collection.yml') |
| 157 | } |
| 158 | |
| 159 | AfterEach { |
| 160 | Remove-Item -Path $script:TempDir -Recurse -Force -ErrorAction SilentlyContinue |
| 161 | } |
| 162 | |
| 163 | It 'Includes experimental for Preview channel' { |
| 164 | $result = Find-CollectionManifestsCore -Channel 'Preview' -CollectionsDir $script:TempDir |
| 165 | $result.MatrixItems | Should -HaveCount 1 |
| 166 | $result.MatrixItems[0].id | Should -Be 'exp-collection' |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | Context 'Multiple collections produce correct matrix' { |
| 171 | BeforeEach { |
| 172 | $script:TempDir = Join-Path ([System.IO.Path]::GetTempPath()) "pester-$([Guid]::NewGuid().ToString('N').Substring(0,8))" |
| 173 | New-Item -ItemType Directory -Path $script:TempDir -Force | Out-Null |
| 174 | |
| 175 | @" |
| 176 | id: stable-one |
| 177 | name: Stable One |
| 178 | maturity: stable |
| 179 | "@ | Set-Content -Path (Join-Path $script:TempDir 'stable-one.collection.yml') |
| 180 | |
| 181 | @" |
| 182 | id: stable-two |
| 183 | name: Stable Two |
| 184 | maturity: stable |
| 185 | "@ | Set-Content -Path (Join-Path $script:TempDir 'stable-two.collection.yml') |
| 186 | |
| 187 | @" |
| 188 | id: deprecated-one |
| 189 | name: Deprecated One |
| 190 | maturity: deprecated |
| 191 | "@ | Set-Content -Path (Join-Path $script:TempDir 'deprecated-one.collection.yml') |
| 192 | } |
| 193 | |
| 194 | AfterEach { |
| 195 | Remove-Item -Path $script:TempDir -Recurse -Force -ErrorAction SilentlyContinue |
| 196 | } |
| 197 | |
| 198 | It 'Includes only non-deprecated collections' { |
| 199 | $result = Find-CollectionManifestsCore -CollectionsDir $script:TempDir |
| 200 | $result.MatrixItems | Should -HaveCount 2 |
| 201 | } |
| 202 | |
| 203 | It 'Produces valid matrix JSON' { |
| 204 | $result = Find-CollectionManifestsCore -CollectionsDir $script:TempDir |
| 205 | { $result.MatrixJson | ConvertFrom-Json } | Should -Not -Throw |
| 206 | } |
| 207 | |
| 208 | It 'Matrix JSON contains include array with correct count' { |
| 209 | $result = Find-CollectionManifestsCore -CollectionsDir $script:TempDir |
| 210 | $parsed = $result.MatrixJson | ConvertFrom-Json |
| 211 | $parsed.include | Should -HaveCount 2 |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | Context 'Skipped collections tracked with reasons' { |
| 216 | BeforeEach { |
| 217 | $script:TempDir = Join-Path ([System.IO.Path]::GetTempPath()) "pester-$([Guid]::NewGuid().ToString('N').Substring(0,8))" |
| 218 | New-Item -ItemType Directory -Path $script:TempDir -Force | Out-Null |
| 219 | |
| 220 | @" |
| 221 | id: good-one |
| 222 | name: Good One |
| 223 | maturity: stable |
| 224 | "@ | Set-Content -Path (Join-Path $script:TempDir 'good.collection.yml') |
| 225 | |
| 226 | @" |
| 227 | id: dep-one |
| 228 | name: Deprecated One |
| 229 | maturity: deprecated |
| 230 | "@ | Set-Content -Path (Join-Path $script:TempDir 'dep.collection.yml') |
| 231 | |
| 232 | @" |
| 233 | id: exp-one |
| 234 | name: Experimental One |
| 235 | maturity: experimental |
| 236 | "@ | Set-Content -Path (Join-Path $script:TempDir 'exp.collection.yml') |
| 237 | } |
| 238 | |
| 239 | AfterEach { |
| 240 | Remove-Item -Path $script:TempDir -Recurse -Force -ErrorAction SilentlyContinue |
| 241 | } |
| 242 | |
| 243 | It 'Tracks all skipped collections' { |
| 244 | $result = Find-CollectionManifestsCore -Channel 'Stable' -CollectionsDir $script:TempDir |
| 245 | $result.Skipped | Should -HaveCount 2 |
| 246 | $result.Skipped.Id | Should -Contain 'dep-one' |
| 247 | $result.Skipped.Id | Should -Contain 'exp-one' |
| 248 | } |
| 249 | |
| 250 | It 'Includes correct reason for deprecated' { |
| 251 | $result = Find-CollectionManifestsCore -Channel 'Stable' -CollectionsDir $script:TempDir |
| 252 | $depSkip = $result.Skipped | Where-Object { $_.Id -eq 'dep-one' } |
| 253 | $depSkip.Reason | Should -Be 'deprecated' |
| 254 | } |
| 255 | |
| 256 | It 'Includes correct reason for experimental' { |
| 257 | $result = Find-CollectionManifestsCore -Channel 'Stable' -CollectionsDir $script:TempDir |
| 258 | $expSkip = $result.Skipped | Where-Object { $_.Id -eq 'exp-one' } |
| 259 | $expSkip.Reason | Should -BeLike '*experimental*' |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | Context 'Missing name falls back to id' { |
| 264 | BeforeEach { |
| 265 | $script:TempDir = Join-Path ([System.IO.Path]::GetTempPath()) "pester-$([Guid]::NewGuid().ToString('N').Substring(0,8))" |
| 266 | New-Item -ItemType Directory -Path $script:TempDir -Force | Out-Null |
| 267 | |
| 268 | @" |
| 269 | id: no-name-collection |
| 270 | maturity: stable |
| 271 | "@ | Set-Content -Path (Join-Path $script:TempDir 'noname.collection.yml') |
| 272 | } |
| 273 | |
| 274 | AfterEach { |
| 275 | Remove-Item -Path $script:TempDir -Recurse -Force -ErrorAction SilentlyContinue |
| 276 | } |
| 277 | |
| 278 | It 'Uses id as name when name field is missing' { |
| 279 | $result = Find-CollectionManifestsCore -CollectionsDir $script:TempDir |
| 280 | $result.MatrixItems[0].name | Should -Be 'no-name-collection' |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | Context 'Missing maturity defaults to stable' { |
| 285 | BeforeEach { |
| 286 | $script:TempDir = Join-Path ([System.IO.Path]::GetTempPath()) "pester-$([Guid]::NewGuid().ToString('N').Substring(0,8))" |
| 287 | New-Item -ItemType Directory -Path $script:TempDir -Force | Out-Null |
| 288 | |
| 289 | @" |
| 290 | id: no-maturity |
| 291 | name: No Maturity |
| 292 | "@ | Set-Content -Path (Join-Path $script:TempDir 'nomaturity.collection.yml') |
| 293 | } |
| 294 | |
| 295 | AfterEach { |
| 296 | Remove-Item -Path $script:TempDir -Recurse -Force -ErrorAction SilentlyContinue |
| 297 | } |
| 298 | |
| 299 | It 'Defaults maturity to stable when missing' { |
| 300 | $result = Find-CollectionManifestsCore -CollectionsDir $script:TempDir |
| 301 | $result.MatrixItems | Should -HaveCount 1 |
| 302 | $result.MatrixItems[0].maturity | Should -Be 'stable' |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | Context 'Script guard execution with skipped collections' { |
| 307 | BeforeEach { |
| 308 | $script:TempDir = Join-Path ([System.IO.Path]::GetTempPath()) "pester-$([Guid]::NewGuid().ToString('N').Substring(0,8))" |
| 309 | New-Item -ItemType Directory -Path $script:TempDir -Force | Out-Null |
| 310 | |
| 311 | @" |
| 312 | id: stable-guard |
| 313 | name: Stable Guard |
| 314 | maturity: stable |
| 315 | "@ | Set-Content -Path (Join-Path $script:TempDir 'stable.collection.yml') |
| 316 | |
| 317 | @" |
| 318 | id: dep-guard |
| 319 | name: Deprecated Guard |
| 320 | maturity: deprecated |
| 321 | "@ | Set-Content -Path (Join-Path $script:TempDir 'dep.collection.yml') |
| 322 | |
| 323 | $script:OutputFile = Join-Path $script:TempDir 'github_output' |
| 324 | New-Item -ItemType File -Path $script:OutputFile -Force | Out-Null |
| 325 | $env:GITHUB_OUTPUT = $script:OutputFile |
| 326 | $env:GITHUB_ACTIONS = 'true' |
| 327 | } |
| 328 | |
| 329 | AfterEach { |
| 330 | $env:GITHUB_OUTPUT = $null |
| 331 | $env:GITHUB_ACTIONS = $null |
| 332 | Remove-Item -Path $script:TempDir -Recurse -Force -ErrorAction SilentlyContinue |
| 333 | } |
| 334 | |
| 335 | It 'Writes matrix output to GITHUB_OUTPUT' { |
| 336 | & $script:ScriptPath -CollectionsDir $script:TempDir |
| 337 | $content = Get-Content $script:OutputFile -Raw |
| 338 | $content | Should -Match 'matrix=' |
| 339 | } |
| 340 | |
| 341 | It 'Emits notice for skipped collections' { |
| 342 | $output = & $script:ScriptPath -CollectionsDir $script:TempDir 6>&1 | Out-String |
| 343 | $output | Should -Match '::notice::Skipping Deprecated Guard' |
| 344 | } |
| 345 | |
| 346 | It 'Outputs discovered collections JSON to host' { |
| 347 | $output = & $script:ScriptPath -CollectionsDir $script:TempDir 6>&1 | Out-String |
| 348 | $output | Should -Match 'Discovered collections:' |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |