Skip to content

Commit

Permalink
Merge pull request #208 from miroman9364/miroman/wi159
Browse files Browse the repository at this point in the history
Fixes #159 resources should have enum labels
  • Loading branch information
SteveL-MSFT committed Sep 28, 2023
2 parents 3ace6b2 + b204c40 commit ac8bc2f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
enum EnsureEnumeration {
Absent
Present
}

function Get-TargetResource {
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
Expand All @@ -9,7 +14,7 @@ function Get-TargetResource {
)

$returnValue = @{
Ensure = 'Absent'
Ensure = [EnsureEnumeration]::Absent
Name = $Name
SourceLocation = $null
ScriptSourceLocation = $null
Expand All @@ -22,7 +27,7 @@ function Get-TargetResource {
}

if ($Name -eq "TestPSRepository1") {
$returnValue.Ensure = 'Present'
$returnValue.Ensure = [EnsureEnumeration]::Present
$returnValue.SourceLocation = 'https://www.powershellgallery.com/api/v2'
$returnValue.ScriptSourceLocation = 'https://www.powershellgallery.com/api/v2/items/psscript'
$returnValue.PublishLocation = 'https://www.powershellgallery.com/api/v2/package/'
Expand Down
9 changes: 9 additions & 0 deletions powershellgroup/Tests/PSTestModule/TestClassResource.psm1
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
enum EnumPropEnumeration {
Unexpected
Expected
}

[DscResource()]
class TestClassResource
{
Expand All @@ -7,6 +12,9 @@ class TestClassResource
[DscProperty()]
[string] $Prop1

[DscProperty()]
[EnumPropEnumeration] $EnumProp

[void] Set()
{
}
Expand All @@ -29,6 +37,7 @@ class TestClassResource
{
$this.Prop1 = "ValueForProp1"
}
$this.EnumProp = [EnumPropEnumeration]::Expected
return $this
}
}
Expand Down
16 changes: 16 additions & 0 deletions powershellgroup/Tests/powershellgroup.resource.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ Describe 'PowerShellGroup resource tests' {
$res.actualState.PublishLocation | Should -BeExactly 'https://www.powershellgallery.com/api/v2/package/'
}

It 'Get uses enum names on class-based resource' -Skip:(!$IsWindows){

$r = "{'Name':'TestClassResource1'}" | dsc resource get -r PSTestModule/TestClassResource
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.EnumProp | Should -BeExactly 'Expected'
}

It 'Get uses enum names on script-based resource' -Skip:(!$IsWindows){

$r = "{'Name':'TestPSRepository1'}" | dsc resource get -r PSTestModule/TestPSRepository
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.Ensure | Should -BeExactly 'Present'
}

It 'Test works on class-based resource' -Skip:(!$IsWindows){

$r = "{'Name':'TestClassResource1','Prop1':'ValueForProp1'}" | dsc resource test -r PSTestModule/TestClassResource
Expand Down
2 changes: 1 addition & 1 deletion powershellgroup/powershellgroup.resource.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ elseif ($Operation -eq 'Get')
}
}

$result | ConvertTo-Json
$result | ConvertTo-Json -EnumsAsStrings
}
elseif ($Operation -eq 'Set')
{
Expand Down

0 comments on commit ac8bc2f

Please sign in to comment.