Skip to content

Commit

Permalink
Compare-DscParameterState: Add support for OrderedDictionary (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
johlju authored Jan 23, 2024
1 parent f06681b commit cbb838f
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `New-NotImplementedException`
- Now takes a parameter `PassThru` that returns the error record that was
created (and does not throw).
- `Compare-DscParameterState`
- Add support for the type `[System.Collections.Specialized.OrderedDictionary]`
passed to parameters `CurrentValues` and `DesiredValues` ([issue #57](https://github.com/dsccommunity/DscResource.Common/issues/57)).

### Changed

Expand Down
3 changes: 2 additions & 1 deletion source/Public/Compare-DscParameterState.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ function Compare-DscParameterState
#region CheckType of object
$types = 'System.Management.Automation.PSBoundParametersDictionary',
'System.Collections.Hashtable',
'Microsoft.Management.Infrastructure.CimInstance'
'Microsoft.Management.Infrastructure.CimInstance',
'System.Collections.Specialized.OrderedDictionary'

if ($DesiredValues.GetType().FullName -notin $types)
{
Expand Down
4 changes: 2 additions & 2 deletions source/en-US/DscResource.Common.strings.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ ConvertFrom-StringData @'
AddressFormatError = Address '{0}' is not in the correct format. Please correct the Address parameter in the configuration and try again. (DRC0011)
AddressIPv4MismatchError = Address '{0}' is in IPv4 format, which does not match server address family {1}. Please correct either of them in the configuration and try again. (DRC0012)
AddressIPv6MismatchError = Address '{0}' is in IPv6 format, which does not match server address family {1}. Please correct either of them in the configuration and try again. (DRC0013)
InvalidDesiredValuesError = Property 'DesiredValues' in Test-DscParameterState must be either a Hashtable or CimInstance. Type detected was '{0}'. (DRC0014)
InvalidCurrentValuesError = Property 'CurrentValues' in Test-DscParameterState must be either a Hashtable, CimInstance, or CimIntance[]. Type detected was '{0}'. (DRC0015)
InvalidDesiredValuesError = Property 'DesiredValues' in Test-DscParameterState must be either a Hashtable, CimInstance, CimIntance[], or System.Collections.Specialized.OrderedDictionary. Type detected was '{0}'. (DRC0014)
InvalidCurrentValuesError = Property 'CurrentValues' in Test-DscParameterState must be either a Hashtable, CimInstance, CimIntance[], or System.Collections.Specialized.OrderedDictionary. Type detected was '{0}'. (DRC0015)
InvalidPropertiesError = If 'DesiredValues' is a CimInstance then property 'Properties' must contain a value. (DRC0016)
MatchPsCredentialUsernameMessage = MATCH: PSCredential username match. Current state is '{0}' and desired state is '{1}'. (DRC0017)
NoMatchPsCredentialUsernameMessage = NOTMATCH: PSCredential username mismatch. Current state is '{0}' and desired state is '{1}'. (DRC0018)
Expand Down
100 changes: 100 additions & 0 deletions tests/Unit/Public/Compare-DscParameterState.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2636,4 +2636,104 @@ Describe 'ComputerManagementDsc.Common\Compare-DscParameterState' {
}
}
}

Context 'When using an ordered dictionary' {
Context 'When some values does not match' {
BeforeAll {
$currentValues = [ordered]@{
String = 'This is a string'
Int = 1
Bool = $true
}
$desiredValues = [ordered]@{
String = 'This is a string'
Int = 99
Bool = $false
}
}

BeforeEach {
$verbose = $true
}

It 'Should not throw exception' {
{ $script:result = Compare-DscParameterState `
-CurrentValues $currentValues `
-DesiredValues $desiredValues `
-IncludeInDesiredState `
-Verbose:$verbose } | Should -Not -Throw
}

It 'Should return non-null result' {
$script:result | Should -Not -BeNullOrEmpty
}

It 'Should return $true for String in property InDesiredState' {
$script:result.Where({
$_.Property -eq 'String'
}).InDesiredState | Should -BeTrue
}

It 'Should return $false for Int in property InDesiredState' {
$script:result.Where({
$_.Property -eq 'Int'
}).InDesiredState | Should -BeFalse
}

It 'Should return $true for Bool in property InDesiredState' {
$script:result.Where({
$_.Property -eq 'Bool'
}).InDesiredState | Should -BeFalse
}
}

Context 'When all values match' {
BeforeAll {
$currentValues = [ordered]@{
String = 'This is a string'
Int = 99
Bool = $true
}
$desiredValues = [ordered]@{
String = 'This is a string'
Int = 99
Bool = $true
}
}

BeforeEach {
$verbose = $true
}

It 'Should not throw exception' {
{ $script:result = Compare-DscParameterState `
-CurrentValues $currentValues `
-DesiredValues $desiredValues `
-IncludeInDesiredState `
-Verbose:$verbose } | Should -Not -Throw
}

It 'Should return non-null result' {
$script:result | Should -Not -BeNullOrEmpty
}

It 'Should return $true for String in property InDesiredState' {
$script:result.Where({
$_.Property -eq 'String'
}).InDesiredState | Should -BeTrue
}

It 'Should return $false for Int in property InDesiredState' {
$script:result.Where({
$_.Property -eq 'Int'
}).InDesiredState | Should -BeTrue
}

It 'Should return $true for Bool in property InDesiredState' {
$script:result.Where({
$_.Property -eq 'Bool'
}).InDesiredState | Should -BeTrue
}
}
}
}

0 comments on commit cbb838f

Please sign in to comment.