microsoft/hve-core
Publicmirrored fromhttps://github.com/microsoft/hve-coreAvailable
scripts/tests/release/Update-VersionFiles.Tests.ps1
197lines · modecode
| 1 | #Requires -Modules Pester |
| 2 | # Copyright (c) Microsoft Corporation. |
| 3 | # SPDX-License-Identifier: MIT |
| 4 | |
| 5 | BeforeAll { |
| 6 | $script:ScriptPath = Join-Path $PSScriptRoot '../../release/Update-VersionFiles.ps1' |
| 7 | # Pass a dummy version to satisfy the mandatory parameter during dot-source. |
| 8 | # The main execution guard prevents any file changes. |
| 9 | . $script:ScriptPath -Version '0.0.0' |
| 10 | Mock Write-Host {} |
| 11 | } |
| 12 | |
| 13 | Describe 'Resolve-RepoRoot' -Tag 'Unit' { |
| 14 | It 'Returns the supplied path when provided' { |
| 15 | $tempDir = Join-Path ([System.IO.Path]::GetTempPath()) "rr-$([guid]::NewGuid())" |
| 16 | New-Item -ItemType Directory -Path $tempDir -Force | Out-Null |
| 17 | try { |
| 18 | $result = Resolve-RepoRoot -Supplied $tempDir |
| 19 | $result | Should -Be (Resolve-Path $tempDir).Path |
| 20 | } |
| 21 | finally { |
| 22 | Remove-Item -Recurse -Force $tempDir |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | It 'Auto-detects repo root when no path is supplied' { |
| 27 | $result = Resolve-RepoRoot -Supplied '' |
| 28 | $result | Should -Not -BeNullOrEmpty |
| 29 | Test-Path (Join-Path $result '.git') | Should -BeTrue |
| 30 | } |
| 31 | |
| 32 | It 'Throws when auto-detection fails and no path is supplied' { |
| 33 | Mock Resolve-Path { return [PSCustomObject]@{ Path = '/nonexistent/path' } } -ParameterFilter { |
| 34 | $Path -like "*../..*" |
| 35 | } |
| 36 | Mock Test-Path { return $false } -ParameterFilter { |
| 37 | $Path -like "*/.git" |
| 38 | } |
| 39 | { Resolve-RepoRoot -Supplied '' } | Should -Throw "*Unable to determine repository root*" |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | Describe 'Update-JsonVersion' -Tag 'Unit' { |
| 44 | BeforeAll { |
| 45 | $script:TempDir = Join-Path ([System.IO.Path]::GetTempPath()) "ujv-$([guid]::NewGuid())" |
| 46 | New-Item -ItemType Directory -Path $script:TempDir -Force | Out-Null |
| 47 | } |
| 48 | |
| 49 | AfterAll { |
| 50 | if (Test-Path $script:TempDir) { |
| 51 | Remove-Item -Recurse -Force $script:TempDir |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | It 'Updates a simple version field' { |
| 56 | $filePath = Join-Path $script:TempDir 'simple.json' |
| 57 | @{ version = '1.0.0'; name = 'test' } | ConvertTo-Json | Set-Content $filePath |
| 58 | |
| 59 | Update-JsonVersion -FilePath $filePath -Description 'simple.json' -Transform { |
| 60 | param($j) $j.version = '2.0.0'; $j |
| 61 | } |
| 62 | |
| 63 | $result = Get-Content -Raw $filePath | ConvertFrom-Json |
| 64 | $result.version | Should -Be '2.0.0' |
| 65 | $result.name | Should -Be 'test' |
| 66 | } |
| 67 | |
| 68 | It 'Skips without error when file does not exist' { |
| 69 | $missingPath = Join-Path $script:TempDir 'does-not-exist.json' |
| 70 | { Update-JsonVersion -FilePath $missingPath -Description 'missing' -Transform { param($j) $j } } | |
| 71 | Should -Not -Throw |
| 72 | } |
| 73 | |
| 74 | It 'Updates nested properties via transform' { |
| 75 | $filePath = Join-Path $script:TempDir 'nested.json' |
| 76 | @{ |
| 77 | metadata = @{ version = '1.0.0' } |
| 78 | plugins = @(@{ version = '1.0.0'; id = 'p1' }) |
| 79 | } | ConvertTo-Json -Depth 10 | Set-Content $filePath |
| 80 | |
| 81 | Update-JsonVersion -FilePath $filePath -Description 'nested.json' -Transform { |
| 82 | param($j) |
| 83 | $j.metadata.version = '3.0.0' |
| 84 | foreach ($p in $j.plugins) { $p.version = '3.0.0' } |
| 85 | $j |
| 86 | } |
| 87 | |
| 88 | $result = Get-Content -Raw $filePath | ConvertFrom-Json -Depth 10 |
| 89 | $result.metadata.version | Should -Be '3.0.0' |
| 90 | $result.plugins[0].version | Should -Be '3.0.0' |
| 91 | } |
| 92 | |
| 93 | It 'Preserves dot-key in release-please manifest' { |
| 94 | $filePath = Join-Path $script:TempDir 'manifest.json' |
| 95 | @{ '.' = '1.0.0' } | ConvertTo-Json | Set-Content $filePath |
| 96 | |
| 97 | Update-JsonVersion -FilePath $filePath -Description 'manifest' -Transform { |
| 98 | param($j) $j.'.' = '4.0.0'; $j |
| 99 | } |
| 100 | |
| 101 | $result = Get-Content -Raw $filePath | ConvertFrom-Json |
| 102 | $result.'.' | Should -Be '4.0.0' |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | Describe 'Update-VersionFiles script execution' -Tag 'Unit' { |
| 107 | BeforeAll { |
| 108 | $script:FakeRoot = Join-Path ([System.IO.Path]::GetTempPath()) "uvf-$([guid]::NewGuid())" |
| 109 | New-Item -ItemType Directory -Path $script:FakeRoot -Force | Out-Null |
| 110 | New-Item -ItemType Directory -Path (Join-Path $script:FakeRoot '.git') -Force | Out-Null |
| 111 | New-Item -ItemType Directory -Path (Join-Path $script:FakeRoot 'extension/templates') -Force | Out-Null |
| 112 | New-Item -ItemType Directory -Path (Join-Path $script:FakeRoot '.github/plugin') -Force | Out-Null |
| 113 | New-Item -ItemType Directory -Path (Join-Path $script:FakeRoot 'plugins/hve-core/.github/plugin') -Force | Out-Null |
| 114 | New-Item -ItemType Directory -Path (Join-Path $script:FakeRoot 'plugins/ado/.github/plugin') -Force | Out-Null |
| 115 | |
| 116 | # Seed all 5 version file types at 1.0.0 |
| 117 | @{ version = '1.0.0'; name = 'hve-core' } | |
| 118 | ConvertTo-Json | Set-Content (Join-Path $script:FakeRoot 'package.json') |
| 119 | @{ version = '1.0.0' } | |
| 120 | ConvertTo-Json | Set-Content (Join-Path $script:FakeRoot 'extension/templates/package.template.json') |
| 121 | @{ |
| 122 | metadata = @{ version = '1.0.0' } |
| 123 | plugins = @( |
| 124 | @{ version = '1.0.0'; id = 'hve-core' } |
| 125 | @{ version = '1.0.0'; id = 'ado' } |
| 126 | ) |
| 127 | } | ConvertTo-Json -Depth 10 | Set-Content (Join-Path $script:FakeRoot '.github/plugin/marketplace.json') |
| 128 | @{ version = '1.0.0' } | |
| 129 | ConvertTo-Json | Set-Content (Join-Path $script:FakeRoot 'plugins/hve-core/.github/plugin/plugin.json') |
| 130 | @{ version = '1.0.0' } | |
| 131 | ConvertTo-Json | Set-Content (Join-Path $script:FakeRoot 'plugins/ado/.github/plugin/plugin.json') |
| 132 | @{ '.' = '1.0.0' } | |
| 133 | ConvertTo-Json | Set-Content (Join-Path $script:FakeRoot '.release-please-manifest.json') |
| 134 | } |
| 135 | |
| 136 | AfterAll { |
| 137 | if (Test-Path $script:FakeRoot) { |
| 138 | Remove-Item -Recurse -Force $script:FakeRoot |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | It 'Updates all version files to the target version' { |
| 143 | & $script:ScriptPath -Version '2.5.0' -RepoRoot $script:FakeRoot -SkipPluginGenerate |
| 144 | |
| 145 | $pkg = Get-Content -Raw (Join-Path $script:FakeRoot 'package.json') | ConvertFrom-Json |
| 146 | $pkg.version | Should -Be '2.5.0' |
| 147 | $pkg.name | Should -Be 'hve-core' |
| 148 | |
| 149 | $tmpl = Get-Content -Raw (Join-Path $script:FakeRoot 'extension/templates/package.template.json') | ConvertFrom-Json |
| 150 | $tmpl.version | Should -Be '2.5.0' |
| 151 | |
| 152 | $mkt = Get-Content -Raw (Join-Path $script:FakeRoot '.github/plugin/marketplace.json') | ConvertFrom-Json -Depth 10 |
| 153 | $mkt.metadata.version | Should -Be '2.5.0' |
| 154 | $mkt.plugins[0].version | Should -Be '2.5.0' |
| 155 | $mkt.plugins[1].version | Should -Be '2.5.0' |
| 156 | |
| 157 | $manifest = Get-Content -Raw (Join-Path $script:FakeRoot '.release-please-manifest.json') | ConvertFrom-Json |
| 158 | $manifest.'.' | Should -Be '2.5.0' |
| 159 | } |
| 160 | |
| 161 | It 'Updates multiple plugin.json files under plugins/' { |
| 162 | $p1 = Get-Content -Raw (Join-Path $script:FakeRoot 'plugins/hve-core/.github/plugin/plugin.json') | ConvertFrom-Json |
| 163 | $p1.version | Should -Be '2.5.0' |
| 164 | |
| 165 | $p2 = Get-Content -Raw (Join-Path $script:FakeRoot 'plugins/ado/.github/plugin/plugin.json') | ConvertFrom-Json |
| 166 | $p2.version | Should -Be '2.5.0' |
| 167 | } |
| 168 | |
| 169 | It 'Succeeds when optional files are missing' { |
| 170 | $sparseRoot = Join-Path ([System.IO.Path]::GetTempPath()) "uvf-sparse-$([guid]::NewGuid())" |
| 171 | New-Item -ItemType Directory -Path $sparseRoot -Force | Out-Null |
| 172 | New-Item -ItemType Directory -Path (Join-Path $sparseRoot '.git') -Force | Out-Null |
| 173 | |
| 174 | # Only create package.json — other files are absent |
| 175 | @{ version = '1.0.0' } | ConvertTo-Json | Set-Content (Join-Path $sparseRoot 'package.json') |
| 176 | |
| 177 | try { |
| 178 | { & $script:ScriptPath -Version '3.0.0' -RepoRoot $sparseRoot -SkipPluginGenerate } | |
| 179 | Should -Not -Throw |
| 180 | |
| 181 | $pkg = Get-Content -Raw (Join-Path $sparseRoot 'package.json') | ConvertFrom-Json |
| 182 | $pkg.version | Should -Be '3.0.0' |
| 183 | } |
| 184 | finally { |
| 185 | Remove-Item -Recurse -Force $sparseRoot |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | It 'Rejects invalid version "<Version>"' -ForEach @( |
| 190 | @{ Version = 'abc' } |
| 191 | @{ Version = '1.2' } |
| 192 | @{ Version = 'v1.2.3' } |
| 193 | ) { |
| 194 | { & $script:ScriptPath -Version $Version -RepoRoot $script:FakeRoot -SkipPluginGenerate } | |
| 195 | Should -Throw |
| 196 | } |
| 197 | } |
| 198 | |