diff --git a/AU/Plugins/GitReleases.ps1 b/AU/Plugins/GitReleases.ps1 index 9fce303d..fd76babf 100644 --- a/AU/Plugins/GitReleases.ps1 +++ b/AU/Plugins/GitReleases.ps1 @@ -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 diff --git a/AU/Private/AUPackage.ps1 b/AU/Private/AUPackage.ps1 index 8605a54e..83dd3f64 100644 --- a/AU/Private/AUPackage.ps1 +++ b/AU/Private/AUPackage.ps1 @@ -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' } @@ -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 } } @@ -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() { @@ -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 } } diff --git a/AU/Public/Update-Package.ps1 b/AU/Public/Update-Package.ps1 index a42b7a2b..490c9e7c 100644 --- a/AU/Public/Update-Package.ps1 +++ b/AU/Public/Update-Package.ps1 @@ -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[$_] @@ -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 diff --git a/tests/AUPackage.Tests.ps1 b/tests/AUPackage.Tests.ps1 new file mode 100644 index 00000000..0c99c922 --- /dev/null +++ b/tests/AUPackage.Tests.ps1 @@ -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.$_ + } + } + } +} diff --git a/tests/Get-Version.Tests.ps1 b/tests/Get-Version.Tests.ps1 index 93a94da6..39551622 100644 --- a/tests/Get-Version.Tests.ps1 +++ b/tests/Get-Version.Tests.ps1 @@ -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" @@ -116,8 +107,5 @@ Describe 'Get-Version' -Tag getversion { $version = [AUVersion] $Value $version | Should Not BeNullOrEmpty } - } - - cd $saved_pwd } diff --git a/tests/Update-AUPackages.Tests.ps1 b/tests/Update-AUPackages.Tests.ps1 index ddc0deb4..62e7b391 100644 --- a/tests/Update-AUPackages.Tests.ps1 +++ b/tests/Update-AUPackages.Tests.ps1 @@ -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' { @@ -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 = @( @@ -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' { @@ -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 } } diff --git a/tests/Update-Package.Streams.Tests.ps1 b/tests/Update-Package.Streams.Tests.ps1 index 5a2ad3be..3ea793ae 100644 --- a/tests/Update-Package.Streams.Tests.ps1 +++ b/tests/Update-Package.Streams.Tests.ps1 @@ -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 @@ -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 @@ -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 @@ -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 @@ -229,7 +239,7 @@ Describe 'Update-Package using streams' -Tag updatestreams { function global:au_SearchReplace { @{ 'test_package_with_streams.nuspec' = @{ - '()(.*)()' = "`$1test.$($Latest.Version)`$3" + '()(.*)()' = "`$1test_package_with_streams.$($Latest.Version)`$3" } } } @@ -237,7 +247,8 @@ Describe 'Update-Package using streams' -Tag updatestreams { 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 } }