microsoft/hve-core
Publicmirrored from https://github.com/microsoft/hve-coreAvailable
scripts/tests/extension/Package-Extension.Tests.ps1
604lines · modecode
| 1 | #Requires -Modules Pester |
| 2 | # Copyright (c) Microsoft Corporation. |
| 3 | # SPDX-License-Identifier: MIT |
| 4 | |
| 5 | BeforeAll { |
| 6 | . $PSScriptRoot/../../extension/Package-Extension.ps1 |
| 7 | Import-Module "$PSScriptRoot/../Mocks/GitMocks.psm1" -Force |
| 8 | Import-Module "$PSScriptRoot/../../lib/Modules/CIHelpers.psm1" -Force |
| 9 | } |
| 10 | |
| 11 | Describe 'Test-VsceAvailable' { |
| 12 | It 'Returns hashtable with IsAvailable property' { |
| 13 | $result = Test-VsceAvailable |
| 14 | $result | Should -BeOfType [hashtable] |
| 15 | $result.Keys | Should -Contain 'IsAvailable' |
| 16 | } |
| 17 | |
| 18 | It 'Returns CommandType when available' { |
| 19 | $result = Test-VsceAvailable |
| 20 | if ($result.IsAvailable) { |
| 21 | $result.CommandType | Should -BeIn @('npx', 'vsce') |
| 22 | $result.Command | Should -Not -BeNullOrEmpty |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | It 'Returns vsce when vsce command is found' { |
| 27 | Mock Get-Command { |
| 28 | param($Name, $ErrorAction) |
| 29 | $null = $ErrorAction # Suppress PSScriptAnalyzer warning |
| 30 | if ($Name -eq 'vsce') { |
| 31 | return [PSCustomObject]@{ Source = 'C:\bin\vsce.cmd' } |
| 32 | } |
| 33 | return $null |
| 34 | } |
| 35 | $result = Test-VsceAvailable |
| 36 | $result.IsAvailable | Should -BeTrue |
| 37 | $result.CommandType | Should -Be 'vsce' |
| 38 | $result.Command | Should -Be 'C:\bin\vsce.cmd' |
| 39 | } |
| 40 | |
| 41 | It 'Returns npx when only npx command is found' { |
| 42 | Mock Get-Command { |
| 43 | param($Name, $ErrorAction) |
| 44 | $null = $ErrorAction # Suppress PSScriptAnalyzer warning |
| 45 | if ($Name -eq 'npx') { |
| 46 | return [PSCustomObject]@{ Source = 'C:\bin\npx.cmd' } |
| 47 | } |
| 48 | return $null |
| 49 | } |
| 50 | $result = Test-VsceAvailable |
| 51 | $result.IsAvailable | Should -BeTrue |
| 52 | $result.CommandType | Should -Be 'npx' |
| 53 | $result.Command | Should -Be 'C:\bin\npx.cmd' |
| 54 | } |
| 55 | |
| 56 | It 'Returns not available when neither vsce nor npx exist' { |
| 57 | Mock Get-Command { return $null } |
| 58 | $result = Test-VsceAvailable |
| 59 | $result.IsAvailable | Should -BeFalse |
| 60 | $result.CommandType | Should -BeNullOrEmpty |
| 61 | $result.Command | Should -BeNullOrEmpty |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | Describe 'Get-ExtensionOutputPath' { |
| 66 | BeforeAll { |
| 67 | $script:testDir = [System.IO.Path]::GetTempPath().TrimEnd([System.IO.Path]::DirectorySeparatorChar) |
| 68 | } |
| 69 | |
| 70 | It 'Constructs correct output path' { |
| 71 | $result = Get-ExtensionOutputPath -ExtensionDirectory $script:testDir -ExtensionName 'my-extension' -PackageVersion '1.0.0' |
| 72 | $expected = [System.IO.Path]::Combine($script:testDir, 'my-extension-1.0.0.vsix') |
| 73 | $result | Should -Be $expected |
| 74 | } |
| 75 | |
| 76 | It 'Handles pre-release version numbers' { |
| 77 | $result = Get-ExtensionOutputPath -ExtensionDirectory $script:testDir -ExtensionName 'ext' -PackageVersion '2.1.0-preview.1' |
| 78 | $expected = [System.IO.Path]::Combine($script:testDir, 'ext-2.1.0-preview.1.vsix') |
| 79 | $result | Should -Be $expected |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | Describe 'Test-ExtensionManifestValid' { |
| 84 | It 'Returns valid result for proper manifest' { |
| 85 | $manifest = [PSCustomObject]@{ |
| 86 | name = 'my-extension' |
| 87 | version = '1.0.0' |
| 88 | publisher = 'my-publisher' |
| 89 | engines = [PSCustomObject]@{ vscode = '^1.80.0' } |
| 90 | } |
| 91 | $result = Test-ExtensionManifestValid -ManifestContent $manifest |
| 92 | $result.IsValid | Should -BeTrue |
| 93 | $result.Errors | Should -BeNullOrEmpty |
| 94 | } |
| 95 | |
| 96 | It 'Returns invalid when name missing' { |
| 97 | $manifest = @{ |
| 98 | version = '1.0.0' |
| 99 | publisher = 'pub' |
| 100 | engines = @{ vscode = '^1.80.0' } |
| 101 | } |
| 102 | $result = Test-ExtensionManifestValid -ManifestContent $manifest |
| 103 | $result.IsValid | Should -BeFalse |
| 104 | $result.Errors | Should -Contain "Missing required 'name' field" |
| 105 | } |
| 106 | |
| 107 | It 'Returns invalid when version missing' { |
| 108 | $manifest = @{ |
| 109 | name = 'ext' |
| 110 | publisher = 'pub' |
| 111 | engines = @{ vscode = '^1.80.0' } |
| 112 | } |
| 113 | $result = Test-ExtensionManifestValid -ManifestContent $manifest |
| 114 | $result.IsValid | Should -BeFalse |
| 115 | $result.Errors | Should -Contain "Missing required 'version' field" |
| 116 | } |
| 117 | |
| 118 | It 'Returns invalid when publisher missing' { |
| 119 | $manifest = @{ |
| 120 | name = 'ext' |
| 121 | version = '1.0.0' |
| 122 | engines = @{ vscode = '^1.80.0' } |
| 123 | } |
| 124 | $result = Test-ExtensionManifestValid -ManifestContent $manifest |
| 125 | $result.IsValid | Should -BeFalse |
| 126 | $result.Errors | Should -Contain "Missing required 'publisher' field" |
| 127 | } |
| 128 | |
| 129 | It 'Returns invalid when engines.vscode missing' { |
| 130 | $manifest = @{ |
| 131 | name = 'ext' |
| 132 | version = '1.0.0' |
| 133 | publisher = 'pub' |
| 134 | } |
| 135 | $result = Test-ExtensionManifestValid -ManifestContent $manifest |
| 136 | $result.IsValid | Should -BeFalse |
| 137 | $result.Errors | Should -Contain "Missing required 'engines' field" |
| 138 | } |
| 139 | |
| 140 | It 'Returns invalid when engines exists but vscode key missing' { |
| 141 | $manifest = [PSCustomObject]@{ |
| 142 | name = 'ext' |
| 143 | version = '1.0.0' |
| 144 | publisher = 'pub' |
| 145 | engines = [PSCustomObject]@{ node = '>=16' } |
| 146 | } |
| 147 | $result = Test-ExtensionManifestValid -ManifestContent $manifest |
| 148 | $result.IsValid | Should -BeFalse |
| 149 | $result.Errors | Should -Contain "Missing required 'engines.vscode' field" |
| 150 | } |
| 151 | |
| 152 | It 'Returns invalid when version format is incorrect' { |
| 153 | $manifest = [PSCustomObject]@{ |
| 154 | name = 'ext' |
| 155 | version = 'invalid-version' |
| 156 | publisher = 'pub' |
| 157 | engines = [PSCustomObject]@{ vscode = '^1.80.0' } |
| 158 | } |
| 159 | $result = Test-ExtensionManifestValid -ManifestContent $manifest |
| 160 | $result.IsValid | Should -BeFalse |
| 161 | $result.Errors | Should -Match 'Invalid version format' |
| 162 | } |
| 163 | |
| 164 | It 'Collects multiple errors' { |
| 165 | $manifest = @{} |
| 166 | $result = Test-ExtensionManifestValid -ManifestContent $manifest |
| 167 | $result.IsValid | Should -BeFalse |
| 168 | $result.Errors.Count | Should -BeGreaterThan 1 |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | Describe 'Get-VscePackageCommand' { |
| 173 | It 'Returns npx command structure for npx type' { |
| 174 | $result = Get-VscePackageCommand -CommandType 'npx' |
| 175 | $result.Executable | Should -Be 'npx' |
| 176 | $result.Arguments | Should -Contain '@vscode/vsce' |
| 177 | $result.Arguments | Should -Contain 'package' |
| 178 | } |
| 179 | |
| 180 | It 'Returns vsce command for vsce type' { |
| 181 | $result = Get-VscePackageCommand -CommandType 'vsce' |
| 182 | $result.Executable | Should -Be 'vsce' |
| 183 | $result.Arguments | Should -Contain 'package' |
| 184 | } |
| 185 | |
| 186 | It 'Includes --pre-release flag when specified' { |
| 187 | $result = Get-VscePackageCommand -CommandType 'npx' -PreRelease |
| 188 | $result.Arguments | Should -Contain '--pre-release' |
| 189 | } |
| 190 | |
| 191 | It 'Excludes --pre-release flag when not specified' { |
| 192 | $result = Get-VscePackageCommand -CommandType 'npx' |
| 193 | $result.Arguments | Should -Not -Contain '--pre-release' |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | Describe 'New-PackagingResult' { |
| 198 | BeforeAll { |
| 199 | $script:testVsixPath = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath().TrimEnd([System.IO.Path]::DirectorySeparatorChar), 'ext.vsix') |
| 200 | } |
| 201 | |
| 202 | It 'Creates success result with all properties' { |
| 203 | $result = New-PackagingResult -Success $true -OutputPath $script:testVsixPath -Version '1.0.0' -ErrorMessage $null |
| 204 | $result.Success | Should -BeTrue |
| 205 | $result.OutputPath | Should -Be $script:testVsixPath |
| 206 | $result.Version | Should -Be '1.0.0' |
| 207 | $result.ErrorMessage | Should -BeNullOrEmpty |
| 208 | } |
| 209 | |
| 210 | It 'Creates failure result with error message' { |
| 211 | $result = New-PackagingResult -Success $false -OutputPath $null -Version $null -ErrorMessage 'Packaging failed' |
| 212 | $result.Success | Should -BeFalse |
| 213 | $result.ErrorMessage | Should -Be 'Packaging failed' |
| 214 | } |
| 215 | |
| 216 | It 'Creates result with default empty strings for optional parameters' { |
| 217 | $result = New-PackagingResult -Success $true |
| 218 | $result.Success | Should -BeTrue |
| 219 | $result.OutputPath | Should -Be '' |
| 220 | $result.Version | Should -Be '' |
| 221 | $result.ErrorMessage | Should -Be '' |
| 222 | } |
| 223 | |
| 224 | It 'Creates failure result with only error message specified' { |
| 225 | $result = New-PackagingResult -Success $false -ErrorMessage 'Something went wrong' |
| 226 | $result.Success | Should -BeFalse |
| 227 | $result.ErrorMessage | Should -Be 'Something went wrong' |
| 228 | $result.OutputPath | Should -Be '' |
| 229 | $result.Version | Should -Be '' |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | Describe 'Get-ResolvedPackageVersion' { |
| 234 | It 'Returns specified version when provided' { |
| 235 | $result = Get-ResolvedPackageVersion -SpecifiedVersion '2.0.0' -ManifestVersion '1.0.0' -DevPatchNumber '' |
| 236 | $result.IsValid | Should -BeTrue |
| 237 | $result.PackageVersion | Should -Be '2.0.0' |
| 238 | } |
| 239 | |
| 240 | It 'Returns manifest version when no specified version' { |
| 241 | $result = Get-ResolvedPackageVersion -SpecifiedVersion '' -ManifestVersion '1.5.0' -DevPatchNumber '' |
| 242 | $result.IsValid | Should -BeTrue |
| 243 | $result.PackageVersion | Should -Be '1.5.0' |
| 244 | } |
| 245 | |
| 246 | It 'Applies dev patch number when provided' { |
| 247 | $result = Get-ResolvedPackageVersion -SpecifiedVersion '' -ManifestVersion '1.0.0' -DevPatchNumber '42' |
| 248 | $result.IsValid | Should -BeTrue |
| 249 | $result.PackageVersion | Should -Be '1.0.0-dev.42' |
| 250 | } |
| 251 | |
| 252 | It 'Specified version with dev patch appends dev suffix' { |
| 253 | $result = Get-ResolvedPackageVersion -SpecifiedVersion '3.0.0' -ManifestVersion '1.0.0' -DevPatchNumber '99' |
| 254 | $result.IsValid | Should -BeTrue |
| 255 | $result.PackageVersion | Should -Be '3.0.0-dev.99' |
| 256 | } |
| 257 | |
| 258 | It 'Returns invalid for malformed specified version' { |
| 259 | $result = Get-ResolvedPackageVersion -SpecifiedVersion 'not-a-version' -ManifestVersion '1.0.0' -DevPatchNumber '' |
| 260 | $result.IsValid | Should -BeFalse |
| 261 | $result.ErrorMessage | Should -Match 'Invalid version format specified' |
| 262 | } |
| 263 | |
| 264 | It 'Returns invalid for malformed manifest version when no specified version' { |
| 265 | $result = Get-ResolvedPackageVersion -SpecifiedVersion '' -ManifestVersion 'bad-version' -DevPatchNumber '' |
| 266 | $result.IsValid | Should -BeFalse |
| 267 | $result.ErrorMessage | Should -Match 'Invalid version format in package.json' |
| 268 | } |
| 269 | |
| 270 | It 'Extracts base version from manifest with prerelease suffix' { |
| 271 | $result = Get-ResolvedPackageVersion -SpecifiedVersion '' -ManifestVersion '1.2.3-beta.1' -DevPatchNumber '' |
| 272 | $result.IsValid | Should -BeTrue |
| 273 | $result.BaseVersion | Should -Be '1.2.3' |
| 274 | $result.PackageVersion | Should -Be '1.2.3' |
| 275 | } |
| 276 | |
| 277 | It 'Returns BaseVersion correctly when specified version provided' { |
| 278 | $result = Get-ResolvedPackageVersion -SpecifiedVersion '4.5.6' -ManifestVersion '1.0.0' -DevPatchNumber '' |
| 279 | $result.IsValid | Should -BeTrue |
| 280 | $result.BaseVersion | Should -Be '4.5.6' |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | Describe 'Invoke-PackageExtension' { |
| 285 | BeforeAll { |
| 286 | $script:testRoot = Join-Path ([System.IO.Path]::GetTempPath()) "pkg-ext-test-$([guid]::NewGuid().ToString('N').Substring(0,8))" |
| 287 | $script:extDir = Join-Path $script:testRoot 'extension' |
| 288 | $script:repoRoot = Join-Path $script:testRoot 'repo' |
| 289 | } |
| 290 | |
| 291 | BeforeEach { |
| 292 | # Create fresh test directories for each test |
| 293 | New-Item -Path $script:extDir -ItemType Directory -Force | Out-Null |
| 294 | New-Item -Path $script:repoRoot -ItemType Directory -Force | Out-Null |
| 295 | New-Item -Path (Join-Path $script:repoRoot '.github') -ItemType Directory -Force | Out-Null |
| 296 | New-Item -Path (Join-Path $script:repoRoot 'scripts/dev-tools') -ItemType Directory -Force | Out-Null |
| 297 | New-Item -Path (Join-Path $script:repoRoot 'docs/templates') -ItemType Directory -Force | Out-Null |
| 298 | } |
| 299 | |
| 300 | AfterEach { |
| 301 | if (Test-Path $script:testRoot) { |
| 302 | Remove-Item -Path $script:testRoot -Recurse -Force -ErrorAction SilentlyContinue |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | It 'Returns failure when extension directory does not exist' { |
| 307 | $nonexistentPath = Join-Path ([System.IO.Path]::GetTempPath()) "nonexistent-ext-$([guid]::NewGuid().ToString('N').Substring(0,8))" |
| 308 | $result = Invoke-PackageExtension -ExtensionDirectory $nonexistentPath -RepoRoot $script:repoRoot |
| 309 | $result.Success | Should -BeFalse |
| 310 | $result.ErrorMessage | Should -Match 'Extension directory not found' |
| 311 | } |
| 312 | |
| 313 | It 'Returns failure when package.json missing' { |
| 314 | $result = Invoke-PackageExtension -ExtensionDirectory $script:extDir -RepoRoot $script:repoRoot |
| 315 | $result.Success | Should -BeFalse |
| 316 | $result.ErrorMessage | Should -Match 'package.json not found' |
| 317 | } |
| 318 | |
| 319 | It 'Returns failure when .github directory missing' { |
| 320 | # Create package.json but remove .github |
| 321 | $manifest = @{ |
| 322 | name = 'test-ext' |
| 323 | version = '1.0.0' |
| 324 | publisher = 'test' |
| 325 | engines = @{ vscode = '^1.80.0' } |
| 326 | } |
| 327 | $manifest | ConvertTo-Json | Set-Content (Join-Path $script:extDir 'package.json') |
| 328 | Remove-Item -Path (Join-Path $script:repoRoot '.github') -Recurse -Force |
| 329 | |
| 330 | $result = Invoke-PackageExtension -ExtensionDirectory $script:extDir -RepoRoot $script:repoRoot |
| 331 | $result.Success | Should -BeFalse |
| 332 | $result.ErrorMessage | Should -Match '.github directory not found' |
| 333 | } |
| 334 | |
| 335 | It 'Returns failure for invalid JSON in package.json' { |
| 336 | Set-Content -Path (Join-Path $script:extDir 'package.json') -Value '{ invalid json }' |
| 337 | |
| 338 | $result = Invoke-PackageExtension -ExtensionDirectory $script:extDir -RepoRoot $script:repoRoot |
| 339 | $result.Success | Should -BeFalse |
| 340 | $result.ErrorMessage | Should -Match 'Failed to parse package.json' |
| 341 | } |
| 342 | |
| 343 | It 'Returns failure for invalid manifest missing required fields' { |
| 344 | $manifest = @{ name = 'only-name' } # Missing version, publisher, engines |
| 345 | $manifest | ConvertTo-Json | Set-Content (Join-Path $script:extDir 'package.json') |
| 346 | |
| 347 | $result = Invoke-PackageExtension -ExtensionDirectory $script:extDir -RepoRoot $script:repoRoot |
| 348 | $result.Success | Should -BeFalse |
| 349 | $result.ErrorMessage | Should -Match 'Invalid package.json' |
| 350 | } |
| 351 | |
| 352 | It 'Returns failure for invalid specified version format' { |
| 353 | $manifest = @{ |
| 354 | name = 'test-ext' |
| 355 | version = '1.0.0' |
| 356 | publisher = 'test' |
| 357 | engines = @{ vscode = '^1.80.0' } |
| 358 | } |
| 359 | $manifest | ConvertTo-Json | Set-Content (Join-Path $script:extDir 'package.json') |
| 360 | |
| 361 | $result = Invoke-PackageExtension -ExtensionDirectory $script:extDir -RepoRoot $script:repoRoot -Version 'invalid-version' |
| 362 | $result.Success | Should -BeFalse |
| 363 | $result.ErrorMessage | Should -Match 'Invalid version format' |
| 364 | } |
| 365 | |
| 366 | It 'Returns structured result hashtable with expected keys' { |
| 367 | Mock Test-VsceAvailable { return @{ IsAvailable = $false; CommandType = ''; Command = '' } } |
| 368 | |
| 369 | $manifest = @{ |
| 370 | name = 'test-ext' |
| 371 | version = '1.0.0' |
| 372 | publisher = 'test' |
| 373 | engines = @{ vscode = '^1.80.0' } |
| 374 | } |
| 375 | $manifest | ConvertTo-Json | Set-Content (Join-Path $script:extDir 'package.json') |
| 376 | |
| 377 | # Will fail at vsce availability check, validates structure |
| 378 | $result = Invoke-PackageExtension -ExtensionDirectory $script:extDir -RepoRoot $script:repoRoot |
| 379 | |
| 380 | $result | Should -BeOfType [hashtable] |
| 381 | $result.Keys | Should -Contain 'Success' |
| 382 | $result.Keys | Should -Contain 'ErrorMessage' |
| 383 | } |
| 384 | |
| 385 | It 'Applies DevPatchNumber to version correctly' { |
| 386 | Mock Test-VsceAvailable { return @{ IsAvailable = $false; CommandType = ''; Command = '' } } |
| 387 | |
| 388 | $manifest = @{ |
| 389 | name = 'test-ext' |
| 390 | version = '2.0.0' |
| 391 | publisher = 'test' |
| 392 | engines = @{ vscode = '^1.80.0' } |
| 393 | } |
| 394 | $manifest | ConvertTo-Json | Set-Content (Join-Path $script:extDir 'package.json') |
| 395 | |
| 396 | # Will fail at vsce availability check, validates version resolution path |
| 397 | $result = Invoke-PackageExtension -ExtensionDirectory $script:extDir -RepoRoot $script:repoRoot -DevPatchNumber '123' |
| 398 | |
| 399 | # Even on failure, the result indicates version was processed |
| 400 | $result | Should -BeOfType [hashtable] |
| 401 | } |
| 402 | |
| 403 | It 'Copies changelog when valid path provided' { |
| 404 | Mock Test-VsceAvailable { return @{ IsAvailable = $true; CommandType = 'vsce'; Command = 'vsce' } } |
| 405 | Mock Get-VscePackageCommand { return @{ Executable = 'echo'; Arguments = @('mocked') } } |
| 406 | |
| 407 | $manifest = @{ |
| 408 | name = 'test-ext' |
| 409 | version = '1.0.0' |
| 410 | publisher = 'test' |
| 411 | engines = @{ vscode = '^1.80.0' } |
| 412 | } |
| 413 | $manifest | ConvertTo-Json | Set-Content (Join-Path $script:extDir 'package.json') |
| 414 | |
| 415 | # Create a changelog file |
| 416 | $changelogPath = Join-Path $script:repoRoot 'CHANGELOG.md' |
| 417 | Set-Content -Path $changelogPath -Value '# Changelog' |
| 418 | |
| 419 | $result = Invoke-PackageExtension -ExtensionDirectory $script:extDir -RepoRoot $script:repoRoot -ChangelogPath $changelogPath |
| 420 | |
| 421 | # Changelog should be copied to extension directory |
| 422 | $destChangelog = Join-Path $script:extDir 'CHANGELOG.md' |
| 423 | Test-Path $destChangelog | Should -BeTrue |
| 424 | $result | Should -Not -BeNullOrEmpty |
| 425 | } |
| 426 | |
| 427 | It 'Warns when changelog path does not exist' { |
| 428 | Mock Test-VsceAvailable { return @{ IsAvailable = $true; CommandType = 'vsce'; Command = 'vsce' } } |
| 429 | Mock Get-VscePackageCommand { return @{ Executable = 'echo'; Arguments = @('mocked') } } |
| 430 | Mock Write-Warning { } |
| 431 | |
| 432 | $manifest = @{ |
| 433 | name = 'test-ext' |
| 434 | version = '1.0.0' |
| 435 | publisher = 'test' |
| 436 | engines = @{ vscode = '^1.80.0' } |
| 437 | } |
| 438 | $manifest | ConvertTo-Json | Set-Content (Join-Path $script:extDir 'package.json') |
| 439 | |
| 440 | $nonexistentChangelog = Join-Path ([System.IO.Path]::GetTempPath()) "changelog-$([guid]::NewGuid().ToString('N').Substring(0,8)).md" |
| 441 | $result = Invoke-PackageExtension -ExtensionDirectory $script:extDir -RepoRoot $script:repoRoot -ChangelogPath $nonexistentChangelog |
| 442 | |
| 443 | Should -Invoke Write-Warning -Times 1 |
| 444 | $result | Should -Not -BeNullOrEmpty |
| 445 | } |
| 446 | |
| 447 | It 'Returns failure when vsce command fails with non-zero exit code' { |
| 448 | Mock Test-VsceAvailable { return @{ IsAvailable = $true; CommandType = 'vsce'; Command = 'vsce' } } |
| 449 | Mock Get-VscePackageCommand { return @{ Executable = 'pwsh'; Arguments = @('-Command', 'exit 1') } } |
| 450 | |
| 451 | $manifest = @{ |
| 452 | name = 'test-ext' |
| 453 | version = '1.0.0' |
| 454 | publisher = 'test' |
| 455 | engines = @{ vscode = '^1.80.0' } |
| 456 | } |
| 457 | $manifest | ConvertTo-Json | Set-Content (Join-Path $script:extDir 'package.json') |
| 458 | |
| 459 | $result = Invoke-PackageExtension -ExtensionDirectory $script:extDir -RepoRoot $script:repoRoot |
| 460 | |
| 461 | $result.Success | Should -BeFalse |
| 462 | $result.ErrorMessage | Should -Match 'vsce package command failed|The term' |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | Describe 'CI Integration - Package-Extension' { |
| 467 | BeforeAll { |
| 468 | $script:testRoot = Join-Path ([System.IO.Path]::GetTempPath()) "ci-int-test-$([guid]::NewGuid().ToString('N').Substring(0,8))" |
| 469 | $script:extDir = Join-Path $script:testRoot 'extension' |
| 470 | $script:repoRoot = Join-Path $script:testRoot 'repo' |
| 471 | } |
| 472 | |
| 473 | AfterAll { |
| 474 | if (Test-Path $script:testRoot) { |
| 475 | Remove-Item -Path $script:testRoot -Recurse -Force -ErrorAction SilentlyContinue |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | Context 'GitHub Actions environment' { |
| 480 | BeforeEach { |
| 481 | Initialize-MockCIEnvironment |
| 482 | New-Item -Path $script:extDir -ItemType Directory -Force | Out-Null |
| 483 | New-Item -Path $script:repoRoot -ItemType Directory -Force | Out-Null |
| 484 | New-Item -Path (Join-Path $script:repoRoot '.github') -ItemType Directory -Force | Out-Null |
| 485 | New-Item -Path (Join-Path $script:repoRoot 'scripts/dev-tools') -ItemType Directory -Force | Out-Null |
| 486 | New-Item -Path (Join-Path $script:repoRoot 'docs/templates') -ItemType Directory -Force | Out-Null |
| 487 | |
| 488 | $manifest = @{ |
| 489 | name = 'test-ext' |
| 490 | version = '1.0.0' |
| 491 | publisher = 'test' |
| 492 | engines = @{ vscode = '^1.80.0' } |
| 493 | } |
| 494 | $manifest | ConvertTo-Json | Set-Content (Join-Path $script:extDir 'package.json') |
| 495 | } |
| 496 | |
| 497 | AfterEach { |
| 498 | Clear-MockCIEnvironment |
| 499 | if (Test-Path $script:testRoot) { |
| 500 | Remove-Item -Path $script:testRoot -Recurse -Force -ErrorAction SilentlyContinue |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | It 'Sets version output variable on successful package' { |
| 505 | Mock Test-VsceAvailable { return @{ IsAvailable = $true; CommandType = 'vsce'; Command = 'vsce' } } |
| 506 | Mock Get-VscePackageCommand { return @{ Executable = 'echo'; Arguments = @('mocked') } } |
| 507 | |
| 508 | $vsixPath = Join-Path $script:extDir 'test-ext-1.0.0.vsix' |
| 509 | Set-Content -Path $vsixPath -Value 'fake-vsix' |
| 510 | |
| 511 | $null = Invoke-PackageExtension -ExtensionDirectory $script:extDir -RepoRoot $script:repoRoot |
| 512 | |
| 513 | $outputContent = Get-Content $env:GITHUB_OUTPUT -Raw |
| 514 | $outputContent | Should -Match 'version=1\.0\.0' |
| 515 | } |
| 516 | |
| 517 | It 'Sets vsix-file output variable on successful package' { |
| 518 | Mock Test-VsceAvailable { return @{ IsAvailable = $true; CommandType = 'vsce'; Command = 'vsce' } } |
| 519 | Mock Get-VscePackageCommand { return @{ Executable = 'echo'; Arguments = @('mocked') } } |
| 520 | |
| 521 | $vsixPath = Join-Path $script:extDir 'test-ext-1.0.0.vsix' |
| 522 | Set-Content -Path $vsixPath -Value 'fake-vsix' |
| 523 | |
| 524 | $null = Invoke-PackageExtension -ExtensionDirectory $script:extDir -RepoRoot $script:repoRoot |
| 525 | |
| 526 | $outputContent = Get-Content $env:GITHUB_OUTPUT -Raw |
| 527 | $outputContent | Should -Match 'vsix-file=test-ext-1\.0\.0\.vsix' |
| 528 | } |
| 529 | |
| 530 | It 'Sets pre-release output variable when PreRelease specified' { |
| 531 | Mock Test-VsceAvailable { return @{ IsAvailable = $true; CommandType = 'vsce'; Command = 'vsce' } } |
| 532 | Mock Get-VscePackageCommand { return @{ Executable = 'echo'; Arguments = @('mocked') } } |
| 533 | |
| 534 | $vsixPath = Join-Path $script:extDir 'test-ext-1.0.0.vsix' |
| 535 | Set-Content -Path $vsixPath -Value 'fake-vsix' |
| 536 | |
| 537 | $null = Invoke-PackageExtension -ExtensionDirectory $script:extDir -RepoRoot $script:repoRoot -PreRelease |
| 538 | |
| 539 | $outputContent = Get-Content $env:GITHUB_OUTPUT -Raw |
| 540 | $outputContent | Should -Match 'pre-release=True' |
| 541 | } |
| 542 | |
| 543 | It 'Sets pre-release output variable to false when PreRelease not specified' { |
| 544 | Mock Test-VsceAvailable { return @{ IsAvailable = $true; CommandType = 'vsce'; Command = 'vsce' } } |
| 545 | Mock Get-VscePackageCommand { return @{ Executable = 'echo'; Arguments = @('mocked') } } |
| 546 | |
| 547 | $vsixPath = Join-Path $script:extDir 'test-ext-1.0.0.vsix' |
| 548 | Set-Content -Path $vsixPath -Value 'fake-vsix' |
| 549 | |
| 550 | $null = Invoke-PackageExtension -ExtensionDirectory $script:extDir -RepoRoot $script:repoRoot |
| 551 | |
| 552 | $outputContent = Get-Content $env:GITHUB_OUTPUT -Raw |
| 553 | $outputContent | Should -Match 'pre-release=False' |
| 554 | } |
| 555 | |
| 556 | It 'Returns failure result when vsce command fails' { |
| 557 | Mock Test-VsceAvailable { return @{ IsAvailable = $true; CommandType = 'vsce'; Command = 'vsce' } } |
| 558 | Mock Get-VscePackageCommand { return @{ Executable = 'pwsh'; Arguments = @('-Command', 'exit 1') } } |
| 559 | |
| 560 | $result = Invoke-PackageExtension -ExtensionDirectory $script:extDir -RepoRoot $script:repoRoot |
| 561 | |
| 562 | $result.Success | Should -BeFalse |
| 563 | $result.ErrorMessage | Should -Match 'vsce package command failed' |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | Context 'Local environment' { |
| 568 | BeforeEach { |
| 569 | Clear-MockCIEnvironment |
| 570 | |
| 571 | New-Item -Path $script:extDir -ItemType Directory -Force | Out-Null |
| 572 | New-Item -Path $script:repoRoot -ItemType Directory -Force | Out-Null |
| 573 | New-Item -Path (Join-Path $script:repoRoot '.github') -ItemType Directory -Force | Out-Null |
| 574 | New-Item -Path (Join-Path $script:repoRoot 'scripts/dev-tools') -ItemType Directory -Force | Out-Null |
| 575 | New-Item -Path (Join-Path $script:repoRoot 'docs/templates') -ItemType Directory -Force | Out-Null |
| 576 | |
| 577 | $manifest = @{ |
| 578 | name = 'test-ext' |
| 579 | version = '1.0.0' |
| 580 | publisher = 'test' |
| 581 | engines = @{ vscode = '^1.80.0' } |
| 582 | } |
| 583 | $manifest | ConvertTo-Json | Set-Content (Join-Path $script:extDir 'package.json') |
| 584 | } |
| 585 | |
| 586 | AfterEach { |
| 587 | if (Test-Path $script:testRoot) { |
| 588 | Remove-Item -Path $script:testRoot -Recurse -Force -ErrorAction SilentlyContinue |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | It 'Completes without error when not in CI environment' { |
| 593 | Mock Test-VsceAvailable { return @{ IsAvailable = $true; CommandType = 'vsce'; Command = 'vsce' } } |
| 594 | Mock Get-VscePackageCommand { return @{ Executable = 'echo'; Arguments = @('mocked') } } |
| 595 | |
| 596 | $vsixPath = Join-Path $script:extDir 'test-ext-1.0.0.vsix' |
| 597 | Set-Content -Path $vsixPath -Value 'fake-vsix' |
| 598 | |
| 599 | $result = Invoke-PackageExtension -ExtensionDirectory $script:extDir -RepoRoot $script:repoRoot |
| 600 | |
| 601 | $result.Success | Should -BeTrue |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |