Skip to content

Commit

Permalink
Merge Streams and StreamsDetails property in AUPackage + add tests on it
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Démoulins committed Nov 9, 2017
1 parent ce04036 commit 25ad3a4
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 49 deletions.
2 changes: 1 addition & 1 deletion AU/Plugins/GitReleases.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ $packagesToRelease = New-Object 'System.Collections.Generic.List[hashtable]'

$packages | % {
if ($_.Streams) {
$_.StreamsDetails.Values | ? { $_.Updated } | % {
$_.Streams.Values | ? { $_.Updated } | % {
$packagesToRelease.Add(@{
Name = $_.Name
NuspecVersion = $_.NuspecVersion
Expand Down
51 changes: 32 additions & 19 deletions AU/Private/AUPackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ class AUPackage {
[xml] $NuspecXml
[bool] $Ignored
[string] $IgnoreMessage

[string] $StreamsPath
[pscustomobject] $Streams
[hashtable] $StreamsDetails
[string] $StreamsPath
[System.Collections.Specialized.OrderedDictionary] $Streams

AUPackage([string] $Path ){
if ([String]::IsNullOrWhiteSpace( $Path )) { throw 'Package path can not be empty' }
Expand All @@ -32,13 +30,12 @@ class AUPackage {
$this.Streams = [AUPackage]::LoadStreams( $this.StreamsPath )
}

[PSCustomObject] GetStreamDetails() {
return [PSCustomObject] @{
[hashtable] GetStreamDetails() {
return @{
Path = $this.Path
Name = $this.Name
Updated = $this.Updated
RemoteVersion = $this.RemoteVersion
NuspecVersion = $this.NuspecVersion
}
}

Expand All @@ -54,21 +51,28 @@ class AUPackage {
[System.IO.File]::WriteAllText($this.NuspecPath, $this.NuspecXml.InnerXml, $Utf8NoBomEncoding)
}

static [pscustomobject] LoadStreams( $StreamsPath ) {
if (!(Test-Path $StreamsPath)) { return $null }
return Get-Content $StreamsPath | ConvertFrom-Json
static [System.Collections.Specialized.OrderedDictionary] LoadStreams( $streamsPath ) {
if (!(Test-Path $streamsPath)) { return $null }
$res = [ordered] @{}
$versions = Get-Content $streamsPath | ConvertFrom-Json
$versions.psobject.Properties | % {
$stream = $_.Name
$res.Add($stream, @{ NuspecVersion = $versions.$stream })
}
return $res
}

UpdateStream( $stream, $version ){
if (!$this.Streams) { $this.Streams = [pscustomobject] @{} }
$s = $stream.ToString()
$v = $version.ToString()
if ($this.Streams | Get-Member $s) {
if ($this.Streams.$s -ne 'ignore') { $this.Streams.$s = $v }
} else {
$this.Streams | Add-Member $s $v
if (!$this.Streams) { $this.Streams = [ordered] @{} }
if (!$this.Streams.Contains($s)) { $this.Streams.$s = @{} }
if ($this.Streams.$s -ne 'ignore') { $this.Streams.$s.NuspecVersion = $v }
$versions = [ordered] @{}
$this.Streams.Keys | % {
$versions.Add($_, $this.Streams.$_.NuspecVersion)
}
$this.Streams | ConvertTo-Json | Set-Content $this.StreamsPath -Encoding UTF8
$versions | ConvertTo-Json | Set-Content $this.StreamsPath -Encoding UTF8
}

Backup() {
Expand All @@ -90,17 +94,26 @@ class AUPackage {

AUPackage( [hashtable] $obj ) {
if (!$obj) { throw 'Obj can not be empty' }
$obj.Keys | % {
$this.$_ = $obj[$_]
$obj.Keys | ? { $_ -ne 'Streams' } | % {
$this.$_ = $obj.$_
}
if ($obj.Streams) {
$this.Streams = [ordered] @{}
$obj.Streams.psobject.Properties | % {
$this.Streams.Add($_.Name, $_.Value)
}
}
}

[hashtable] Serialize() {
$res = @{}
$this | Get-Member -Type Properties | % {
$this | Get-Member -Type Properties | ? { $_.Name -ne 'Streams' } | % {
$property = $_.Name
$res.Add($property, $this.$property)
}
if ($this.Streams) {
$res.Add('Streams', [PSCustomObject] $this.Streams)
}
return $res
}
}
10 changes: 5 additions & 5 deletions AU/Public/Update-Package.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,6 @@ function Update-Package {
$res.Keys | ? { $_ -ne 'Streams' } | % { $global:au_Latest.Remove($_) }
$global:au_Latest += $res

$package.StreamsDetails = @{}
$streams | % {
$stream = $res.Streams[$_]

Expand All @@ -446,17 +445,18 @@ function Update-Package {
if ($stream -eq 'ignore') { return }
if ($stream -isnot [HashTable]) { throw "au_GetLatest's $_ stream doesn't return a HashTable result but $($stream.GetType())" }

if ($package.Streams.$_ -eq 'ignore') {
if ($package.Streams.$_.NuspecVersion -eq 'ignore') {
'Ignored' | result
return
}

set_latest $stream $package.Streams.$_ $_
set_latest $stream $package.Streams.$_.NuspecVersion $_
process_stream

$package.StreamsDetails.Add($_, $package.GetStreamDetails())
$package.Streams.$_ += $package.GetStreamDetails()
}
$package.StreamsDetails.Values | ? { $_.updated } | % { $package.Updated = $true }
$package.Updated = $false
$package.Streams.Values | ? { $_.Updated } | % { $package.Updated = $true }
} else {
'' | result
set_latest $res $package.NuspecVersion
Expand Down
57 changes: 57 additions & 0 deletions tests/AUPackage.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
remove-module AU -ea ignore
import-module $PSScriptRoot\..\AU

Describe 'AUPackage' -Tag aupackage {
InModuleScope AU {
It 'throws an error when intanciating without a path' {
{ [AUPackage]::new('') } | Should Throw 'empty'
}

It 'throws an error when intanciating without a hashtable' {
{ [AUPackage]::new([hashtable] $null) } | Should Throw 'empty'
}

It 'can serialize and deserialize' {
$expected = @{
Path = 'path'
Name = 'name'
Updated = $true
Pushed = $true
RemoteVersion = '1.2.3'
NuspecVersion = '0.1.2'
Result = 'result1,result2,result3' -split ','
Error = 'error'
NuspecPath = 'nuspecPath'
Ignored = $true
IgnoreMessage = 'ignoreMessage'
StreamsPath = 'streamsPath'
Streams = [PSCustomObject] @{
'0.1' = @{
NuspecVersion = '0.1.2'
Path = 'path'
Name = 'name'
Updated = $true
RemoteVersion = '1.2.3'
}
'0.2' = @{
NuspecVersion = '0.2.2'
Path = 'path'
Name = 'name'
Updated = $true
RemoteVersion = '1.2.3'
}
}
}

$package = [AUPackage]::new($expected)
$actual = $package.Serialize()

$expected.Keys | ? { $_ -ne 'Streams' } | % {
$actual.$_ | Should Be $expected.$_
}
$expected.Streams.psobject.Properties | % {
$actual.Streams.$_ | Should Be $expected.Streams.$_
}
}
}
}
12 changes: 0 additions & 12 deletions tests/Get-Version.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@ remove-module AU -ea ignore
import-module $PSScriptRoot\..\AU

Describe 'Get-Version' -Tag getversion {
$saved_pwd = $pwd

BeforeEach {
cd TestDrive:\
rm -Recurse -Force TestDrive:\test_package -ea ignore
cp -Recurse -Force $PSScriptRoot\test_package TestDrive:\test_package
}

InModuleScope AU {

It 'should convert a strict version' {
$expectedVersionStart = '1.2'
$expectedVersion = "$expectedVersionStart.3.4"
Expand Down Expand Up @@ -116,8 +107,5 @@ Describe 'Get-Version' -Tag getversion {
$version = [AUVersion] $Value
$version | Should Not BeNullOrEmpty
}

}

cd $saved_pwd
}
20 changes: 10 additions & 10 deletions tests/Update-AUPackages.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,18 @@ Describe 'Update-AUPackages' -Tag updateall {
$info.Options.Test.MyPassword | Should Be '*****'
}

It 'should execute GitReleases plugin when there are no updates' {
It 'should not execute GitReleases plugin when there are no updates' {
$Options.GitReleases = @{
ApiToken = 'apiToken'
ReleaseType = 'package'
Force = $true
}

Mock Invoke-RestMethod {} -ModuleName AU
Mock -ModuleName AU Invoke-RestMethod {}

updateall -NoPlugins:$false -Options $Options 6> $null

Assert-MockCalled Invoke-RestMethod -Exactly 0 -ModuleName AU
Assert-MockCalled -ModuleName AU Invoke-RestMethod -Exactly 0 -Scope It
}

It 'should execute GitReleases plugin per package when there are updates' {
Expand All @@ -162,7 +162,7 @@ Describe 'Update-AUPackages' -Tag updateall {
Force = $true
}

Mock Invoke-RestMethod {
Mock -ModuleName AU Invoke-RestMethod {
return @{
tag_name = 'test_package_1-1.3'
assets = @(
Expand All @@ -172,11 +172,11 @@ Describe 'Update-AUPackages' -Tag updateall {
}
)
}
} -ModuleName AU
}

updateall -NoPlugins:$false -Options $Options 6> $null

Assert-MockCalled Invoke-RestMethod -Exactly 3 -ModuleName AU
Assert-MockCalled -ModuleName AU Invoke-RestMethod -Exactly 3 -Scope It
}

It 'should execute GitReleases plugin per date when there are updates' {
Expand All @@ -190,13 +190,13 @@ Describe 'Update-AUPackages' -Tag updateall {
Force = $true
}

Mock Get-Date { return '2017-11-05' } -ParameterFilter { $UFormat -eq '{0:yyyy-MM-dd}' } -ModuleName AU
Mock Invoke-RestMethod { return @{ tag_name = '2017-11-05' } } -ModuleName AU
Mock -ModuleName AU Get-Date { return '2017-11-05' } -ParameterFilter { $UFormat -eq '{0:yyyy-MM-dd}' }
Mock -ModuleName AU Invoke-RestMethod { return @{ tag_name = '2017-11-05' } }

updateall -NoPlugins:$false -Options $Options 6> $null

Assert-MockCalled Get-Date -Exactly 1 -ModuleName AU
Assert-MockCalled Invoke-RestMethod -Exactly 2 -ModuleName AU
Assert-MockCalled -ModuleName AU Get-Date -Exactly 1 -Scope It
Assert-MockCalled -ModuleName AU Invoke-RestMethod -Exactly 2 -Scope It
}
}

Expand Down
15 changes: 13 additions & 2 deletions tests/Update-Package.Streams.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ Describe 'Update-Package using streams' -Tag updatestreams {
$res = update

$res.Updated | Should Be $true
$res.Streams.'1.2'.RemoteVersion | Should Be 1.2.4
$res.Streams.'1.3'.RemoteVersion | Should Be 1.3.1
$res.Streams.'1.4'.RemoteVersion | Should Be 1.4-beta1
$res.Result[-1] | Should Be 'Package updated'
(nuspec_file).package.metadata.version | Should Be 1.2.4
(json_file).'1.2' | Should Be 1.2.4
Expand All @@ -177,6 +180,9 @@ Describe 'Update-Package using streams' -Tag updatestreams {
$res = update

$res.Updated | Should Be $true
$res.Streams.'1.2'.RemoteVersion | Should Be 1.2.4
$res.Streams.'1.3'.RemoteVersion | Should Be 1.3.1
$res.Streams.'1.4'.RemoteVersion | Should Be 1.4.0
$res.Result[-1] | Should Be 'Package updated'
(json_file).'1.2' | Should Be 1.2.4
(json_file).'1.3' | Should Be 1.3.1
Expand All @@ -189,6 +195,9 @@ Describe 'Update-Package using streams' -Tag updatestreams {
$res = update

$res.Updated | Should Be $false
$res.Streams.'1.2'.RemoteVersion | Should Be 1.2.3
$res.Streams.'1.3'.RemoteVersion | Should Be 1.3.1
$res.Streams.'1.4'.RemoteVersion | Should Be 1.4-beta1
(nuspec_file).package.metadata.version | Should Be 1.2.3
(json_file).'1.2' | Should Be 1.2.3
(json_file).'1.3' | Should Be 1.3.1
Expand Down Expand Up @@ -219,6 +228,7 @@ Describe 'Update-Package using streams' -Tag updatestreams {
$res = update -Force -Include 1.2

$res.Updated | Should Be $true
$res.Streams.'1.2'.RemoteVersion | Should Be 1.2.4
(nuspec_file).package.metadata.version | Should Be 1.2.4
(json_file).'1.2' | Should Be 1.2.4
(json_file).'1.3' | Should Be 1.3.1
Expand All @@ -229,15 +239,16 @@ Describe 'Update-Package using streams' -Tag updatestreams {
function global:au_SearchReplace {
@{
'test_package_with_streams.nuspec' = @{
'(<releaseNotes>)(.*)(</releaseNotes>)' = "`$1test.$($Latest.Version)`$3"
'(<releaseNotes>)(.*)(</releaseNotes>)' = "`$1test_package_with_streams.$($Latest.Version)`$3"
}
}
}

update

$nu = (nuspec_file).package.metadata
$nu.releaseNotes | Should Be 'test.1.2.4'
$nu.releaseNotes | Should Be 'test_package_with_streams.1.2.4'
$nu.id | Should Be 'test_package_with_streams'
$nu.version | Should Be 1.2.4
}
}
Expand Down

0 comments on commit 25ad3a4

Please sign in to comment.