diff --git a/CHANGELOG.md b/CHANGELOG.md index aa0a8bc..6ab8b35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - New-*Exception - Use `ThrowTerminatingError` instead of `throw`. Fixes [#177](https://github.com/dsccommunity/DscResource.Common/issues/177). +- Compare-DscParameterState + - Fixed a bug comparing single-valued arrays in the desired state when + TurnOffTypeChecking is used [#184](https://github.com/dsccommunity/DscResource.Common/issues/184). - Fix typo in `Clear-ZeroedEnumPropertyValue` help text. Fixes [#181](https://github.com/dsccommunity/DscResource.Common/issues/181). ### Changed diff --git a/source/Public/Compare-DscParameterState.ps1 b/source/Public/Compare-DscParameterState.ps1 index 3679e1e..b48e457 100644 --- a/source/Public/Compare-DscParameterState.ps1 +++ b/source/Public/Compare-DscParameterState.ps1 @@ -359,7 +359,7 @@ function Compare-DscParameterState } #endregion TestType #region Check if the value of Current and desired state is the same but only if they are not an array - if ($currentValue -eq $desiredValue -and -not $desiredType.IsArray) + if ($currentValue -eq $desiredValue -and -not $desiredType.IsArray -and -not $currentType.IsArray) { Write-Debug -Message ($script:localizedData.MatchValueMessage -f $desiredType.FullName, $key, $currentValue, $desiredValue) continue # pass to the next key @@ -650,7 +650,7 @@ function Compare-DscParameterState We use .foreach() method as we are sure that $returnValue is an array. #> [Array]$returnValue = @( - $returnValue.foreach( + $returnValue.ForEach( { if ($_ -is [System.Collections.Hashtable]) { diff --git a/tests/Unit/Public/Compare-DscParameterState.Tests.ps1 b/tests/Unit/Public/Compare-DscParameterState.Tests.ps1 index d347f47..2f43869 100644 --- a/tests/Unit/Public/Compare-DscParameterState.Tests.ps1 +++ b/tests/Unit/Public/Compare-DscParameterState.Tests.ps1 @@ -1258,6 +1258,7 @@ Describe 'DscResource.Common\Compare-DscParameterState' { Bool = $true Int = 99 Array = 'a', 'b', 'c', 1 + SingleValueArray = 'v1', 'v2' #for testing the comparison of single value arrays in the desired state Hashtable = @{ k1 = 'Test' k2 = 123 @@ -1514,6 +1515,41 @@ Describe 'DscResource.Common\Compare-DscParameterState' { } } + Context 'When there is a single-valued array and TurnOffTypeChecking is used' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c', 1 + SingleValueArray = 'v1' #for testing the comparison of single value arrays in the desired state + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' + } + } + } + + It 'Should not throw exception' { + { $script:result = Compare-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -TurnOffTypeChecking ` + -Verbose:$verbose } | Should -Not -Throw + } + + It 'Should not be null' { + $script:result | Should -Not -BeNullOrEmpty + } + + It 'Should return $false for SingleValueArray InDesiredState' { + $script:result.Where({ + $_.Property -eq 'SingleValueArray' + }).InDesiredState | Should -BeFalse + } + } + Context 'When both arrays are empty' { BeforeAll { $currentValues = @{ diff --git a/tests/Unit/Public/Test-DscParameterState.Tests.ps1 b/tests/Unit/Public/Test-DscParameterState.Tests.ps1 index 3eea1f3..4498114 100644 --- a/tests/Unit/Public/Test-DscParameterState.Tests.ps1 +++ b/tests/Unit/Public/Test-DscParameterState.Tests.ps1 @@ -400,6 +400,7 @@ Describe 'Test-DscParameterState' { Bool = $true Int = 99 Array = 'a', 'b', 'c', 1 + SingleValueArray = 'v1', 'v2' #for testing the comparison of single value arrays in the desired state Hashtable = @{ k1 = 'Test' k2 = 123 @@ -540,7 +541,6 @@ Describe 'Test-DscParameterState' { } } - Context 'When array has a value with a different type' { BeforeAll { $desiredValues = [PSObject] @{ @@ -593,6 +593,39 @@ Describe 'Test-DscParameterState' { } } + Context 'When there is a single-valued array and TurnOffTypeChecking is used' { + BeforeAll { + $desiredValues = [PSObject] @{ + String = 'a string' + Bool = $true + Int = 99 + Array = 'a', 'b', 'c', 1 + SingleValueArray = 'v1' #for testing the comparison of single value arrays in the desired state + Hashtable = @{ + k1 = 'Test' + k2 = 123 + k3 = 'v1', 'v2', 'v3' + } + } + } + + It 'Should not throw exception' { + { Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -TurnOffTypeChecking ` + -Verbose:$verbose } | Should -Not -Throw + } + + It 'Should return $false' { + Test-DscParameterState ` + -CurrentValues $currentValues ` + -DesiredValues $desiredValues ` + -TurnOffTypeChecking ` + -Verbose:$verbose | Should -BeFalse + } + } + Context 'When both arrays are empty' { BeforeAll { $currentValues = @{ @@ -849,39 +882,6 @@ Describe 'Test-DscParameterState' { } } - Context 'When an array in hashtable has different order but SortArrayValues is used' { - BeforeAll { - $desiredValues = [PSObject] @{ - String = 'a string' - Bool = $true - Int = 99 - Array = 'a', 'b', 'c' - Hashtable = @{ - k1 = 'Test' - k2 = 123 - k3 = 'v3', 'v2', 'v1', 99 - } - } - } - - It 'Should not throw exception' { - { Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -SortArrayValues ` - -Verbose:$verbose } | Should -Not -Throw - } - - It 'Should return $true' { - Test-DscParameterState ` - -CurrentValues $currentValues ` - -DesiredValues $desiredValues ` - -SortArrayValues ` - -Verbose:$verbose | Should -BeTrue - } - } - - Context 'When hashtable has a value with a different type' { BeforeAll { $desiredValues = [PSObject] @{