Skip to content

Commit d97d1e1

Browse files
authored
Add correct support for Reasons (#10)
- DscResource.Base - A new private function `ConvertFrom-Reason` was added which takes an array of `[Reason]` and coverts it to an array of `[System.Collections.Hashtable]`. - The private function `ConvertTo-Reason` was renamed `Resolve-Reason`. - `ResourceBase` - The property `Reasons` in derived class-based resources is now expected to use the type `[System.Collections.Hashtable[]]` (issue #4).
1 parent bdac96b commit d97d1e1

File tree

9 files changed

+240
-31
lines changed

9 files changed

+240
-31
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
#### This Pull Request (PR) fixes the following issues
1818
<!--
19-
Replace this comment block with the list of issues or n/a.
20-
Use format:
21-
- Fixes #123
22-
- Fixes #124
19+
Replace this comment block with the list of issues or n/a.
20+
Use format:
21+
- Fixes #123
22+
- Fixes #124
2323
-->
2424

2525
#### Task list

.vscode/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
"powershell"
2626
],
2727
"cSpell.words": [
28+
"Hashtable",
29+
"notin"
2830
],
2931
"cSpell.ignorePaths": [
3032
".git"

CHANGELOG.md

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

66
## [Unreleased]
77

8+
### Added
9+
10+
- DscResource.Base
11+
- A new private function `ConvertFrom-Reason` was added which takes an
12+
array of `[Reason]` and coverts it to an array of `[System.Collections.Hashtable]`.
13+
814
### Changed
915

1016
- DscResource.Base
1117
- Enable Pester's new code coverage method.
18+
- The private function `ConvertTo-Reason` was renamed `Resolve-Reason`.
19+
- `ResourceBase`
20+
- The property `Reasons` in derived class-based resources is now expected
21+
to use the type `[System.Collections.Hashtable[]]` ([issue #4](https://github.com/dsccommunity/DscResource.Base/issues/4)).
1222

1323
### Fixed
1424

source/Classes/010.ResourceBase.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ class ResourceBase
122122
{
123123
# Always return an empty array if all properties are in desired state.
124124
$dscResourceObject.Reasons = $propertiesNotInDesiredState |
125-
ConvertTo-Reason -ResourceName $this.GetType().Name
125+
Resolve-Reason -ResourceName $this.GetType().Name |
126+
ConvertFrom-Reason
126127
}
127128

128129
# Return properties.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<#
2+
.SYNOPSIS
3+
Returns a array of the type `System.Collections.Hashtable`.
4+
5+
.DESCRIPTION
6+
This command converts an array of [Reason] that is returned by the command
7+
`Resolve-Reason`. The result is an array of the type `[System.Collections.Hashtable]`
8+
that can be returned as the value of a DSC resource's property **Reasons**.
9+
10+
.PARAMETER Reason
11+
Specifies an array of `[Reason]`. Normally the result from the command `Resolve-Reason`.
12+
13+
.EXAMPLE
14+
Resolve-Reason -Reason (Resolve-Reason) -ResourceName 'MyResource'
15+
16+
Returns an array of `[System.Collections.Hashtable]` with the converted
17+
`[Reason[]]`.
18+
19+
.OUTPUTS
20+
[System.Collections.Hashtable[]]
21+
#>
22+
function ConvertFrom-Reason
23+
{
24+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseOutputTypeCorrectly', '', Justification = 'Because the rule does not understands that the command returns [System.Collections.Hashtable[]] when using , (comma) in the return statement')]
25+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the rule does not yet support parsing the code when the output type is not available. The ScriptAnalyzer rule UseSyntacticallyCorrectExamples will always error in the editor due to https://github.com/indented-automation/Indented.ScriptAnalyzerRules/issues/8.')]
26+
[CmdletBinding()]
27+
[OutputType([System.Collections.Hashtable[]])]
28+
param
29+
(
30+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
31+
[AllowEmptyCollection()]
32+
[AllowNull()]
33+
[Reason[]]
34+
$Reason
35+
)
36+
37+
begin
38+
{
39+
# Always return an empty array if there are nothing to convert.
40+
$reasonsAsHashtable = [System.Collections.Hashtable[]] @()
41+
}
42+
43+
process
44+
{
45+
foreach ($currentReason in $Reason)
46+
{
47+
$reasonsAsHashtable += [System.Collections.Hashtable] @{
48+
Code = $currentReason.Code
49+
Phrase = $currentReason.Phrase
50+
}
51+
}
52+
}
53+
54+
end
55+
{
56+
return , [System.Collections.Hashtable[]] $reasonsAsHashtable
57+
}
58+
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
Returns a array of the type `[Reason]`.
44
55
.DESCRIPTION
6-
This command converts the array of properties that is returned by the command
7-
`Compare-DscParameterState`. The result is an array of the type `[Reason]` that
8-
can be returned in a DSC resource's property **Reasons**.
6+
This command builds an array from the properties that is returned by the command
7+
`Compare-DscParameterState`. The result is an array of the type `[Reason]`.
98
109
.PARAMETER Property
1110
The result from the command Compare-DscParameterState.
@@ -15,15 +14,15 @@
1514
the correct value.
1615
1716
.EXAMPLE
18-
ConvertTo-Reason -Property (Compare-DscParameterState) -ResourceName 'MyResource'
17+
Resolve-Reason -Property (Compare-DscParameterState) -ResourceName 'MyResource'
1918
2019
Returns an array of `[Reason]` that contain all the properties not in desired
2120
state and why a specific property is not in desired state.
2221
2322
.OUTPUTS
2423
[Reason[]]
2524
#>
26-
function ConvertTo-Reason
25+
function Resolve-Reason
2726
{
2827
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the rule does not yet support parsing the code when the output type is not available. The ScriptAnalyzer rule UseSyntacticallyCorrectExamples will always error in the editor due to https://github.com/indented-automation/Indented.ScriptAnalyzerRules/issues/8.')]
2928
[CmdletBinding()]

tests/Unit/Classes/ResourceBase.Tests.ps1

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class MyMockResource : ResourceBase
162162
$MyResourceProperty2
163163
164164
[DscProperty(NotConfigurable)]
165-
[Reason[]]
165+
[System.Collections.Hashtable[]]
166166
$Reasons
167167
168168
<#
@@ -213,6 +213,8 @@ $script:mockResourceBaseInstance = [MyMockResource]::new()
213213
$getResult.MyResourceKeyProperty1 | Should -Be 'MyValue1'
214214
$getResult.MyResourceProperty2 | Should -Be 'MyValue2'
215215
$getResult.Ensure | Should -Be ([Ensure]::Present)
216+
217+
Should -ActualValue $getResult.Reasons -HaveType [System.Collections.Hashtable[]]
216218
$getResult.Reasons | Should -BeNullOrEmpty
217219
}
218220
}
@@ -248,7 +250,7 @@ class MyMockResource : ResourceBase
248250
$MyResourceProperty2
249251
250252
[DscProperty(NotConfigurable)]
251-
[Reason[]]
253+
[System.Collections.Hashtable[]]
252254
$Reasons
253255
254256
<#
@@ -292,6 +294,8 @@ $script:mockResourceBaseInstance = [MyMockResource]::new()
292294
$getResult.MyResourceKeyProperty1 | Should -Be 'MyValue1'
293295
$getResult.MyResourceProperty2 | Should -BeNullOrEmpty
294296
$getResult.Ensure | Should -Be ([Ensure]::Absent)
297+
298+
Should -ActualValue $getResult.Reasons -HaveType [System.Collections.Hashtable[]]
295299
$getResult.Reasons | Should -BeNullOrEmpty
296300
}
297301
}
@@ -328,7 +332,7 @@ class MyMockResource : ResourceBase
328332
$MyResourceProperty2
329333
330334
[DscProperty(NotConfigurable)]
331-
[Reason[]]
335+
[System.Collections.Hashtable[]]
332336
$Reasons
333337
334338
[System.Collections.Hashtable] GetCurrentState([System.Collections.Hashtable] $properties)
@@ -364,6 +368,8 @@ $script:mockResourceBaseInstance = [MyMockResource]::new()
364368
$getResult.MyResourceKeyProperty1 | Should -Be 'MyValue1'
365369
$getResult.MyResourceProperty2 | Should -Be 'MyValue2'
366370
$getResult.Ensure | Should -Be ([Ensure]::Present)
371+
372+
Should -ActualValue $getResult.Reasons -HaveType [System.Collections.Hashtable[]]
367373
$getResult.Reasons | Should -BeNullOrEmpty
368374
}
369375
}
@@ -399,7 +405,7 @@ class MyMockResource : ResourceBase
399405
$MyResourceProperty2
400406
401407
[DscProperty(NotConfigurable)]
402-
[Reason[]]
408+
[System.Collections.Hashtable[]]
403409
$Reasons
404410
405411
[System.Collections.Hashtable] GetCurrentState([System.Collections.Hashtable] $properties)
@@ -435,6 +441,8 @@ $script:mockResourceBaseInstance = [MyMockResource]::new()
435441
$getResult.MyResourceKeyProperty1 | Should -Be 'MyValue1'
436442
$getResult.MyResourceProperty2 | Should -BeNullOrEmpty
437443
$getResult.Ensure | Should -Be ([Ensure]::Absent)
444+
445+
Should -ActualValue $getResult.Reasons -HaveType [System.Collections.Hashtable[]]
438446
$getResult.Reasons | Should -BeNullOrEmpty
439447
}
440448
}
@@ -476,7 +484,7 @@ class MyMockResource : ResourceBase
476484
$MyResourceProperty2
477485
478486
[DscProperty(NotConfigurable)]
479-
[Reason[]]
487+
[System.Collections.Hashtable[]]
480488
$Reasons
481489
482490
MyMockResource() : base ()
@@ -518,6 +526,8 @@ $script:mockResourceBaseInstance = [MyMockResource]::new()
518526
$getResult.MyResourceProperty2 | Should -Be 'MyValue2'
519527
$getResult.Ensure | Should -Be ([Ensure]::Present)
520528

529+
Should -ActualValue $getResult.Reasons -HaveType [System.Collections.Hashtable[]]
530+
521531
$getResult.Reasons | Should -HaveCount 1
522532
$getResult.Reasons[0].Code | Should -Be 'MyMockResource:MyMockResource:MyResourceProperty2'
523533
$getResult.Reasons[0].Phrase | Should -Be 'The property MyResourceProperty2 should be "NewValue2", but was "MyValue2"'
@@ -550,7 +560,7 @@ class MyMockResource : ResourceBase
550560
$MyResourceProperty2
551561
552562
[DscProperty(NotConfigurable)]
553-
[Reason[]]
563+
[System.Collections.Hashtable[]]
554564
$Reasons
555565
556566
MyMockResource() : base ()
@@ -588,6 +598,8 @@ $script:mockResourceBaseInstance = [MyMockResource]::new()
588598
$getResult.MyResourceKeyProperty1 | Should -Be 'MyValue1'
589599
$getResult.Ensure | Should -Be ([Ensure]::Absent)
590600

601+
Should -ActualValue $getResult.Reasons -HaveType [System.Collections.Hashtable[]]
602+
591603
$getResult.Reasons | Should -HaveCount 1
592604
$getResult.Reasons[0].Code | Should -Be 'MyMockResource:MyMockResource:Ensure'
593605
$getResult.Reasons[0].Phrase | Should -Be 'The property Ensure should be "Present", but was "Absent"'
@@ -626,7 +638,7 @@ class MyMockResource : ResourceBase
626638
$MyResourceProperty2
627639
628640
[DscProperty(NotConfigurable)]
629-
[Reason[]]
641+
[System.Collections.Hashtable[]]
630642
$Reasons
631643
632644
MyMockResource() : base ()
@@ -668,6 +680,8 @@ $script:mockResourceBaseInstance = [MyMockResource]::new()
668680
$getResult.MyResourceProperty2 | Should -Be 'MyValue2'
669681
$getResult.Ensure | Should -Be ([Ensure]::Present)
670682

683+
Should -ActualValue $getResult.Reasons -HaveType [System.Collections.Hashtable[]]
684+
671685
$getResult.Reasons | Should -HaveCount 1
672686
$getResult.Reasons[0].Code | Should -Be 'MyMockResource:MyMockResource:Ensure'
673687
$getResult.Reasons[0].Phrase | Should -Be 'The property Ensure should be "Absent", but was "Present"'
@@ -701,7 +715,7 @@ class MyMockResource : ResourceBase
701715
$MyResourceProperty2
702716
703717
[DscProperty(NotConfigurable)]
704-
[Reason[]]
718+
[System.Collections.Hashtable[]]
705719
$Reasons
706720
707721
[System.Collections.Hashtable] GetCurrentState([System.Collections.Hashtable] $properties)
@@ -738,6 +752,8 @@ $script:mockResourceBaseInstance = [MyMockResource]::new()
738752
$getResult.MyResourceProperty2 | Should -Be 'MyValue2'
739753
$getResult.Ensure | Should -Be ([Ensure]::Absent)
740754

755+
Should -ActualValue $getResult.Reasons -HaveType [System.Collections.Hashtable[]]
756+
741757
$getResult.Reasons | Should -HaveCount 2
742758

743759
# The order in the array was sometimes different so could not use array index ($getResult.Reasons[0]).
@@ -779,7 +795,7 @@ class MyMockResource : ResourceBase
779795
$MyResourceProperty2
780796
781797
[DscProperty(NotConfigurable)]
782-
[Reason[]]
798+
[System.Collections.Hashtable[]]
783799
$Reasons
784800
785801
[System.Collections.Hashtable] GetCurrentState([System.Collections.Hashtable] $properties)
@@ -816,6 +832,8 @@ $script:mockResourceBaseInstance = [MyMockResource]::new()
816832
$getResult.MyResourceProperty2 | Should -Be 'MyValue2'
817833
$getResult.Ensure | Should -Be ([Ensure]::Present)
818834

835+
Should -ActualValue $getResult.Reasons -HaveType [System.Collections.Hashtable[]]
836+
819837
$getResult.Reasons | Should -HaveCount 1
820838

821839
$getResult.Reasons[0].Code | Should -Be 'MyMockResource:MyMockResource:Ensure'

0 commit comments

Comments
 (0)