Skip to content

Commit

Permalink
Fix issue when converting to AUVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Démoulins committed Oct 29, 2017
1 parent 8b1058c commit a8108cf
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 83 deletions.
8 changes: 8 additions & 0 deletions AU/Private/AUVersion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ class AUVersion : System.IComparable {
$this.BuildMetadata = $buildMetadata
}

AUVersion($input) {
if (!$input) { throw 'Input cannot be null.' }
$v = [AUVersion]::Parse($input -as [string])
$this.Version = $v.Version
$this.Prerelease = $v.Prerelease
$this.BuildMetadata = $v.BuildMetadata
}

static [AUVersion] Parse([string] $input) { return [AUVersion]::Parse($input, $true) }

static [AUVersion] Parse([string] $input, [bool] $strict) {
Expand Down
4 changes: 2 additions & 2 deletions AU/Public/Update-Package.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ function Update-Package {
if ($Include -is [double]) { $Include = $Include -as [string] }
if ($Include -is [string]) { [Array] $Include = $Include -split ',' | foreach { ,$_.Trim() } }
} elseif ($Force) {
$Include = @($res.Streams.Keys | sort { [AUVersion]$_ } -Descending | select -First 1)
$Include = @($res.Streams.Keys | sort { [AUVersion] $_ } -Descending | select -First 1)
}
if ($Force -and (!$Include -or $Include.Length -ne 1)) { throw 'A single stream must be included when forcing package update' }

Expand All @@ -419,7 +419,7 @@ function Update-Package {
$streams = $res.Streams
}

$streams.Keys | ? { !$Include -or $_ -in $Include } | sort { [AUVersion]$_ } | % {
$streams.Keys | ? { !$Include -or $_ -in $Include } | sort { [AUVersion] $_ } | % {
$stream = $streams[$_]

'' | result
Expand Down
181 changes: 100 additions & 81 deletions tests/Get-Version.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,94 +10,113 @@ Describe 'Get-Version' -Tag getversion {
cp -Recurse -Force $PSScriptRoot\test_package TestDrive:\test_package
}

It 'should convert a strict version' {
$expectedVersionStart = '1.2'
$expectedVersion = "$expectedVersionStart.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'
$expected = "$expectedVersion-$expectedPrerelease+$expectedBuildMetadata"
$res = ConvertTo-AUVersion $expected
InModuleScope AU {

$res | Should Not BeNullOrEmpty
$res.Version | Should Be ([version] $expectedVersion)
$res.Prerelease | Should BeExactly $expectedPrerelease
$res.BuildMetadata | Should BeExactly $expectedBuildMetadata
$res.ToString() | Should BeExactly $expected
$res.ToString(2) | Should BeExactly $expectedVersionStart
$res.ToString(-1) | Should BeExactly $expectedVersion
}
It 'should convert a strict version' {
$expectedVersionStart = '1.2'
$expectedVersion = "$expectedVersionStart.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'
$expected = "$expectedVersion-$expectedPrerelease+$expectedBuildMetadata"
$res = ConvertTo-AUVersion $expected

It 'should not convert a non-strict version' {
{ ConvertTo-AUVersion '1.2.3.4a' } | 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
}
$res | Should Not BeNullOrEmpty
$res.Version | Should Be ([version] $expectedVersion)
$res.Prerelease | Should BeExactly $expectedPrerelease
$res.BuildMetadata | Should BeExactly $expectedBuildMetadata
$res.ToString() | Should BeExactly $expected
$res.ToString(2) | Should BeExactly $expectedVersionStart
$res.ToString(-1) | Should BeExactly $expectedVersion
}

It 'should parse a non strict version' {
$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"
It 'should not convert a non-strict version' {
{ ConvertTo-AUVersion '1.2.3.4a' } | 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
}

$res | Should Not BeNullOrEmpty
$res.Version | Should Be ([version] $expectedVersion)
$res.Prerelease | Should BeExactly $expectedPrerelease
$res.BuildMetadata | Should BeExactly $expectedBuildMetadata
}
It 'should parse a non strict version' {
$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"

$testCases = @(
@{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 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' ; 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}
#@{A = '1.0.0-beta' ; B = '1.0.0-beta.2' ; ExpectedResult = -1}
#@{A = '1.0.0-beta.2' ; B = '1.0.0-beta.11' ; ExpectedResult = -1}
#@{A = '1.0.0-beta.11' ; B = '1.0.0-rc.1' ; ExpectedResult = -1}
#@{A = '1.0.0-rc.1' ; B = '1.0.0' ; ExpectedResult = -1}
@{A = '1.0.0' ; B = '1.0.0+1' ; ExpectedResult = 0}
@{A = '1.0.0+1' ; B = '1.0.0+2' ; ExpectedResult = 0}
@{A = '1.0.0-alpha' ; B = '1.0.0-alpha+1' ; ExpectedResult = 0}
@{A = '1.0.0-alpha+1' ; B = '1.0.0-alpha+2' ; ExpectedResult = 0}
)
$res | Should Not BeNullOrEmpty
$res.Version | Should Be ([version] $expectedVersion)
$res.Prerelease | Should BeExactly $expectedPrerelease
$res.BuildMetadata | Should BeExactly $expectedBuildMetadata
}

It 'should compare 2 versions successfully' -TestCases $testCases { param([string] $A, [string] $B, [int] $ExpectedResult)
$VersionA = ConvertTo-AUVersion $A
$VersionB = ConvertTo-AUVersion $B
if ($ExpectedResult -gt 0 ) {
$VersionA | Should BeGreaterThan $VersionB
} elseif ($ExpectedResult -lt 0 ) {
$VersionA | Should BeLessThan $VersionB
} else {
$VersionA | Should Be $VersionB
$testCases = @(
@{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 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' ; 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}
#@{A = '1.0.0-beta' ; B = '1.0.0-beta.2' ; ExpectedResult = -1}
#@{A = '1.0.0-beta.2' ; B = '1.0.0-beta.11' ; ExpectedResult = -1}
#@{A = '1.0.0-beta.11' ; B = '1.0.0-rc.1' ; ExpectedResult = -1}
#@{A = '1.0.0-rc.1' ; B = '1.0.0' ; ExpectedResult = -1}
@{A = '1.0.0' ; B = '1.0.0+1' ; ExpectedResult = 0}
@{A = '1.0.0+1' ; B = '1.0.0+2' ; ExpectedResult = 0}
@{A = '1.0.0-alpha' ; B = '1.0.0-alpha+1' ; ExpectedResult = 0}
@{A = '1.0.0-alpha+1' ; B = '1.0.0-alpha+2' ; ExpectedResult = 0}
)

It 'should compare 2 versions successfully' -TestCases $testCases { param([string] $A, [string] $B, [int] $ExpectedResult)
$VersionA = ConvertTo-AUVersion $A
$VersionB = ConvertTo-AUVersion $B
if ($ExpectedResult -gt 0 ) {
$VersionA | Should BeGreaterThan $VersionB
} elseif ($ExpectedResult -lt 0 ) {
$VersionA | Should BeLessThan $VersionB
} else {
$VersionA | Should Be $VersionB
}
}

$testCases = @(
@{Value = '1.2'}
@{Value = '1.2-beta+003'}
@{Value = [AUVersion] '1.2'}
@{Value = [AUVersion] '1.2-beta+003'}
@{Value = [version] '1.2'}
@{Value = [regex]::Match('1.2', '^(.+)$').Groups[1]}
@{Value = [regex]::Match('1.2-beta+003', '^(.+)$').Groups[1]}
)

It 'converts from any type of values' -TestCases $testCases { param($Value)
$version = [AUVersion] $Value
$version | Should Not BeNullOrEmpty
}

}

cd $saved_pwd
Expand Down
15 changes: 15 additions & 0 deletions tests/Update-Package.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,21 @@ Describe 'Update-Package' -Tag update {
update
$global:Latest.NewValue | Should Be 1
}

It 'supports returning [string] as Version' {
function global:au_GetLatest { @{ Version = '1.2' } }
{ update } | Should Not Throw
}

It 'supports returning [version] as Version' {
function global:au_GetLatest { @{ Version = [version] '1.2' } }
{ update } | Should Not Throw
}

It 'supports returning [group] as Version' {
function global:au_GetLatest { @{ Version = [regex]::Match('1.2', '^(.+)$').Groups[1] } }
{ update } | Should Not Throw
}
}

Context 'Before and after update' {
Expand Down

0 comments on commit a8108cf

Please sign in to comment.