Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions PSDepend/PSDependScripts/PSGalleryModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 20 additions & 1 deletion PSDepend/PSDependScripts/Package.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +187 to +191
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolving source version should be cheap so I'm ok with this.

$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]"
Expand Down
15 changes: 15 additions & 0 deletions Tests/PSGalleryModule.Type.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions Tests/Package.Type.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Loading