Skip to content

Commit d323dbd

Browse files
Gijsreynadityapatwardhan
authored andcommitted
Fix tests
1 parent b65cfce commit d323dbd

2 files changed

Lines changed: 49 additions & 12 deletions

File tree

src/dsc/psresourceget.ps1

Lines changed: 33 additions & 8 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
@@ -385,6 +388,12 @@ function GetPSResourceList {
385388
}
386389
}
387390
}
391+
elseif (-not ($resourcesExist | Where-Object { $_.Name -eq $resource.Name })) {
392+
# No version constraint: any installed version means the resource exists.
393+
# Only record the first match so that one input resource maps to one current resource.
394+
Write-Trace -message "No version constraint for input: $($inputResource.Name). Treating installed version $($resource.Version) as a match." -level debug
395+
$resourcesExist += $resource
396+
}
388397
}
389398
}
390399
}
@@ -613,6 +622,22 @@ function WhatIfPSResourceList {
613622
}
614623
}
615624

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

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)