Skip to content

Commit 00ea42e

Browse files
Gijsreynadityapatwardhan
authored andcommitted
Fix tests
1 parent dc28864 commit 00ea42e

2 files changed

Lines changed: 52 additions & 15 deletions

File tree

src/dsc/psresourceget.ps1

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,18 @@ class PSResourceList {
161161
}
162162

163163
[string] ToJson() {
164-
$resourceObjects = if ($this.resources) {
165-
@($this.resources | ForEach-Object {
164+
## Assign the array directly so that an empty list serializes as [] rather than null
165+
[object[]]$resourceObjects = @()
166+
if ($this.resources) {
167+
$resourceObjects = @($this.resources | ForEach-Object {
166168
[string[]]$excludeProps = @('_inDesiredState')
167169
if ($null -eq $_._metadata) { $excludeProps += '_metadata' }
168170
$_ | Select-Object -ExcludeProperty $excludeProps
169171
})
170-
} else { @() }
172+
}
171173
$retVal = [ordered]@{
172174
repositoryName = $this.repositoryName
173-
resources = [object[]]$resourceObjects
175+
resources = $resourceObjects
174176
} | ConvertTo-Json -Compress -Depth 5 -EnumsAsStrings
175177
Write-Trace -message "Serializing PSResourceList to JSON. RepositoryName: $($this.repositoryName), TrustedRepository: $($this.trustedRepository), Resources count: $($this.resources.Count)" -level debug
176178
Write-Trace -message "Serialized JSON: $retVal" -level trace
@@ -179,16 +181,17 @@ class PSResourceList {
179181

180182
[string] ToJsonForTest() {
181183
Write-Trace -message "Serializing PSResourceList to JSON for test output. RepositoryName: $($this.repositoryName), TrustedRepository: $($this.trustedRepository), Resources count: $($this.resources.Count)" -level debug
182-
$resourceObjects = if ($this.resources) {
183-
@($this.resources | ForEach-Object {
184+
[object[]]$resourceObjects = @()
185+
if ($this.resources) {
186+
$resourceObjects = @($this.resources | ForEach-Object {
184187
[string[]]$excludeProps = @()
185188
if ($null -eq $_._metadata) { $excludeProps += '_metadata' }
186189
if ($excludeProps.Count -gt 0) { $_ | Select-Object -ExcludeProperty $excludeProps } else { $_ }
187190
})
188-
} else { @() }
191+
}
189192
$retVal = [ordered]@{
190193
repositoryName = $this.repositoryName
191-
resources = [object[]]$resourceObjects
194+
resources = $resourceObjects
192195
trustedRepository = $this.trustedRepository
193196
_inDesiredState = $this._inDesiredState
194197
} | ConvertTo-Json -Compress -Depth 5 -EnumsAsStrings
@@ -375,21 +378,27 @@ function GetPSResourceList {
375378
$preferred = $matchingResources | Where-Object {
376379
try { SatisfiesVersion -version $_.Version -versionRange $inputResource.Version } catch { $false }
377380
} | Select-Object -First 1
378-
} else {
381+
}
382+
elseif (-not ($resolvedResources | Where-Object { $_.Name -eq $inputResource.Name })) {
383+
# No version constraint: any installed version means the resource exists.
384+
# Only record the first match so that one input resource maps to one current resource.
385+
Write-Trace -message "No version constraint for input: $($inputResource.Name). Treating installed version $($matchingResources[0].Version) as a match." -level debug
379386
$preferred = $matchingResources | Select-Object -First 1
380387
}
381388

382389
if ($preferred) {
383390
Write-Trace -message "Resource '$($inputResource.Name)' version '$($preferred.Version)' satisfies requested range '$($inputResource.Version)'." -level debug
384391
$resolvedResources += $preferred
385-
} else {
392+
}
393+
else {
386394
# Installed but doesn't satisfy the version range - report actual installed version with _exist = false
387395
$fallback = $matchingResources | Select-Object -First 1
388396
Write-Trace -message "Resource '$($inputResource.Name)' installed at '$($fallback.Version)' does not satisfy requested range '$($inputResource.Version)'. Reporting _exist = false." -level debug
389397
$fallback._exist = $false
390398
$resolvedResources += $fallback
391399
}
392-
} else {
400+
}
401+
else {
393402
Write-Trace -message "Resource '$($inputResource.Name)' is not installed. Reporting _exist = false." -level debug
394403
$resolvedResources += [PSResource]::new($inputResource.Name)
395404
}
@@ -618,6 +627,22 @@ function WhatIfPSResourceList {
618627
}
619628
}
620629

630+
## Report the same failures a real set operation would hit before installing anything
631+
$installRequired = @($projectedResources | Where-Object { $_._exist -and $null -ne $_._metadata }).Count -gt 0
632+
if ($installRequired) {
633+
$psRepository = Get-PSResourceRepository -Name $repositoryName -ErrorAction SilentlyContinue
634+
635+
if (-not $psRepository) {
636+
Write-Trace -level error -message "Repository '$repositoryName' not found. Cannot install resources."
637+
exit [ExitCode]::RepositoryNotFound
638+
}
639+
640+
if (-not $psRepository.Trusted -and -not $inputObj.trustedRepository) {
641+
Write-Trace -level error -message "Repository '$repositoryName' is not trusted. Cannot install resources."
642+
exit [ExitCode]::RepositoryNotTrusted
643+
}
644+
}
645+
621646
$list = [PSResourceList]::new($repositoryName, $projectedResources, $currentState.trustedRepository)
622647
$list.ToJson()
623648
}

src/dsc/psresourcelist.dsc.resource.json

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,24 @@
3232
"-ExecutionPolicy",
3333
"Bypass",
3434
"-Command",
35-
"$Input | ./psresourceget.ps1 -resourcetype 'psresourcelist' -operation set",
36-
{ "whatIfArg": "-WhatIf" }
35+
"$Input | ./psresourceget.ps1 -resourcetype 'psresourcelist' -operation set; exit $LASTEXITCODE"
3736
],
3837
"input": "stdin",
39-
"return": "stateAndDiff",
40-
"whatIfReturns": "state"
38+
"return": "stateAndDiff"
39+
},
40+
"whatIf": {
41+
"executable": "pwsh",
42+
"args": [
43+
"-NoLogo",
44+
"-NonInteractive",
45+
"-NoProfile",
46+
"-ExecutionPolicy",
47+
"Bypass",
48+
"-Command",
49+
"$Input | ./psresourceget.ps1 -resourcetype 'psresourcelist' -operation set -WhatIf; exit $LASTEXITCODE"
50+
],
51+
"input": "stdin",
52+
"return": "state"
4153
},
4254
"export": {
4355
"executable": "pwsh",

0 commit comments

Comments
 (0)