Skip to content

Commit

Permalink
Support alphabetical streams
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Démoulins committed Nov 6, 2017
1 parent 78ce40c commit 4cce4e9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
33 changes: 20 additions & 13 deletions AU/Public/Update-Package.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -401,33 +401,40 @@ function Update-Package {

if ($res.ContainsKey('Streams')) {
if (!$res.Streams) { throw "au_GetLatest's streams returned nothing" }
if ($res.Streams -isnot [HashTable]) { throw "au_GetLatest's streams don't return a HashTable result but $($res.Streams.GetType())" }
if ($res.Streams -isnot [System.Collections.Specialized.OrderedDictionary] -and $res.Streams -isnot [HashTable]) {
throw "au_GetLatest doesn't return an OrderedDictionary or HashTable result for streams but $($res.Streams.GetType())"
}

# Streams are expected to be sorted starting with the most recent one
$streams = @($res.Streams.Keys)
# In case of HashTable (i.e. not sorted), let's sort streams alphabetically descending
if ($res.Streams -is [HashTable]) { $streams = $streams | sort -Descending }

if ($Include) {
if ($Include -isnot [string] -and $Include -isnot [double] -and $Include -isnot [Array]) {
throw "`$Include must be either a String, a Double or an Array but is $($Include.GetType())"
}
if ($Include -is [double]) { $Include = $Include -as [string] }
if ($Include -is [string]) { [Array] $Include = $Include -split ',' | foreach { ,$_.Trim() } }
if ($Include -is [string]) {
# Forcing type in order to handle case when only one version is included
[Array] $Include = $Include -split ',' | % { $_.Trim() }
}
} elseif ($Force) {
$Include = @($res.Streams.Keys | sort { [AUVersion] $_ } -Descending | select -First 1)
# When forcing update, a single stream is expected
# By default, we take the first one (i.e. the most recent one)
$Include = @($streams | select -First 1)
}
if ($Force -and (!$Include -or $Include.Length -ne 1)) { throw 'A single stream must be included when forcing package update' }

if ($Include) {
$streams = @{}
$res.Streams.Keys | ? { $_ -in $Include } | % {
$streams.Add($_, $res.Streams[$_])
}
} else {
$streams = $res.Streams
}
if ($Include) { $streams = $streams | ? { $_ -in $Include } }
# Let's reverse the order in order to process streams starting with the oldest one
[Array]::Reverse($streams)

$res.Keys | ? { $_ -ne 'Streams' } | % { $global:au_Latest.Remove($_) }
$global:au_Latest += $res

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

'' | result
"*** Stream: $_ ***" | result
Expand Down
27 changes: 22 additions & 5 deletions tests/Update-Package.Streams.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Describe 'Update-Package using streams' -Tag updatestreams {
function global:get_latest([string] $Version, [string] $URL32, [string] $Checksum32) {
$streams = @{
'1.4' = @{ Version = '1.4-beta1'; URL32 = 'test.1.4-beta1' }
'1.2' = @{ Version = '1.2.4'; URL32 = 'test.1.2.4' }
'1.3' = @{ Version = '1.3.1'; URL32 = 'test.1.3.1' }
'1.2' = @{ Version = '1.2.4'; URL32 = 'test.1.2.4' }
}
if ($Version) {
$stream = (ConvertTo-AUVersion $Version).ToString(2)
Expand All @@ -20,8 +20,8 @@ Describe 'Update-Package using streams' -Tag updatestreams {
if ($Checksum32) { $s += @{ Checksum32 = $Checksum32 } }
$streams.Add($stream, $s)
}
$command = "function global:au_GetLatest { @{ Fake = 1; Streams = @{`n"
foreach ($item in $streams.Keys) {
$command = "function global:au_GetLatest { @{ Fake = 1; Streams = [ordered] @{`n"
foreach ($item in ($streams.Keys| sort { ConvertTo-AUVersion $_ } -Descending)) {
$command += "'$item' = @{Version = '$($streams.$item.Version)'; URL32 = '$($streams.$item.URL32)'"
if ($streams.$item.Checksum32) { $command += "; Checksum32 = '$($streams.$item.Checksum32)'" }
$command += "}`n"
Expand Down Expand Up @@ -283,10 +283,10 @@ Describe 'Update-Package using streams' -Tag updatestreams {

Context 'au_GetLatest' {

It "throws if au_GetLatest doesn't return HashTable" {
It "throws if au_GetLatest doesn't return OrderedDictionary or HashTable for streams" {
$return_value = @(1)
function global:au_GetLatest { @{ Streams = $return_value } }
{ update } | Should Throw "don't return a HashTable"
{ update } | Should Throw "doesn't return an OrderedDictionary or HashTable"
$return_value = @()
{ update } | Should Throw "returned nothing"
}
Expand All @@ -296,6 +296,23 @@ Describe 'Update-Package using streams' -Tag updatestreams {
function au_BeforeUpdate { $global:Latest.Fake | Should Be 1 }
update
}

It 'supports alphabetical streams' {
$return_value = @{
dev = @{ Version = '1.4.0' }
beta = @{ Version = '1.3.1' }
stable = @{ Version = '1.2.4' }
}
function global:au_GetLatest { @{ Streams = $return_value } }

$res = update

$res.Updated | Should Be $true
$res.Result[-1] | Should Be 'Package updated'
(json_file).stable | Should Be 1.2.4
(json_file).beta | Should Be 1.3.1
(json_file).dev | Should Be 1.4.0
}
}

Context 'Before and after update' {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"1.2": "1.2.3",
"1.3": "1.3.1",
"1.4": "1.4-beta1"
"1.4": "1.4-beta1",
"stable": "1.2.3",
"beta": "1.3.1",
"dev": "1.4-beta1"
}

0 comments on commit 4cce4e9

Please sign in to comment.