From a8108cf7696b0a2c745eb3caded82e4588a0aa0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20D=C3=A9moulins?= Date: Sun, 29 Oct 2017 17:43:11 +0100 Subject: [PATCH] Fix issue when converting to AUVersion --- AU/Private/AUVersion.ps1 | 8 ++ AU/Public/Update-Package.ps1 | 4 +- tests/Get-Version.Tests.ps1 | 181 ++++++++++++++++++--------------- tests/Update-Package.Tests.ps1 | 15 +++ 4 files changed, 125 insertions(+), 83 deletions(-) diff --git a/AU/Private/AUVersion.ps1 b/AU/Private/AUVersion.ps1 index a2f86cb5..7b33bf42 100644 --- a/AU/Private/AUVersion.ps1 +++ b/AU/Private/AUVersion.ps1 @@ -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) { diff --git a/AU/Public/Update-Package.ps1 b/AU/Public/Update-Package.ps1 index 9f7f7980..32394120 100644 --- a/AU/Public/Update-Package.ps1 +++ b/AU/Public/Update-Package.ps1 @@ -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' } @@ -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 diff --git a/tests/Get-Version.Tests.ps1 b/tests/Get-Version.Tests.ps1 index 14e7e907..93a94da6 100644 --- a/tests/Get-Version.Tests.ps1 +++ b/tests/Get-Version.Tests.ps1 @@ -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 diff --git a/tests/Update-Package.Tests.ps1 b/tests/Update-Package.Tests.ps1 index 44c7c278..53782e1f 100644 --- a/tests/Update-Package.Tests.ps1 +++ b/tests/Update-Package.Tests.ps1 @@ -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' {