Skip to content

Commit

Permalink
Fix a bug on versions when a prerelease contains numbers + add more t…
Browse files Browse the repository at this point in the history
…ests on versions
  • Loading branch information
Thomas Démoulins committed Oct 24, 2017
1 parent 02a0489 commit d1adb33
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
3 changes: 2 additions & 1 deletion AU/Private/AUVersion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 += $_
Expand Down
36 changes: 28 additions & 8 deletions tests/Get-Version.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit d1adb33

Please sign in to comment.