Skip to content

Commit 02f0201

Browse files
authored
GetDesiredState handle zeroed enum values using Get-DscProperty (#34)
1 parent 15362c3 commit 02f0201

File tree

5 files changed

+163
-207
lines changed

5 files changed

+163
-207
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,25 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
55

66
## [Unreleased]
77

8+
### Removed
9+
10+
- Removed `Clear-ZeroedEnumPropertyValue` as it was moved and implemented
11+
the `Get-DscProperty` command in the _DscResource.Common_ module.
12+
813
### Added
914

1015
- Wiki
1116
- How to use this base class. Fixes [#5](https://github.com/dsccommunity/DscResource.Base/issues/5)
1217
and [#19](https://github.com/dsccommunity/DscResource.Base/issues/19).
1318
- Add WikiContent to release assets.
1419

20+
### Changed
21+
22+
- `ResourceBase`
23+
- Refactor `GetDesiredState` method to handle zeroed enum values using
24+
`Get-DscProperty` when the property `FeatureOptionalEnums` is set to
25+
`$true`.
26+
1527
### Fixed
1628

1729
- build.yaml

source/Classes/010.ResourceBase.ps1

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,23 @@ class ResourceBase
267267
# This is a private method and should normally not be overridden.
268268
hidden [System.Collections.Hashtable] GetDesiredState()
269269
{
270-
# Get the properties that has a non-null value and is not of type Read.
271-
$desiredState = $this | Get-DscProperty -Attribute @('Key', 'Mandatory', 'Optional') -HasValue
270+
$getDscPropertyParameters = @{
271+
Attribute = @(
272+
'Key'
273+
'Mandatory'
274+
'Optional'
275+
)
276+
HasValue = $true
277+
}
272278

273279
if ($this.FeatureOptionalEnums)
274280
{
275-
$desiredState = $desiredState | Clear-ZeroedEnumPropertyValue
281+
$getDscPropertyParameters.IgnoreZeroEnumValue = $true
276282
}
277283

284+
# Get the properties that has a non-null value and is not of type Read.
285+
$desiredState = $this | Get-DscProperty @getDscPropertyParameters
286+
278287
return $desiredState
279288
}
280289

source/Private/Clear-ZeroedEnumPropertyValue.ps1

Lines changed: 0 additions & 47 deletions
This file was deleted.

tests/Unit/Classes/ResourceBase.Tests.ps1

Lines changed: 139 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,6 @@ Describe 'ResourceBase\Assert()' -Tag 'Assert' {
152152
}
153153
}
154154

155-
Mock -CommandName Clear-ZeroedEnumPropertyValue
156-
157155
$inModuleScopeScriptBlock = @'
158156
using module DscResource.Base
159157
@@ -223,7 +221,6 @@ $script:mockResourceBaseInstance = [MyMockResource]::new()
223221
}
224222

225223
Should -Invoke -CommandName Get-DscProperty -Exactly -Times 1 -Scope It
226-
Should -Invoke -CommandName Clear-ZeroedEnumPropertyValue -Exactly -Times 1 -Scope It
227224
}
228225
}
229226
}
@@ -243,8 +240,6 @@ Describe 'ResourceBase\Normalize()' -Tag 'Normalize' {
243240
}
244241
}
245242

246-
Mock -CommandName Clear-ZeroedEnumPropertyValue
247-
248243
$inModuleScopeScriptBlock = @'
249244
using module DscResource.Base
250245
@@ -314,7 +309,6 @@ $script:mockResourceBaseInstance = [MyMockResource]::new()
314309
}
315310

316311
Should -Invoke -CommandName Get-DscProperty -Exactly -Times 1 -Scope It
317-
Should -Invoke -CommandName Clear-ZeroedEnumPropertyValue -Exactly -Times 1 -Scope It
318312
}
319313
}
320314
}
@@ -1751,3 +1745,142 @@ $script:mockResourceBaseInstance = [MyMockResource]::new()
17511745
}
17521746
}
17531747
}
1748+
1749+
Describe 'ResourceBase\GetDesiredState()' -Tag 'GetDesiredState' {
1750+
BeforeAll {
1751+
Mock -CommandName Get-ClassName -MockWith {
1752+
# Only return localized strings for this class name.
1753+
@('ResourceBase')
1754+
}
1755+
}
1756+
1757+
Context 'When FeatureOptionalEnums is disabled' {
1758+
BeforeAll {
1759+
$inModuleScopeScriptBlock = @'
1760+
using module DscResource.Base
1761+
1762+
class MyMockResource : ResourceBase
1763+
{
1764+
[DscProperty(Key)]
1765+
[System.String]
1766+
$MyResourceKeyProperty1
1767+
1768+
[DscProperty()]
1769+
[System.String]
1770+
$MyResourceProperty2
1771+
1772+
[DscProperty()]
1773+
[System.String]
1774+
$MyResourceProperty3
1775+
1776+
[DscProperty(NotConfigurable)]
1777+
[System.String]
1778+
$MyResourceReadProperty
1779+
1780+
MyMockResource () {
1781+
$this.FeatureOptionalEnums = $false
1782+
}
1783+
}
1784+
1785+
$script:mockResourceBaseInstance = [MyMockResource]::new()
1786+
'@
1787+
1788+
InModuleScope -ScriptBlock ([Scriptblock]::Create($inModuleScopeScriptBlock))
1789+
1790+
Mock -CommandName Get-DscProperty -MockWith {
1791+
return @{
1792+
MyResourceKeyProperty1 = 'KeyValue'
1793+
MyResourceProperty2 = 'TestValue'
1794+
MyResourceProperty3 = 'Value3'
1795+
}
1796+
}
1797+
}
1798+
1799+
It 'Should have correctly instantiated the resource class' {
1800+
InModuleScope -ScriptBlock {
1801+
$mockResourceBaseInstance | Should -Not -BeNullOrEmpty
1802+
$mockResourceBaseInstance.GetType().BaseType.Name | Should -Be 'ResourceBase'
1803+
}
1804+
}
1805+
1806+
It 'Should call Get-DscProperty with the correct parameters' {
1807+
InModuleScope -ScriptBlock {
1808+
$null = $mockResourceBaseInstance.GetDesiredState()
1809+
}
1810+
1811+
Should -Invoke -CommandName Get-DscProperty -ParameterFilter {
1812+
(-not $PesterBoundParameters.ContainsKey('IgnoreZeroEnumValue'))
1813+
} -Exactly -Times 1 -Scope It
1814+
}
1815+
1816+
It 'Should return the correct hashtable' {
1817+
InModuleScope -ScriptBlock {
1818+
$result = $mockResourceBaseInstance.GetDesiredState()
1819+
$result.Keys | Should -HaveCount 3
1820+
$result.MyResourceKeyProperty1 | Should -Be 'KeyValue'
1821+
$result.MyResourceProperty2 | Should -Be 'TestValue'
1822+
$result.MyResourceProperty3 | Should -Be 'Value3'
1823+
}
1824+
}
1825+
}
1826+
1827+
Context 'When FeatureOptionalEnums is enabled' {
1828+
BeforeAll {
1829+
$inModuleScopeScriptBlock = @'
1830+
using module DscResource.Base
1831+
1832+
enum MyMockEnum {
1833+
Value1 = 0
1834+
Value2 = 1
1835+
Value3 = 2
1836+
}
1837+
1838+
class MyMockResource : ResourceBase
1839+
{
1840+
[DscProperty(Key)]
1841+
[System.String]
1842+
$MyResourceKeyProperty1
1843+
1844+
[DscProperty()]
1845+
[System.String]
1846+
$MyResourceProperty2
1847+
1848+
[DscProperty()]
1849+
[MyMockEnum]
1850+
$MyResourceEnumProperty = [MyMockEnum]::Value1
1851+
1852+
[DscProperty(NotConfigurable)]
1853+
[System.String]
1854+
$MyResourceReadProperty
1855+
1856+
MyMockResource () {
1857+
$this.FeatureOptionalEnums = $true
1858+
}
1859+
}
1860+
1861+
$script:mockResourceBaseInstance = [MyMockResource]::new()
1862+
'@
1863+
1864+
InModuleScope -ScriptBlock ([Scriptblock]::Create($inModuleScopeScriptBlock))
1865+
1866+
Mock -CommandName Get-DscProperty
1867+
}
1868+
1869+
It 'Should have correctly instantiated the resource class' {
1870+
InModuleScope -ScriptBlock {
1871+
$mockResourceBaseInstance | Should -Not -BeNullOrEmpty
1872+
$mockResourceBaseInstance.GetType().BaseType.Name | Should -Be 'ResourceBase'
1873+
}
1874+
}
1875+
1876+
It 'Should call Get-DscProperty with the correct parameters including IgnoreZeroEnumValue' {
1877+
InModuleScope -ScriptBlock {
1878+
$null = $mockResourceBaseInstance.GetDesiredState()
1879+
}
1880+
1881+
Should -Invoke -CommandName Get-DscProperty -ParameterFilter {
1882+
$IgnoreZeroEnumValue -eq $true
1883+
} -Exactly -Times 1 -Scope It
1884+
}
1885+
}
1886+
}

0 commit comments

Comments
 (0)