diff --git a/PSDepend/PSDependScripts/PSGalleryModule.ps1 b/PSDepend/PSDependScripts/PSGalleryModule.ps1 index 36dc584..7fad890 100644 --- a/PSDepend/PSDependScripts/PSGalleryModule.ps1 +++ b/PSDepend/PSDependScripts/PSGalleryModule.ps1 @@ -251,16 +251,22 @@ if($Existing) { } $GalleryVersion = Find-Module @FindModuleParams | Measure-Object -Property Version -Maximum | Select-Object -ExpandProperty Maximum - [System.Version]$parsedVersion = $null - [System.Management.Automation.SemanticVersion]$parsedSemanticVersion = $null - [System.Management.Automation.SemanticVersion]$parsedTempSemanticVersion = $null + [System.Version]$parsedExistingVersion = $null + [System.Version]$parsedGalleryVersion = $null + [System.Management.Automation.SemanticVersion]$parsedExistingSemanticVersion = $null + [System.Management.Automation.SemanticVersion]$parsedGallerySemanticVersion = $null $isGalleryVersionLessEquals = if ( - [System.Management.Automation.SemanticVersion]::TryParse($ExistingVersion, [ref]$parsedSemanticVersion) -and - [System.Management.Automation.SemanticVersion]::TryParse($GalleryVersion, [ref]$parsedTempSemanticVersion) + [System.Management.Automation.SemanticVersion]::TryParse([string]$ExistingVersion, [ref]$parsedExistingSemanticVersion) -and + [System.Management.Automation.SemanticVersion]::TryParse([string]$GalleryVersion, [ref]$parsedGallerySemanticVersion) ) { - $GalleryVersion -le $parsedSemanticVersion - } elseif ([System.Version]::TryParse($ExistingVersion, [ref]$parsedVersion)) { - $GalleryVersion -le $parsedVersion + $parsedGallerySemanticVersion -le $parsedExistingSemanticVersion + } elseif ( + [System.Version]::TryParse([string]$ExistingVersion, [ref]$parsedExistingVersion) -and + [System.Version]::TryParse([string]$GalleryVersion, [ref]$parsedGalleryVersion) + ) { + $parsedGalleryVersion -le $parsedExistingVersion + } else { + $false } # latest, and we have latest diff --git a/PSDepend/PSDependScripts/Package.ps1 b/PSDepend/PSDependScripts/Package.ps1 index bc6e74a..a6223ba 100644 --- a/PSDepend/PSDependScripts/Package.ps1 +++ b/PSDepend/PSDependScripts/Package.ps1 @@ -184,10 +184,29 @@ if($Existing) return $null } + $SourceVersion = (& $GetSourceVersion) + [System.Version]$parsedExistingVersion = $null + [System.Version]$parsedSourceVersion = $null + [System.Management.Automation.SemanticVersion]$parsedExistingSemanticVersion = $null + [System.Management.Automation.SemanticVersion]$parsedSourceSemanticVersion = $null + $isSourceVersionLessEquals = if ( + [System.Management.Automation.SemanticVersion]::TryParse([string]$ExistingVersion, [ref]$parsedExistingSemanticVersion) -and + [System.Management.Automation.SemanticVersion]::TryParse([string]$SourceVersion, [ref]$parsedSourceSemanticVersion) + ) { + $parsedSourceSemanticVersion -le $parsedExistingSemanticVersion + } elseif ( + [System.Version]::TryParse([string]$ExistingVersion, [ref]$parsedExistingVersion) -and + [System.Version]::TryParse([string]$SourceVersion, [ref]$parsedSourceVersion) + ) { + $parsedSourceVersion -le $parsedExistingVersion + } else { + $false + } + # latest, and we have latest if( $Version -and ($Version -eq 'latest' -or $Version -like '') -and - ($SourceVersion = (& $GetSourceVersion)) -le $ExistingVersion + $isSourceVersionLessEquals ) { Write-Verbose "You have the latest version of [$Name], with installed version [$ExistingVersion] and package source version [$SourceVersion]" diff --git a/Tests/PSGalleryModule.Type.Tests.ps1 b/Tests/PSGalleryModule.Type.Tests.ps1 index 09dac5a..6a7603f 100644 --- a/Tests/PSGalleryModule.Type.Tests.ps1 +++ b/Tests/PSGalleryModule.Type.Tests.ps1 @@ -119,6 +119,21 @@ Describe 'PSGalleryModule script' { } } + Context 'Latest version comparison' { + It 'Installs when installed version 2.8.0 is behind gallery version 2.10.0' { + InModuleScope PSDepend { + Mock Get-Module { [PSCustomObject]@{ Name = 'TestModule'; Version = [version]'2.8.0' } } -ParameterFilter { $ListAvailable } + Mock Find-Module { [PSCustomObject]@{ Name = 'TestModule'; Version = [version]'2.10.0' } } + } + $dep = New-PSDependFixture -DependencyName 'TestModule' -Version 'latest' + InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } { + & $ScriptPath -Dependency $Dep + } + + Should -Invoke -CommandName Install-Module -ModuleName PSDepend -Times 1 + } + } + Context 'Target as path uses Save-Module instead of Install-Module' { It 'Calls Save-Module with the target path' { $savePath = (New-Item 'TestDrive:/save' -ItemType Directory -Force).FullName diff --git a/Tests/Package.Type.Tests.ps1 b/Tests/Package.Type.Tests.ps1 index a808789..a75d8c1 100644 --- a/Tests/Package.Type.Tests.ps1 +++ b/Tests/Package.Type.Tests.ps1 @@ -63,4 +63,19 @@ Describe 'Package script' { } } | Should -Throw -ExpectedMessage '*Nuget*Target*' } + + It 'Installs when installed version 2.8.0 is behind source version 2.10.0 and latest is requested' { + $targetDir = (New-Item 'TestDrive:/pkg3' -ItemType Directory -Force).FullName + InModuleScope PSDepend { + Mock Get-Package { [PSCustomObject]@{ Name = 'jquery'; Version = [version]'2.8.0' } } + Mock Find-Package { [PSCustomObject]@{ Name = 'jquery'; Version = [version]'2.10.0' } } + } + + $dep = New-PSDependFixture -DependencyName 'jquery' -DependencyType 'Package' -Target $targetDir -Source 'nuget.org' -Version 'latest' + InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } { + & $ScriptPath -Dependency $Dep + } + + Should -Invoke -CommandName Install-Package -ModuleName PSDepend -Times 1 + } }