|
| 1 | +#Requires -Version 7.0 |
| 2 | +<# |
| 3 | +.How-To-Run |
| 4 | +This test file uses Pester, a testing framework for PowerShell. |
| 5 | +For more information about Pester, see: https://pester.dev/docs/quick-start |
| 6 | +
|
| 7 | +First, ensure you have `pester` installed: |
| 8 | +
|
| 9 | +`Install-Module Pester -Force` |
| 10 | +
|
| 11 | +Then invoke tests with: |
| 12 | +
|
| 13 | +`Invoke-Pester ./Update-PackageVersionSuffix.tests.ps1` |
| 14 | +
|
| 15 | +#> |
| 16 | + |
| 17 | +. (Join-Path $PSScriptRoot ".." ".." "common" "scripts" "Helpers" PSModule-Helpers.ps1) |
| 18 | +Install-ModuleIfNotInstalled "Pester" "5.3.3" | Import-Module |
| 19 | + |
| 20 | +# Source the function from the main script |
| 21 | +$scriptContent = Get-Content (Join-Path $PSScriptRoot "../Invoke-GenerateAndBuildV2.ps1") -Raw |
| 22 | +$functionPattern = '(?s)function Update-PackageVersionSuffix \{.*?\n\}\n' |
| 23 | +if ($scriptContent -match $functionPattern) { |
| 24 | + $functionCode = $Matches[0] |
| 25 | + Invoke-Expression $functionCode |
| 26 | +} else { |
| 27 | + throw "Could not extract Update-PackageVersionSuffix function from Invoke-GenerateAndBuildV2.ps1" |
| 28 | +} |
| 29 | + |
| 30 | +Describe "Update-PackageVersionSuffix" { |
| 31 | + BeforeAll { |
| 32 | + $testDir = Join-Path ([System.IO.Path]::GetTempPath()) "PackageVersionSuffixTests" |
| 33 | + if (Test-Path $testDir) { |
| 34 | + Remove-Item $testDir -Recurse -Force |
| 35 | + } |
| 36 | + New-Item -Path $testDir -ItemType Directory | Out-Null |
| 37 | + } |
| 38 | + |
| 39 | + AfterAll { |
| 40 | + if (Test-Path $testDir) { |
| 41 | + Remove-Item $testDir -Recurse -Force |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + Context "When sdkReleaseType is 'beta'" { |
| 46 | + It "Should add beta suffix to stable version" { |
| 47 | + $testCsproj = Join-Path $testDir "test-add-beta.csproj" |
| 48 | + $csprojContent = @" |
| 49 | +<Project Sdk="Microsoft.NET.Sdk"> |
| 50 | + <PropertyGroup> |
| 51 | + <Version>1.0.0</Version> |
| 52 | + </PropertyGroup> |
| 53 | +</Project> |
| 54 | +"@ |
| 55 | + Set-Content -Path $testCsproj -Value $csprojContent |
| 56 | + |
| 57 | + Update-PackageVersionSuffix -csprojPath $testCsproj -sdkReleaseType "beta" |
| 58 | + |
| 59 | + [xml]$updatedCsproj = Get-Content $testCsproj |
| 60 | + $version = $updatedCsproj.Project.PropertyGroup.Version |
| 61 | + $version | Should -Be "1.0.0-beta.1" |
| 62 | + } |
| 63 | + |
| 64 | + It "Should not modify version that already has beta suffix" { |
| 65 | + $testCsproj = Join-Path $testDir "test-keep-beta.csproj" |
| 66 | + $csprojContent = @" |
| 67 | +<Project Sdk="Microsoft.NET.Sdk"> |
| 68 | + <PropertyGroup> |
| 69 | + <Version>1.2.0-beta.3</Version> |
| 70 | + </PropertyGroup> |
| 71 | +</Project> |
| 72 | +"@ |
| 73 | + Set-Content -Path $testCsproj -Value $csprojContent |
| 74 | + |
| 75 | + Update-PackageVersionSuffix -csprojPath $testCsproj -sdkReleaseType "beta" |
| 76 | + |
| 77 | + [xml]$updatedCsproj = Get-Content $testCsproj |
| 78 | + $version = $updatedCsproj.Project.PropertyGroup.Version |
| 79 | + $version | Should -Be "1.2.0-beta.3" |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + Context "When sdkReleaseType is 'stable'" { |
| 84 | + It "Should remove beta suffix from version" { |
| 85 | + $testCsproj = Join-Path $testDir "test-remove-beta.csproj" |
| 86 | + $csprojContent = @" |
| 87 | +<Project Sdk="Microsoft.NET.Sdk"> |
| 88 | + <PropertyGroup> |
| 89 | + <Version>2.0.0-beta.5</Version> |
| 90 | + </PropertyGroup> |
| 91 | +</Project> |
| 92 | +"@ |
| 93 | + Set-Content -Path $testCsproj -Value $csprojContent |
| 94 | + |
| 95 | + Update-PackageVersionSuffix -csprojPath $testCsproj -sdkReleaseType "stable" |
| 96 | + |
| 97 | + [xml]$updatedCsproj = Get-Content $testCsproj |
| 98 | + $version = $updatedCsproj.Project.PropertyGroup.Version |
| 99 | + $version | Should -Be "2.0.0" |
| 100 | + } |
| 101 | + |
| 102 | + It "Should not modify stable version" { |
| 103 | + $testCsproj = Join-Path $testDir "test-keep-stable.csproj" |
| 104 | + $csprojContent = @" |
| 105 | +<Project Sdk="Microsoft.NET.Sdk"> |
| 106 | + <PropertyGroup> |
| 107 | + <Version>3.1.0</Version> |
| 108 | + </PropertyGroup> |
| 109 | +</Project> |
| 110 | +"@ |
| 111 | + Set-Content -Path $testCsproj -Value $csprojContent |
| 112 | + |
| 113 | + Update-PackageVersionSuffix -csprojPath $testCsproj -sdkReleaseType "stable" |
| 114 | + |
| 115 | + [xml]$updatedCsproj = Get-Content $testCsproj |
| 116 | + $version = $updatedCsproj.Project.PropertyGroup.Version |
| 117 | + $version | Should -Be "3.1.0" |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + Context "Edge cases" { |
| 122 | + It "Should handle missing sdkReleaseType" { |
| 123 | + $testCsproj = Join-Path $testDir "test-no-type.csproj" |
| 124 | + $csprojContent = @" |
| 125 | +<Project Sdk="Microsoft.NET.Sdk"> |
| 126 | + <PropertyGroup> |
| 127 | + <Version>1.0.0</Version> |
| 128 | + </PropertyGroup> |
| 129 | +</Project> |
| 130 | +"@ |
| 131 | + Set-Content -Path $testCsproj -Value $csprojContent |
| 132 | + |
| 133 | + Update-PackageVersionSuffix -csprojPath $testCsproj -sdkReleaseType "" |
| 134 | + |
| 135 | + [xml]$updatedCsproj = Get-Content $testCsproj |
| 136 | + $version = $updatedCsproj.Project.PropertyGroup.Version |
| 137 | + $version | Should -Be "1.0.0" |
| 138 | + } |
| 139 | + |
| 140 | + It "Should handle unknown sdkReleaseType" { |
| 141 | + $testCsproj = Join-Path $testDir "test-unknown-type.csproj" |
| 142 | + $csprojContent = @" |
| 143 | +<Project Sdk="Microsoft.NET.Sdk"> |
| 144 | + <PropertyGroup> |
| 145 | + <Version>1.0.0</Version> |
| 146 | + </PropertyGroup> |
| 147 | +</Project> |
| 148 | +"@ |
| 149 | + Set-Content -Path $testCsproj -Value $csprojContent |
| 150 | + |
| 151 | + Update-PackageVersionSuffix -csprojPath $testCsproj -sdkReleaseType "unknown" |
| 152 | + |
| 153 | + [xml]$updatedCsproj = Get-Content $testCsproj |
| 154 | + $version = $updatedCsproj.Project.PropertyGroup.Version |
| 155 | + $version | Should -Be "1.0.0" |
| 156 | + } |
| 157 | + |
| 158 | + It "Should handle missing csproj file" { |
| 159 | + $testCsproj = Join-Path $testDir "nonexistent.csproj" |
| 160 | + |
| 161 | + { Update-PackageVersionSuffix -csprojPath $testCsproj -sdkReleaseType "beta" } | Should -Not -Throw |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments