Skip to content

Commit 0d170e3

Browse files
Gijsreynadityapatwardhan
authored andcommitted
Return original code with other fix
1 parent 1ddb80c commit 0d170e3

2 files changed

Lines changed: 38 additions & 19 deletions

File tree

‎src/dsc/psresourceget.ps1‎

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -342,27 +342,38 @@ function GetPSResourceList {
342342
)
343343
}
344344

345-
$resourcesExist = @()
345+
$resolvedResources = @()
346346

347347
foreach ($inputResource in $inputResources) {
348348
$matchingResources = $allPSResources | Where-Object { $_.Name -eq $inputResource.Name }
349349

350350
if ($matchingResources) {
351-
# Prefer a version that satisfies the requested range; fall back to any installed version
352-
$preferred = if ($inputResource.Version) {
353-
$matchingResources | Where-Object {
351+
$preferred = $null
352+
if ($inputResource.Version) {
353+
$preferred = $matchingResources | Where-Object {
354354
try { SatisfiesVersion -version $_.Version -versionRange $inputResource.Version } catch { $false }
355355
} | Select-Object -First 1
356+
} else {
357+
$preferred = $matchingResources | Select-Object -First 1
356358
}
357359

358-
$toAdd = if ($preferred) { $preferred } else { $matchingResources | Select-Object -First 1 }
359-
Write-Trace -message "Found matching resource for input: $($inputResource.Name). Returning version: $($toAdd.Version)" -level debug
360-
$resourcesExist += $toAdd
360+
if ($preferred) {
361+
Write-Trace -message "Resource '$($inputResource.Name)' version '$($preferred.Version)' satisfies requested range '$($inputResource.Version)'." -level debug
362+
$resolvedResources += $preferred
363+
} else {
364+
# Installed but doesn't satisfy the version range - report actual installed version with _exist = false
365+
$fallback = $matchingResources | Select-Object -First 1
366+
Write-Trace -message "Resource '$($inputResource.Name)' installed at '$($fallback.Version)' does not satisfy requested range '$($inputResource.Version)'. Reporting _exist = false." -level debug
367+
$fallback._exist = $false
368+
$resolvedResources += $fallback
369+
}
370+
} else {
371+
Write-Trace -message "Resource '$($inputResource.Name)' is not installed. Reporting _exist = false." -level debug
372+
$resolvedResources += [PSResource]::new($inputResource.Name)
361373
}
362374
}
363375

364-
## For get operation we only need the first resource that exists, which is always the latest for currentUser
365-
PopulatePSResourceListObjectByRepository -resourcesExist $resourcesExist -inputResources $inputResources -repositoryName $inputPSResourceList.RepositoryName -trustedRepository $inputPSResourceList.trustedRepository
376+
PopulatePSResourceListObjectByRepository -resourcesExist $resolvedResources -inputResources $inputResources -repositoryName $inputPSResourceList.RepositoryName -trustedRepository $inputPSResourceList.trustedRepository
366377
}
367378

368379
function GetOperation {
@@ -765,13 +776,20 @@ function PopulatePSResourceListObjectByRepository {
765776
}
766777
else {
767778
$resources += $resourcesExist | ForEach-Object {
768-
[PSResource]::new(
769-
$_.Name,
770-
$_.Version.PreRelease ? $_.Version.ToString() + "-" + $_.PreRelease : $_.Version.ToString(),
771-
$_.Scope,
772-
$_.RepositoryName,
773-
$_.PreRelease ? $true : $false
774-
)
779+
$srcExist = $_._exist
780+
$r = if ($_.version) {
781+
[PSResource]::new(
782+
$_.Name,
783+
$_.Version.ToString(),
784+
$_.Scope,
785+
$_.RepositoryName,
786+
$_.PreRelease ? $true : $false
787+
)
788+
} else {
789+
[PSResource]::new($_.Name)
790+
}
791+
$r._exist = $srcExist
792+
$r
775793
}
776794
}
777795

‎test/DscResource/PSResourceGetDSCResource.Tests.ps1‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,9 @@ Describe "PSResourceList Resource Tests" -Tags 'CI' {
371371
$testResult.inDesiredState | Should -BeFalse
372372
}
373373

374-
It 'Get returns actual installed version when installed version does not satisfy requested version' {
375-
# Install 1.0.0 but request 5.0.0 - get should return the actual installed version (1.0.0)
374+
It 'Get returns actual installed version with _exist false when installed version does not satisfy requested version' {
375+
# Install 1.0.0 but request 5.0.0 - get should report _exist = false (requested version absent)
376+
# but still surface the actually installed version (1.0.0)
376377
Install-PSResource -Name $script:testModuleName -Version '1.0.0' -Repository $script:localRepo -Reinstall -TrustRepository -ErrorAction SilentlyContinue
377378
Uninstall-PSResource -Name $script:testModuleName -Version '5.0.0' -ErrorAction SilentlyContinue
378379

@@ -391,7 +392,7 @@ Describe "PSResourceList Resource Tests" -Tags 'CI' {
391392
$getResult.actualState.resources.Count | Should -Be 1
392393
$getResult.actualState.resources[0].name | Should -BeExactly $script:testModuleName
393394
$getResult.actualState.resources[0].version | Should -BeExactly '1.0.0'
394-
$getResult.actualState.resources[0]._exist | Should -BeTrue
395+
$getResult.actualState.resources[0]._exist | Should -BeFalse
395396
}
396397

397398
It 'Get prefers installed version that satisfies requested version range when multiple versions are installed' {

0 commit comments

Comments
 (0)