From df361a2a89e5c3e4778a488466fdba582a170194 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Tue, 23 Jan 2024 13:29:07 +0100 Subject: [PATCH] Compare-DscParameterState: Add support for OrderedDictionary type (#117) --- CHANGELOG.md | 3 + source/Public/Compare-DscParameterState.ps1 | 7 +- .../Compare-DscParameterState.Tests.ps1 | 230 +++++++++++++++++- 3 files changed, 234 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e34253..f1d2c5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `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)). + - Add support for `DesiredValues` (and `CurrentValues`) to pass a value, + e.g a hashtable, that includes a property with the type `[System.Collections.Specialized.OrderedDictionary]` + or an array of `[System.Collections.Specialized.OrderedDictionary]` ([issue #57](https://github.com/dsccommunity/DscResource.Common/issues/57)). ### Changed diff --git a/source/Public/Compare-DscParameterState.ps1 b/source/Public/Compare-DscParameterState.ps1 index 2e5bb85..d64b135 100644 --- a/source/Public/Compare-DscParameterState.ps1 +++ b/source/Public/Compare-DscParameterState.ps1 @@ -338,7 +338,7 @@ function Compare-DscParameterState } #endregion check same value #region Check if the DesiredValuesClean has the key and if it doesn't have, it's not necessary to check his value - if ($desiredValuesClean.GetType().Name -in 'HashTable', 'PSBoundParametersDictionary') + if ($desiredValuesClean.GetType().Name -in 'HashTable', 'PSBoundParametersDictionary', 'OrderedDictionary') { $checkDesiredValue = $desiredValuesClean.ContainsKey($key) } @@ -459,7 +459,7 @@ function Compare-DscParameterState } } - if ($desiredType -eq [System.Collections.Hashtable] -and $currentType -eq [System.Collections.Hashtable]) + if (($desiredType -eq [System.Collections.Hashtable] -or $desiredType -eq [System.Collections.Specialized.OrderedDictionary]) -and ($currentType -eq [System.Collections.Hashtable]-or $currentType -eq [System.Collections.Specialized.OrderedDictionary])) { $param = @{} + $PSBoundParameters $param.CurrentValues = $currentArrayValues[$i] @@ -495,10 +495,9 @@ function Compare-DscParameterState continue } } - } } - elseif ($desiredType -eq [System.Collections.Hashtable] -and $currentType -eq [System.Collections.Hashtable]) + elseif (($desiredType -eq [System.Collections.Hashtable] -or $desiredType -eq [System.Collections.Specialized.OrderedDictionary]) -and ($currentType -eq [System.Collections.Hashtable]-or $currentType -eq [System.Collections.Specialized.OrderedDictionary])) { $param = @{} + $PSBoundParameters $param.CurrentValues = $currentValue diff --git a/tests/Unit/Public/Compare-DscParameterState.Tests.ps1 b/tests/Unit/Public/Compare-DscParameterState.Tests.ps1 index 012e2a7..1cc5586 100644 --- a/tests/Unit/Public/Compare-DscParameterState.Tests.ps1 +++ b/tests/Unit/Public/Compare-DscParameterState.Tests.ps1 @@ -2689,12 +2689,12 @@ Describe 'ComputerManagementDsc.Common\Compare-DscParameterState' { Context 'When all values match' { BeforeAll { - $currentValues = [ordered]@{ + $currentValues = [ordered] @{ String = 'This is a string' Int = 99 Bool = $true } - $desiredValues = [ordered]@{ + $desiredValues = [ordered] @{ String = 'This is a string' Int = 99 Bool = $true @@ -2736,4 +2736,230 @@ Describe 'ComputerManagementDsc.Common\Compare-DscParameterState' { } } } + + Context 'When a property has an ordered dictionary' { + Context 'When the property with ordered property does not match' { + BeforeAll { + $currentValues = [ordered] @{ + String = 'This is a string' + OrderedProperty = [ordered] @{ + Int = 99 + } + Bool = $true + } + $desiredValues = [ordered] @{ + String = 'This is a string' + OrderedProperty = [ordered] @{ + Int = 1 + } + 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 OrderedProperty in property InDesiredState' { + $script:result.Where({ + $_.Property -eq 'OrderedProperty' + }).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' + OrderedProperty = [ordered] @{ + Int = 99 + } + Bool = $true + } + $desiredValues = [ordered] @{ + String = 'This is a string' + OrderedProperty = [ordered] @{ + 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 OrderedProperty in property InDesiredState' { + $script:result.Where({ + $_.Property -eq 'OrderedProperty' + }).InDesiredState | Should -BeTrue + } + + It 'Should return $true for Bool in property InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Bool' + }).InDesiredState | Should -BeTrue + } + } + } + + Context 'When a property has an ordered dictionary array' { + Context 'When the property with ordered property does not match' { + BeforeAll { + $currentValues = [ordered] @{ + String = 'This is a string' + OrderedProperty = @( + [ordered] @{ + Int = 99 + } + [ordered] @{ + String = 'Yes' + } + ) + Bool = $true + } + $desiredValues = [ordered] @{ + String = 'This is a string' + OrderedProperty = @( + [ordered] @{ + Int = 99 + } + [ordered] @{ + String = 'No' + } + ) + 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 OrderedProperty in property InDesiredState' { + $script:result.Where({ + $_.Property -eq 'OrderedProperty' + }).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' + OrderedProperty = [ordered] @{ + Int = 99 + } + Bool = $true + } + $desiredValues = [ordered] @{ + String = 'This is a string' + OrderedProperty = [ordered] @{ + 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 OrderedProperty in property InDesiredState' { + $script:result.Where({ + $_.Property -eq 'OrderedProperty' + }).InDesiredState | Should -BeTrue + } + + It 'Should return $true for Bool in property InDesiredState' { + $script:result.Where({ + $_.Property -eq 'Bool' + }).InDesiredState | Should -BeTrue + } + } + } }