Skip to content

Commit cc9ae96

Browse files
In progress
1 parent da3f83c commit cc9ae96

3 files changed

Lines changed: 107 additions & 2 deletions

File tree

src/dsc/psresourceget.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class PSResourceList {
123123
}
124124

125125
[string] ToJson() {
126-
$resourceJson = ($this.resources | ForEach-Object { $_.ToJson() }) -join ','
126+
$resourceJson = if ($this.resources) {($this.resources | ForEach-Object { $_.ToJson() }) -join ',' } else {''}
127127
$resourceJson = "[$resourceJson]"
128128
$jsonString = "{'repositoryName': '$($this.repositoryName)','resources': $resourceJson}"
129129
$jsonString = $jsonString -replace "'", '"'

src/dsc/psresourcelist.dsc.resource.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
"title": "Version",
122122
"description": "The version range of the resource.",
123123
"type": "string",
124-
"pattern": "^(\\[|\\()\\s*\\d+(\\.\\d+){0,2}(-[0-9A-Za-z-.]+)?\\s*(,\\s*(\\d+(\\.\\d+){0,2}(-[0-9A-Za-z-.]+)?)?\\s*(\\]|\\)))?$"
124+
"pattern": "^\\d+(\\.\\d+){1,3}(-[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$"
125125
},
126126
"scope": {
127127
"title": "Scope",

test/DscResource/PSResourceGetDSCResource.Tests.ps1

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
$modPath = "$psscriptroot/../PSGetTestUtils.psm1"
2+
Import-Module $modPath -Force -Verbose
3+
14
Describe "DSC resource tests" -tags 'CI' {
25
BeforeAll {
36
$DSC_ROOT = $env:DSC_ROOT
@@ -132,3 +135,105 @@ Describe 'Repository Resource Tests' -Tags 'CI' {
132135
}
133136
}
134137

138+
Describe "PSResourceList Resource Tests" -Tags 'CI' {
139+
BeforeAll {
140+
$DSC_ROOT = $env:DSC_ROOT
141+
if (-not (Test-Path -Path $DSC_ROOT)) {
142+
throw "DSC_ROOT environment variable is not set or path does not exist."
143+
}
144+
145+
$env:PATH += ";$DSC_ROOT"
146+
147+
# Ensure DSC v3 is available
148+
if (-not (Get-Command -name dsc -CommandType Application -ErrorAction SilentlyContinue)) {
149+
throw "DSC v3 is not installed"
150+
}
151+
152+
$dscExe = Get-Command -name dsc -CommandType Application | Select-Object -First 1
153+
154+
$localRepo = "psgettestlocal"
155+
$testModuleName = "test_local_mod"
156+
$testModuleName2 = "test_local_mod2"
157+
$testModuleName3 = "Test_Local_Mod3"
158+
Get-NewPSResourceRepositoryFile
159+
Register-LocalRepos
160+
161+
$localRepoUriAddress = Join-Path -Path $TestDrive -ChildPath "testdir"
162+
163+
New-TestModule -moduleName $testModuleName -repoName $localRepo -packageVersion "1.0.0" -prereleaseLabel "" -tags @()
164+
New-TestModule -moduleName $testModuleName -repoName $localRepo -packageVersion "5.0.0" -prereleaseLabel "" -tags @()
165+
New-TestModule -moduleName $testModuleName2 -repoName $localRepo -packageVersion "5.0.0" -prereleaseLabel "" -tags @()
166+
New-TestModule -moduleName $testModuleName3 -repoName $localRepo -packageVersion "1.0.0" -prereleaseLabel "" -tags @()
167+
}
168+
AfterAll {
169+
# Clean up the test repository
170+
Get-RevertPSResourceRepositoryFile
171+
}
172+
173+
It 'Can get a PSResourceList resource instance' {
174+
$psResourceListParams = @{
175+
repositoryName = $localRepo
176+
resources = @()
177+
}
178+
179+
$resourceInput = $psResourceListParams | ConvertTo-Json -Depth 5
180+
181+
$getResult = & $dscExe resource get --resource Microsoft.PowerShell.PSResourceGet/PSResourceList --input $resourceInput -o json | ConvertFrom-Json
182+
183+
$getResult.actualState.repositoryName | Should -BeExactly $localRepo
184+
$getResult.actualState.resources.Count | Should -Be 0
185+
}
186+
187+
It 'Can get a PSResourceList resource instance with resources' {
188+
$psResourceListParams = @{
189+
repositoryName = $localRepo
190+
resources = @(
191+
@{
192+
name = $testModuleName
193+
version = '1.0.0'
194+
},
195+
@{
196+
name = $testModuleName2
197+
version = '5.0.0'
198+
}
199+
)
200+
}
201+
202+
$resourceInput = $psResourceListParams | ConvertTo-Json -Depth 5
203+
$getResult = & $dscExe resource get --resource Microsoft.PowerShell.PSResourceGet/PSResourceList --input $resourceInput -o json | ConvertFrom-Json
204+
$getResult.actualState.repositoryName | Should -BeExactly $localRepo
205+
$getResult.actualState.resources.Count | Should -Be 2
206+
$getResult.actualState.resources[0].name | Should -BeExactly $testModuleName
207+
$getResult.actualState.resources[0]._exist | Should -BeFalse
208+
$getResult.actualState.resources[1].name | Should -BeExactly $testModuleName2
209+
$getResult.actualState.resources[1]._exist | Should -BeFalse
210+
}
211+
212+
It 'Can set a PSResourceList resource instance with resources' {
213+
$psResourceListParams = @{
214+
repositoryName = $localRepo
215+
resources = @(
216+
@{
217+
name = $testModuleName
218+
version = '1.0.0'
219+
},
220+
@{
221+
name = $testModuleName2
222+
version = '5.0.0'
223+
}
224+
)
225+
}
226+
227+
$resourceInput = $psResourceListParams | ConvertTo-Json -Depth 5
228+
$getResult = & $dscExe resource set --resource Microsoft.PowerShell.PSResourceGet/PSResourceList --input $resourceInput -o json | ConvertFrom-Json
229+
$getResult.actualState.repositoryName | Should -BeExactly $localRepo
230+
$getResult.actualState.resources.Count | Should -Be 2
231+
$getResult.actualState.resources[0].name | Should -BeExactly $testModuleName
232+
$getResult.actualState.resources[0].version | Should -BeExactly '5.0.0'
233+
$getResult.actualState.resources[1].name | Should -BeExactly $testModuleName2
234+
$getResult.actualState.resources[1].version | Should -BeExactly '1.0.0'
235+
}
236+
237+
238+
}
239+

0 commit comments

Comments
 (0)