Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 13 additions & 19 deletions src/dsc/psresourceget.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -344,26 +344,20 @@ function GetPSResourceList {

$resourcesExist = @()

foreach ($resource in $allPSResources) {
foreach ($inputResource in $inputResources) {
if ($resource.Name -eq $inputResource.Name) {
Write-Trace -message "Found matching resource for input: $($inputResource.Name). Checking version constraints. Input version: $($inputResource.Version), Resource version: $($resource.Version)" -level debug
if ($inputResource.Version) {
# Use the NuGet.Versioning package if available, otherwise do a simple comparison
try {
if (SatisfiesVersion -version $resource.Version -versionRange $inputResource.Version) {
$resourcesExist += $resource
}
}
catch {
Write-Trace -message "Error checking version constraints for resource: $($inputResource.Name). Error details: $($_.Exception.Message)" -level debug
# Fallback: simple string comparison (not full NuGet range support)
if ($resource.Version.ToString() -eq $inputResource.Version) {
$resourcesExist += $resource
}
}
}
foreach ($inputResource in $inputResources) {
$matchingResources = $allPSResources | Where-Object { $_.Name -eq $inputResource.Name }

if ($matchingResources) {
# Prefer a version that satisfies the requested range; fall back to any installed version
$preferred = if ($inputResource.Version) {
$matchingResources | Where-Object {
try { SatisfiesVersion -version $_.Version -versionRange $inputResource.Version } catch { $false }
Comment thread
Gijsreyn marked this conversation as resolved.
Outdated
} | Select-Object -First 1
}

$toAdd = if ($preferred) { $preferred } else { $matchingResources | Select-Object -First 1 }
Comment thread
Gijsreyn marked this conversation as resolved.
Outdated
Write-Trace -message "Found matching resource for input: $($inputResource.Name). Returning version: $($toAdd.Version)" -level debug
$resourcesExist += $toAdd
}
}

Expand Down
46 changes: 46 additions & 0 deletions test/DscResource/PSResourceGetDSCResource.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,52 @@ Describe "PSResourceList Resource Tests" -Tags 'CI' {
$testResult = & $script:dscExe resource test --resource Microsoft.PowerShell.PSResourceGet/PSResourceList --input $resourceInput -o json | ConvertFrom-Json
$testResult.inDesiredState | Should -BeFalse
}

It 'Get returns actual installed version when installed version does not satisfy requested version' {
# Install 1.0.0 but request 5.0.0 - get should return the actual installed version (1.0.0)
Install-PSResource -Name $script:testModuleName -Version '1.0.0' -Repository $script:localRepo -Reinstall -TrustRepository -ErrorAction SilentlyContinue
Uninstall-PSResource -Name $script:testModuleName -Version '5.0.0' -ErrorAction SilentlyContinue

Comment thread
Gijsreyn marked this conversation as resolved.
$psResourceListParams = @{
repositoryName = $script:localRepo
resources = @(
@{
name = $script:testModuleName
version = '5.0.0'
}
)
}

$resourceInput = $psResourceListParams | ConvertTo-Json -Depth 5
$getResult = & $script:dscExe resource get --resource Microsoft.PowerShell.PSResourceGet/PSResourceList --input $resourceInput -o json | ConvertFrom-Json
$getResult.actualState.resources.Count | Should -Be 1
$getResult.actualState.resources[0].name | Should -BeExactly $script:testModuleName
$getResult.actualState.resources[0].version | Should -BeExactly '1.0.0'
$getResult.actualState.resources[0]._exist | Should -BeTrue
}

It 'Get prefers installed version that satisfies requested version range when multiple versions are installed' {
# Install both 1.0.0 and 5.0.0 but request 5.0.0 - get should prefer the satisfying version (5.0.0)
Comment thread
Gijsreyn marked this conversation as resolved.
Install-PSResource -Name $script:testModuleName -Version '1.0.0' -Repository $script:localRepo -Reinstall -TrustRepository -ErrorAction SilentlyContinue
Install-PSResource -Name $script:testModuleName -Version '5.0.0' -Repository $script:localRepo -Reinstall -TrustRepository -ErrorAction SilentlyContinue

$psResourceListParams = @{
repositoryName = $script:localRepo
resources = @(
@{
name = $script:testModuleName
version = '5.0.0'
}
)
}

$resourceInput = $psResourceListParams | ConvertTo-Json -Depth 5
$getResult = & $script:dscExe resource get --resource Microsoft.PowerShell.PSResourceGet/PSResourceList --input $resourceInput -o json | ConvertFrom-Json
$getResult.actualState.resources.Count | Should -Be 1
$getResult.actualState.resources[0].name | Should -BeExactly $script:testModuleName
$getResult.actualState.resources[0].version | Should -BeExactly '5.0.0'
$getResult.actualState.resources[0]._exist | Should -BeTrue
}
}

Describe 'E2E tests for Repository resource' -Tags 'CI' {
Expand Down