From 7e4eb3a1e95b831e3b2b732ae5cf5c1869fb3958 Mon Sep 17 00:00:00 2001 From: Gilbert Sanchez Date: Fri, 12 Jun 2026 16:39:59 -0700 Subject: [PATCH] test: pin requested-version import for PSGalleryModule and PSResourceGet (#44, #55) Adds regression tests for two fixed behaviours: - PSGalleryModule (#44): when multiple versions are installed and an older one is explicitly requested, Import-PSDependModule receives that exact version (not the maximum). Mirrors the equivalent assertion already present in PSResourceGet.Type.Tests.ps1. - PSResourceGet (#44): extends the existing 'Multiple installed versions' context with an import-version assertion (Test,Import action) to lock in the same guarantee for the PSResourceGet handler. - PSGalleryModule (#55): Repository = $null skips Get-PSRepository and calls Install-Module without a -Repository argument, allowing PowerShellGet to search all registered repositories. Co-Authored-By: Claude Sonnet 4.6 --- Tests/PSGalleryModule.Type.Tests.ps1 | 32 ++++++++++++++++++++++++++++ Tests/PSResourceGet.Type.Tests.ps1 | 18 ++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/Tests/PSGalleryModule.Type.Tests.ps1 b/Tests/PSGalleryModule.Type.Tests.ps1 index aa6a781..84a5abe 100644 --- a/Tests/PSGalleryModule.Type.Tests.ps1 +++ b/Tests/PSGalleryModule.Type.Tests.ps1 @@ -172,4 +172,36 @@ Describe 'PSGalleryModule script' { Should -Invoke -CommandName Install-Module -ModuleName PSDepend -Times 0 } } + + Context 'Multiple installed versions — requested version present but not the maximum' { + It 'Imports the requested version and skips Install when an older requested version is installed alongside a newer one' { + InModuleScope PSDepend { + Mock Get-Module { + @( + [PSCustomObject]@{ Name = 'TestModule'; Version = [version]'1.2.3' } + [PSCustomObject]@{ Name = 'TestModule'; Version = [version]'2.0.0' } + ) + } -ParameterFilter { $ListAvailable } + } + $dep = New-PSDependFixture -DependencyName 'TestModule' -Version '1.2.3' + InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } { + & $ScriptPath -Dependency $Dep -PSDependAction Test, Import + } + Should -Invoke -CommandName Import-PSDependModule -ModuleName PSDepend -Times 1 -Exactly ` + -ParameterFilter { $Version.ToString() -eq '1.2.3' } + Should -Invoke -CommandName Install-Module -ModuleName PSDepend -Times 0 + } + } + + Context 'Repository = $null falls through to all registered repositories' { + It 'Skips repository validation and calls Install-Module without -Repository when Repository is null' { + $dep = New-PSDependFixture -DependencyName 'TestModule' + InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } { + & $ScriptPath -Dependency $Dep -Repository $null + } + Should -Invoke -CommandName Get-PSRepository -ModuleName PSDepend -Times 0 + Should -Invoke -CommandName Install-Module -ModuleName PSDepend -Times 1 -Exactly ` + -ParameterFilter { -not $PSBoundParameters.ContainsKey('Repository') } + } + } } diff --git a/Tests/PSResourceGet.Type.Tests.ps1 b/Tests/PSResourceGet.Type.Tests.ps1 index cd6d57e..0f94ce4 100644 --- a/Tests/PSResourceGet.Type.Tests.ps1 +++ b/Tests/PSResourceGet.Type.Tests.ps1 @@ -195,6 +195,24 @@ Describe 'PSResourceGet script' { $result | Should -Be $true Should -Invoke -CommandName Install-PSResource -ModuleName PSDepend -Times 0 } + + It 'Imports the requested version (not the maximum) when both are installed' { + InModuleScope PSDepend { + Mock Get-Module { + @( + [PSCustomObject]@{ Name = 'TestModule'; Version = [version]'1.2.3' } + [PSCustomObject]@{ Name = 'TestModule'; Version = [version]'2.0.0' } + ) + } -ParameterFilter { $ListAvailable } + } + $dep = New-PSDependFixture -DependencyName 'TestModule' -DependencyType 'PSResourceGet' -Version '1.2.3' + InModuleScope PSDepend -Parameters @{ Dep = $dep; ScriptPath = $script:ScriptPath } { + & $ScriptPath -Dependency $Dep -PSDependAction Test, Import + } + Should -Invoke -CommandName Import-PSDependModule -ModuleName PSDepend -Times 1 -Exactly ` + -ParameterFilter { $Version.ToString() -eq '1.2.3' } + Should -Invoke -CommandName Install-PSResource -ModuleName PSDepend -Times 0 + } } Context 'Target as path uses Save-PSResource instead of Install-PSResource' {