From d1adb33c9bba7d5c329773431394fdf770777d50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20D=C3=A9moulins?= Date: Wed, 25 Oct 2017 01:32:53 +0200 Subject: [PATCH] Fix a bug on versions when a prerelease contains numbers + add more tests on versions --- AU/Private/AUVersion.ps1 | 3 ++- tests/Get-Version.Tests.ps1 | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/AU/Private/AUVersion.ps1 b/AU/Private/AUVersion.ps1 index 7a7d46f8..a2f86cb5 100644 --- a/AU/Private/AUVersion.ps1 +++ b/AU/Private/AUVersion.ps1 @@ -85,7 +85,8 @@ class AUVersion : System.IComparable { $result = @($this.Version) if ($this.Prerelease) { $this.Prerelease -split '\.' | % { - if ($_ -match '[0-9]+') { + # if identifier is exclusively numeric, cast it to an int + if ($_ -match '^[0-9]+$') { $result += [int] $_ } else { $result += $_ diff --git a/tests/Get-Version.Tests.ps1 b/tests/Get-Version.Tests.ps1 index d064aa89..14e7e907 100644 --- a/tests/Get-Version.Tests.ps1 +++ b/tests/Get-Version.Tests.ps1 @@ -33,28 +33,48 @@ Describe 'Get-Version' -Tag getversion { It 'should not convert a non-strict version' { { ConvertTo-AUVersion '1.2.3.4a' } | Should Throw - { ConvertTo-AUVersion 'v1.2.3.4-beta.1+xyz.001' } | Should Throw + # for now, chocolatey does only support SemVer v1 (no dot separated identifiers in pre-release): + { ConvertTo-AUVersion 'v1.2.3.4-beta1+xyz001' } | Should Throw + # here is the SemVer v2 equivalent: + #{ ConvertTo-AUVersion 'v1.2.3.4-beta.1+xyz.001' } | Should Throw } It 'should parse a non strict version' { - $res = Get-Version 'v1.2.3.4beta.1+xyz.001' + $expectedVersion = "1.2.3.4" + # for now, chocolatey does only support SemVer v1 (no dot separated identifiers in pre-release): + $expectedPrerelease = 'beta1' + $expectedBuildMetadata = 'xyz001' + # here is the SemVer v2 equivalent: + #$expectedPrerelease = 'beta.1' + #$expectedBuildMetadata = 'xyz.001' + $res = Get-Version "v$expectedVersion$expectedPrerelease+$expectedBuildMetadata" $res | Should Not BeNullOrEmpty - $res.Version | Should Be ([version] '1.2.3.4') - $res.Prerelease | Should BeExactly 'beta.1' - $res.BuildMetadata | Should BeExactly 'xyz.001' + $res.Version | Should Be ([version] $expectedVersion) + $res.Prerelease | Should BeExactly $expectedPrerelease + $res.BuildMetadata | Should BeExactly $expectedBuildMetadata } $testCases = @( - @{A = '1.0.0' ; B = '1.0.0' ; ExpectedResult = 0} + @{A = '1.9.0' ; B = '1.9.0' ; ExpectedResult = 0} + @{A = '1.9.0' ; B = '1.10.0' ; ExpectedResult = -1} + @{A = '1.10.0' ; B = '1.11.0' ; ExpectedResult = -1} @{A = '1.0.0' ; B = '2.0.0' ; ExpectedResult = -1} @{A = '2.0.0' ; B = '2.1.0' ; ExpectedResult = -1} @{A = '2.1.0' ; B = '2.1.1' ; ExpectedResult = -1} @{A = '1.0.0-alpha' ; B = '1.0.0-alpha' ; ExpectedResult = 0} @{A = '1.0.0-alpha' ; B = '1.0.0' ; ExpectedResult = -1} - # for now, chocolatey does not support SemVer v2 (no dot separated identifiers in pre-release): + # for now, chocolatey does only support SemVer v1 (no dot separated identifiers in pre-release): + @{A = '1.0.0-alpha1' ; B = '1.0.0-alpha1' ; ExpectedResult = 0} + @{A = '1.0.0-alpha' ; B = '1.0.0-alpha1' ; ExpectedResult = -1} + @{A = '1.0.0-alpha1' ; B = '1.0.0-alphabeta' ; ExpectedResult = -1} + @{A = '1.0.0-alphabeta' ; B = '1.0.0-beta' ; ExpectedResult = -1} + @{A = '1.0.0-beta' ; B = '1.0.0-beta2' ; ExpectedResult = -1} + @{A = '1.0.0-beta2' ; B = '1.0.0-rc1' ; ExpectedResult = -1} + @{A = '1.0.0-rc1' ; B = '1.0.0' ; ExpectedResult = -1} + # here is the SemVer v2 equivalent: #@{A = '1.0.0-alpha.1' ; B = '1.0.0-alpha.1' ; ExpectedResult = 0} - #@{A = '1.0.0-alpha.1' ; B = '1.0.0-alpha.01' ; ExpectedResult = 0} + #@{A = '1.0.0-alpha.1' ; B = '1.0.0-alpha.01' ; ExpectedResult = 0} #@{A = '1.0.0-alpha' ; B = '1.0.0-alpha.1' ; ExpectedResult = -1} #@{A = '1.0.0-alpha.1' ; B = '1.0.0-alpha.beta'; ExpectedResult = -1} #@{A = '1.0.0-alpha.beta'; B = '1.0.0-beta' ; ExpectedResult = -1}