microsoft/hve-core
Publicmirrored fromhttps://github.com/microsoft/hve-coreAvailable
scripts/tests/extension/Package-Extension.Tests.ps1
175lines · modecode
| 1 | #Requires -Modules Pester |
| 2 | |
| 3 | BeforeAll { |
| 4 | . $PSScriptRoot/../../extension/Package-Extension.ps1 |
| 5 | } |
| 6 | |
| 7 | Describe 'Test-VsceAvailable' { |
| 8 | It 'Returns hashtable with IsAvailable property' { |
| 9 | $result = Test-VsceAvailable |
| 10 | $result | Should -BeOfType [hashtable] |
| 11 | $result.Keys | Should -Contain 'IsAvailable' |
| 12 | } |
| 13 | |
| 14 | It 'Returns CommandType when available' { |
| 15 | $result = Test-VsceAvailable |
| 16 | if ($result.IsAvailable) { |
| 17 | $result.CommandType | Should -BeIn @('npx', 'global') |
| 18 | $result.Command | Should -Not -BeNullOrEmpty |
| 19 | } |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | Describe 'Get-ExtensionOutputPath' { |
| 24 | BeforeAll { |
| 25 | $script:testDir = [System.IO.Path]::GetTempPath().TrimEnd([System.IO.Path]::DirectorySeparatorChar) |
| 26 | } |
| 27 | |
| 28 | It 'Constructs correct output path' { |
| 29 | $result = Get-ExtensionOutputPath -ExtensionDirectory $script:testDir -ExtensionName 'my-extension' -PackageVersion '1.0.0' |
| 30 | $expected = [System.IO.Path]::Combine($script:testDir, 'my-extension-1.0.0.vsix') |
| 31 | $result | Should -Be $expected |
| 32 | } |
| 33 | |
| 34 | It 'Handles pre-release version numbers' { |
| 35 | $result = Get-ExtensionOutputPath -ExtensionDirectory $script:testDir -ExtensionName 'ext' -PackageVersion '2.1.0-preview.1' |
| 36 | $expected = [System.IO.Path]::Combine($script:testDir, 'ext-2.1.0-preview.1.vsix') |
| 37 | $result | Should -Be $expected |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | Describe 'Test-ExtensionManifestValid' { |
| 42 | It 'Returns valid result for proper manifest' { |
| 43 | $manifest = [PSCustomObject]@{ |
| 44 | name = 'my-extension' |
| 45 | version = '1.0.0' |
| 46 | publisher = 'my-publisher' |
| 47 | engines = [PSCustomObject]@{ vscode = '^1.80.0' } |
| 48 | } |
| 49 | $result = Test-ExtensionManifestValid -ManifestContent $manifest |
| 50 | $result.IsValid | Should -BeTrue |
| 51 | $result.Errors | Should -BeNullOrEmpty |
| 52 | } |
| 53 | |
| 54 | It 'Returns invalid when name missing' { |
| 55 | $manifest = @{ |
| 56 | version = '1.0.0' |
| 57 | publisher = 'pub' |
| 58 | engines = @{ vscode = '^1.80.0' } |
| 59 | } |
| 60 | $result = Test-ExtensionManifestValid -ManifestContent $manifest |
| 61 | $result.IsValid | Should -BeFalse |
| 62 | $result.Errors | Should -Contain "Missing required 'name' field" |
| 63 | } |
| 64 | |
| 65 | It 'Returns invalid when version missing' { |
| 66 | $manifest = @{ |
| 67 | name = 'ext' |
| 68 | publisher = 'pub' |
| 69 | engines = @{ vscode = '^1.80.0' } |
| 70 | } |
| 71 | $result = Test-ExtensionManifestValid -ManifestContent $manifest |
| 72 | $result.IsValid | Should -BeFalse |
| 73 | $result.Errors | Should -Contain "Missing required 'version' field" |
| 74 | } |
| 75 | |
| 76 | It 'Returns invalid when publisher missing' { |
| 77 | $manifest = @{ |
| 78 | name = 'ext' |
| 79 | version = '1.0.0' |
| 80 | engines = @{ vscode = '^1.80.0' } |
| 81 | } |
| 82 | $result = Test-ExtensionManifestValid -ManifestContent $manifest |
| 83 | $result.IsValid | Should -BeFalse |
| 84 | $result.Errors | Should -Contain "Missing required 'publisher' field" |
| 85 | } |
| 86 | |
| 87 | It 'Returns invalid when engines.vscode missing' { |
| 88 | $manifest = @{ |
| 89 | name = 'ext' |
| 90 | version = '1.0.0' |
| 91 | publisher = 'pub' |
| 92 | } |
| 93 | $result = Test-ExtensionManifestValid -ManifestContent $manifest |
| 94 | $result.IsValid | Should -BeFalse |
| 95 | $result.Errors | Should -Contain "Missing required 'engines' field" |
| 96 | } |
| 97 | |
| 98 | It 'Collects multiple errors' { |
| 99 | $manifest = @{} |
| 100 | $result = Test-ExtensionManifestValid -ManifestContent $manifest |
| 101 | $result.IsValid | Should -BeFalse |
| 102 | $result.Errors.Count | Should -BeGreaterThan 1 |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | Describe 'Get-VscePackageCommand' { |
| 107 | It 'Returns npx command structure for npx type' { |
| 108 | $result = Get-VscePackageCommand -CommandType 'npx' |
| 109 | $result.Executable | Should -Be 'npx' |
| 110 | $result.Arguments | Should -Contain '@vscode/vsce' |
| 111 | $result.Arguments | Should -Contain 'package' |
| 112 | } |
| 113 | |
| 114 | It 'Returns vsce command for vsce type' { |
| 115 | $result = Get-VscePackageCommand -CommandType 'vsce' |
| 116 | $result.Executable | Should -Be 'vsce' |
| 117 | $result.Arguments | Should -Contain 'package' |
| 118 | } |
| 119 | |
| 120 | It 'Includes --pre-release flag when specified' { |
| 121 | $result = Get-VscePackageCommand -CommandType 'npx' -PreRelease |
| 122 | $result.Arguments | Should -Contain '--pre-release' |
| 123 | } |
| 124 | |
| 125 | It 'Excludes --pre-release flag when not specified' { |
| 126 | $result = Get-VscePackageCommand -CommandType 'npx' |
| 127 | $result.Arguments | Should -Not -Contain '--pre-release' |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | Describe 'New-PackagingResult' { |
| 132 | BeforeAll { |
| 133 | $script:testVsixPath = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath().TrimEnd([System.IO.Path]::DirectorySeparatorChar), 'ext.vsix') |
| 134 | } |
| 135 | |
| 136 | It 'Creates success result with all properties' { |
| 137 | $result = New-PackagingResult -Success $true -OutputPath $script:testVsixPath -Version '1.0.0' -ErrorMessage $null |
| 138 | $result.Success | Should -BeTrue |
| 139 | $result.OutputPath | Should -Be $script:testVsixPath |
| 140 | $result.Version | Should -Be '1.0.0' |
| 141 | $result.ErrorMessage | Should -BeNullOrEmpty |
| 142 | } |
| 143 | |
| 144 | It 'Creates failure result with error message' { |
| 145 | $result = New-PackagingResult -Success $false -OutputPath $null -Version $null -ErrorMessage 'Packaging failed' |
| 146 | $result.Success | Should -BeFalse |
| 147 | $result.ErrorMessage | Should -Be 'Packaging failed' |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | Describe 'Get-ResolvedPackageVersion' { |
| 152 | It 'Returns specified version when provided' { |
| 153 | $result = Get-ResolvedPackageVersion -SpecifiedVersion '2.0.0' -ManifestVersion '1.0.0' -DevPatchNumber '' |
| 154 | $result.IsValid | Should -BeTrue |
| 155 | $result.PackageVersion | Should -Be '2.0.0' |
| 156 | } |
| 157 | |
| 158 | It 'Returns manifest version when no specified version' { |
| 159 | $result = Get-ResolvedPackageVersion -SpecifiedVersion '' -ManifestVersion '1.5.0' -DevPatchNumber '' |
| 160 | $result.IsValid | Should -BeTrue |
| 161 | $result.PackageVersion | Should -Be '1.5.0' |
| 162 | } |
| 163 | |
| 164 | It 'Applies dev patch number when provided' { |
| 165 | $result = Get-ResolvedPackageVersion -SpecifiedVersion '' -ManifestVersion '1.0.0' -DevPatchNumber '42' |
| 166 | $result.IsValid | Should -BeTrue |
| 167 | $result.PackageVersion | Should -Be '1.0.0-dev.42' |
| 168 | } |
| 169 | |
| 170 | It 'Specified version with dev patch appends dev suffix' { |
| 171 | $result = Get-ResolvedPackageVersion -SpecifiedVersion '3.0.0' -ManifestVersion '1.0.0' -DevPatchNumber '99' |
| 172 | $result.IsValid | Should -BeTrue |
| 173 | $result.PackageVersion | Should -Be '3.0.0-dev.99' |
| 174 | } |
| 175 | } |
| 176 | |