From e198b37e3fd64802495f7d922e03412e94462eb9 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Thu, 23 Aug 2018 16:42:36 -0400 Subject: [PATCH 01/46] Adding file encoding functionality --- .../DSR_KeyValuePairFile.psm1 | 77 ++++++++++++++---- .../DSR_KeyValuePairFile.schema.mof | Bin 1186 -> 3032 bytes .../en-US/DSR_KeyValuePairFile.strings.psd1 | 1 + .../DSR_ReplaceText/DSR_ReplaceText.psm1 | 71 ++++++++++++---- .../DSR_ReplaceText.schema.mof | Bin 757 -> 2168 bytes .../en-US/DSR_ReplaceText.strings.psd1 | 1 + .../FileContentDsc.Common.psm1 | 50 ++++++++++++ 7 files changed, 169 insertions(+), 31 deletions(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 index edf9d4d..806c388 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 @@ -52,6 +52,7 @@ function Get-TargetResource Assert-ParametersValid @PSBoundParameters $fileContent = Get-Content -Path $Path -Raw + $fileEncoding = Get-FileEncoding $Path Write-Verbose -Message ($localizedData.SearchForKeyMessage -f ` $Path, $Name) @@ -91,6 +92,7 @@ function Get-TargetResource return @{ Path = $Path Name = $Name + Encoding = $fileEncoding Ensure = $ensure Type = 'Text' Text = $text @@ -129,6 +131,9 @@ function Get-TargetResource .PARAMETER IgnoreValueCase Ignore the case of any text or secret when determining if it they need to be updated. Defaults to $False. + + .PARAMETER Encoding + Specifies the file encoding. The default value is ASCII. #> function Set-TargetResource { @@ -172,12 +177,17 @@ function Set-TargetResource [Parameter()] [System.Boolean] - $IgnoreValueCase = $false + $IgnoreValueCase = $false, + + [Parameter()] + [String] + $Encoding = 'Default' ) Assert-ParametersValid @PSBoundParameters $fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue + $fileEncoding = Get-FileEncoding $Path Write-Verbose -Message ($localizedData.SearchForKeyMessage -f ` $Path, $Name) @@ -218,7 +228,7 @@ function Set-TargetResource $fileContent += $keyValuePair Write-Verbose -Message ($localizedData.KeyAddMessage -f ` - $Path, $Name) + $Path, $Name) } else { @@ -226,15 +236,27 @@ function Set-TargetResource $fileContent = [regex]::Replace($fileContent, "^[\s]*$Name=(.*)($eolChars*)", $keyValuePair, $regExOptions) Write-Verbose -Message ($localizedData.KeyUpdateMessage -f ` - $Path, $Name) + $Path, $Name) } # if } else { if ($results.Count -eq 0) { - # The Key does not exists and should not so don't do anything - return + # The Key does not exists and should not so don't do anything, unless encoding doesn't match + if ($Encoding -ne $fileEncoding) + { + Set-Content ` + -Path $Path ` + -Value $fileContent ` + -Encoding $Encoding ` + -NoNewline ` + -Force + } + else + { + return + } } else { @@ -242,7 +264,7 @@ function Set-TargetResource $fileContent = [regex]::Replace($fileContent, "^[\s]*$Name=(.*)$eolChars", '', $regExOptions) Write-Verbose -Message ($localizedData.KeyRemoveMessage -f ` - $Path, $Name) + $Path, $Name) } } # if } @@ -254,6 +276,7 @@ function Set-TargetResource Set-Content ` -Path $Path ` -Value $fileContent ` + -Encoding $Encoding ` -NoNewline ` -Force } @@ -288,6 +311,9 @@ function Set-TargetResource .PARAMETER IgnoreValueCase Ignore the case of any text or secret when determining if it they need to be updated. Defaults to $False. + + .PARAMETER Encoding + Specifies the file encoding. The default value is ASCII. #> function Test-TargetResource { @@ -330,7 +356,11 @@ function Test-TargetResource [Parameter()] [System.Boolean] - $IgnoreValueCase = $false + $IgnoreValueCase = $false, + + [Parameter()] + [String] + $Encoding ) Assert-ParametersValid @PSBoundParameters @@ -343,11 +373,23 @@ function Test-TargetResource { return $false } + else + { + $fileEncoding = Get-FileEncoding $Path + + if ($Encoding -ne $fileEncoding) + { + Write-Verbose -Message ($localizedData.FileEncodingNotSetProperly -f ` + $fileEncoding, $Encoding) + + return $false + } + } $fileContent = Get-Content -Path $Path -Raw Write-Verbose -Message ($localizedData.SearchForKeyMessage -f ` - $Path, $Name) + $Path, $Name) # Setup the Regex Options that will be used $regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline @@ -366,7 +408,7 @@ function Test-TargetResource { # The key value pairs should exist but do not Write-Verbose -Message ($localizedData.KeyNotFoundButShouldExistMessage -f ` - $Path, $Name) + $Path, $Name) $desiredConfigurationMatch = $false } @@ -374,7 +416,7 @@ function Test-TargetResource { # The key value pairs should exist and do Write-Verbose -Message ($localizedData.KeyNotFoundAndShouldNotExistMessage -f ` - $Path, $Name) + $Path, $Name) } # if } else @@ -401,19 +443,19 @@ function Test-TargetResource if ($desiredConfigurationMatch) { Write-Verbose -Message ($localizedData.KeyFoundButNoReplacementMessage -f ` - $Path, $Name) + $Path, $Name) } else { Write-Verbose -Message ($localizedData.KeyFoundReplacementRequiredMessage -f ` - $Path, $Name) + $Path, $Name) } # if } else { # The key value pairs should not exist Write-Verbose -Message ($localizedData.KeyFoundButShouldNotExistMessage -f ` - $Path, $Name) + $Path, $Name) $desiredConfigurationMatch = $false } # if @@ -453,6 +495,9 @@ function Test-TargetResource .PARAMETER IgnoreValueCase Ignore the case of any text or secret when determining if it they need to be updated. Defaults to $False. + + .PARAMETER Encoding + Specifies the file encoding. The default value is ASCII. #> function Assert-ParametersValid { @@ -494,7 +539,11 @@ function Assert-ParametersValid [Parameter()] [System.Boolean] - $IgnoreValueCase = $false + $IgnoreValueCase = $false, + + [Parameter()] + [String] + $Encoding ) # Does the file's parent path exist? diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof index 05fee809eb81e9d4c3870ed2de85cce980c1f325..5b84d7f4faca84a2026cf34e168321dd5a182411 100644 GIT binary patch literal 3032 zcmds3U2hUW6g|%-{)Y)MX-yj9$AgVN6{{G-MZM=6;@g?p%KUI7J-;Twub_MC zb!YfM{jXe0<2}D;v~(HW!k}Ko*9g2Gv{x^_t3uG{uiFk zxc)ZJ%*iS@$tL*Dw?V(ivsz?om|1T9Ww_$G@R0p?kBl#)zOXiou5UfZ@vZXeD-?VE zWTR5#>yc>_lZ3dYHa8)ye4SVAh1GhDrnKOZb=s3VmV312ZA|Dv6&;Xc#-f|jvk*?n zyWq2o_fEvT7Lgj^hIKe%rixpP&AC>-73|?M+Pmd$VD01qdS|p&uJHl=Oe`CdQl0+< z;xYenGE%h>8sh4;&BrkBxJJr-WP44$A?^qTLz%$KD%a|bK6Qn8URaRTEp&Jyc5m?} zaCbmJDxmD8JicvesnZf_RUmKlHZ5-mT&qkb9#l%om#>KUW`!YOSE-@a{d-k=*LtcHI zZ~u&4Du01vUE8aRxA~@9es#A#V`j=iRSnaiM$bd;CKd-nR6;axo`{q z4tDG2tDkO~fa33vIXD#7-Nbw3cbzqElFM$^)u?p^%_Zrp4kvul!lM3_t(NPMQLpi8 zE>oTpL33s?_Um{e`n}~oPuPhKyqVi~>)mLb@n3;@XUg~TN%FsEym<4gmN6e(*WMQY GH+})c*!yb$ literal 1186 zcmcJOO=}x55Qgvm6~hup0>-r0=F~VbnBvA{O?n8y$g6QyRF)Qvw$2jr-#Z%Fj%{LS z4`DB2H6PEsujX-R7109*GTvPjUk6{(S6udC6bw3TyPs->7sU_k9+Yiys!SLei^b(r zuS%(KQ{K;yZ{ZuD_4gfa%>o?*)ISEtK4CI-LUmfO!v!${_@U?Lu&lnqv9X^=Kp9Zky=R@%PKq~vc0RCv3}5kLZD08D zYd`SxY@{sZ-MRMOqH>VSq(vBVmH+9cT$lCoVJn}Qp)q1u8JAX*x5ls~HE*#)c8iT( WAheBE5%uAFJIN*sli}X3dVc|3vzIym diff --git a/DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1 b/DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1 index 7134505..cf80c24 100644 --- a/DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1 +++ b/DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1 @@ -15,4 +15,5 @@ ConvertFrom-StringData @' KeyFoundReplacementRequiredMessage = Key '{1}' found in file '{0}' and should exist but value(s) are not correct. Change required. KeyFoundButShouldNotExistMessage = Key '{1}' found in file '{0}' but should not exist. Change required. FileParentNotFoundError = File parent path '{0}' not found. + FileEncodingNotSetProperly = File encoding is set to {0} but should be set to {1}, change required. '@ diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 index e39958d..ce25c8e 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 @@ -52,9 +52,10 @@ function Get-TargetResource Assert-ParametersValid @PSBoundParameters $fileContent = Get-Content -Path $Path -Raw + $fileEncoding = Get-FileEncoding $Path Write-Verbose -Message ($localizedData.SearchForTextMessage -f ` - $Path, $Search) + $Path, $Search) $text = '' @@ -65,21 +66,22 @@ function Get-TargetResource { # No matches found - already in state Write-Verbose -Message ($localizedData.StringNotFoundMessage -f ` - $Path, $Search) + $Path, $Search) } else { $text = ($results.Value -join ',') Write-Verbose -Message ($localizedData.StringMatchFoundMessage -f ` - $Path, $Search, $text) + $Path, $Search, $text) } # if return @{ - Path = $Path - Search = $Search - Type = 'Text' - Text = $text + Path = $Path + Search = $Search + Type = 'Text' + Text = $text + Encoding = $fileEncoding } } @@ -103,6 +105,9 @@ function Get-TargetResource .PARAMETER Secret The secret text to replace the text identified by the RegEx. Only used when Type is set to 'Secret'. + + .PARAMETER Encoding + Specifies the file encoding. The default value is ASCII. #> function Set-TargetResource { @@ -133,7 +138,11 @@ function Set-TargetResource [Parameter()] [System.Management.Automation.PSCredential] [System.Management.Automation.Credential()] - $Secret + $Secret, + + [Parameter()] + [String] + $Encoding = 'Default' ) Assert-ParametersValid @PSBoundParameters @@ -143,14 +152,14 @@ function Set-TargetResource if ($Type -eq 'Secret') { Write-Verbose -Message ($localizedData.StringReplaceSecretMessage -f ` - $Path) + $Path) $Text = $Secret.GetNetworkCredential().Password } else { Write-Verbose -Message ($localizedData.StringReplaceTextMessage -f ` - $Path, $Text) + $Path, $Text) } # if if ($null -eq $fileContent) @@ -165,6 +174,7 @@ function Set-TargetResource Set-Content ` -Path $Path ` -Value $fileContent ` + -Encoding $Encoding ` -NoNewline ` -Force } @@ -189,6 +199,9 @@ function Set-TargetResource .PARAMETER Secret The secret text to replace the text identified by the RegEx. Only used when Type is set to 'Secret'. + + .PARAMETER Encoding + Specifies the file encoding. The default value is ASCII. #> function Test-TargetResource { @@ -218,21 +231,38 @@ function Test-TargetResource [Parameter()] [System.Management.Automation.PSCredential] [System.Management.Automation.Credential()] - $Secret + $Secret, + + [Parameter()] + [String] + $Encoding ) Assert-ParametersValid @PSBoundParameters - # Check if file being managed exists. If not return $False. + # Check for the file being managed. If it does not exist return $false. + # If it exists but is using the wrong encoding return $false. if (-not (Test-Path -Path $Path)) { return $false } + else + { + $fileEncoding = Get-FileEncoding $Path + + if ($Encoding -ne $fileEncoding) + { + Write-Verbose -Message ($localizedData.FileEncodingNotSetProperly -f ` + $fileEncoding, $Encoding) + + return $false + } + } $fileContent = Get-Content -Path $Path -Raw Write-Verbose -Message ($localizedData.SearchForTextMessage -f ` - $Path, $Search) + $Path, $Search) # Search the file content for any matches $results = [regex]::Matches($fileContent, $Search) @@ -241,7 +271,7 @@ function Test-TargetResource { # No matches found - already in state Write-Verbose -Message ($localizedData.StringNotFoundMessage -f ` - $Path, $Search) + $Path, $Search) return $true } @@ -265,12 +295,12 @@ function Test-TargetResource if ($desiredConfigurationMatch) { Write-Verbose -Message ($localizedData.StringNoReplacementMessage -f ` - $Path, $Search) + $Path, $Search) } else { Write-Verbose -Message ($localizedData.StringReplacementRequiredMessage -f ` - $Path, $Search) + $Path, $Search) } # if return $desiredConfigurationMatch @@ -297,6 +327,9 @@ function Test-TargetResource .PARAMETER Secret The secret text to replace the text identified by the RegEx. Only used when Type is set to 'Secret'. + + .PARAMETER Encoding + Specifies the file encoding. The default value is ASCII. #> function Assert-ParametersValid { @@ -325,7 +358,11 @@ function Assert-ParametersValid [Parameter()] [System.Management.Automation.PSCredential] [System.Management.Automation.Credential()] - $Secret + $Secret, + + [Parameter()] + [String] + $Encoding ) # Does the file's parent path exist? diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof index 7b374096d7b4dd38914b58625228ce54e79d9a3a..0d980b728fbbd41565d4b199b9bad2bef5f9ee3e 100644 GIT binary patch literal 2168 zcmds&UrQT76vgjzq2FQI7b{5UpNAIv)M^4!QcDxe6%#Zc-bHU%l<`%j?9xhmX1TqvmOXF2xoz5(W!Ax} zz_zpzz6<+^|2OvX_{?+aZXB}e^Zj8n&NuDYp4t-|HqkQMwI22-YkV@>Cf2@nZ4aCG z4&&I_R^Xe1K@oK4PxqT)ePkavTR4=vtSW4Tbqa=w#m+XSpuFb!dBue~S2v%T_!|59^uk!Pb}>6pl&NBXaf~WE1M>(Jq{2z#X!XE~CdX z`%+vl4=B26N*iYHpN=u`qEdPxjj`LBf8Zt#k^%V3ICGC{hU+>6b zhy3^p8@wj1Rc4sBawKYuAU7SN5{8(KL$PA0sTBGQvLX#&EVe<^g~(X$-@i%rrF{kMC4EtHx@tcB?-}3i>-H@@c;(&H^X?D2Qf9dT literal 757 zcmb`FQES356oudWD=v9ap_J`)zRfAfaE`XP2N@F6s|I3Pl5|?c|Gwl_w$9DB(U-KR z_ndP-vK%RH?E;OJrS7x)d?oYu{e|5+Ka|i52zx#^UIdF7uTG4p#=tTZ1|2)MlKm}w5y*Y; z&87h^0li{L-(=yNg*^&bH?#`jN?@*46Hpa!*rA4y0Mdd*_oMcA1-}PXIQ#)CRIs$o p|2JtV5yJ6)jYWaQR9nZj@b`3 +function Get-FileEncoding +{ + [CmdletBinding()] + Param + ( + [Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)] + [string] + $Path + ) + + [byte[]]$byte = get-content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $Path + + if ( $byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf ) + { + Write-Output 'UTF8' + } + elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff) + { + Write-Output 'Unicode' + } + elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xfe -and $byte[3] -eq 0xff) + { + Write-Output 'UTF32' + } + elseif ($byte[0] -eq 0x2b -and $byte[1] -eq 0x2f -and $byte[2] -eq 0x76) + { + Write-Output 'UTF7' + } + else + { + Write-Output 'ASCII' + } +} + Export-ModuleMember -Function * From 1cbb817068c0fcc32776d5553c47201e655a4b91 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Mon, 10 Sep 2018 16:13:51 -0400 Subject: [PATCH 02/46] Updating keyvaluepairfile with encoding support --- .../DSR_KeyValuePairFile.psm1 | 63 +- .../en-US/DSR_KeyValuePairFile.strings.psd1 | 2 +- .../en-US/DSR_ReplaceText.strings.psd1 | 2 +- Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 | 880 ++++++++++++++++-- Tests/Unit/DSR_ReplaceText.Tests.ps1 | 205 +++- 5 files changed, 1015 insertions(+), 137 deletions(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 index 806c388..edc5d16 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 @@ -52,10 +52,11 @@ function Get-TargetResource Assert-ParametersValid @PSBoundParameters $fileContent = Get-Content -Path $Path -Raw - $fileEncoding = Get-FileEncoding $Path Write-Verbose -Message ($localizedData.SearchForKeyMessage -f ` - $Path, $Name) + $Path, $Name) + + $fileEncoding = Get-FileEncoding -Path $Path # Setup the Regex Options that will be used $regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline @@ -70,7 +71,7 @@ function Get-TargetResource { # No matches found Write-Verbose -Message ($localizedData.KeyNotFoundMessage -f ` - $Path, $Name) + $Path, $Name) } else { @@ -86,7 +87,7 @@ function Get-TargetResource $text = ($textValues -join ',') Write-Verbose -Message ($localizedData.KeyFoundMessage -f ` - $Path, $Name, $text) + $Path, $Name, $text) } # if return @{ @@ -187,10 +188,10 @@ function Set-TargetResource Assert-ParametersValid @PSBoundParameters $fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue - $fileEncoding = Get-FileEncoding $Path + $fileEncoding = Get-FileEncoding -Path $Path Write-Verbose -Message ($localizedData.SearchForKeyMessage -f ` - $Path, $Name) + $Path, $Name) if ($Type -eq 'Secret') { @@ -243,19 +244,15 @@ function Set-TargetResource { if ($results.Count -eq 0) { - # The Key does not exists and should not so don't do anything, unless encoding doesn't match - if ($Encoding -ne $fileEncoding) + if ($Encoding -eq $fileEncoding) { - Set-Content ` - -Path $Path ` - -Value $fileContent ` - -Encoding $Encoding ` - -NoNewline ` - -Force + # The Key does not exists and should not, and encoding is in the desired state, so don't do anything + return } else { - return + Write-Verbose -Message ($localizedData.FileEncodingNotInDesiredState -f ` + $fileEncoding, $Encoding) } } else @@ -373,24 +370,14 @@ function Test-TargetResource { return $false } - else - { - $fileEncoding = Get-FileEncoding $Path - - if ($Encoding -ne $fileEncoding) - { - Write-Verbose -Message ($localizedData.FileEncodingNotSetProperly -f ` - $fileEncoding, $Encoding) - - return $false - } - } $fileContent = Get-Content -Path $Path -Raw Write-Verbose -Message ($localizedData.SearchForKeyMessage -f ` $Path, $Name) + $fileEncoding = Get-FileEncoding -Path $Path + # Setup the Regex Options that will be used $regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline if ($IgnoreNameCase) @@ -415,13 +402,17 @@ function Test-TargetResource else { # The key value pairs should exist and do - Write-Verbose -Message ($localizedData.KeyNotFoundAndShouldNotExistMessage -f ` + + if ($Encoding -eq $fileEncoding) + { + Write-Verbose -Message ($localizedData.KeyNotFoundAndShouldNotExistMessage -f ` $Path, $Name) + } } # if } else { - # One of more key value pairs were found + # One or more key value pairs were found if ($Ensure -eq 'Present') { # The key value pairs should exist - but check values @@ -445,11 +436,6 @@ function Test-TargetResource Write-Verbose -Message ($localizedData.KeyFoundButNoReplacementMessage -f ` $Path, $Name) } - else - { - Write-Verbose -Message ($localizedData.KeyFoundReplacementRequiredMessage -f ` - $Path, $Name) - } # if } else { @@ -461,6 +447,15 @@ function Test-TargetResource } # if } # if + if ($Encoding -ne $fileEncoding) + { + # File encoding is not in desired state + Write-Verbose -Message ($localizedData.FileEncodingNotInDesiredState -f ` + $fileEncoding, $Encoding) + + $desiredConfigurationMatch = $false + } + return $desiredConfigurationMatch } diff --git a/DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1 b/DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1 index cf80c24..8436c2e 100644 --- a/DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1 +++ b/DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1 @@ -15,5 +15,5 @@ ConvertFrom-StringData @' KeyFoundReplacementRequiredMessage = Key '{1}' found in file '{0}' and should exist but value(s) are not correct. Change required. KeyFoundButShouldNotExistMessage = Key '{1}' found in file '{0}' but should not exist. Change required. FileParentNotFoundError = File parent path '{0}' not found. - FileEncodingNotSetProperly = File encoding is set to {0} but should be set to {1}, change required. + FileEncodingNotInDesiredState = File encoding is set to {0} but should be set to {1}, Change required. '@ diff --git a/DSCResources/DSR_ReplaceText/en-US/DSR_ReplaceText.strings.psd1 b/DSCResources/DSR_ReplaceText/en-US/DSR_ReplaceText.strings.psd1 index c6b61b5..e318d70 100644 --- a/DSCResources/DSR_ReplaceText/en-US/DSR_ReplaceText.strings.psd1 +++ b/DSCResources/DSR_ReplaceText/en-US/DSR_ReplaceText.strings.psd1 @@ -9,5 +9,5 @@ ConvertFrom-StringData @' StringReplaceTextMessage = String replaced by '{1}' in file '{0}'. StringReplaceSecretMessage = String replaced by secret text in file '{0}'. FileParentNotFoundError = File parent path '{0}' not found. - FileEncodingNotSetProperly = File encoding is set to {0} but should be set to {1}, change required. + FileEncodingNotInDesiredState = File encoding is set to {0} but should be set to {1}, Change required. '@ diff --git a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 index f700346..dd7f09e 100644 --- a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 +++ b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 @@ -40,6 +40,21 @@ try $script:testSecureSecret = ConvertTo-SecureString -String $script:testSecret -AsPlainText -Force $script:testSecretCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ('Dummy', $script:testSecureSecret) + $script:fileEncodingParameters = @{ + Path = $script:testTextFile + Encoding = 'ASCII' + } + + $script:testCompliantEncoding = @{ + Path = $script:fileEncodingParameters.Path + Encoding = $script:fileEncodingParameters.Encoding + } + + $script:testNonCompliantEncoding = @{ + Path = $script:fileEncodingParameters.Path + Encoding = 'UTF8' + } + $script:testFileContent = @" Setting1=Value1 $($script:testName)=Value 2 @@ -96,7 +111,61 @@ $($script:testAddedName)=$($script:testText) #region Function Get-TargetResource Describe 'DSR_KeyValuePairFile\Get-TargetResource' { - Context 'File exists and contains matching key' { + Context 'File exists and contains matching key and encoding is compliant' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_KeyValuePairFile' ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileExpectedTextContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + + $script:result = $null + + It 'Should not throw an exception' { + { $script:result = Get-TargetResource ` + -Path $script:testTextFile ` + -Name $script:testName ` + -Verbose + } | Should -Not -Throw + } + + It 'Should return expected values' { + $script:result.Path | Should -Be $script:testTextFile + $script:result.Name | Should -Be $script:testName + $script:result.Ensure | Should -Be 'Present' + $script:result.Type | Should -Be 'Text' + $script:result.Text | Should -Be "$($script:testText),$($script:testText),$($script:testText)" + $script:result.Encoding | Should -Be $script:fileEncodingParameters.Encoding + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + + Context 'File exists and contains matching key and encoding is not compliant' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -109,6 +178,12 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileExpectedTextContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -Verifiable + $script:result = $null It 'Should not throw an exception' { @@ -125,6 +200,61 @@ $($script:testAddedName)=$($script:testText) $script:result.Ensure | Should -Be 'Present' $script:result.Type | Should -Be 'Text' $script:result.Text | Should -Be "$($script:testText),$($script:testText),$($script:testText)" + $script:result.Encoding | Should -Be $script:testNonCompliantEncoding.Encoding + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + + Context 'File exists and does not contain matching key and encoding is compliant' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_KeyValuePairFile' ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileExpectedTextContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + + $script:result = $null + + It 'Should not throw an exception' { + { $script:result = Get-TargetResource ` + -Path $script:testTextFile ` + -Name $script:testName.ToUpper() ` + -Verbose + } | Should -Not -Throw + } + + It 'Should return expected values' { + $script:result.Path | Should -Be $script:testTextFile + $script:result.Name | Should -Be $script:testName + $script:result.Ensure | Should -Be 'Absent' + $script:result.Type | Should -Be 'Text' + $script:result.Text | Should -BeNullOrEmpty + $script:result.Encoding | Should -Be $script:fileEncodingParameters.Encoding } It 'Should call the expected mocks' { @@ -135,10 +265,15 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } - Context 'File exists and does not contain matching key' { + Context 'File exists and does not contain matching key and encoding is compliant' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -151,6 +286,12 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileExpectedTextContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + $script:result = $null It 'Should not throw an exception' { @@ -167,6 +308,7 @@ $($script:testAddedName)=$($script:testText) $script:result.Ensure | Should -Be 'Absent' $script:result.Type | Should -Be 'Text' $script:result.Text | Should -BeNullOrEmpty + $script:result.Encoding | Should -Be $script:fileEncodingParameters.Encoding } It 'Should call the expected mocks' { @@ -177,6 +319,11 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } } @@ -247,20 +394,214 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + + It 'Should not throw an exception' { + { Set-TargetResource ` + -Path $script:testTextFile ` + -Name $script:testName ` + -Ensure 'Present' ` + -Text $script:testText ` + -Encoding $script:fileEncodingParameters.Encoding ` + -Verbose + } | Should -Not -Throw + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + + Context 'File exists and contains matching key that should exist but encoding is not compliant' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_KeyValuePairFile' ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -Verifiable + + It 'Should not throw an exception' { + { Set-TargetResource ` + -Path $script:testTextFile ` + -Name $script:testName ` + -Ensure 'Present' ` + -Text $script:testText ` + -Encoding $script:fileEncodingParameters.Encoding ` + -Verbose + } | Should -Not -Throw + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + + Context 'File exists and contains matching key that should exist and contain a secret' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_KeyValuePairFile' ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + + It 'Should not throw an exception' { + { Set-TargetResource ` + -Path $script:testTextFile ` + -Name $script:testName ` + -Ensure 'Present' ` + -Type 'Secret' ` + -Secret $script:testSecretCredential ` + -Encoding $script:fileEncodingParameters.Encoding ` + -Verbose + } | Should -Not -Throw + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + + Context 'File exists and contains matching key that should exist and contain a secret but encoding is not compliant' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_KeyValuePairFile' ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -Verifiable + + It 'Should not throw an exception' { + { Set-TargetResource ` + -Path $script:testTextFile ` + -Name $script:testName ` + -Ensure 'Present' ` + -Type 'Secret' ` + -Secret $script:testSecretCredential ` + -Encoding $script:fileEncodingParameters.Encoding ` + -Verbose + } | Should -Not -Throw + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + + Context 'File exists does not contain matching key but key should exist' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_KeyValuePairFile' ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileContent } ` + -Verifiable + Mock ` -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContent) + ($value -eq $script:testFileExpectedTextContentAdded) } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable It 'Should not throw an exception' { { Set-TargetResource ` -Path $script:testTextFile ` - -Name $script:testName ` + -Name $script:testAddedName ` -Ensure 'Present' ` -Text $script:testText ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -274,17 +615,23 @@ $($script:testAddedName)=$($script:testText) -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + Assert-MockCalled ` -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContent) + ($value -eq $script:testFileExpectedTextContentAdded) -and ` + ($encoding -eq $script:fileEncodingParameters.Encoding) } ` -Exactly 1 } } - Context 'File exists and contains matching key that should exist and contain a secret' { + Context 'File exists and contains key with a different case that should exist and IgnoreNameCase is True' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -301,17 +648,24 @@ $($script:testAddedName)=$($script:testText) -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedSecretContent) + ($value -eq $script:testFileExpectedTextContentUpper) } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + It 'Should not throw an exception' { { Set-TargetResource ` -Path $script:testTextFile ` - -Name $script:testName ` + -Name $script:testName.ToUpper() ` -Ensure 'Present' ` - -Type 'Secret' ` - -Secret $script:testSecretCredential ` + -Text $script:testText ` + -Encoding $script:fileEncodingParameters.Encoding ` + -IgnoreNameCase:$true ` -Verbose } | Should -Not -Throw } @@ -325,17 +679,129 @@ $($script:testAddedName)=$($script:testText) -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + Assert-MockCalled ` -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedSecretContent) + ($value -eq $script:testFileExpectedTextContentUpper) -and ` + ($encoding -eq $script:fileEncodingParameters.Encoding) } ` -Exactly 1 } } - Context 'File exists does not contain matching key but key should exist' { + Context 'File exists and does not contain key with matching case and should not' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_KeyValuePairFile' ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + + # non-verifiable mocks + Mock ` + -CommandName Set-Content + + It 'Should not throw an exception' { + { Set-TargetResource ` + -Path $script:testTextFile ` + -Name $script:testName.ToUpper() ` + -Encoding $script:fileEncodingParameters.Encoding ` + -Ensure 'Absent' ` + -Verbose + } | Should -Not -Throw + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Set-Content ` + -Exactly 0 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + + Context 'File exists and does not contain key with matching case and should not but encoding is not compliant' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_KeyValuePairFile' ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -Verifiable + + # non-verifiable mocks + Mock ` + -CommandName Set-Content + + It 'Should not throw an exception' { + { Set-TargetResource ` + -Path $script:testTextFile ` + -Name $script:testName.ToUpper() ` + -Encoding $script:fileEncodingParameters.Encoding ` + -Ensure 'Absent' ` + -Verbose + } | Should -Not -Throw + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Set-Content ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + + Context 'File exists and contains key with a different case but should not and IgnoreNameCase is True' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -352,20 +818,95 @@ $($script:testAddedName)=$($script:testText) -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContentAdded) + ($value -eq $script:testFileExpectedAbsentContent) } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + + It 'Should not throw an exception' { + { Set-TargetResource ` + -Path $script:testTextFile ` + -Name $script:testName ` + -Encoding $script:fileEncodingParameters.Encoding ` + -Ensure 'Absent' ` + -IgnoreNameCase:$true ` + -Verbose + } | Should -Not -Throw + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Set-Content ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($value -eq $script:testFileExpectedAbsentContent) + } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + } + #endregion + + #region Function Test-TargetResource + Describe 'DSR_KeyValuePairFile\Test-TargetResource' { + Context 'File exists and does not contain matching key but it should' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_KeyValuePairFile' ` + -Verifiable + + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_KeyValuePairFile' ` + -MockWith { $true } ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileExpectedTextContentAdded } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + It 'Should not throw an exception' { - { Set-TargetResource ` + { $script:result = Test-TargetResource ` -Path $script:testTextFile ` - -Name $script:testAddedName ` + -Name $script:testName.ToUpper() ` -Ensure 'Present' ` -Text $script:testText ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } + It 'Should return false' { + $script:result | Should -Be $false + } + It 'Should call the expected mocks' { Assert-VerifiableMock Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 @@ -376,22 +917,25 @@ $($script:testAddedName)=$($script:testText) -Exactly 1 Assert-MockCalled ` - -CommandName Set-Content ` - -ParameterFilter { - ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContentAdded) - } ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 } } - Context 'File exists and contains key with a different case that should exist and IgnoreNameCase is True' { + Context 'File exists and does not contain matching key and should not' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_KeyValuePairFile' ` + -MockWith { $true } ` + -Verifiable + Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` @@ -399,24 +943,25 @@ $($script:testAddedName)=$($script:testText) -Verifiable Mock ` - -CommandName Set-Content ` - -ParameterFilter { - ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContentUpper) - } ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable It 'Should not throw an exception' { - { Set-TargetResource ` + { $script:result = Test-TargetResource ` -Path $script:testTextFile ` -Name $script:testName.ToUpper() ` - -Ensure 'Present' ` - -Text $script:testText ` - -IgnoreNameCase:$true ` + -Ensure 'Absent' ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } + It 'Should return true' { + $script:result | Should -Be $true + } + It 'Should call the expected mocks' { Assert-VerifiableMock Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 @@ -426,42 +971,52 @@ $($script:testAddedName)=$($script:testText) -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - Assert-MockCalled ` - -CommandName Set-Content ` - -ParameterFilter { - ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContentUpper) - } ` + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 } } - Context 'File exists and does not contain key with matching case and should not' { + Context 'File exists and does not contain matching key and should not but file encoding is not compliant' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_KeyValuePairFile' ` + -MockWith { $true } ` + -Verifiable + Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -MockWith { $script:testFileContent } ` -Verifiable - # non-verifiable mocks Mock ` - -CommandName Set-Content + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -Verifiable It 'Should not throw an exception' { - { Set-TargetResource ` + { $script:result = Test-TargetResource ` -Path $script:testTextFile ` -Name $script:testName.ToUpper() ` -Ensure 'Absent' ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } + It 'Should return false' { + $script:result | Should -Be $false + } + It 'Should call the expected mocks' { Assert-VerifiableMock Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 @@ -472,42 +1027,52 @@ $($script:testAddedName)=$($script:testText) -Exactly 1 Assert-MockCalled ` - -CommandName Set-Content ` - -Exactly 0 + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } - Context 'File exists and contains key with a different case but should not and IgnoreNameCase is True' { + Context 'File exists and contains matching key that should exist and values match' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_KeyValuePairFile' ` + -MockWith { $true } ` + -Verifiable + Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileContent } ` + -MockWith { $script:testFileExpectedTextContent } ` -Verifiable Mock ` - -CommandName Set-Content ` - -ParameterFilter { - ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedAbsentContent) - } ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable It 'Should not throw an exception' { - { Set-TargetResource ` + { $script:result = Test-TargetResource ` -Path $script:testTextFile ` -Name $script:testName ` - -Ensure 'Absent' ` - -IgnoreNameCase:$true ` + -Ensure 'Present' ` + -Text $script:testText ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } + It 'Should return true' { + $script:result | Should -Be $true + } + It 'Should call the expected mocks' { Assert-VerifiableMock Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 @@ -516,22 +1081,10 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - - Assert-MockCalled ` - -CommandName Set-Content ` - -ParameterFilter { - ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedAbsentContent) - } ` - -Exactly 1 } } - } - #endregion - #region Function Test-TargetResource - Describe 'DSR_KeyValuePairFile\Test-TargetResource' { - Context 'File exists and does not contain matching key but it should' { + Context 'File exists and contains matching key that should exist and values match but file encoding is not compliant' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -547,15 +1100,22 @@ $($script:testAddedName)=$($script:testText) Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileContent } ` + -MockWith { $script:testFileExpectedTextContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` -Verifiable It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` - -Name $script:testName.ToUpper() ` + -Name $script:testName ` -Ensure 'Present' ` -Text $script:testText ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -572,10 +1132,15 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } - Context 'File exists and does not contain matching key but should not' { + Context 'File exists and contains matching key that should exist and values do not match secret text' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -594,17 +1159,26 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` - -Name $script:testName.ToUpper() ` - -Ensure 'Absent' ` + -Name $script:testName ` + -Ensure 'Present' ` + -Type 'Secret' ` + -Secret $script:testSecretCredential ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } - It 'Should return true' { - $script:result | Should -Be $true + It 'Should return false' { + $script:result | Should -Be $false } It 'Should call the expected mocks' { @@ -615,10 +1189,15 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } - Context 'File exists and contains matching key that should exist and values match' { + Context 'File exists and contains matching key that should exist and values match secret text' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -634,7 +1213,13 @@ $($script:testAddedName)=$($script:testText) Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileExpectedTextContent } ` + -MockWith { $script:testFileExpectedSecretContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable It 'Should not throw an exception' { @@ -642,7 +1227,9 @@ $($script:testAddedName)=$($script:testText) -Path $script:testTextFile ` -Name $script:testName ` -Ensure 'Present' ` - -Text $script:testText ` + -Type 'Secret' ` + -Secret $script:testSecretCredential ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -659,10 +1246,15 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } - Context 'File exists and contains matching key that should exist and values do not match secret text' { + Context 'File exists and contains matching key that should exist and values match secret text but file encoding is not compliant' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -678,7 +1270,13 @@ $($script:testAddedName)=$($script:testText) Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileContent } ` + -MockWith { $script:testFileExpectedSecretContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` -Verifiable It 'Should not throw an exception' { @@ -688,6 +1286,7 @@ $($script:testAddedName)=$($script:testText) -Ensure 'Present' ` -Type 'Secret' ` -Secret $script:testSecretCredential ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -704,10 +1303,15 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } - Context 'File exists and contains matching key that should exist and values match secret text' { + Context 'File exists and contains key with different case that should exist and values match and IgnoreNameCase is True' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -723,16 +1327,23 @@ $($script:testAddedName)=$($script:testText) Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileExpectedSecretContent } ` + -MockWith { $script:testFileExpectedTextContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` - -Name $script:testName ` + -Name $script:testName.ToUpper() ` -Ensure 'Present' ` - -Type 'Secret' ` - -Secret $script:testSecretCredential ` + -Text $script:testText ` + -IgnoreNameCase:$true ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -749,10 +1360,15 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } - Context 'File exists and contains key with different case that should exist and values match and IgnoreNameCase is True' { + Context 'File exists and contains key with different case that should exist and values match and IgnoreNameCase is True but file encoding is not compliant' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -771,6 +1387,12 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileExpectedTextContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -Verifiable + It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` @@ -778,12 +1400,13 @@ $($script:testAddedName)=$($script:testText) -Ensure 'Present' ` -Text $script:testText ` -IgnoreNameCase:$true ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } - It 'Should return true' { - $script:result | Should -Be $true + It 'Should return false' { + $script:result | Should -Be $false } It 'Should call the expected mocks' { @@ -794,6 +1417,11 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } @@ -816,12 +1444,19 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileExpectedTextContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` -Name $script:testName ` -Ensure 'Present' ` -Text $script:testText.ToUpper() ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -838,6 +1473,11 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } @@ -860,12 +1500,19 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileExpectedTextContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` -Name $script:testName ` -Ensure 'Present' ` -Text $script:testText.ToUpper() ` + -Encoding $script:fileEncodingParameters.Encoding ` -IgnoreValueCase:$true ` -Verbose } | Should -Not -Throw @@ -883,10 +1530,15 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } - Context 'File exists and contains matching key that should not exist' { + Context 'File exists and contains matching key that should exist and values match but are different case and IgnoreValueCase is True but file encoding is not compliant' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -905,13 +1557,21 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileExpectedTextContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -Verifiable + It 'Should not throw an exception' { { $script:result = Test-TargetResource ` - -Path $script:testTextFile ` - -Name $script:testName ` - -Ensure 'Absent' ` - -Text $script:testText ` - -Verbose + -Path $script:testTextFile ` + -Name $script:testName ` + -Ensure 'Present' ` + -Text $script:testText.ToUpper() ` + -Encoding $script:fileEncodingParameters.Encoding ` + -IgnoreValueCase:$true ` + -Verbose } | Should -Not -Throw } @@ -927,10 +1587,15 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } - Context 'File exists and does not contain matching key but it should' { + Context 'File exists and contains matching key that should not exist' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -940,15 +1605,28 @@ $($script:testAddedName)=$($script:testText) Mock ` -CommandName Test-Path ` -ModuleName 'DSR_KeyValuePairFile' ` - -MockWith { $false } ` + -MockWith { $true } ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileExpectedTextContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` -Verifiable It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` - -Name $script:testName.ToUpper() ` - -Ensure 'Present' ` + -Name $script:testName ` + -Ensure 'Absent' ` -Text $script:testText ` + -Encoding $script:testNonCompliantEncoding.Encoding ` -Verbose } | Should -Not -Throw } @@ -960,10 +1638,20 @@ $($script:testAddedName)=$($script:testText) It 'Should call the expected mocks' { Assert-VerifiableMock Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } - } + #endregion #region Function Assert-ParametersValid diff --git a/Tests/Unit/DSR_ReplaceText.Tests.ps1 b/Tests/Unit/DSR_ReplaceText.Tests.ps1 index b993dee..2a80fbe 100644 --- a/Tests/Unit/DSR_ReplaceText.Tests.ps1 +++ b/Tests/Unit/DSR_ReplaceText.Tests.ps1 @@ -41,6 +41,21 @@ try $script:testSecureSecretReplace = ConvertTo-SecureString -String $script:testSecretReplace -AsPlainText -Force $script:testSecretCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ('Dummy', $script:testSecureSecretReplace) + $script:fileEncodingParameters = @{ + Path = $script:testTextFile + Encoding = 'ASCII' + } + + $script:testCompliantEncoding = @{ + Path = $script:fileEncodingParameters.Path + Encoding = $script:fileEncodingParameters.Encoding + } + + $script:testNonCompliantEncoding = @{ + Path = $script:fileEncodingParameters.Path + Encoding = 'UTF8' + } + $script:testFileContent = @" Setting1=Value1 Setting.Two='Value2' @@ -71,7 +86,7 @@ Setting3.Test=Value4 #region Function Get-TargetResource Describe 'DSR_ReplaceText\Get-TargetResource' { - Context 'File exists and search text can be found' { + Context 'File exists and search text can be found and encoding is compliant' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -84,6 +99,12 @@ Setting3.Test=Value4 -MockWith { $script:testFileExpectedTextContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + $script:result = $null It 'Should not throw an exception' { @@ -99,6 +120,7 @@ Setting3.Test=Value4 $script:result.Search | Should -Be $script:testSearch $script:result.Type | Should -Be 'Text' $script:result.Text | Should -Be "$($script:testTextReplace),$($script:testTextReplace),$($script:testTextReplace)" + $script:result.Encoding | Should -Be $script:fileEncodingParameters.Encoding } It 'Should call the expected mocks' { @@ -109,10 +131,68 @@ Setting3.Test=Value4 -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } - Context 'File exists and search text can not be found' { + Context 'File exists and search text can be found but encoding is not compliant' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_ReplaceText' ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileExpectedTextContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -Verifiable + + $script:result = $null + + It 'Should not throw an exception' { + { $script:result = Get-TargetResource ` + -Path $script:testTextFile ` + -Search $script:testSearch ` + -Verbose + } | Should -Not -Throw + } + + It 'Should return expected values' { + $script:result.Path | Should -Be $script:testTextFile + $script:result.Search | Should -Be $script:testSearch + $script:result.Type | Should -Be 'Text' + $script:result.Text | Should -Be "$($script:testTextReplace),$($script:testTextReplace),$($script:testTextReplace)" + $script:result.Encoding | Should -Be $script:testNonCompliantEncoding.Encoding + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + + Context 'File exists and search text can not be found and encoding is compliant' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -125,6 +205,12 @@ Setting3.Test=Value4 -MockWith { $script:testFileExpectedTextContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + $script:result = $null It 'Should not throw an exception' { @@ -140,6 +226,7 @@ Setting3.Test=Value4 $script:result.Search | Should -Be $script:testSearchNoFind $script:result.Type | Should -Be 'Text' $script:result.Text | Should -BeNullOrEmpty + $script:result.Encoding | Should -Be $script:fileEncodingParameters.Encoding } It 'Should call the expected mocks' { @@ -150,6 +237,64 @@ Setting3.Test=Value4 -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + + Context 'File exists and search text can not be found but encoding is not compliant' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_ReplaceText' ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileExpectedTextContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -Verifiable + + $script:result = $null + + It 'Should not throw an exception' { + { $script:result = Get-TargetResource ` + -Path $script:testTextFile ` + -Search $script:testSearchNoFind ` + -Verbose + } | Should -Not -Throw + } + + It 'Should return expected values' { + $script:result.Path | Should -Be $script:testTextFile + $script:result.Search | Should -Be $script:testSearchNoFind + $script:result.Type | Should -Be 'Text' + $script:result.Text | Should -BeNullOrEmpty + $script:result.Encoding | Should -Be $script:testNonCompliantEncoding.Encoding + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } } @@ -157,7 +302,7 @@ Setting3.Test=Value4 #region Function Set-TargetResource Describe 'DSR_ReplaceText\Set-TargetResource' { - Context 'File exists and search text can be found' { + Context 'File exists and search text can be found and encoding is compliant' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -178,11 +323,18 @@ Setting3.Test=Value4 } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + It 'Should not throw an exception' { { Set-TargetResource ` -Path $script:testTextFile ` -Search $script:testSearch ` -Text $script:testTextReplace ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -196,6 +348,11 @@ Setting3.Test=Value4 -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + Assert-MockCalled ` -CommandName Set-Content ` -ParameterFilter { @@ -206,7 +363,7 @@ Setting3.Test=Value4 } } - Context 'File exists and search secret can be found' { + Context 'File exists and search secret can be found and encoding is compliant' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -219,11 +376,18 @@ Setting3.Test=Value4 -MockWith { $script:testFileContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + Mock ` -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedSecretContent) + ($value -eq $script:testFileExpectedSecretContent) -and ` + ($encoding -eq $script:fileEncodingParameters.Encoding) } ` -Verifiable @@ -233,6 +397,7 @@ Setting3.Test=Value4 -Search $script:testSearch ` -Type 'Secret' ` -Secret $script:testSecretCredential ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -246,6 +411,11 @@ Setting3.Test=Value4 -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + Assert-MockCalled ` -CommandName Set-Content ` -ParameterFilter { @@ -329,6 +499,11 @@ Setting3.Test=Value4 -MockWith { $script:testFileContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Verifiable + $script:result = $null It 'Should not throw an exception' { @@ -374,6 +549,11 @@ Setting3.Test=Value4 -MockWith { $script:testFileContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Verifiable + $script:result = $null It 'Should not throw an exception' { @@ -419,6 +599,11 @@ Setting3.Test=Value4 -MockWith { $script:testFileExpectedTextContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Verifiable + $script:result = $null It 'Should not throw an exception' { @@ -464,6 +649,11 @@ Setting3.Test=Value4 -MockWith { $script:testFileContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Verifiable + $script:result = $null It 'Should not throw an exception' { @@ -510,6 +700,11 @@ Setting3.Test=Value4 -MockWith { $script:testFileExpectedSecretContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Verifiable + $script:result = $null It 'Should not throw an exception' { From d6297b6c424817e816e6c533a4d0539f6b074ade Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Mon, 10 Sep 2018 17:45:49 -0400 Subject: [PATCH 03/46] fixing corrupt schema files --- .../DSR_KeyValuePairFile.schema.mof | Bin 3032 -> 1502 bytes .../DSR_ReplaceText.schema.mof | Bin 2168 -> 1073 bytes 2 files changed, 0 insertions(+), 0 deletions(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof index 5b84d7f4faca84a2026cf34e168321dd5a182411..b6204853a61f276db6c6b92f940cafffc47226e9 100644 GIT binary patch literal 1502 zcmchXO=}x55Qgvm6~j_U0><>?&{Cgb#|x&oG1*Oe2*t>&u`MD?i=?fyg#7o6q_wka z;?f>kd|Bp0^UOOlz8_0w?Hw8`v^q~d4Bq=+a?yv>2vl6w-`N(=lW$nxF9o1qW0SGG~NZ!<^!Wr=L?q40^SHuAS+d37J2PaCn50l~{P$|ZeBE|=l!IeP7zzC*tg zWh?Wi+)+GNm*^#}ECP!S87w6$a7zSWgOCFn#fnwZQGkZCoU}e8wOYj9{{G-MZM=6;@g?p%KUI7J-;Twub_MC zb!YfM{jXe0<2}D;v~(HW!k}Ko*9g2Gv{x^_t3uG{uiFk zxc)ZJ%*iS@$tL*Dw?V(ivsz?om|1T9Ww_$G@R0p?kBl#)zOXiou5UfZ@vZXeD-?VE zWTR5#>yc>_lZ3dYHa8)ye4SVAh1GhDrnKOZb=s3VmV312ZA|Dv6&;Xc#-f|jvk*?n zyWq2o_fEvT7Lgj^hIKe%rixpP&AC>-73|?M+Pmd$VD01qdS|p&uJHl=Oe`CdQl0+< z;xYenGE%h>8sh4;&BrkBxJJr-WP44$A?^qTLz%$KD%a|bK6Qn8URaRTEp&Jyc5m?} zaCbmJDxmD8JicvesnZf_RUmKlHZ5-mT&qkb9#l%om#>KUW`!YOSE-@a{d-k=*LtcHI zZ~u&4Du01vUE8aRxA~@9es#A#V`j=iRSnaiM$bd;CKd-nR6;axo`{q z4tDG2tDkO~fa33vIXD#7-Nbw3cbzqElFM$^)u?p^%_Zrp4kvul!lM3_t(NPMQLpi8 zE>oTpL33s?_Um{e`n}~oPuPhKyqVi~>)mLb@n3;@XUg~TN%FsEym<4gmN6e(*WMQY GH+})c*!yb$ diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof index 0d980b728fbbd41565d4b199b9bad2bef5f9ee3e..66d0c369b144671ef3ba5de2d26ade75a11cc761 100644 GIT binary patch literal 1073 zcmcgqO-sW-5WVMD47pUH6h97%J!x&ANL8As2a&SfowkeVChTslLHzH|PJ*UtZz8>< z^LE~Q^JX3gxp3|tt<#0siw}DT;TP|BA+Z{b%ByR!!o7HcWiBLU`0C^Rr$~k_xJ)P0 z764~(GagMZgu@BCVr?ZxZxO)b6;@riL?^8-J?mskgi`n=_yRnAd*X$;&grWFTWc}k zywzp_+Vn^k()!;mb@q1?PjE4Og{G7E){bc%3M)ymE!SpE8E4)Ht37_RoR%o{T%%)# z7m=@twJMo9D2N(p%&*Y+#zK!qpNnUKYFNQ{GVeX8khqztKlH;RLX?D+G<@r&4#i7#byvvdkE@jDS4LzG;V_L`X Q-@>1p9yU4jqxXLF0j9uYIsgCw literal 2168 zcmds&UrQT76vgjzq2FQI7b{5UpNAIv)M^4!QcDxe6%#Zc-bHU%l<`%j?9xhmX1TqvmOXF2xoz5(W!Ax} zz_zpzz6<+^|2OvX_{?+aZXB}e^Zj8n&NuDYp4t-|HqkQMwI22-YkV@>Cf2@nZ4aCG z4&&I_R^Xe1K@oK4PxqT)ePkavTR4=vtSW4Tbqa=w#m+XSpuFb!dBue~S2v%T_!|59^uk!Pb}>6pl&NBXaf~WE1M>(Jq{2z#X!XE~CdX z`%+vl4=B26N*iYHpN=u`qEdPxjj`LBf8Zt#k^%V3ICGC{hU+>6b zhy3^p8@wj1Rc4sBawKYuAU7SN5{8(KL$PA0sTBGQvLX#&EVe<^g~(X$-@i%rrF{kMC4EtHx@tcB?-}3i>-H@@c;(&H^X?D2Qf9dT From b881d608ed83710c7295777a80f8e6aed2020470 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Wed, 12 Sep 2018 13:52:30 -0400 Subject: [PATCH 04/46] keyvaluepair unit test and psm1 updates --- .../DSR_KeyValuePairFile.psm1 | 10 +- Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 | 150 ++++++++++++------ 2 files changed, 108 insertions(+), 52 deletions(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 index edc5d16..373f63e 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 @@ -52,12 +52,11 @@ function Get-TargetResource Assert-ParametersValid @PSBoundParameters $fileContent = Get-Content -Path $Path -Raw + $fileEncoding = Get-FileEncoding -Path $Path Write-Verbose -Message ($localizedData.SearchForKeyMessage -f ` $Path, $Name) - $fileEncoding = Get-FileEncoding -Path $Path - # Setup the Regex Options that will be used $regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline @@ -372,12 +371,11 @@ function Test-TargetResource } $fileContent = Get-Content -Path $Path -Raw + $fileEncoding = Get-FileEncoding -Path $Path Write-Verbose -Message ($localizedData.SearchForKeyMessage -f ` $Path, $Name) - $fileEncoding = Get-FileEncoding -Path $Path - # Setup the Regex Options that will be used $regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline if ($IgnoreNameCase) @@ -433,8 +431,8 @@ function Test-TargetResource if ($desiredConfigurationMatch) { - Write-Verbose -Message ($localizedData.KeyFoundButNoReplacementMessage -f ` - $Path, $Name) + Write-Verbose -Message ($localizedData.KeyUpdateMessage -f ` + $Path, $Name) } } else diff --git a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 index dd7f09e..7d555b3 100644 --- a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 +++ b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 @@ -111,7 +111,7 @@ $($script:testAddedName)=$($script:testText) #region Function Get-TargetResource Describe 'DSR_KeyValuePairFile\Get-TargetResource' { - Context 'File exists and contains matching key and encoding is compliant' { + Context 'File exists, contains matching key, and encoding is in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -141,11 +141,11 @@ $($script:testAddedName)=$($script:testText) } It 'Should return expected values' { - $script:result.Path | Should -Be $script:testTextFile - $script:result.Name | Should -Be $script:testName - $script:result.Ensure | Should -Be 'Present' - $script:result.Type | Should -Be 'Text' - $script:result.Text | Should -Be "$($script:testText),$($script:testText),$($script:testText)" + $script:result.Path | Should -Be $script:testTextFile + $script:result.Name | Should -Be $script:testName + $script:result.Ensure | Should -Be 'Present' + $script:result.Type | Should -Be 'Text' + $script:result.Text | Should -Be "$($script:testText),$($script:testText),$($script:testText)" $script:result.Encoding | Should -Be $script:fileEncodingParameters.Encoding } @@ -165,7 +165,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists and contains matching key and encoding is not compliant' { + Context 'File exists, contains matching key but encoding is not in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -195,11 +195,11 @@ $($script:testAddedName)=$($script:testText) } It 'Should return expected values' { - $script:result.Path | Should -Be $script:testTextFile - $script:result.Name | Should -Be $script:testName - $script:result.Ensure | Should -Be 'Present' - $script:result.Type | Should -Be 'Text' - $script:result.Text | Should -Be "$($script:testText),$($script:testText),$($script:testText)" + $script:result.Path | Should -Be $script:testTextFile + $script:result.Name | Should -Be $script:testName + $script:result.Ensure | Should -Be 'Present' + $script:result.Type | Should -Be 'Text' + $script:result.Text | Should -Be "$($script:testText),$($script:testText),$($script:testText)" $script:result.Encoding | Should -Be $script:testNonCompliantEncoding.Encoding } @@ -219,7 +219,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists and does not contain matching key and encoding is compliant' { + Context 'File exists, does not contain matching key and encoding is in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -249,11 +249,11 @@ $($script:testAddedName)=$($script:testText) } It 'Should return expected values' { - $script:result.Path | Should -Be $script:testTextFile - $script:result.Name | Should -Be $script:testName - $script:result.Ensure | Should -Be 'Absent' - $script:result.Type | Should -Be 'Text' - $script:result.Text | Should -BeNullOrEmpty + $script:result.Path | Should -Be $script:testTextFile + $script:result.Name | Should -Be $script:testName + $script:result.Ensure | Should -Be 'Absent' + $script:result.Type | Should -Be 'Text' + $script:result.Text | Should -BeNullOrEmpty $script:result.Encoding | Should -Be $script:fileEncodingParameters.Encoding } @@ -273,7 +273,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists and does not contain matching key and encoding is compliant' { + Context 'File exists, does not contain matching key and encoding is in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -303,11 +303,11 @@ $($script:testAddedName)=$($script:testText) } It 'Should return expected values' { - $script:result.Path | Should -Be $script:testTextFile - $script:result.Name | Should -Be $script:testName - $script:result.Ensure | Should -Be 'Absent' - $script:result.Type | Should -Be 'Text' - $script:result.Text | Should -BeNullOrEmpty + $script:result.Path | Should -Be $script:testTextFile + $script:result.Name | Should -Be $script:testName + $script:result.Ensure | Should -Be 'Absent' + $script:result.Type | Should -Be 'Text' + $script:result.Text | Should -BeNullOrEmpty $script:result.Encoding | Should -Be $script:fileEncodingParameters.Encoding } @@ -345,11 +345,9 @@ $($script:testAddedName)=$($script:testText) -Verifiable Mock ` - -CommandName Set-Content ` - -ParameterFilter { - ($path -eq $script:testTextFile) -and ` - ($value -eq "$script:testName=$script:testText") - } ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $null } ` -Verifiable It 'Should not throw an exception' { @@ -358,6 +356,7 @@ $($script:testAddedName)=$($script:testText) -Name $script:testName ` -Ensure 'Present' ` -Text $script:testText ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -365,23 +364,10 @@ $($script:testAddedName)=$($script:testText) It 'Should call the expected mocks' { Assert-VerifiableMock Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - - Assert-MockCalled ` - -CommandName Set-Content ` - -ParameterFilter { - ($path -eq $script:testTextFile) -and ` - ($value -eq "$script:testName=$script:testText") - } ` - -Exactly 1 } } - Context 'File exists and contains matching key that should exist' { + Context 'File exists, contains matching key that should exist and encoding is in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -400,6 +386,15 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable + Mock ` + -CommandName Set-Content ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($value -eq $script:testFileExpectedTextContent) -and ` + ($encoding -eq $script:fileEncodingParameters.Encoding) + } ` + -Verifiable + It 'Should not throw an exception' { { Set-TargetResource ` -Path $script:testTextFile ` @@ -424,10 +419,19 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Set-Content ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($value -eq $script:testFileExpectedTextContent) -and ` + ($encoding -eq $script:fileEncodingParameters.Encoding) + } ` + -Exactly 1 } } - Context 'File exists and contains matching key that should exist but encoding is not compliant' { + Context 'File exists, contains matching key that should exist but encoding is not in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -446,6 +450,15 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testNonCompliantEncoding.Encoding } ` -Verifiable + Mock ` + -CommandName Set-Content ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($value -eq $script:testFileExpectedTextContent) -and ` + ($encoding -eq $script:fileEncodingParameters.Encoding) + } ` + -Verifiable + It 'Should not throw an exception' { { Set-TargetResource ` -Path $script:testTextFile ` @@ -470,10 +483,19 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Set-Content ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($value -eq $script:testFileExpectedTextContent) -and ` + ($encoding -eq $script:fileEncodingParameters.Encoding) + } ` + -Exactly 1 } } - Context 'File exists and contains matching key that should exist and contain a secret' { + Context 'File exists, contains matching key that should exist, contains a secret and encoding is in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -492,6 +514,15 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable + Mock ` + -CommandName Set-Content ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($value -eq $script:testFileExpectedSecretContent) + } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + It 'Should not throw an exception' { { Set-TargetResource ` -Path $script:testTextFile ` @@ -517,10 +548,19 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Set-Content ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($value -eq $script:testFileExpectedSecretContent) -and ` + ($encoding -eq $script:fileEncodingParameters.Encoding) + } ` + -Exactly 1 } } - Context 'File exists and contains matching key that should exist and contain a secret but encoding is not compliant' { + Context 'File exists, contains matching key that should exist, contains a secret but encoding is not in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -539,6 +579,15 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testNonCompliantEncoding.Encoding } ` -Verifiable + Mock ` + -CommandName Set-Content ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($value -eq $script:testFileExpectedSecretContent) + } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + It 'Should not throw an exception' { { Set-TargetResource ` -Path $script:testTextFile ` @@ -564,6 +613,15 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Set-Content ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($value -eq $script:testFileExpectedSecretContent) -and ` + ($encoding -eq $script:fileEncodingParameters.Encoding) + } ` + -Exactly 1 } } From da04f8936b9370d86e2564f537622d2c5f52be8e Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Tue, 2 Oct 2018 10:08:02 -0400 Subject: [PATCH 05/46] update keyvaluepair tests --- Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 | 555 ++-------------------- 1 file changed, 34 insertions(+), 521 deletions(-) diff --git a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 index 7d555b3..5db3e56 100644 --- a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 +++ b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 @@ -111,7 +111,7 @@ $($script:testAddedName)=$($script:testText) #region Function Get-TargetResource Describe 'DSR_KeyValuePairFile\Get-TargetResource' { - Context 'File exists, contains matching key, and encoding is in desired state' { + Context 'File exists and contains matching key and encoding is in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -165,7 +165,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists, contains matching key but encoding is not in desired state' { + Context 'File exists and contains matching key but encoding is not in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -219,7 +219,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists, does not contain matching key and encoding is in desired state' { + Context 'File exists and does not contain matching key and encoding is in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -238,6 +238,7 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable + $script:result = $null It 'Should not throw an exception' { @@ -273,7 +274,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists, does not contain matching key and encoding is in desired state' { + Context 'File exists and does not contain matching key and encoding is not in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -289,7 +290,7 @@ $($script:testAddedName)=$($script:testText) Mock ` -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` -Verifiable $script:result = $null @@ -308,7 +309,7 @@ $($script:testAddedName)=$($script:testText) $script:result.Ensure | Should -Be 'Absent' $script:result.Type | Should -Be 'Text' $script:result.Text | Should -BeNullOrEmpty - $script:result.Encoding | Should -Be $script:fileEncodingParameters.Encoding + $script:result.Encoding | Should -Be $script:testNonCompliantEncoding.Encoding } It 'Should call the expected mocks' { @@ -367,71 +368,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists, contains matching key that should exist and encoding is in desired state' { - # verifiable (should be called) mocks - Mock ` - -CommandName Assert-ParametersValid ` - -ModuleName 'DSR_KeyValuePairFile' ` - -Verifiable - - Mock ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileContent } ` - -Verifiable - - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` - -Verifiable - - Mock ` - -CommandName Set-Content ` - -ParameterFilter { - ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContent) -and ` - ($encoding -eq $script:fileEncodingParameters.Encoding) - } ` - -Verifiable - - It 'Should not throw an exception' { - { Set-TargetResource ` - -Path $script:testTextFile ` - -Name $script:testName ` - -Ensure 'Present' ` - -Text $script:testText ` - -Encoding $script:fileEncodingParameters.Encoding ` - -Verbose - } | Should -Not -Throw - } - - It 'Should call the expected mocks' { - Assert-VerifiableMock - Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - - Assert-MockCalled ` - -CommandName Set-Content ` - -ParameterFilter { - ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContent) -and ` - ($encoding -eq $script:fileEncodingParameters.Encoding) - } ` - -Exactly 1 - } - } - - Context 'File exists, contains matching key that should exist but encoding is not in desired state' { + Context 'File exists and contains matching key that should exist' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -444,18 +381,11 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileContent } ` -Verifiable - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testNonCompliantEncoding.Encoding } ` - -Verifiable - Mock ` -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContent) -and ` - ($encoding -eq $script:fileEncodingParameters.Encoding) + ($value -eq $script:testFileExpectedTextContent) ` } ` -Verifiable @@ -465,7 +395,6 @@ $($script:testAddedName)=$($script:testText) -Name $script:testName ` -Ensure 'Present' ` -Text $script:testText ` - -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -479,23 +408,17 @@ $($script:testAddedName)=$($script:testText) -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - Assert-MockCalled ` -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContent) -and ` - ($encoding -eq $script:fileEncodingParameters.Encoding) + ($value -eq $script:testFileExpectedTextContent) } ` -Exactly 1 } } - Context 'File exists, contains matching key that should exist, contains a secret and encoding is in desired state' { + Context 'File exists and contains matching key that should exist and contains a secret' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -508,19 +431,12 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileContent } ` -Verifiable - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` - -Verifiable - Mock ` -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` ($value -eq $script:testFileExpectedSecretContent) } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable It 'Should not throw an exception' { @@ -530,7 +446,6 @@ $($script:testAddedName)=$($script:testText) -Ensure 'Present' ` -Type 'Secret' ` -Secret $script:testSecretCredential ` - -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -544,88 +459,17 @@ $($script:testAddedName)=$($script:testText) -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - Assert-MockCalled ` -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedSecretContent) -and ` - ($encoding -eq $script:fileEncodingParameters.Encoding) + ($value -eq $script:testFileExpectedSecretContent) } ` -Exactly 1 } } - Context 'File exists, contains matching key that should exist, contains a secret but encoding is not in desired state' { - # verifiable (should be called) mocks - Mock ` - -CommandName Assert-ParametersValid ` - -ModuleName 'DSR_KeyValuePairFile' ` - -Verifiable - - Mock ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileContent } ` - -Verifiable - - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testNonCompliantEncoding.Encoding } ` - -Verifiable - - Mock ` - -CommandName Set-Content ` - -ParameterFilter { - ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedSecretContent) - } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` - -Verifiable - - It 'Should not throw an exception' { - { Set-TargetResource ` - -Path $script:testTextFile ` - -Name $script:testName ` - -Ensure 'Present' ` - -Type 'Secret' ` - -Secret $script:testSecretCredential ` - -Encoding $script:fileEncodingParameters.Encoding ` - -Verbose - } | Should -Not -Throw - } - - It 'Should call the expected mocks' { - Assert-VerifiableMock - Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - - Assert-MockCalled ` - -CommandName Set-Content ` - -ParameterFilter { - ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedSecretContent) -and ` - ($encoding -eq $script:fileEncodingParameters.Encoding) - } ` - -Exactly 1 - } - } - - Context 'File exists does not contain matching key but key should exist' { + Context 'File exists and does not contain matching key but key should exist' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -644,13 +488,6 @@ $($script:testAddedName)=$($script:testText) ($path -eq $script:testTextFile) -and ` ($value -eq $script:testFileExpectedTextContentAdded) } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` - -Verifiable - - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable It 'Should not throw an exception' { @@ -659,7 +496,6 @@ $($script:testAddedName)=$($script:testText) -Name $script:testAddedName ` -Ensure 'Present' ` -Text $script:testText ` - -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -673,17 +509,11 @@ $($script:testAddedName)=$($script:testText) -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - Assert-MockCalled ` -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContentAdded) -and ` - ($encoding -eq $script:fileEncodingParameters.Encoding) + ($value -eq $script:testFileExpectedTextContentAdded) } ` -Exactly 1 } @@ -710,19 +540,12 @@ $($script:testAddedName)=$($script:testText) } ` -Verifiable - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` - -Verifiable - It 'Should not throw an exception' { { Set-TargetResource ` -Path $script:testTextFile ` -Name $script:testName.ToUpper() ` -Ensure 'Present' ` -Text $script:testText ` - -Encoding $script:fileEncodingParameters.Encoding ` -IgnoreNameCase:$true ` -Verbose } | Should -Not -Throw @@ -737,23 +560,17 @@ $($script:testAddedName)=$($script:testText) -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - Assert-MockCalled ` -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContentUpper) -and ` - ($encoding -eq $script:fileEncodingParameters.Encoding) + ($value -eq $script:testFileExpectedTextContentUpper) } ` -Exactly 1 } } - Context 'File exists and does not contain key with matching case and should not' { + Context 'File exists and does not contain key with matching case and should not and encoding is in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -806,7 +623,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists and does not contain key with matching case and should not but encoding is not compliant' { + Context 'File exists and does not contain key with matching case and should not but encoding in not in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -822,7 +639,8 @@ $($script:testAddedName)=$($script:testText) Mock ` -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -MockWith { $script:testNonCompliantEncoding.Encoding + } ` -Verifiable # non-verifiable mocks @@ -880,17 +698,10 @@ $($script:testAddedName)=$($script:testText) } ` -Verifiable - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` - -Verifiable - It 'Should not throw an exception' { { Set-TargetResource ` -Path $script:testTextFile ` -Name $script:testName ` - -Encoding $script:fileEncodingParameters.Encoding ` -Ensure 'Absent' ` -IgnoreNameCase:$true ` -Verbose @@ -913,11 +724,6 @@ $($script:testAddedName)=$($script:testText) ($value -eq $script:testFileExpectedAbsentContent) } ` -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 } } } @@ -944,19 +750,12 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileExpectedTextContentAdded } ` -Verifiable - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` - -Verifiable - It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` -Name $script:testName.ToUpper() ` -Ensure 'Present' ` -Text $script:testText ` - -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -973,15 +772,10 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 } } - Context 'File exists and does not contain matching key and should not' { + Context 'File exists and does not contain matching key and should not and encoding is in desired state ' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -1036,7 +830,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists and does not contain matching key and should not but file encoding is not compliant' { + Context 'File exists and does not contain matching key and should not but encoding is not in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -1071,7 +865,7 @@ $($script:testAddedName)=$($script:testText) } | Should -Not -Throw } - It 'Should return false' { + It 'Should return true' { $script:result | Should -Be $false } @@ -1084,7 +878,7 @@ $($script:testAddedName)=$($script:testText) -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - Assert-MockCalled ` + Assert-MockCalled ` -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 @@ -1142,62 +936,6 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists and contains matching key that should exist and values match but file encoding is not compliant' { - # verifiable (should be called) mocks - Mock ` - -CommandName Assert-ParametersValid ` - -ModuleName 'DSR_KeyValuePairFile' ` - -Verifiable - - Mock ` - -CommandName Test-Path ` - -ModuleName 'DSR_KeyValuePairFile' ` - -MockWith { $true } ` - -Verifiable - - Mock ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileExpectedTextContent } ` - -Verifiable - - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testNonCompliantEncoding.Encoding } ` - -Verifiable - - It 'Should not throw an exception' { - { $script:result = Test-TargetResource ` - -Path $script:testTextFile ` - -Name $script:testName ` - -Ensure 'Present' ` - -Text $script:testText ` - -Encoding $script:fileEncodingParameters.Encoding ` - -Verbose - } | Should -Not -Throw - } - - It 'Should return false' { - $script:result | Should -Be $false - } - - It 'Should call the expected mocks' { - Assert-VerifiableMock - Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - } - } - Context 'File exists and contains matching key that should exist and values do not match secret text' { # verifiable (should be called) mocks Mock ` @@ -1217,12 +955,6 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileContent } ` -Verifiable - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` - -Verifiable - It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` @@ -1230,7 +962,6 @@ $($script:testAddedName)=$($script:testText) -Ensure 'Present' ` -Type 'Secret' ` -Secret $script:testSecretCredential ` - -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -1247,11 +978,6 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 } } @@ -1312,177 +1038,6 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists and contains matching key that should exist and values match secret text but file encoding is not compliant' { - # verifiable (should be called) mocks - Mock ` - -CommandName Assert-ParametersValid ` - -ModuleName 'DSR_KeyValuePairFile' ` - -Verifiable - - Mock ` - -CommandName Test-Path ` - -ModuleName 'DSR_KeyValuePairFile' ` - -MockWith { $true } ` - -Verifiable - - Mock ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileExpectedSecretContent } ` - -Verifiable - - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testNonCompliantEncoding.Encoding } ` - -Verifiable - - It 'Should not throw an exception' { - { $script:result = Test-TargetResource ` - -Path $script:testTextFile ` - -Name $script:testName ` - -Ensure 'Present' ` - -Type 'Secret' ` - -Secret $script:testSecretCredential ` - -Encoding $script:fileEncodingParameters.Encoding ` - -Verbose - } | Should -Not -Throw - } - - It 'Should return false' { - $script:result | Should -Be $false - } - - It 'Should call the expected mocks' { - Assert-VerifiableMock - Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - } - } - - Context 'File exists and contains key with different case that should exist and values match and IgnoreNameCase is True' { - # verifiable (should be called) mocks - Mock ` - -CommandName Assert-ParametersValid ` - -ModuleName 'DSR_KeyValuePairFile' ` - -Verifiable - - Mock ` - -CommandName Test-Path ` - -ModuleName 'DSR_KeyValuePairFile' ` - -MockWith { $true } ` - -Verifiable - - Mock ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileExpectedTextContent } ` - -Verifiable - - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` - -Verifiable - - It 'Should not throw an exception' { - { $script:result = Test-TargetResource ` - -Path $script:testTextFile ` - -Name $script:testName.ToUpper() ` - -Ensure 'Present' ` - -Text $script:testText ` - -IgnoreNameCase:$true ` - -Encoding $script:fileEncodingParameters.Encoding ` - -Verbose - } | Should -Not -Throw - } - - It 'Should return true' { - $script:result | Should -Be $true - } - - It 'Should call the expected mocks' { - Assert-VerifiableMock - Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - } - } - - Context 'File exists and contains key with different case that should exist and values match and IgnoreNameCase is True but file encoding is not compliant' { - # verifiable (should be called) mocks - Mock ` - -CommandName Assert-ParametersValid ` - -ModuleName 'DSR_KeyValuePairFile' ` - -Verifiable - - Mock ` - -CommandName Test-Path ` - -ModuleName 'DSR_KeyValuePairFile' ` - -MockWith { $true } ` - -Verifiable - - Mock ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileExpectedTextContent } ` - -Verifiable - - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testNonCompliantEncoding.Encoding } ` - -Verifiable - - It 'Should not throw an exception' { - { $script:result = Test-TargetResource ` - -Path $script:testTextFile ` - -Name $script:testName.ToUpper() ` - -Ensure 'Present' ` - -Text $script:testText ` - -IgnoreNameCase:$true ` - -Encoding $script:fileEncodingParameters.Encoding ` - -Verbose - } | Should -Not -Throw - } - - It 'Should return false' { - $script:result | Should -Be $false - } - - It 'Should call the expected mocks' { - Assert-VerifiableMock - Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - } - } - Context 'File exists and contains matching key that should exist and values match but are different case and IgnoreValueCase is False' { # verifiable (should be called) mocks Mock ` @@ -1502,19 +1057,12 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileExpectedTextContent } ` -Verifiable - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` - -Verifiable - It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` -Name $script:testName ` -Ensure 'Present' ` -Text $script:testText.ToUpper() ` - -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -1531,11 +1079,6 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 } } @@ -1596,7 +1139,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists and contains matching key that should exist and values match but are different case and IgnoreValueCase is True but file encoding is not compliant' { + Context 'File exists and contains matching key that should not exist' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -1615,21 +1158,13 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileExpectedTextContent } ` -Verifiable - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testNonCompliantEncoding.Encoding } ` - -Verifiable - It 'Should not throw an exception' { { $script:result = Test-TargetResource ` - -Path $script:testTextFile ` - -Name $script:testName ` - -Ensure 'Present' ` - -Text $script:testText.ToUpper() ` - -Encoding $script:fileEncodingParameters.Encoding ` - -IgnoreValueCase:$true ` - -Verbose + -Path $script:testTextFile ` + -Name $script:testName ` + -Ensure 'Absent' ` + -Text $script:testText ` + -Verbose } | Should -Not -Throw } @@ -1645,51 +1180,34 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 } } - Context 'File exists and contains matching key that should not exist' { + Context 'File exists and encoding is not in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable - Mock ` - -CommandName Test-Path ` - -ModuleName 'DSR_KeyValuePairFile' ` - -MockWith { $true } ` - -Verifiable - - Mock ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileExpectedTextContent } ` - -Verifiable - Mock ` -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` -MockWith { $script:testNonCompliantEncoding.Encoding } ` -Verifiable + $script:result = $null + It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` -Name $script:testName ` - -Ensure 'Absent' ` - -Text $script:testText ` - -Encoding $script:testNonCompliantEncoding.Encoding ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } - It 'Should return false' { + It 'Should return true' { $script:result | Should -Be $false } @@ -1697,11 +1215,6 @@ $($script:testAddedName)=$($script:testText) Assert-VerifiableMock Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 - Assert-MockCalled ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - Assert-MockCalled ` -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` From e762e85389aa1154d554a6978b0e8d1f9ee5cba5 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Tue, 2 Oct 2018 12:53:02 -0400 Subject: [PATCH 06/46] test and code fixes --- .../DSR_ReplaceText/DSR_ReplaceText.psm1 | 38 ++++---- Tests/Unit/DSR_ReplaceText.Tests.ps1 | 97 +++++++++++++------ 2 files changed, 88 insertions(+), 47 deletions(-) diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 index ce25c8e..5ffc5c5 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 @@ -235,31 +235,19 @@ function Test-TargetResource [Parameter()] [String] - $Encoding + $Encoding = 'ASCII' ) Assert-ParametersValid @PSBoundParameters # Check for the file being managed. If it does not exist return $false. - # If it exists but is using the wrong encoding return $false. if (-not (Test-Path -Path $Path)) { return $false } - else - { - $fileEncoding = Get-FileEncoding $Path - - if ($Encoding -ne $fileEncoding) - { - Write-Verbose -Message ($localizedData.FileEncodingNotSetProperly -f ` - $fileEncoding, $Encoding) - - return $false - } - } $fileContent = Get-Content -Path $Path -Raw + $fileEncoding = Get-FileEncoding $Path Write-Verbose -Message ($localizedData.SearchForTextMessage -f ` $Path, $Search) @@ -267,17 +255,27 @@ function Test-TargetResource # Search the file content for any matches $results = [regex]::Matches($fileContent, $Search) + # Flag to signal whether settings are correct + [Boolean] $desiredConfigurationMatch = $true + if ($results.Count -eq 0) { - # No matches found - already in state - Write-Verbose -Message ($localizedData.StringNotFoundMessage -f ` + if ($Encoding -eq $fileEncoding) + { + # No matches found - already in state + Write-Verbose -Message ($localizedData.StringNotFoundMessage -f ` $Path, $Search) - return $true - } + return $true + } + else + { + Write-Verbose -Message ($localizedData.FileEncodingNotInDesiredState -f ` + $fileEncoding, $Encoding) - # Flag to signal whether settings are correct - [Boolean] $desiredConfigurationMatch = $true + $desiredConfigurationMatch = $false + } + } if ($Type -eq 'Secret') { diff --git a/Tests/Unit/DSR_ReplaceText.Tests.ps1 b/Tests/Unit/DSR_ReplaceText.Tests.ps1 index 2a80fbe..c66d64f 100644 --- a/Tests/Unit/DSR_ReplaceText.Tests.ps1 +++ b/Tests/Unit/DSR_ReplaceText.Tests.ps1 @@ -302,7 +302,7 @@ Setting3.Test=Value4 #region Function Set-TargetResource Describe 'DSR_ReplaceText\Set-TargetResource' { - Context 'File exists and search text can be found and encoding is compliant' { + Context 'File exists and search text can be found' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -321,12 +321,7 @@ Setting3.Test=Value4 ($path -eq $script:testTextFile) -and ` ($value -eq $script:testFileExpectedTextContent) } ` - -Verifiable - - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` + -MockWith { $script:fileEncodingParameters.Encoding } ` -Verifiable It 'Should not throw an exception' { @@ -348,11 +343,6 @@ Setting3.Test=Value4 -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - Assert-MockCalled ` -CommandName Set-Content ` -ParameterFilter { @@ -363,7 +353,7 @@ Setting3.Test=Value4 } } - Context 'File exists and search secret can be found and encoding is compliant' { + Context 'File exists and search secret can be found' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -376,19 +366,13 @@ Setting3.Test=Value4 -MockWith { $script:testFileContent } ` -Verifiable - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` - -Verifiable - Mock ` -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedSecretContent) -and ` - ($encoding -eq $script:fileEncodingParameters.Encoding) + ($value -eq $script:testFileExpectedSecretContent) } ` + -MockWith { $script:fileEncodingParameters.Encoding } ` -Verifiable It 'Should not throw an exception' { @@ -411,11 +395,6 @@ Setting3.Test=Value4 -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - Assert-MockCalled ` -CommandName Set-Content ` -ParameterFilter { @@ -480,7 +459,7 @@ Setting3.Test=Value4 #region Function Test-TargetResource Describe 'DSR_ReplaceString\Test-TargetResource' { - Context 'File exists and search text can not be found' { + Context 'File exists and search text can not be found and encoding is in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -502,6 +481,7 @@ Setting3.Test=Value4 Mock ` -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable $script:result = $null @@ -511,6 +491,7 @@ Setting3.Test=Value4 -Path $script:testTextFile ` -Search $script:testSearchNoFind ` -Text $script:testTextReplace ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -527,6 +508,68 @@ Setting3.Test=Value4 -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + + Context 'File exists and search text can not be found and encoding is not in desired state' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_ReplaceText' ` + -Verifiable + + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_ReplaceText' ` + -MockWith { $true } ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -Verifiable + + $script:result = $null + + It 'Should not throw an exception' { + { $script:result = Test-TargetResource ` + -Path $script:testTextFile ` + -Search $script:testSearchNoFind ` + -Text $script:testTextReplace ` + -Encoding $script:fileEncodingParameters.Encoding ` + -Verbose + } | Should -Not -Throw + } + + It 'Should return false' { + $script:result | Should -Be $false + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } From 3cfbe0c8faca30bbe3d7fc98d5cb7a43ebe19a57 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Tue, 2 Oct 2018 15:08:25 -0400 Subject: [PATCH 07/46] minor formatting, description... updates --- .../DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 | 5 ++--- DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 | 4 ++-- Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 | 3 ++- Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 | 9 ++++----- Tests/Unit/DSR_ReplaceText.Tests.ps1 | 8 ++++---- 5 files changed, 14 insertions(+), 15 deletions(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 index 373f63e..1a35f6d 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 @@ -399,8 +399,7 @@ function Test-TargetResource } else { - # The key value pairs should exist and do - + # The key value pairs should exist and do and encoding is in desired state if ($Encoding -eq $fileEncoding) { Write-Verbose -Message ($localizedData.KeyNotFoundAndShouldNotExistMessage -f ` @@ -431,7 +430,7 @@ function Test-TargetResource if ($desiredConfigurationMatch) { - Write-Verbose -Message ($localizedData.KeyUpdateMessage -f ` + Write-Verbose -Message ($localizedData.KeyFoundButNoReplacementMessage -f ` $Path, $Name) } } diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 index 5ffc5c5..f54a700 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 @@ -240,7 +240,7 @@ function Test-TargetResource Assert-ParametersValid @PSBoundParameters - # Check for the file being managed. If it does not exist return $false. + # Check if file being managed exists. If not return $false. if (-not (Test-Path -Path $Path)) { return $false @@ -262,7 +262,7 @@ function Test-TargetResource { if ($Encoding -eq $fileEncoding) { - # No matches found - already in state + # No matches found and encoding is in desired state Write-Verbose -Message ($localizedData.StringNotFoundMessage -f ` $Path, $Search) diff --git a/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 b/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 index b878229..71c35be 100644 --- a/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 +++ b/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 @@ -159,7 +159,8 @@ function Get-IniSettingFileValue This command gets ps1 files in current directory where encoding is not ASCII .EXAMPLE - Get-ChildItem *.ps1 | select FullName, @{n='Encoding';e={Get-FileEncoding $_.FullName}} | where {$_.Encoding -ne 'ASCII'} | foreach {(get-content $_.FullName) | set-content $_.FullName -Encoding ASCII} + Get-ChildItem *.ps1 | select FullName, @{n='Encoding';e={Get-FileEncoding $_.FullName}} | where {$_.Encoding -ne 'ASCII'} | ` + foreach {(get-content $_.FullName) | set-content $_.FullName -Encoding ASCII} Same as previous example but fixes encoding using set-content #> function Get-FileEncoding diff --git a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 index 5db3e56..7688204 100644 --- a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 +++ b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 @@ -238,7 +238,6 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable - $script:result = $null It 'Should not throw an exception' { @@ -418,7 +417,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists and contains matching key that should exist and contains a secret' { + Context 'File exists and contains matching key that should exist and contain a secret' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -747,7 +746,7 @@ $($script:testAddedName)=$($script:testText) Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileExpectedTextContentAdded } ` + -MockWith { $script:testFileContent } ` -Verifiable It 'Should not throw an exception' { @@ -823,7 +822,7 @@ $($script:testAddedName)=$($script:testText) -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - Assert-MockCalled ` + Assert-MockCalled ` -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 @@ -1207,7 +1206,7 @@ $($script:testAddedName)=$($script:testText) } | Should -Not -Throw } - It 'Should return true' { + It 'Should return false' { $script:result | Should -Be $false } diff --git a/Tests/Unit/DSR_ReplaceText.Tests.ps1 b/Tests/Unit/DSR_ReplaceText.Tests.ps1 index c66d64f..dae09e9 100644 --- a/Tests/Unit/DSR_ReplaceText.Tests.ps1 +++ b/Tests/Unit/DSR_ReplaceText.Tests.ps1 @@ -86,7 +86,7 @@ Setting3.Test=Value4 #region Function Get-TargetResource Describe 'DSR_ReplaceText\Get-TargetResource' { - Context 'File exists and search text can be found and encoding is compliant' { + Context 'File exists and search text can be found and encoding is in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -139,7 +139,7 @@ Setting3.Test=Value4 } } - Context 'File exists and search text can be found but encoding is not compliant' { + Context 'File exists and search text can be found but encoding is not in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -192,7 +192,7 @@ Setting3.Test=Value4 } } - Context 'File exists and search text can not be found and encoding is compliant' { + Context 'File exists and search text can not be found and encoding is in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -245,7 +245,7 @@ Setting3.Test=Value4 } } - Context 'File exists and search text can not be found but encoding is not compliant' { + Context 'File exists and search text can not be found but encoding is not in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` From 3977a76435a073b4223a6a07be36cb518ff2a7a2 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Tue, 2 Oct 2018 15:28:46 -0400 Subject: [PATCH 08/46] update with change info --- CHANGELOG.md | 1 + README.md | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6d17d8..b56671e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - Refactor Test-TargetResource to return $false in all DSC resource - Fixes [Issue #12](https://github.com/PlagueHO/FileContentDsc/issues/13). - Correct configuration names in Examples - fixes [Issue #15](https://github.com/PowerShell/FileContentDsc/issues/15). +- Added an Encoding parameter to the KeyValuePairFile and ReplaceText resources - fixes [Issue #5](https://github.com/PlagueHO/FileContentDsc/issues/5) ## 1.0.0.0 diff --git a/README.md b/README.md index 0740b7a..4abab6b 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,9 @@ The **FileContent** module contains the following resources: - **IniSettingsFile**: Add, set or clear entries in Windows INI settings files. - **KeyValuePairFile**: Add, remove or set key/value pairs in a text file containing - key/value pairs. -- **ReplaceText**: Replaces strings matching a regular expression in a file. + key/value pairs, and set file encoding. +- **ReplaceText**: Replaces strings matching a regular expression in a file, + and sets file encoding. **This project is not maintained or supported by Microsoft.** From 0e5ca2fbff3f0df745673986e44a92e059745b89 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Thu, 23 Aug 2018 16:42:36 -0400 Subject: [PATCH 09/46] Adding file encoding functionality --- .../DSR_KeyValuePairFile.psm1 | 77 ++++++++++++++---- .../DSR_KeyValuePairFile.schema.mof | Bin 1186 -> 3032 bytes .../en-US/DSR_KeyValuePairFile.strings.psd1 | 1 + .../DSR_ReplaceText/DSR_ReplaceText.psm1 | 71 ++++++++++++---- .../en-US/DSR_ReplaceText.strings.psd1 | 1 + .../FileContentDsc.Common.psm1 | 50 ++++++++++++ 6 files changed, 169 insertions(+), 31 deletions(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 index edf9d4d..806c388 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 @@ -52,6 +52,7 @@ function Get-TargetResource Assert-ParametersValid @PSBoundParameters $fileContent = Get-Content -Path $Path -Raw + $fileEncoding = Get-FileEncoding $Path Write-Verbose -Message ($localizedData.SearchForKeyMessage -f ` $Path, $Name) @@ -91,6 +92,7 @@ function Get-TargetResource return @{ Path = $Path Name = $Name + Encoding = $fileEncoding Ensure = $ensure Type = 'Text' Text = $text @@ -129,6 +131,9 @@ function Get-TargetResource .PARAMETER IgnoreValueCase Ignore the case of any text or secret when determining if it they need to be updated. Defaults to $False. + + .PARAMETER Encoding + Specifies the file encoding. The default value is ASCII. #> function Set-TargetResource { @@ -172,12 +177,17 @@ function Set-TargetResource [Parameter()] [System.Boolean] - $IgnoreValueCase = $false + $IgnoreValueCase = $false, + + [Parameter()] + [String] + $Encoding = 'Default' ) Assert-ParametersValid @PSBoundParameters $fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue + $fileEncoding = Get-FileEncoding $Path Write-Verbose -Message ($localizedData.SearchForKeyMessage -f ` $Path, $Name) @@ -218,7 +228,7 @@ function Set-TargetResource $fileContent += $keyValuePair Write-Verbose -Message ($localizedData.KeyAddMessage -f ` - $Path, $Name) + $Path, $Name) } else { @@ -226,15 +236,27 @@ function Set-TargetResource $fileContent = [regex]::Replace($fileContent, "^[\s]*$Name=(.*)($eolChars*)", $keyValuePair, $regExOptions) Write-Verbose -Message ($localizedData.KeyUpdateMessage -f ` - $Path, $Name) + $Path, $Name) } # if } else { if ($results.Count -eq 0) { - # The Key does not exists and should not so don't do anything - return + # The Key does not exists and should not so don't do anything, unless encoding doesn't match + if ($Encoding -ne $fileEncoding) + { + Set-Content ` + -Path $Path ` + -Value $fileContent ` + -Encoding $Encoding ` + -NoNewline ` + -Force + } + else + { + return + } } else { @@ -242,7 +264,7 @@ function Set-TargetResource $fileContent = [regex]::Replace($fileContent, "^[\s]*$Name=(.*)$eolChars", '', $regExOptions) Write-Verbose -Message ($localizedData.KeyRemoveMessage -f ` - $Path, $Name) + $Path, $Name) } } # if } @@ -254,6 +276,7 @@ function Set-TargetResource Set-Content ` -Path $Path ` -Value $fileContent ` + -Encoding $Encoding ` -NoNewline ` -Force } @@ -288,6 +311,9 @@ function Set-TargetResource .PARAMETER IgnoreValueCase Ignore the case of any text or secret when determining if it they need to be updated. Defaults to $False. + + .PARAMETER Encoding + Specifies the file encoding. The default value is ASCII. #> function Test-TargetResource { @@ -330,7 +356,11 @@ function Test-TargetResource [Parameter()] [System.Boolean] - $IgnoreValueCase = $false + $IgnoreValueCase = $false, + + [Parameter()] + [String] + $Encoding ) Assert-ParametersValid @PSBoundParameters @@ -343,11 +373,23 @@ function Test-TargetResource { return $false } + else + { + $fileEncoding = Get-FileEncoding $Path + + if ($Encoding -ne $fileEncoding) + { + Write-Verbose -Message ($localizedData.FileEncodingNotSetProperly -f ` + $fileEncoding, $Encoding) + + return $false + } + } $fileContent = Get-Content -Path $Path -Raw Write-Verbose -Message ($localizedData.SearchForKeyMessage -f ` - $Path, $Name) + $Path, $Name) # Setup the Regex Options that will be used $regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline @@ -366,7 +408,7 @@ function Test-TargetResource { # The key value pairs should exist but do not Write-Verbose -Message ($localizedData.KeyNotFoundButShouldExistMessage -f ` - $Path, $Name) + $Path, $Name) $desiredConfigurationMatch = $false } @@ -374,7 +416,7 @@ function Test-TargetResource { # The key value pairs should exist and do Write-Verbose -Message ($localizedData.KeyNotFoundAndShouldNotExistMessage -f ` - $Path, $Name) + $Path, $Name) } # if } else @@ -401,19 +443,19 @@ function Test-TargetResource if ($desiredConfigurationMatch) { Write-Verbose -Message ($localizedData.KeyFoundButNoReplacementMessage -f ` - $Path, $Name) + $Path, $Name) } else { Write-Verbose -Message ($localizedData.KeyFoundReplacementRequiredMessage -f ` - $Path, $Name) + $Path, $Name) } # if } else { # The key value pairs should not exist Write-Verbose -Message ($localizedData.KeyFoundButShouldNotExistMessage -f ` - $Path, $Name) + $Path, $Name) $desiredConfigurationMatch = $false } # if @@ -453,6 +495,9 @@ function Test-TargetResource .PARAMETER IgnoreValueCase Ignore the case of any text or secret when determining if it they need to be updated. Defaults to $False. + + .PARAMETER Encoding + Specifies the file encoding. The default value is ASCII. #> function Assert-ParametersValid { @@ -494,7 +539,11 @@ function Assert-ParametersValid [Parameter()] [System.Boolean] - $IgnoreValueCase = $false + $IgnoreValueCase = $false, + + [Parameter()] + [String] + $Encoding ) # Does the file's parent path exist? diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof index 05fee809eb81e9d4c3870ed2de85cce980c1f325..5b84d7f4faca84a2026cf34e168321dd5a182411 100644 GIT binary patch literal 3032 zcmds3U2hUW6g|%-{)Y)MX-yj9$AgVN6{{G-MZM=6;@g?p%KUI7J-;Twub_MC zb!YfM{jXe0<2}D;v~(HW!k}Ko*9g2Gv{x^_t3uG{uiFk zxc)ZJ%*iS@$tL*Dw?V(ivsz?om|1T9Ww_$G@R0p?kBl#)zOXiou5UfZ@vZXeD-?VE zWTR5#>yc>_lZ3dYHa8)ye4SVAh1GhDrnKOZb=s3VmV312ZA|Dv6&;Xc#-f|jvk*?n zyWq2o_fEvT7Lgj^hIKe%rixpP&AC>-73|?M+Pmd$VD01qdS|p&uJHl=Oe`CdQl0+< z;xYenGE%h>8sh4;&BrkBxJJr-WP44$A?^qTLz%$KD%a|bK6Qn8URaRTEp&Jyc5m?} zaCbmJDxmD8JicvesnZf_RUmKlHZ5-mT&qkb9#l%om#>KUW`!YOSE-@a{d-k=*LtcHI zZ~u&4Du01vUE8aRxA~@9es#A#V`j=iRSnaiM$bd;CKd-nR6;axo`{q z4tDG2tDkO~fa33vIXD#7-Nbw3cbzqElFM$^)u?p^%_Zrp4kvul!lM3_t(NPMQLpi8 zE>oTpL33s?_Um{e`n}~oPuPhKyqVi~>)mLb@n3;@XUg~TN%FsEym<4gmN6e(*WMQY GH+})c*!yb$ literal 1186 zcmcJOO=}x55Qgvm6~hup0>-r0=F~VbnBvA{O?n8y$g6QyRF)Qvw$2jr-#Z%Fj%{LS z4`DB2H6PEsujX-R7109*GTvPjUk6{(S6udC6bw3TyPs->7sU_k9+Yiys!SLei^b(r zuS%(KQ{K;yZ{ZuD_4gfa%>o?*)ISEtK4CI-LUmfO!v!${_@U?Lu&lnqv9X^=Kp9Zky=R@%PKq~vc0RCv3}5kLZD08D zYd`SxY@{sZ-MRMOqH>VSq(vBVmH+9cT$lCoVJn}Qp)q1u8JAX*x5ls~HE*#)c8iT( WAheBE5%uAFJIN*sli}X3dVc|3vzIym diff --git a/DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1 b/DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1 index 7134505..cf80c24 100644 --- a/DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1 +++ b/DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1 @@ -15,4 +15,5 @@ ConvertFrom-StringData @' KeyFoundReplacementRequiredMessage = Key '{1}' found in file '{0}' and should exist but value(s) are not correct. Change required. KeyFoundButShouldNotExistMessage = Key '{1}' found in file '{0}' but should not exist. Change required. FileParentNotFoundError = File parent path '{0}' not found. + FileEncodingNotSetProperly = File encoding is set to {0} but should be set to {1}, change required. '@ diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 index 96d6e90..dae2267 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 @@ -52,9 +52,10 @@ function Get-TargetResource Assert-ParametersValid @PSBoundParameters $fileContent = Get-Content -Path $Path -Raw + $fileEncoding = Get-FileEncoding $Path Write-Verbose -Message ($localizedData.SearchForTextMessage -f ` - $Path, $Search) + $Path, $Search) $text = '' @@ -65,21 +66,22 @@ function Get-TargetResource { # No matches found - already in state Write-Verbose -Message ($localizedData.StringNotFoundMessage -f ` - $Path, $Search) + $Path, $Search) } else { $text = ($results.Value -join ',') Write-Verbose -Message ($localizedData.StringMatchFoundMessage -f ` - $Path, $Search, $text) + $Path, $Search, $text) } # if return @{ - Path = $Path - Search = $Search - Type = 'Text' - Text = $text + Path = $Path + Search = $Search + Type = 'Text' + Text = $text + Encoding = $fileEncoding } } @@ -106,6 +108,9 @@ function Get-TargetResource .PARAMETER AllowAppend Specifies to append text to the file being modified. Adds the ability to add a configuration entry. + + .PARAMETER Encoding + Specifies the file encoding. The default value is ASCII. #> function Set-TargetResource { @@ -140,7 +145,11 @@ function Set-TargetResource [Parameter()] [System.Boolean] - $AllowAppend = $false + $AllowAppend = $false, + + [Parameter()] + [String] + $Encoding = 'Default' ) Assert-ParametersValid @PSBoundParameters @@ -150,14 +159,14 @@ function Set-TargetResource if ($Type -eq 'Secret') { Write-Verbose -Message ($localizedData.StringReplaceSecretMessage -f ` - $Path) + $Path) $Text = $Secret.GetNetworkCredential().Password } else { Write-Verbose -Message ($localizedData.StringReplaceTextMessage -f ` - $Path, $Text) + $Path, $Text) } # if if ($null -eq $fileContent) @@ -179,6 +188,7 @@ function Set-TargetResource Set-Content ` -Path $Path ` -Value $fileContent ` + -Encoding $Encoding ` -NoNewline ` -Force } @@ -206,6 +216,9 @@ function Set-TargetResource .PARAMETER AllowAppend Specifies to append text to the file being modified. Adds the ability to add a configuration entry. + + .PARAMETER Encoding + Specifies the file encoding. The default value is ASCII. #> function Test-TargetResource { @@ -239,21 +252,38 @@ function Test-TargetResource [Parameter()] [System.Boolean] - $AllowAppend = $false + $AllowAppend = $false, + + [Parameter()] + [String] + $Encoding ) Assert-ParametersValid @PSBoundParameters - # Check if file being managed exists. If not return $False. + # Check for the file being managed. If it does not exist return $false. + # If it exists but is using the wrong encoding return $false. if (-not (Test-Path -Path $Path)) { return $false } + else + { + $fileEncoding = Get-FileEncoding $Path + + if ($Encoding -ne $fileEncoding) + { + Write-Verbose -Message ($localizedData.FileEncodingNotSetProperly -f ` + $fileEncoding, $Encoding) + + return $false + } + } $fileContent = Get-Content -Path $Path -Raw Write-Verbose -Message ($localizedData.SearchForTextMessage -f ` - $Path, $Search) + $Path, $Search) # Search the file content for any matches $results = [regex]::Matches($fileContent, $Search) @@ -271,7 +301,7 @@ function Test-TargetResource # No matches found - already in state Write-Verbose -Message ($localizedData.StringNotFoundMessage -f ` - $Path, $Search) + $Path, $Search) return $true } @@ -295,12 +325,12 @@ function Test-TargetResource if ($desiredConfigurationMatch) { Write-Verbose -Message ($localizedData.StringNoReplacementMessage -f ` - $Path, $Search) + $Path, $Search) } else { Write-Verbose -Message ($localizedData.StringReplacementRequiredMessage -f ` - $Path, $Search) + $Path, $Search) } # if return $desiredConfigurationMatch @@ -327,6 +357,9 @@ function Test-TargetResource .PARAMETER Secret The secret text to replace the text identified by the RegEx. Only used when Type is set to 'Secret'. + + .PARAMETER Encoding + Specifies the file encoding. The default value is ASCII. #> function Assert-ParametersValid { @@ -359,7 +392,11 @@ function Assert-ParametersValid [Parameter()] [System.Boolean] - $AllowAppend = $false + $AllowAppend = $false, + + [Parameter()] + [String] + $Encoding ) # Does the file's parent path exist? diff --git a/DSCResources/DSR_ReplaceText/en-US/DSR_ReplaceText.strings.psd1 b/DSCResources/DSR_ReplaceText/en-US/DSR_ReplaceText.strings.psd1 index 98ee192..e0fbc28 100644 --- a/DSCResources/DSR_ReplaceText/en-US/DSR_ReplaceText.strings.psd1 +++ b/DSCResources/DSR_ReplaceText/en-US/DSR_ReplaceText.strings.psd1 @@ -10,4 +10,5 @@ ConvertFrom-StringData @' StringReplaceTextMessage = String replaced by '{1}' in file '{0}'. StringReplaceSecretMessage = String replaced by secret text in file '{0}'. FileParentNotFoundError = File parent path '{0}' not found. + FileEncodingNotSetProperly = File encoding is set to {0} but should be set to {1}, change required. '@ diff --git a/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 b/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 index 4507376..b878229 100644 --- a/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 +++ b/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 @@ -146,4 +146,54 @@ function Get-IniSettingFileValue Return [IniFile]::GetIniSetting($fullPath, $Section, $Key, '') } +<# + .SYNOPSIS + Gets file encoding. + + .DESCRIPTION + The Get-FileEncoding function determines encoding by looking at Byte Order Mark (BOM). + Based on port of C# code from http://www.west-wind.com/Weblog/posts/197245.aspx + + .EXAMPLE + Get-ChildItem *.ps1 | select FullName, @{n='Encoding';e={Get-FileEncoding $_.FullName}} | where {$_.Encoding -ne 'ASCII'} + This command gets ps1 files in current directory where encoding is not ASCII + + .EXAMPLE + Get-ChildItem *.ps1 | select FullName, @{n='Encoding';e={Get-FileEncoding $_.FullName}} | where {$_.Encoding -ne 'ASCII'} | foreach {(get-content $_.FullName) | set-content $_.FullName -Encoding ASCII} + Same as previous example but fixes encoding using set-content +#> +function Get-FileEncoding +{ + [CmdletBinding()] + Param + ( + [Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)] + [string] + $Path + ) + + [byte[]]$byte = get-content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $Path + + if ( $byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf ) + { + Write-Output 'UTF8' + } + elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff) + { + Write-Output 'Unicode' + } + elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xfe -and $byte[3] -eq 0xff) + { + Write-Output 'UTF32' + } + elseif ($byte[0] -eq 0x2b -and $byte[1] -eq 0x2f -and $byte[2] -eq 0x76) + { + Write-Output 'UTF7' + } + else + { + Write-Output 'ASCII' + } +} + Export-ModuleMember -Function * From 46f0e221b1a8d572cfb53c396817eda0b7fdd9a5 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Mon, 10 Sep 2018 16:13:51 -0400 Subject: [PATCH 10/46] Updating keyvaluepairfile with encoding support --- .../DSR_KeyValuePairFile.psm1 | 63 +- .../en-US/DSR_KeyValuePairFile.strings.psd1 | 2 +- .../en-US/DSR_ReplaceText.strings.psd1 | 2 +- Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 | 880 ++++++++++++++++-- Tests/Unit/DSR_ReplaceText.Tests.ps1 | 215 ++++- 5 files changed, 1020 insertions(+), 142 deletions(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 index 806c388..edc5d16 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 @@ -52,10 +52,11 @@ function Get-TargetResource Assert-ParametersValid @PSBoundParameters $fileContent = Get-Content -Path $Path -Raw - $fileEncoding = Get-FileEncoding $Path Write-Verbose -Message ($localizedData.SearchForKeyMessage -f ` - $Path, $Name) + $Path, $Name) + + $fileEncoding = Get-FileEncoding -Path $Path # Setup the Regex Options that will be used $regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline @@ -70,7 +71,7 @@ function Get-TargetResource { # No matches found Write-Verbose -Message ($localizedData.KeyNotFoundMessage -f ` - $Path, $Name) + $Path, $Name) } else { @@ -86,7 +87,7 @@ function Get-TargetResource $text = ($textValues -join ',') Write-Verbose -Message ($localizedData.KeyFoundMessage -f ` - $Path, $Name, $text) + $Path, $Name, $text) } # if return @{ @@ -187,10 +188,10 @@ function Set-TargetResource Assert-ParametersValid @PSBoundParameters $fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue - $fileEncoding = Get-FileEncoding $Path + $fileEncoding = Get-FileEncoding -Path $Path Write-Verbose -Message ($localizedData.SearchForKeyMessage -f ` - $Path, $Name) + $Path, $Name) if ($Type -eq 'Secret') { @@ -243,19 +244,15 @@ function Set-TargetResource { if ($results.Count -eq 0) { - # The Key does not exists and should not so don't do anything, unless encoding doesn't match - if ($Encoding -ne $fileEncoding) + if ($Encoding -eq $fileEncoding) { - Set-Content ` - -Path $Path ` - -Value $fileContent ` - -Encoding $Encoding ` - -NoNewline ` - -Force + # The Key does not exists and should not, and encoding is in the desired state, so don't do anything + return } else { - return + Write-Verbose -Message ($localizedData.FileEncodingNotInDesiredState -f ` + $fileEncoding, $Encoding) } } else @@ -373,24 +370,14 @@ function Test-TargetResource { return $false } - else - { - $fileEncoding = Get-FileEncoding $Path - - if ($Encoding -ne $fileEncoding) - { - Write-Verbose -Message ($localizedData.FileEncodingNotSetProperly -f ` - $fileEncoding, $Encoding) - - return $false - } - } $fileContent = Get-Content -Path $Path -Raw Write-Verbose -Message ($localizedData.SearchForKeyMessage -f ` $Path, $Name) + $fileEncoding = Get-FileEncoding -Path $Path + # Setup the Regex Options that will be used $regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline if ($IgnoreNameCase) @@ -415,13 +402,17 @@ function Test-TargetResource else { # The key value pairs should exist and do - Write-Verbose -Message ($localizedData.KeyNotFoundAndShouldNotExistMessage -f ` + + if ($Encoding -eq $fileEncoding) + { + Write-Verbose -Message ($localizedData.KeyNotFoundAndShouldNotExistMessage -f ` $Path, $Name) + } } # if } else { - # One of more key value pairs were found + # One or more key value pairs were found if ($Ensure -eq 'Present') { # The key value pairs should exist - but check values @@ -445,11 +436,6 @@ function Test-TargetResource Write-Verbose -Message ($localizedData.KeyFoundButNoReplacementMessage -f ` $Path, $Name) } - else - { - Write-Verbose -Message ($localizedData.KeyFoundReplacementRequiredMessage -f ` - $Path, $Name) - } # if } else { @@ -461,6 +447,15 @@ function Test-TargetResource } # if } # if + if ($Encoding -ne $fileEncoding) + { + # File encoding is not in desired state + Write-Verbose -Message ($localizedData.FileEncodingNotInDesiredState -f ` + $fileEncoding, $Encoding) + + $desiredConfigurationMatch = $false + } + return $desiredConfigurationMatch } diff --git a/DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1 b/DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1 index cf80c24..8436c2e 100644 --- a/DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1 +++ b/DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1 @@ -15,5 +15,5 @@ ConvertFrom-StringData @' KeyFoundReplacementRequiredMessage = Key '{1}' found in file '{0}' and should exist but value(s) are not correct. Change required. KeyFoundButShouldNotExistMessage = Key '{1}' found in file '{0}' but should not exist. Change required. FileParentNotFoundError = File parent path '{0}' not found. - FileEncodingNotSetProperly = File encoding is set to {0} but should be set to {1}, change required. + FileEncodingNotInDesiredState = File encoding is set to {0} but should be set to {1}, Change required. '@ diff --git a/DSCResources/DSR_ReplaceText/en-US/DSR_ReplaceText.strings.psd1 b/DSCResources/DSR_ReplaceText/en-US/DSR_ReplaceText.strings.psd1 index e0fbc28..64a37cf 100644 --- a/DSCResources/DSR_ReplaceText/en-US/DSR_ReplaceText.strings.psd1 +++ b/DSCResources/DSR_ReplaceText/en-US/DSR_ReplaceText.strings.psd1 @@ -10,5 +10,5 @@ ConvertFrom-StringData @' StringReplaceTextMessage = String replaced by '{1}' in file '{0}'. StringReplaceSecretMessage = String replaced by secret text in file '{0}'. FileParentNotFoundError = File parent path '{0}' not found. - FileEncodingNotSetProperly = File encoding is set to {0} but should be set to {1}, change required. + FileEncodingNotInDesiredState = File encoding is set to {0} but should be set to {1}, Change required. '@ diff --git a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 index f700346..dd7f09e 100644 --- a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 +++ b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 @@ -40,6 +40,21 @@ try $script:testSecureSecret = ConvertTo-SecureString -String $script:testSecret -AsPlainText -Force $script:testSecretCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ('Dummy', $script:testSecureSecret) + $script:fileEncodingParameters = @{ + Path = $script:testTextFile + Encoding = 'ASCII' + } + + $script:testCompliantEncoding = @{ + Path = $script:fileEncodingParameters.Path + Encoding = $script:fileEncodingParameters.Encoding + } + + $script:testNonCompliantEncoding = @{ + Path = $script:fileEncodingParameters.Path + Encoding = 'UTF8' + } + $script:testFileContent = @" Setting1=Value1 $($script:testName)=Value 2 @@ -96,7 +111,61 @@ $($script:testAddedName)=$($script:testText) #region Function Get-TargetResource Describe 'DSR_KeyValuePairFile\Get-TargetResource' { - Context 'File exists and contains matching key' { + Context 'File exists and contains matching key and encoding is compliant' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_KeyValuePairFile' ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileExpectedTextContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + + $script:result = $null + + It 'Should not throw an exception' { + { $script:result = Get-TargetResource ` + -Path $script:testTextFile ` + -Name $script:testName ` + -Verbose + } | Should -Not -Throw + } + + It 'Should return expected values' { + $script:result.Path | Should -Be $script:testTextFile + $script:result.Name | Should -Be $script:testName + $script:result.Ensure | Should -Be 'Present' + $script:result.Type | Should -Be 'Text' + $script:result.Text | Should -Be "$($script:testText),$($script:testText),$($script:testText)" + $script:result.Encoding | Should -Be $script:fileEncodingParameters.Encoding + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + + Context 'File exists and contains matching key and encoding is not compliant' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -109,6 +178,12 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileExpectedTextContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -Verifiable + $script:result = $null It 'Should not throw an exception' { @@ -125,6 +200,61 @@ $($script:testAddedName)=$($script:testText) $script:result.Ensure | Should -Be 'Present' $script:result.Type | Should -Be 'Text' $script:result.Text | Should -Be "$($script:testText),$($script:testText),$($script:testText)" + $script:result.Encoding | Should -Be $script:testNonCompliantEncoding.Encoding + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + + Context 'File exists and does not contain matching key and encoding is compliant' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_KeyValuePairFile' ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileExpectedTextContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + + $script:result = $null + + It 'Should not throw an exception' { + { $script:result = Get-TargetResource ` + -Path $script:testTextFile ` + -Name $script:testName.ToUpper() ` + -Verbose + } | Should -Not -Throw + } + + It 'Should return expected values' { + $script:result.Path | Should -Be $script:testTextFile + $script:result.Name | Should -Be $script:testName + $script:result.Ensure | Should -Be 'Absent' + $script:result.Type | Should -Be 'Text' + $script:result.Text | Should -BeNullOrEmpty + $script:result.Encoding | Should -Be $script:fileEncodingParameters.Encoding } It 'Should call the expected mocks' { @@ -135,10 +265,15 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } - Context 'File exists and does not contain matching key' { + Context 'File exists and does not contain matching key and encoding is compliant' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -151,6 +286,12 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileExpectedTextContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + $script:result = $null It 'Should not throw an exception' { @@ -167,6 +308,7 @@ $($script:testAddedName)=$($script:testText) $script:result.Ensure | Should -Be 'Absent' $script:result.Type | Should -Be 'Text' $script:result.Text | Should -BeNullOrEmpty + $script:result.Encoding | Should -Be $script:fileEncodingParameters.Encoding } It 'Should call the expected mocks' { @@ -177,6 +319,11 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } } @@ -247,20 +394,214 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + + It 'Should not throw an exception' { + { Set-TargetResource ` + -Path $script:testTextFile ` + -Name $script:testName ` + -Ensure 'Present' ` + -Text $script:testText ` + -Encoding $script:fileEncodingParameters.Encoding ` + -Verbose + } | Should -Not -Throw + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + + Context 'File exists and contains matching key that should exist but encoding is not compliant' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_KeyValuePairFile' ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -Verifiable + + It 'Should not throw an exception' { + { Set-TargetResource ` + -Path $script:testTextFile ` + -Name $script:testName ` + -Ensure 'Present' ` + -Text $script:testText ` + -Encoding $script:fileEncodingParameters.Encoding ` + -Verbose + } | Should -Not -Throw + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + + Context 'File exists and contains matching key that should exist and contain a secret' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_KeyValuePairFile' ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + + It 'Should not throw an exception' { + { Set-TargetResource ` + -Path $script:testTextFile ` + -Name $script:testName ` + -Ensure 'Present' ` + -Type 'Secret' ` + -Secret $script:testSecretCredential ` + -Encoding $script:fileEncodingParameters.Encoding ` + -Verbose + } | Should -Not -Throw + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + + Context 'File exists and contains matching key that should exist and contain a secret but encoding is not compliant' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_KeyValuePairFile' ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -Verifiable + + It 'Should not throw an exception' { + { Set-TargetResource ` + -Path $script:testTextFile ` + -Name $script:testName ` + -Ensure 'Present' ` + -Type 'Secret' ` + -Secret $script:testSecretCredential ` + -Encoding $script:fileEncodingParameters.Encoding ` + -Verbose + } | Should -Not -Throw + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + + Context 'File exists does not contain matching key but key should exist' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_KeyValuePairFile' ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileContent } ` + -Verifiable + Mock ` -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContent) + ($value -eq $script:testFileExpectedTextContentAdded) } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable It 'Should not throw an exception' { { Set-TargetResource ` -Path $script:testTextFile ` - -Name $script:testName ` + -Name $script:testAddedName ` -Ensure 'Present' ` -Text $script:testText ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -274,17 +615,23 @@ $($script:testAddedName)=$($script:testText) -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + Assert-MockCalled ` -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContent) + ($value -eq $script:testFileExpectedTextContentAdded) -and ` + ($encoding -eq $script:fileEncodingParameters.Encoding) } ` -Exactly 1 } } - Context 'File exists and contains matching key that should exist and contain a secret' { + Context 'File exists and contains key with a different case that should exist and IgnoreNameCase is True' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -301,17 +648,24 @@ $($script:testAddedName)=$($script:testText) -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedSecretContent) + ($value -eq $script:testFileExpectedTextContentUpper) } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + It 'Should not throw an exception' { { Set-TargetResource ` -Path $script:testTextFile ` - -Name $script:testName ` + -Name $script:testName.ToUpper() ` -Ensure 'Present' ` - -Type 'Secret' ` - -Secret $script:testSecretCredential ` + -Text $script:testText ` + -Encoding $script:fileEncodingParameters.Encoding ` + -IgnoreNameCase:$true ` -Verbose } | Should -Not -Throw } @@ -325,17 +679,129 @@ $($script:testAddedName)=$($script:testText) -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + Assert-MockCalled ` -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedSecretContent) + ($value -eq $script:testFileExpectedTextContentUpper) -and ` + ($encoding -eq $script:fileEncodingParameters.Encoding) } ` -Exactly 1 } } - Context 'File exists does not contain matching key but key should exist' { + Context 'File exists and does not contain key with matching case and should not' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_KeyValuePairFile' ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + + # non-verifiable mocks + Mock ` + -CommandName Set-Content + + It 'Should not throw an exception' { + { Set-TargetResource ` + -Path $script:testTextFile ` + -Name $script:testName.ToUpper() ` + -Encoding $script:fileEncodingParameters.Encoding ` + -Ensure 'Absent' ` + -Verbose + } | Should -Not -Throw + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Set-Content ` + -Exactly 0 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + + Context 'File exists and does not contain key with matching case and should not but encoding is not compliant' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_KeyValuePairFile' ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -Verifiable + + # non-verifiable mocks + Mock ` + -CommandName Set-Content + + It 'Should not throw an exception' { + { Set-TargetResource ` + -Path $script:testTextFile ` + -Name $script:testName.ToUpper() ` + -Encoding $script:fileEncodingParameters.Encoding ` + -Ensure 'Absent' ` + -Verbose + } | Should -Not -Throw + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Set-Content ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + + Context 'File exists and contains key with a different case but should not and IgnoreNameCase is True' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -352,20 +818,95 @@ $($script:testAddedName)=$($script:testText) -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContentAdded) + ($value -eq $script:testFileExpectedAbsentContent) } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + + It 'Should not throw an exception' { + { Set-TargetResource ` + -Path $script:testTextFile ` + -Name $script:testName ` + -Encoding $script:fileEncodingParameters.Encoding ` + -Ensure 'Absent' ` + -IgnoreNameCase:$true ` + -Verbose + } | Should -Not -Throw + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Set-Content ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($value -eq $script:testFileExpectedAbsentContent) + } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + } + #endregion + + #region Function Test-TargetResource + Describe 'DSR_KeyValuePairFile\Test-TargetResource' { + Context 'File exists and does not contain matching key but it should' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_KeyValuePairFile' ` + -Verifiable + + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_KeyValuePairFile' ` + -MockWith { $true } ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileExpectedTextContentAdded } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + It 'Should not throw an exception' { - { Set-TargetResource ` + { $script:result = Test-TargetResource ` -Path $script:testTextFile ` - -Name $script:testAddedName ` + -Name $script:testName.ToUpper() ` -Ensure 'Present' ` -Text $script:testText ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } + It 'Should return false' { + $script:result | Should -Be $false + } + It 'Should call the expected mocks' { Assert-VerifiableMock Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 @@ -376,22 +917,25 @@ $($script:testAddedName)=$($script:testText) -Exactly 1 Assert-MockCalled ` - -CommandName Set-Content ` - -ParameterFilter { - ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContentAdded) - } ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 } } - Context 'File exists and contains key with a different case that should exist and IgnoreNameCase is True' { + Context 'File exists and does not contain matching key and should not' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_KeyValuePairFile' ` + -MockWith { $true } ` + -Verifiable + Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` @@ -399,24 +943,25 @@ $($script:testAddedName)=$($script:testText) -Verifiable Mock ` - -CommandName Set-Content ` - -ParameterFilter { - ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContentUpper) - } ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable It 'Should not throw an exception' { - { Set-TargetResource ` + { $script:result = Test-TargetResource ` -Path $script:testTextFile ` -Name $script:testName.ToUpper() ` - -Ensure 'Present' ` - -Text $script:testText ` - -IgnoreNameCase:$true ` + -Ensure 'Absent' ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } + It 'Should return true' { + $script:result | Should -Be $true + } + It 'Should call the expected mocks' { Assert-VerifiableMock Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 @@ -426,42 +971,52 @@ $($script:testAddedName)=$($script:testText) -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - Assert-MockCalled ` - -CommandName Set-Content ` - -ParameterFilter { - ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContentUpper) - } ` + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 } } - Context 'File exists and does not contain key with matching case and should not' { + Context 'File exists and does not contain matching key and should not but file encoding is not compliant' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_KeyValuePairFile' ` + -MockWith { $true } ` + -Verifiable + Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -MockWith { $script:testFileContent } ` -Verifiable - # non-verifiable mocks Mock ` - -CommandName Set-Content + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -Verifiable It 'Should not throw an exception' { - { Set-TargetResource ` + { $script:result = Test-TargetResource ` -Path $script:testTextFile ` -Name $script:testName.ToUpper() ` -Ensure 'Absent' ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } + It 'Should return false' { + $script:result | Should -Be $false + } + It 'Should call the expected mocks' { Assert-VerifiableMock Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 @@ -472,42 +1027,52 @@ $($script:testAddedName)=$($script:testText) -Exactly 1 Assert-MockCalled ` - -CommandName Set-Content ` - -Exactly 0 + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } - Context 'File exists and contains key with a different case but should not and IgnoreNameCase is True' { + Context 'File exists and contains matching key that should exist and values match' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_KeyValuePairFile' ` + -MockWith { $true } ` + -Verifiable + Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileContent } ` + -MockWith { $script:testFileExpectedTextContent } ` -Verifiable Mock ` - -CommandName Set-Content ` - -ParameterFilter { - ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedAbsentContent) - } ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable It 'Should not throw an exception' { - { Set-TargetResource ` + { $script:result = Test-TargetResource ` -Path $script:testTextFile ` -Name $script:testName ` - -Ensure 'Absent' ` - -IgnoreNameCase:$true ` + -Ensure 'Present' ` + -Text $script:testText ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } + It 'Should return true' { + $script:result | Should -Be $true + } + It 'Should call the expected mocks' { Assert-VerifiableMock Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 @@ -516,22 +1081,10 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - - Assert-MockCalled ` - -CommandName Set-Content ` - -ParameterFilter { - ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedAbsentContent) - } ` - -Exactly 1 } } - } - #endregion - #region Function Test-TargetResource - Describe 'DSR_KeyValuePairFile\Test-TargetResource' { - Context 'File exists and does not contain matching key but it should' { + Context 'File exists and contains matching key that should exist and values match but file encoding is not compliant' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -547,15 +1100,22 @@ $($script:testAddedName)=$($script:testText) Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileContent } ` + -MockWith { $script:testFileExpectedTextContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` -Verifiable It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` - -Name $script:testName.ToUpper() ` + -Name $script:testName ` -Ensure 'Present' ` -Text $script:testText ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -572,10 +1132,15 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } - Context 'File exists and does not contain matching key but should not' { + Context 'File exists and contains matching key that should exist and values do not match secret text' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -594,17 +1159,26 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` - -Name $script:testName.ToUpper() ` - -Ensure 'Absent' ` + -Name $script:testName ` + -Ensure 'Present' ` + -Type 'Secret' ` + -Secret $script:testSecretCredential ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } - It 'Should return true' { - $script:result | Should -Be $true + It 'Should return false' { + $script:result | Should -Be $false } It 'Should call the expected mocks' { @@ -615,10 +1189,15 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } - Context 'File exists and contains matching key that should exist and values match' { + Context 'File exists and contains matching key that should exist and values match secret text' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -634,7 +1213,13 @@ $($script:testAddedName)=$($script:testText) Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileExpectedTextContent } ` + -MockWith { $script:testFileExpectedSecretContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable It 'Should not throw an exception' { @@ -642,7 +1227,9 @@ $($script:testAddedName)=$($script:testText) -Path $script:testTextFile ` -Name $script:testName ` -Ensure 'Present' ` - -Text $script:testText ` + -Type 'Secret' ` + -Secret $script:testSecretCredential ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -659,10 +1246,15 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } - Context 'File exists and contains matching key that should exist and values do not match secret text' { + Context 'File exists and contains matching key that should exist and values match secret text but file encoding is not compliant' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -678,7 +1270,13 @@ $($script:testAddedName)=$($script:testText) Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileContent } ` + -MockWith { $script:testFileExpectedSecretContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` -Verifiable It 'Should not throw an exception' { @@ -688,6 +1286,7 @@ $($script:testAddedName)=$($script:testText) -Ensure 'Present' ` -Type 'Secret' ` -Secret $script:testSecretCredential ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -704,10 +1303,15 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } - Context 'File exists and contains matching key that should exist and values match secret text' { + Context 'File exists and contains key with different case that should exist and values match and IgnoreNameCase is True' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -723,16 +1327,23 @@ $($script:testAddedName)=$($script:testText) Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileExpectedSecretContent } ` + -MockWith { $script:testFileExpectedTextContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` - -Name $script:testName ` + -Name $script:testName.ToUpper() ` -Ensure 'Present' ` - -Type 'Secret' ` - -Secret $script:testSecretCredential ` + -Text $script:testText ` + -IgnoreNameCase:$true ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -749,10 +1360,15 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } - Context 'File exists and contains key with different case that should exist and values match and IgnoreNameCase is True' { + Context 'File exists and contains key with different case that should exist and values match and IgnoreNameCase is True but file encoding is not compliant' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -771,6 +1387,12 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileExpectedTextContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -Verifiable + It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` @@ -778,12 +1400,13 @@ $($script:testAddedName)=$($script:testText) -Ensure 'Present' ` -Text $script:testText ` -IgnoreNameCase:$true ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } - It 'Should return true' { - $script:result | Should -Be $true + It 'Should return false' { + $script:result | Should -Be $false } It 'Should call the expected mocks' { @@ -794,6 +1417,11 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } @@ -816,12 +1444,19 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileExpectedTextContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` -Name $script:testName ` -Ensure 'Present' ` -Text $script:testText.ToUpper() ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -838,6 +1473,11 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } @@ -860,12 +1500,19 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileExpectedTextContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` -Name $script:testName ` -Ensure 'Present' ` -Text $script:testText.ToUpper() ` + -Encoding $script:fileEncodingParameters.Encoding ` -IgnoreValueCase:$true ` -Verbose } | Should -Not -Throw @@ -883,10 +1530,15 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } - Context 'File exists and contains matching key that should not exist' { + Context 'File exists and contains matching key that should exist and values match but are different case and IgnoreValueCase is True but file encoding is not compliant' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -905,13 +1557,21 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileExpectedTextContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -Verifiable + It 'Should not throw an exception' { { $script:result = Test-TargetResource ` - -Path $script:testTextFile ` - -Name $script:testName ` - -Ensure 'Absent' ` - -Text $script:testText ` - -Verbose + -Path $script:testTextFile ` + -Name $script:testName ` + -Ensure 'Present' ` + -Text $script:testText.ToUpper() ` + -Encoding $script:fileEncodingParameters.Encoding ` + -IgnoreValueCase:$true ` + -Verbose } | Should -Not -Throw } @@ -927,10 +1587,15 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } - Context 'File exists and does not contain matching key but it should' { + Context 'File exists and contains matching key that should not exist' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -940,15 +1605,28 @@ $($script:testAddedName)=$($script:testText) Mock ` -CommandName Test-Path ` -ModuleName 'DSR_KeyValuePairFile' ` - -MockWith { $false } ` + -MockWith { $true } ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileExpectedTextContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` -Verifiable It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` - -Name $script:testName.ToUpper() ` - -Ensure 'Present' ` + -Name $script:testName ` + -Ensure 'Absent' ` -Text $script:testText ` + -Encoding $script:testNonCompliantEncoding.Encoding ` -Verbose } | Should -Not -Throw } @@ -960,10 +1638,20 @@ $($script:testAddedName)=$($script:testText) It 'Should call the expected mocks' { Assert-VerifiableMock Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } - } + #endregion #region Function Assert-ParametersValid diff --git a/Tests/Unit/DSR_ReplaceText.Tests.ps1 b/Tests/Unit/DSR_ReplaceText.Tests.ps1 index cd8c723..3d02012 100644 --- a/Tests/Unit/DSR_ReplaceText.Tests.ps1 +++ b/Tests/Unit/DSR_ReplaceText.Tests.ps1 @@ -42,6 +42,21 @@ try $script:testSecureSecretReplace = ConvertTo-SecureString -String $script:testSecretReplace -AsPlainText -Force $script:testSecretCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ('Dummy', $script:testSecureSecretReplace) + $script:fileEncodingParameters = @{ + Path = $script:testTextFile + Encoding = 'ASCII' + } + + $script:testCompliantEncoding = @{ + Path = $script:fileEncodingParameters.Path + Encoding = $script:fileEncodingParameters.Encoding + } + + $script:testNonCompliantEncoding = @{ + Path = $script:fileEncodingParameters.Path + Encoding = 'UTF8' + } + $script:testFileContent = @" Setting1=Value1 Setting.Two='Value2' @@ -82,7 +97,7 @@ Setting3.Test=Value4 #region Function Get-TargetResource Describe 'DSR_ReplaceText\Get-TargetResource' { - Context 'File exists and search text can be found' { + Context 'File exists and search text can be found and encoding is compliant' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -95,6 +110,12 @@ Setting3.Test=Value4 -MockWith { $script:testFileExpectedTextContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + $script:result = $null It 'Should not throw an exception' { @@ -110,6 +131,60 @@ Setting3.Test=Value4 $script:result.Search | Should -Be $script:testSearch $script:result.Type | Should -Be 'Text' $script:result.Text | Should -Be "$($script:testTextReplace),$($script:testTextReplace),$($script:testTextReplace)" + $script:result.Encoding | Should -Be $script:fileEncodingParameters.Encoding + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + + Context 'File exists and search text can be found but encoding is not compliant' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_ReplaceText' ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileExpectedTextContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -Verifiable + + $script:result = $null + + It 'Should not throw an exception' { + { $script:result = Get-TargetResource ` + -Path $script:testTextFile ` + -Search $script:testSearch ` + -Verbose + } | Should -Not -Throw + } + + It 'Should return expected values' { + $script:result.Path | Should -Be $script:testTextFile + $script:result.Search | Should -Be $script:testSearch + $script:result.Type | Should -Be 'Text' + $script:result.Text | Should -Be "$($script:testTextReplace),$($script:testTextReplace),$($script:testTextReplace)" + $script:result.Encoding | Should -Be $script:testNonCompliantEncoding.Encoding } It 'Should call the expected mocks' { @@ -120,10 +195,15 @@ Setting3.Test=Value4 -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } - Context 'File exists and search text can not be found' { + Context 'File exists and search text can not be found and encoding is compliant' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -136,6 +216,12 @@ Setting3.Test=Value4 -MockWith { $script:testFileExpectedTextContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + $script:result = $null It 'Should not throw an exception' { @@ -151,6 +237,7 @@ Setting3.Test=Value4 $script:result.Search | Should -Be $script:testSearchNoFind $script:result.Type | Should -Be 'Text' $script:result.Text | Should -BeNullOrEmpty + $script:result.Encoding | Should -Be $script:fileEncodingParameters.Encoding } It 'Should call the expected mocks' { @@ -161,6 +248,64 @@ Setting3.Test=Value4 -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + + Context 'File exists and search text can not be found but encoding is not compliant' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_ReplaceText' ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileExpectedTextContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -Verifiable + + $script:result = $null + + It 'Should not throw an exception' { + { $script:result = Get-TargetResource ` + -Path $script:testTextFile ` + -Search $script:testSearchNoFind ` + -Verbose + } | Should -Not -Throw + } + + It 'Should return expected values' { + $script:result.Path | Should -Be $script:testTextFile + $script:result.Search | Should -Be $script:testSearchNoFind + $script:result.Type | Should -Be 'Text' + $script:result.Text | Should -BeNullOrEmpty + $script:result.Encoding | Should -Be $script:testNonCompliantEncoding.Encoding + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } } @@ -168,7 +313,7 @@ Setting3.Test=Value4 #region Function Set-TargetResource Describe 'DSR_ReplaceText\Set-TargetResource' { - Context 'File exists and search text can be found' { + Context 'File exists and search text can be found and encoding is compliant' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -238,6 +383,12 @@ Setting3.Test=Value4 } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + It 'Should not throw an exception' { { $script:result = Set-TargetResource ` -Path $script:testTextFile ` @@ -257,6 +408,11 @@ Setting3.Test=Value4 -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + Assert-MockCalled ` -CommandName Set-Content ` -ParameterFilter { @@ -267,7 +423,8 @@ Setting3.Test=Value4 } } - Context 'File exists search text can not be found and AllowAppend is FALSE' { + Context 'File exists and search secret can be found and encoding is compliant' { + # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` -ModuleName 'DSR_ReplaceText' ` @@ -279,6 +436,12 @@ Setting3.Test=Value4 -MockWith { $script:testFileContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + Mock ` -CommandName Set-Content ` -ParameterFilter { @@ -310,7 +473,8 @@ Setting3.Test=Value4 -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileContent) + ($value -eq $script:testFileExpectedSecretContent) -and ` + ($encoding -eq $script:fileEncodingParameters.Encoding) } ` -Exactly 1 } @@ -339,11 +503,12 @@ Setting3.Test=Value4 It 'Should not throw an exception' { { Set-TargetResource ` - -Path $script:testTextFile ` - -Search $script:testSearch ` - -Type 'Secret' ` - -Secret $script:testSecretCredential ` - -Verbose + -Path $script:testTextFile ` + -Search $script:testSearch ` + -Type 'Secret' ` + -Secret $script:testSecretCredential ` + -Encoding $script:fileEncodingParameters.Encoding ` + -Verbose } | Should -Not -Throw } @@ -356,6 +521,11 @@ Setting3.Test=Value4 -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + Assert-MockCalled ` -CommandName Set-Content ` -ParameterFilter { @@ -439,6 +609,11 @@ Setting3.Test=Value4 -MockWith { $script:testFileContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Verifiable + $script:result = $null It 'Should not throw an exception' { @@ -532,6 +707,11 @@ Setting3.Test=Value4 -MockWith { $script:testFileContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Verifiable + $script:result = $null It 'Should not throw an exception' { @@ -577,6 +757,11 @@ Setting3.Test=Value4 -MockWith { $script:testFileExpectedTextContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Verifiable + $script:result = $null It 'Should not throw an exception' { @@ -622,6 +807,11 @@ Setting3.Test=Value4 -MockWith { $script:testFileContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Verifiable + $script:result = $null It 'Should not throw an exception' { @@ -668,6 +858,11 @@ Setting3.Test=Value4 -MockWith { $script:testFileExpectedSecretContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Verifiable + $script:result = $null It 'Should not throw an exception' { From c1bdecafbeff0072e94bc10fc35afdea7d92eed7 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Mon, 10 Sep 2018 17:45:49 -0400 Subject: [PATCH 11/46] fixing corrupt schema files --- .../DSR_KeyValuePairFile.schema.mof | Bin 3032 -> 1502 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof index 5b84d7f4faca84a2026cf34e168321dd5a182411..b6204853a61f276db6c6b92f940cafffc47226e9 100644 GIT binary patch literal 1502 zcmchXO=}x55Qgvm6~j_U0><>?&{Cgb#|x&oG1*Oe2*t>&u`MD?i=?fyg#7o6q_wka z;?f>kd|Bp0^UOOlz8_0w?Hw8`v^q~d4Bq=+a?yv>2vl6w-`N(=lW$nxF9o1qW0SGG~NZ!<^!Wr=L?q40^SHuAS+d37J2PaCn50l~{P$|ZeBE|=l!IeP7zzC*tg zWh?Wi+)+GNm*^#}ECP!S87w6$a7zSWgOCFn#fnwZQGkZCoU}e8wOYj9{{G-MZM=6;@g?p%KUI7J-;Twub_MC zb!YfM{jXe0<2}D;v~(HW!k}Ko*9g2Gv{x^_t3uG{uiFk zxc)ZJ%*iS@$tL*Dw?V(ivsz?om|1T9Ww_$G@R0p?kBl#)zOXiou5UfZ@vZXeD-?VE zWTR5#>yc>_lZ3dYHa8)ye4SVAh1GhDrnKOZb=s3VmV312ZA|Dv6&;Xc#-f|jvk*?n zyWq2o_fEvT7Lgj^hIKe%rixpP&AC>-73|?M+Pmd$VD01qdS|p&uJHl=Oe`CdQl0+< z;xYenGE%h>8sh4;&BrkBxJJr-WP44$A?^qTLz%$KD%a|bK6Qn8URaRTEp&Jyc5m?} zaCbmJDxmD8JicvesnZf_RUmKlHZ5-mT&qkb9#l%om#>KUW`!YOSE-@a{d-k=*LtcHI zZ~u&4Du01vUE8aRxA~@9es#A#V`j=iRSnaiM$bd;CKd-nR6;axo`{q z4tDG2tDkO~fa33vIXD#7-Nbw3cbzqElFM$^)u?p^%_Zrp4kvul!lM3_t(NPMQLpi8 zE>oTpL33s?_Um{e`n}~oPuPhKyqVi~>)mLb@n3;@XUg~TN%FsEym<4gmN6e(*WMQY GH+})c*!yb$ From a1ea8ea0876d0e6409ba95405e9331f4e62ff88f Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Wed, 12 Sep 2018 13:52:30 -0400 Subject: [PATCH 12/46] keyvaluepair unit test and psm1 updates --- .../DSR_KeyValuePairFile.psm1 | 10 +- Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 | 150 ++++++++++++------ 2 files changed, 108 insertions(+), 52 deletions(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 index edc5d16..373f63e 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 @@ -52,12 +52,11 @@ function Get-TargetResource Assert-ParametersValid @PSBoundParameters $fileContent = Get-Content -Path $Path -Raw + $fileEncoding = Get-FileEncoding -Path $Path Write-Verbose -Message ($localizedData.SearchForKeyMessage -f ` $Path, $Name) - $fileEncoding = Get-FileEncoding -Path $Path - # Setup the Regex Options that will be used $regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline @@ -372,12 +371,11 @@ function Test-TargetResource } $fileContent = Get-Content -Path $Path -Raw + $fileEncoding = Get-FileEncoding -Path $Path Write-Verbose -Message ($localizedData.SearchForKeyMessage -f ` $Path, $Name) - $fileEncoding = Get-FileEncoding -Path $Path - # Setup the Regex Options that will be used $regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline if ($IgnoreNameCase) @@ -433,8 +431,8 @@ function Test-TargetResource if ($desiredConfigurationMatch) { - Write-Verbose -Message ($localizedData.KeyFoundButNoReplacementMessage -f ` - $Path, $Name) + Write-Verbose -Message ($localizedData.KeyUpdateMessage -f ` + $Path, $Name) } } else diff --git a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 index dd7f09e..7d555b3 100644 --- a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 +++ b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 @@ -111,7 +111,7 @@ $($script:testAddedName)=$($script:testText) #region Function Get-TargetResource Describe 'DSR_KeyValuePairFile\Get-TargetResource' { - Context 'File exists and contains matching key and encoding is compliant' { + Context 'File exists, contains matching key, and encoding is in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -141,11 +141,11 @@ $($script:testAddedName)=$($script:testText) } It 'Should return expected values' { - $script:result.Path | Should -Be $script:testTextFile - $script:result.Name | Should -Be $script:testName - $script:result.Ensure | Should -Be 'Present' - $script:result.Type | Should -Be 'Text' - $script:result.Text | Should -Be "$($script:testText),$($script:testText),$($script:testText)" + $script:result.Path | Should -Be $script:testTextFile + $script:result.Name | Should -Be $script:testName + $script:result.Ensure | Should -Be 'Present' + $script:result.Type | Should -Be 'Text' + $script:result.Text | Should -Be "$($script:testText),$($script:testText),$($script:testText)" $script:result.Encoding | Should -Be $script:fileEncodingParameters.Encoding } @@ -165,7 +165,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists and contains matching key and encoding is not compliant' { + Context 'File exists, contains matching key but encoding is not in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -195,11 +195,11 @@ $($script:testAddedName)=$($script:testText) } It 'Should return expected values' { - $script:result.Path | Should -Be $script:testTextFile - $script:result.Name | Should -Be $script:testName - $script:result.Ensure | Should -Be 'Present' - $script:result.Type | Should -Be 'Text' - $script:result.Text | Should -Be "$($script:testText),$($script:testText),$($script:testText)" + $script:result.Path | Should -Be $script:testTextFile + $script:result.Name | Should -Be $script:testName + $script:result.Ensure | Should -Be 'Present' + $script:result.Type | Should -Be 'Text' + $script:result.Text | Should -Be "$($script:testText),$($script:testText),$($script:testText)" $script:result.Encoding | Should -Be $script:testNonCompliantEncoding.Encoding } @@ -219,7 +219,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists and does not contain matching key and encoding is compliant' { + Context 'File exists, does not contain matching key and encoding is in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -249,11 +249,11 @@ $($script:testAddedName)=$($script:testText) } It 'Should return expected values' { - $script:result.Path | Should -Be $script:testTextFile - $script:result.Name | Should -Be $script:testName - $script:result.Ensure | Should -Be 'Absent' - $script:result.Type | Should -Be 'Text' - $script:result.Text | Should -BeNullOrEmpty + $script:result.Path | Should -Be $script:testTextFile + $script:result.Name | Should -Be $script:testName + $script:result.Ensure | Should -Be 'Absent' + $script:result.Type | Should -Be 'Text' + $script:result.Text | Should -BeNullOrEmpty $script:result.Encoding | Should -Be $script:fileEncodingParameters.Encoding } @@ -273,7 +273,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists and does not contain matching key and encoding is compliant' { + Context 'File exists, does not contain matching key and encoding is in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -303,11 +303,11 @@ $($script:testAddedName)=$($script:testText) } It 'Should return expected values' { - $script:result.Path | Should -Be $script:testTextFile - $script:result.Name | Should -Be $script:testName - $script:result.Ensure | Should -Be 'Absent' - $script:result.Type | Should -Be 'Text' - $script:result.Text | Should -BeNullOrEmpty + $script:result.Path | Should -Be $script:testTextFile + $script:result.Name | Should -Be $script:testName + $script:result.Ensure | Should -Be 'Absent' + $script:result.Type | Should -Be 'Text' + $script:result.Text | Should -BeNullOrEmpty $script:result.Encoding | Should -Be $script:fileEncodingParameters.Encoding } @@ -345,11 +345,9 @@ $($script:testAddedName)=$($script:testText) -Verifiable Mock ` - -CommandName Set-Content ` - -ParameterFilter { - ($path -eq $script:testTextFile) -and ` - ($value -eq "$script:testName=$script:testText") - } ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $null } ` -Verifiable It 'Should not throw an exception' { @@ -358,6 +356,7 @@ $($script:testAddedName)=$($script:testText) -Name $script:testName ` -Ensure 'Present' ` -Text $script:testText ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -365,23 +364,10 @@ $($script:testAddedName)=$($script:testText) It 'Should call the expected mocks' { Assert-VerifiableMock Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - - Assert-MockCalled ` - -CommandName Set-Content ` - -ParameterFilter { - ($path -eq $script:testTextFile) -and ` - ($value -eq "$script:testName=$script:testText") - } ` - -Exactly 1 } } - Context 'File exists and contains matching key that should exist' { + Context 'File exists, contains matching key that should exist and encoding is in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -400,6 +386,15 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable + Mock ` + -CommandName Set-Content ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($value -eq $script:testFileExpectedTextContent) -and ` + ($encoding -eq $script:fileEncodingParameters.Encoding) + } ` + -Verifiable + It 'Should not throw an exception' { { Set-TargetResource ` -Path $script:testTextFile ` @@ -424,10 +419,19 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Set-Content ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($value -eq $script:testFileExpectedTextContent) -and ` + ($encoding -eq $script:fileEncodingParameters.Encoding) + } ` + -Exactly 1 } } - Context 'File exists and contains matching key that should exist but encoding is not compliant' { + Context 'File exists, contains matching key that should exist but encoding is not in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -446,6 +450,15 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testNonCompliantEncoding.Encoding } ` -Verifiable + Mock ` + -CommandName Set-Content ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($value -eq $script:testFileExpectedTextContent) -and ` + ($encoding -eq $script:fileEncodingParameters.Encoding) + } ` + -Verifiable + It 'Should not throw an exception' { { Set-TargetResource ` -Path $script:testTextFile ` @@ -470,10 +483,19 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Set-Content ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($value -eq $script:testFileExpectedTextContent) -and ` + ($encoding -eq $script:fileEncodingParameters.Encoding) + } ` + -Exactly 1 } } - Context 'File exists and contains matching key that should exist and contain a secret' { + Context 'File exists, contains matching key that should exist, contains a secret and encoding is in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -492,6 +514,15 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable + Mock ` + -CommandName Set-Content ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($value -eq $script:testFileExpectedSecretContent) + } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + It 'Should not throw an exception' { { Set-TargetResource ` -Path $script:testTextFile ` @@ -517,10 +548,19 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Set-Content ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($value -eq $script:testFileExpectedSecretContent) -and ` + ($encoding -eq $script:fileEncodingParameters.Encoding) + } ` + -Exactly 1 } } - Context 'File exists and contains matching key that should exist and contain a secret but encoding is not compliant' { + Context 'File exists, contains matching key that should exist, contains a secret but encoding is not in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -539,6 +579,15 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testNonCompliantEncoding.Encoding } ` -Verifiable + Mock ` + -CommandName Set-Content ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($value -eq $script:testFileExpectedSecretContent) + } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + It 'Should not throw an exception' { { Set-TargetResource ` -Path $script:testTextFile ` @@ -564,6 +613,15 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Set-Content ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($value -eq $script:testFileExpectedSecretContent) -and ` + ($encoding -eq $script:fileEncodingParameters.Encoding) + } ` + -Exactly 1 } } From 3c2309ce9251bd6493f25dd93044292b1956b4f1 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Tue, 2 Oct 2018 10:08:02 -0400 Subject: [PATCH 13/46] update keyvaluepair tests --- Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 | 555 ++-------------------- 1 file changed, 34 insertions(+), 521 deletions(-) diff --git a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 index 7d555b3..5db3e56 100644 --- a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 +++ b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 @@ -111,7 +111,7 @@ $($script:testAddedName)=$($script:testText) #region Function Get-TargetResource Describe 'DSR_KeyValuePairFile\Get-TargetResource' { - Context 'File exists, contains matching key, and encoding is in desired state' { + Context 'File exists and contains matching key and encoding is in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -165,7 +165,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists, contains matching key but encoding is not in desired state' { + Context 'File exists and contains matching key but encoding is not in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -219,7 +219,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists, does not contain matching key and encoding is in desired state' { + Context 'File exists and does not contain matching key and encoding is in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -238,6 +238,7 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable + $script:result = $null It 'Should not throw an exception' { @@ -273,7 +274,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists, does not contain matching key and encoding is in desired state' { + Context 'File exists and does not contain matching key and encoding is not in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -289,7 +290,7 @@ $($script:testAddedName)=$($script:testText) Mock ` -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` -Verifiable $script:result = $null @@ -308,7 +309,7 @@ $($script:testAddedName)=$($script:testText) $script:result.Ensure | Should -Be 'Absent' $script:result.Type | Should -Be 'Text' $script:result.Text | Should -BeNullOrEmpty - $script:result.Encoding | Should -Be $script:fileEncodingParameters.Encoding + $script:result.Encoding | Should -Be $script:testNonCompliantEncoding.Encoding } It 'Should call the expected mocks' { @@ -367,71 +368,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists, contains matching key that should exist and encoding is in desired state' { - # verifiable (should be called) mocks - Mock ` - -CommandName Assert-ParametersValid ` - -ModuleName 'DSR_KeyValuePairFile' ` - -Verifiable - - Mock ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileContent } ` - -Verifiable - - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` - -Verifiable - - Mock ` - -CommandName Set-Content ` - -ParameterFilter { - ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContent) -and ` - ($encoding -eq $script:fileEncodingParameters.Encoding) - } ` - -Verifiable - - It 'Should not throw an exception' { - { Set-TargetResource ` - -Path $script:testTextFile ` - -Name $script:testName ` - -Ensure 'Present' ` - -Text $script:testText ` - -Encoding $script:fileEncodingParameters.Encoding ` - -Verbose - } | Should -Not -Throw - } - - It 'Should call the expected mocks' { - Assert-VerifiableMock - Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - - Assert-MockCalled ` - -CommandName Set-Content ` - -ParameterFilter { - ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContent) -and ` - ($encoding -eq $script:fileEncodingParameters.Encoding) - } ` - -Exactly 1 - } - } - - Context 'File exists, contains matching key that should exist but encoding is not in desired state' { + Context 'File exists and contains matching key that should exist' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -444,18 +381,11 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileContent } ` -Verifiable - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testNonCompliantEncoding.Encoding } ` - -Verifiable - Mock ` -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContent) -and ` - ($encoding -eq $script:fileEncodingParameters.Encoding) + ($value -eq $script:testFileExpectedTextContent) ` } ` -Verifiable @@ -465,7 +395,6 @@ $($script:testAddedName)=$($script:testText) -Name $script:testName ` -Ensure 'Present' ` -Text $script:testText ` - -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -479,23 +408,17 @@ $($script:testAddedName)=$($script:testText) -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - Assert-MockCalled ` -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContent) -and ` - ($encoding -eq $script:fileEncodingParameters.Encoding) + ($value -eq $script:testFileExpectedTextContent) } ` -Exactly 1 } } - Context 'File exists, contains matching key that should exist, contains a secret and encoding is in desired state' { + Context 'File exists and contains matching key that should exist and contains a secret' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -508,19 +431,12 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileContent } ` -Verifiable - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` - -Verifiable - Mock ` -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` ($value -eq $script:testFileExpectedSecretContent) } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable It 'Should not throw an exception' { @@ -530,7 +446,6 @@ $($script:testAddedName)=$($script:testText) -Ensure 'Present' ` -Type 'Secret' ` -Secret $script:testSecretCredential ` - -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -544,88 +459,17 @@ $($script:testAddedName)=$($script:testText) -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - Assert-MockCalled ` -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedSecretContent) -and ` - ($encoding -eq $script:fileEncodingParameters.Encoding) + ($value -eq $script:testFileExpectedSecretContent) } ` -Exactly 1 } } - Context 'File exists, contains matching key that should exist, contains a secret but encoding is not in desired state' { - # verifiable (should be called) mocks - Mock ` - -CommandName Assert-ParametersValid ` - -ModuleName 'DSR_KeyValuePairFile' ` - -Verifiable - - Mock ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileContent } ` - -Verifiable - - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testNonCompliantEncoding.Encoding } ` - -Verifiable - - Mock ` - -CommandName Set-Content ` - -ParameterFilter { - ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedSecretContent) - } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` - -Verifiable - - It 'Should not throw an exception' { - { Set-TargetResource ` - -Path $script:testTextFile ` - -Name $script:testName ` - -Ensure 'Present' ` - -Type 'Secret' ` - -Secret $script:testSecretCredential ` - -Encoding $script:fileEncodingParameters.Encoding ` - -Verbose - } | Should -Not -Throw - } - - It 'Should call the expected mocks' { - Assert-VerifiableMock - Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - - Assert-MockCalled ` - -CommandName Set-Content ` - -ParameterFilter { - ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedSecretContent) -and ` - ($encoding -eq $script:fileEncodingParameters.Encoding) - } ` - -Exactly 1 - } - } - - Context 'File exists does not contain matching key but key should exist' { + Context 'File exists and does not contain matching key but key should exist' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -644,13 +488,6 @@ $($script:testAddedName)=$($script:testText) ($path -eq $script:testTextFile) -and ` ($value -eq $script:testFileExpectedTextContentAdded) } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` - -Verifiable - - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable It 'Should not throw an exception' { @@ -659,7 +496,6 @@ $($script:testAddedName)=$($script:testText) -Name $script:testAddedName ` -Ensure 'Present' ` -Text $script:testText ` - -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -673,17 +509,11 @@ $($script:testAddedName)=$($script:testText) -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - Assert-MockCalled ` -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContentAdded) -and ` - ($encoding -eq $script:fileEncodingParameters.Encoding) + ($value -eq $script:testFileExpectedTextContentAdded) } ` -Exactly 1 } @@ -710,19 +540,12 @@ $($script:testAddedName)=$($script:testText) } ` -Verifiable - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` - -Verifiable - It 'Should not throw an exception' { { Set-TargetResource ` -Path $script:testTextFile ` -Name $script:testName.ToUpper() ` -Ensure 'Present' ` -Text $script:testText ` - -Encoding $script:fileEncodingParameters.Encoding ` -IgnoreNameCase:$true ` -Verbose } | Should -Not -Throw @@ -737,23 +560,17 @@ $($script:testAddedName)=$($script:testText) -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - Assert-MockCalled ` -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContentUpper) -and ` - ($encoding -eq $script:fileEncodingParameters.Encoding) + ($value -eq $script:testFileExpectedTextContentUpper) } ` -Exactly 1 } } - Context 'File exists and does not contain key with matching case and should not' { + Context 'File exists and does not contain key with matching case and should not and encoding is in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -806,7 +623,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists and does not contain key with matching case and should not but encoding is not compliant' { + Context 'File exists and does not contain key with matching case and should not but encoding in not in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -822,7 +639,8 @@ $($script:testAddedName)=$($script:testText) Mock ` -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -MockWith { $script:testNonCompliantEncoding.Encoding + } ` -Verifiable # non-verifiable mocks @@ -880,17 +698,10 @@ $($script:testAddedName)=$($script:testText) } ` -Verifiable - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` - -Verifiable - It 'Should not throw an exception' { { Set-TargetResource ` -Path $script:testTextFile ` -Name $script:testName ` - -Encoding $script:fileEncodingParameters.Encoding ` -Ensure 'Absent' ` -IgnoreNameCase:$true ` -Verbose @@ -913,11 +724,6 @@ $($script:testAddedName)=$($script:testText) ($value -eq $script:testFileExpectedAbsentContent) } ` -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 } } } @@ -944,19 +750,12 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileExpectedTextContentAdded } ` -Verifiable - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` - -Verifiable - It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` -Name $script:testName.ToUpper() ` -Ensure 'Present' ` -Text $script:testText ` - -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -973,15 +772,10 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 } } - Context 'File exists and does not contain matching key and should not' { + Context 'File exists and does not contain matching key and should not and encoding is in desired state ' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -1036,7 +830,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists and does not contain matching key and should not but file encoding is not compliant' { + Context 'File exists and does not contain matching key and should not but encoding is not in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -1071,7 +865,7 @@ $($script:testAddedName)=$($script:testText) } | Should -Not -Throw } - It 'Should return false' { + It 'Should return true' { $script:result | Should -Be $false } @@ -1084,7 +878,7 @@ $($script:testAddedName)=$($script:testText) -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - Assert-MockCalled ` + Assert-MockCalled ` -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 @@ -1142,62 +936,6 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists and contains matching key that should exist and values match but file encoding is not compliant' { - # verifiable (should be called) mocks - Mock ` - -CommandName Assert-ParametersValid ` - -ModuleName 'DSR_KeyValuePairFile' ` - -Verifiable - - Mock ` - -CommandName Test-Path ` - -ModuleName 'DSR_KeyValuePairFile' ` - -MockWith { $true } ` - -Verifiable - - Mock ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileExpectedTextContent } ` - -Verifiable - - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testNonCompliantEncoding.Encoding } ` - -Verifiable - - It 'Should not throw an exception' { - { $script:result = Test-TargetResource ` - -Path $script:testTextFile ` - -Name $script:testName ` - -Ensure 'Present' ` - -Text $script:testText ` - -Encoding $script:fileEncodingParameters.Encoding ` - -Verbose - } | Should -Not -Throw - } - - It 'Should return false' { - $script:result | Should -Be $false - } - - It 'Should call the expected mocks' { - Assert-VerifiableMock - Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - } - } - Context 'File exists and contains matching key that should exist and values do not match secret text' { # verifiable (should be called) mocks Mock ` @@ -1217,12 +955,6 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileContent } ` -Verifiable - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` - -Verifiable - It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` @@ -1230,7 +962,6 @@ $($script:testAddedName)=$($script:testText) -Ensure 'Present' ` -Type 'Secret' ` -Secret $script:testSecretCredential ` - -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -1247,11 +978,6 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 } } @@ -1312,177 +1038,6 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists and contains matching key that should exist and values match secret text but file encoding is not compliant' { - # verifiable (should be called) mocks - Mock ` - -CommandName Assert-ParametersValid ` - -ModuleName 'DSR_KeyValuePairFile' ` - -Verifiable - - Mock ` - -CommandName Test-Path ` - -ModuleName 'DSR_KeyValuePairFile' ` - -MockWith { $true } ` - -Verifiable - - Mock ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileExpectedSecretContent } ` - -Verifiable - - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testNonCompliantEncoding.Encoding } ` - -Verifiable - - It 'Should not throw an exception' { - { $script:result = Test-TargetResource ` - -Path $script:testTextFile ` - -Name $script:testName ` - -Ensure 'Present' ` - -Type 'Secret' ` - -Secret $script:testSecretCredential ` - -Encoding $script:fileEncodingParameters.Encoding ` - -Verbose - } | Should -Not -Throw - } - - It 'Should return false' { - $script:result | Should -Be $false - } - - It 'Should call the expected mocks' { - Assert-VerifiableMock - Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - } - } - - Context 'File exists and contains key with different case that should exist and values match and IgnoreNameCase is True' { - # verifiable (should be called) mocks - Mock ` - -CommandName Assert-ParametersValid ` - -ModuleName 'DSR_KeyValuePairFile' ` - -Verifiable - - Mock ` - -CommandName Test-Path ` - -ModuleName 'DSR_KeyValuePairFile' ` - -MockWith { $true } ` - -Verifiable - - Mock ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileExpectedTextContent } ` - -Verifiable - - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` - -Verifiable - - It 'Should not throw an exception' { - { $script:result = Test-TargetResource ` - -Path $script:testTextFile ` - -Name $script:testName.ToUpper() ` - -Ensure 'Present' ` - -Text $script:testText ` - -IgnoreNameCase:$true ` - -Encoding $script:fileEncodingParameters.Encoding ` - -Verbose - } | Should -Not -Throw - } - - It 'Should return true' { - $script:result | Should -Be $true - } - - It 'Should call the expected mocks' { - Assert-VerifiableMock - Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - } - } - - Context 'File exists and contains key with different case that should exist and values match and IgnoreNameCase is True but file encoding is not compliant' { - # verifiable (should be called) mocks - Mock ` - -CommandName Assert-ParametersValid ` - -ModuleName 'DSR_KeyValuePairFile' ` - -Verifiable - - Mock ` - -CommandName Test-Path ` - -ModuleName 'DSR_KeyValuePairFile' ` - -MockWith { $true } ` - -Verifiable - - Mock ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileExpectedTextContent } ` - -Verifiable - - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testNonCompliantEncoding.Encoding } ` - -Verifiable - - It 'Should not throw an exception' { - { $script:result = Test-TargetResource ` - -Path $script:testTextFile ` - -Name $script:testName.ToUpper() ` - -Ensure 'Present' ` - -Text $script:testText ` - -IgnoreNameCase:$true ` - -Encoding $script:fileEncodingParameters.Encoding ` - -Verbose - } | Should -Not -Throw - } - - It 'Should return false' { - $script:result | Should -Be $false - } - - It 'Should call the expected mocks' { - Assert-VerifiableMock - Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - } - } - Context 'File exists and contains matching key that should exist and values match but are different case and IgnoreValueCase is False' { # verifiable (should be called) mocks Mock ` @@ -1502,19 +1057,12 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileExpectedTextContent } ` -Verifiable - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` - -Verifiable - It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` -Name $script:testName ` -Ensure 'Present' ` -Text $script:testText.ToUpper() ` - -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -1531,11 +1079,6 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 } } @@ -1596,7 +1139,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists and contains matching key that should exist and values match but are different case and IgnoreValueCase is True but file encoding is not compliant' { + Context 'File exists and contains matching key that should not exist' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -1615,21 +1158,13 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileExpectedTextContent } ` -Verifiable - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testNonCompliantEncoding.Encoding } ` - -Verifiable - It 'Should not throw an exception' { { $script:result = Test-TargetResource ` - -Path $script:testTextFile ` - -Name $script:testName ` - -Ensure 'Present' ` - -Text $script:testText.ToUpper() ` - -Encoding $script:fileEncodingParameters.Encoding ` - -IgnoreValueCase:$true ` - -Verbose + -Path $script:testTextFile ` + -Name $script:testName ` + -Ensure 'Absent' ` + -Text $script:testText ` + -Verbose } | Should -Not -Throw } @@ -1645,51 +1180,34 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 } } - Context 'File exists and contains matching key that should not exist' { + Context 'File exists and encoding is not in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable - Mock ` - -CommandName Test-Path ` - -ModuleName 'DSR_KeyValuePairFile' ` - -MockWith { $true } ` - -Verifiable - - Mock ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileExpectedTextContent } ` - -Verifiable - Mock ` -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` -MockWith { $script:testNonCompliantEncoding.Encoding } ` -Verifiable + $script:result = $null + It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` -Name $script:testName ` - -Ensure 'Absent' ` - -Text $script:testText ` - -Encoding $script:testNonCompliantEncoding.Encoding ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } - It 'Should return false' { + It 'Should return true' { $script:result | Should -Be $false } @@ -1697,11 +1215,6 @@ $($script:testAddedName)=$($script:testText) Assert-VerifiableMock Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 - Assert-MockCalled ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - Assert-MockCalled ` -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` From a2720a9c98783133e131ff5b866b688028f7251e Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Tue, 2 Oct 2018 12:53:02 -0400 Subject: [PATCH 14/46] test and code fixes --- .../DSR_ReplaceText/DSR_ReplaceText.psm1 | 39 +++++++++---------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 index dae2267..85f3b48 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 @@ -256,31 +256,19 @@ function Test-TargetResource [Parameter()] [String] - $Encoding + $Encoding = 'ASCII' ) Assert-ParametersValid @PSBoundParameters # Check for the file being managed. If it does not exist return $false. - # If it exists but is using the wrong encoding return $false. if (-not (Test-Path -Path $Path)) { return $false } - else - { - $fileEncoding = Get-FileEncoding $Path - - if ($Encoding -ne $fileEncoding) - { - Write-Verbose -Message ($localizedData.FileEncodingNotSetProperly -f ` - $fileEncoding, $Encoding) - - return $false - } - } $fileContent = Get-Content -Path $Path -Raw + $fileEncoding = Get-FileEncoding $Path Write-Verbose -Message ($localizedData.SearchForTextMessage -f ` $Path, $Search) @@ -288,6 +276,9 @@ function Test-TargetResource # Search the file content for any matches $results = [regex]::Matches($fileContent, $Search) + # Flag to signal whether settings are correct + [Boolean] $desiredConfigurationMatch = $true + if ($results.Count -eq 0) { if ($AllowAppend -eq $true) @@ -298,16 +289,22 @@ function Test-TargetResource return $false } - - # No matches found - already in state - Write-Verbose -Message ($localizedData.StringNotFoundMessage -f ` + elseif ($Encoding -eq $fileEncoding) + { + # No matches found - already in state + Write-Verbose -Message ($localizedData.StringNotFoundMessage -f ` $Path, $Search) - return $true - } + return $true + } + else + { + Write-Verbose -Message ($localizedData.FileEncodingNotInDesiredState -f ` + $fileEncoding, $Encoding) - # Flag to signal whether settings are correct - [Boolean] $desiredConfigurationMatch = $true + $desiredConfigurationMatch = $false + } + } if ($Type -eq 'Secret') { From c88d96a93883166e79dddb346769b82ff269c441 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Tue, 2 Oct 2018 15:08:25 -0400 Subject: [PATCH 15/46] minor formatting, description... updates --- .../DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 | 5 ++--- DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 | 4 ++-- Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 | 3 ++- Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 | 9 ++++----- Tests/Unit/DSR_ReplaceText.Tests.ps1 | 8 ++++---- 5 files changed, 14 insertions(+), 15 deletions(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 index 373f63e..1a35f6d 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 @@ -399,8 +399,7 @@ function Test-TargetResource } else { - # The key value pairs should exist and do - + # The key value pairs should exist and do and encoding is in desired state if ($Encoding -eq $fileEncoding) { Write-Verbose -Message ($localizedData.KeyNotFoundAndShouldNotExistMessage -f ` @@ -431,7 +430,7 @@ function Test-TargetResource if ($desiredConfigurationMatch) { - Write-Verbose -Message ($localizedData.KeyUpdateMessage -f ` + Write-Verbose -Message ($localizedData.KeyFoundButNoReplacementMessage -f ` $Path, $Name) } } diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 index 85f3b48..243476e 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 @@ -261,7 +261,7 @@ function Test-TargetResource Assert-ParametersValid @PSBoundParameters - # Check for the file being managed. If it does not exist return $false. + # Check if file being managed exists. If not return $false. if (-not (Test-Path -Path $Path)) { return $false @@ -291,7 +291,7 @@ function Test-TargetResource } elseif ($Encoding -eq $fileEncoding) { - # No matches found - already in state + # No matches found and encoding is in desired state Write-Verbose -Message ($localizedData.StringNotFoundMessage -f ` $Path, $Search) diff --git a/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 b/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 index b878229..71c35be 100644 --- a/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 +++ b/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 @@ -159,7 +159,8 @@ function Get-IniSettingFileValue This command gets ps1 files in current directory where encoding is not ASCII .EXAMPLE - Get-ChildItem *.ps1 | select FullName, @{n='Encoding';e={Get-FileEncoding $_.FullName}} | where {$_.Encoding -ne 'ASCII'} | foreach {(get-content $_.FullName) | set-content $_.FullName -Encoding ASCII} + Get-ChildItem *.ps1 | select FullName, @{n='Encoding';e={Get-FileEncoding $_.FullName}} | where {$_.Encoding -ne 'ASCII'} | ` + foreach {(get-content $_.FullName) | set-content $_.FullName -Encoding ASCII} Same as previous example but fixes encoding using set-content #> function Get-FileEncoding diff --git a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 index 5db3e56..7688204 100644 --- a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 +++ b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 @@ -238,7 +238,6 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable - $script:result = $null It 'Should not throw an exception' { @@ -418,7 +417,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists and contains matching key that should exist and contains a secret' { + Context 'File exists and contains matching key that should exist and contain a secret' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -747,7 +746,7 @@ $($script:testAddedName)=$($script:testText) Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileExpectedTextContentAdded } ` + -MockWith { $script:testFileContent } ` -Verifiable It 'Should not throw an exception' { @@ -823,7 +822,7 @@ $($script:testAddedName)=$($script:testText) -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - Assert-MockCalled ` + Assert-MockCalled ` -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 @@ -1207,7 +1206,7 @@ $($script:testAddedName)=$($script:testText) } | Should -Not -Throw } - It 'Should return true' { + It 'Should return false' { $script:result | Should -Be $false } diff --git a/Tests/Unit/DSR_ReplaceText.Tests.ps1 b/Tests/Unit/DSR_ReplaceText.Tests.ps1 index 3d02012..618eb00 100644 --- a/Tests/Unit/DSR_ReplaceText.Tests.ps1 +++ b/Tests/Unit/DSR_ReplaceText.Tests.ps1 @@ -97,7 +97,7 @@ Setting3.Test=Value4 #region Function Get-TargetResource Describe 'DSR_ReplaceText\Get-TargetResource' { - Context 'File exists and search text can be found and encoding is compliant' { + Context 'File exists and search text can be found and encoding is in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -150,7 +150,7 @@ Setting3.Test=Value4 } } - Context 'File exists and search text can be found but encoding is not compliant' { + Context 'File exists and search text can be found but encoding is not in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -203,7 +203,7 @@ Setting3.Test=Value4 } } - Context 'File exists and search text can not be found and encoding is compliant' { + Context 'File exists and search text can not be found and encoding is in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -256,7 +256,7 @@ Setting3.Test=Value4 } } - Context 'File exists and search text can not be found but encoding is not compliant' { + Context 'File exists and search text can not be found but encoding is not in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` From 1bc1bc3346f673131d90db0c65a6a15b9a78c394 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Tue, 2 Oct 2018 15:28:46 -0400 Subject: [PATCH 16/46] update with change info --- CHANGELOG.md | 1 + README.md | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a60cd41..c62f2ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - Added .VSCode settings for applying DSC PSSA rules - fixes [Issue #25](https://github.com/PlagueHO/FileContentDsc/issues/25). +- Added an Encoding parameter to the KeyValuePairFile and ReplaceText resources - fixes [Issue #5](https://github.com/PlagueHO/FileContentDsc/issues/5) ## 1.1.0.0 diff --git a/README.md b/README.md index 0740b7a..4abab6b 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,9 @@ The **FileContent** module contains the following resources: - **IniSettingsFile**: Add, set or clear entries in Windows INI settings files. - **KeyValuePairFile**: Add, remove or set key/value pairs in a text file containing - key/value pairs. -- **ReplaceText**: Replaces strings matching a regular expression in a file. + key/value pairs, and set file encoding. +- **ReplaceText**: Replaces strings matching a regular expression in a file, + and sets file encoding. **This project is not maintained or supported by Microsoft.** From f5588973c0f9488a7a6ab1a10dcd9cb11bdcc9bd Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Tue, 2 Oct 2018 22:35:20 -0400 Subject: [PATCH 17/46] minor fixes --- .../DSR_ReplaceText/DSR_ReplaceText.psm1 | 11 +- Tests/Unit/DSR_ReplaceText.Tests.ps1 | 104 ++++++++---------- 2 files changed, 54 insertions(+), 61 deletions(-) diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 index 243476e..d1091f6 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 @@ -276,9 +276,6 @@ function Test-TargetResource # Search the file content for any matches $results = [regex]::Matches($fileContent, $Search) - # Flag to signal whether settings are correct - [Boolean] $desiredConfigurationMatch = $true - if ($results.Count -eq 0) { if ($AllowAppend -eq $true) @@ -289,7 +286,7 @@ function Test-TargetResource return $false } - elseif ($Encoding -eq $fileEncoding) + if ($Encoding -eq $fileEncoding) { # No matches found and encoding is in desired state Write-Verbose -Message ($localizedData.StringNotFoundMessage -f ` @@ -299,13 +296,17 @@ function Test-TargetResource } else { + # No matches found and encoding is in desired state Write-Verbose -Message ($localizedData.FileEncodingNotInDesiredState -f ` $fileEncoding, $Encoding) - $desiredConfigurationMatch = $false + return $false } } + # Flag to signal whether settings are correct + [Boolean] $desiredConfigurationMatch = $true + if ($Type -eq 'Secret') { $Text = $Secret.GetNetworkCredential().Password diff --git a/Tests/Unit/DSR_ReplaceText.Tests.ps1 b/Tests/Unit/DSR_ReplaceText.Tests.ps1 index 02cc86d..c306b67 100644 --- a/Tests/Unit/DSR_ReplaceText.Tests.ps1 +++ b/Tests/Unit/DSR_ReplaceText.Tests.ps1 @@ -127,11 +127,11 @@ Setting3.Test=Value4 } It 'Should return expected values' { - $script:result.Path | Should -Be $script:testTextFile - $script:result.Search | Should -Be $script:testSearch - $script:result.Type | Should -Be 'Text' - $script:result.Text | Should -Be "$($script:testTextReplace),$($script:testTextReplace),$($script:testTextReplace)" - $script:result.Encoding | Should -Be $script:fileEncodingParameters.Encoding + $script:result.Path | Should -Be $script:testTextFile + $script:result.Search | Should -Be $script:testSearch + $script:result.Type | Should -Be 'Text' + $script:result.Text | Should -Be "$($script:testTextReplace),$($script:testTextReplace),$($script:testTextReplace)" + $script:result.Encoding | Should -Be $script:testCompliantEncoding.Encoding } It 'Should call the expected mocks' { @@ -313,7 +313,7 @@ Setting3.Test=Value4 #region Function Set-TargetResource Describe 'DSR_ReplaceText\Set-TargetResource' { - Context 'File exists and search text can be found and encoding is compliant' { + Context 'File exists and search text can be found' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -331,7 +331,8 @@ Setting3.Test=Value4 -ParameterFilter { ($path -eq $script:testTextFile) -and ` ($value -eq $script:testFileExpectedTextContent) - } ` + } ` + -MockWith { $script:fileEncodingParameters.Encoding } ` -Verifiable It 'Should not throw an exception' { @@ -339,6 +340,7 @@ Setting3.Test=Value4 -Path $script:testTextFile ` -Search $script:testSearch ` -Text $script:testTextReplace ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -357,7 +359,7 @@ Setting3.Test=Value4 -ParameterFilter { ($path -eq $script:testTextFile) -and ` ($value -eq $script:testFileExpectedTextContent) - } ` + } ` -Exactly 1 } } @@ -383,18 +385,13 @@ Setting3.Test=Value4 } ` -Verifiable - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` - -Verifiable - It 'Should not throw an exception' { { $script:result = Set-TargetResource ` -Path $script:testTextFile ` -Search $script:testSearchNoFind ` -Text $script:testTextReplaceNoFind ` -AllowAppend $true ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -408,11 +405,6 @@ Setting3.Test=Value4 -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - Assert-MockCalled ` -CommandName Set-Content ` -ParameterFilter { @@ -423,8 +415,7 @@ Setting3.Test=Value4 } } - Context 'File exists and search secret can be found and encoding is compliant' { - # verifiable (should be called) mocks + Context 'File exists search text can not be found and AllowAppend is FALSE' { Mock ` -CommandName Assert-ParametersValid ` -ModuleName 'DSR_ReplaceText' ` @@ -436,12 +427,6 @@ Setting3.Test=Value4 -MockWith { $script:testFileContent } ` -Verifiable - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testCompliantEncoding.Encoding } ` - -Verifiable - Mock ` -CommandName Set-Content ` -ParameterFilter { @@ -456,6 +441,7 @@ Setting3.Test=Value4 -Search $script:testSearchNoFind ` -Text $script:testTextReplaceNoFind ` -AllowAppend $false ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -473,8 +459,7 @@ Setting3.Test=Value4 -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedSecretContent) -and ` - ($encoding -eq $script:fileEncodingParameters.Encoding) + ($value -eq $script:testFileContent) } ` -Exactly 1 } @@ -503,12 +488,12 @@ Setting3.Test=Value4 It 'Should not throw an exception' { { Set-TargetResource ` - -Path $script:testTextFile ` - -Search $script:testSearch ` - -Type 'Secret' ` - -Secret $script:testSecretCredential ` - -Encoding $script:fileEncodingParameters.Encoding ` - -Verbose + -Path $script:testTextFile ` + -Search $script:testSearch ` + -Type 'Secret' ` + -Secret $script:testSecretCredential ` + -Encoding $script:fileEncodingParameters.Encoding ` + -Verbose } | Should -Not -Throw } @@ -521,11 +506,6 @@ Setting3.Test=Value4 -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - Assert-MockCalled ` -CommandName Set-Content ` -ParameterFilter { @@ -584,13 +564,12 @@ Setting3.Test=Value4 -Exactly 1 } } - } #endregion #region Function Test-TargetResource Describe 'DSR_ReplaceString\Test-TargetResource' { - Context 'File exists and search text can not be found and encoding is in desired state' { + Context 'File exists search text cannot be found and AllowAppend is TRUE' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -619,11 +598,12 @@ Setting3.Test=Value4 It 'Should not throw an exception' { { $script:result = Test-TargetResource ` - -Path $script:testTextFile ` - -Search $script:testSearchNoFind ` - -Text $script:testTextReplace ` - -Encoding $script:fileEncodingParameters.Encoding ` - -Verbose + -Path $script:testTextFile ` + -Search $script:testSearchNoFind ` + -Text $script:testTextReplace ` + -AllowAppend $true ` + -Encoding $script:fileEncodingParameters ` + -Verbose } | Should -Not -Throw } @@ -639,10 +619,15 @@ Setting3.Test=Value4 -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 } } - Context 'File exists search text can not be found and AllowAppend is FALSE' { + Context 'File exists search text cannot be found and AllowAppend is FALSE and encoding is in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -661,6 +646,12 @@ Setting3.Test=Value4 -MockWith { $script:testFileContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + $script:result = $null It 'Should not throw an exception' { @@ -669,11 +660,12 @@ Setting3.Test=Value4 -Search $script:testSearchNoFind ` -Text $script:testTextReplace ` -AllowAppend $false ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } - It 'Should return false' { + It 'Should return true' { $script:result | Should -Be $true } @@ -693,7 +685,7 @@ Setting3.Test=Value4 } } - Context 'File exists and search text can not be found and encoding is not in desired state' { + Context 'File exists search text cannot be found and AllowAppend is FALSE and encoding is not in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -722,11 +714,12 @@ Setting3.Test=Value4 It 'Should not throw an exception' { { $script:result = Test-TargetResource ` - -Path $script:testTextFile ` - -Search $script:testSearchNoFind ` - -Text $script:testTextReplace ` - -Encoding $script:fileEncodingParameters.Encoding ` - -Verbose + -Path $script:testTextFile ` + -Search $script:testSearchNoFind ` + -Text $script:testTextReplace ` + -AllowAppend $false ` + -Encoding $script:fileEncodingParameters.Encoding ` + -Verbose } | Should -Not -Throw } @@ -750,7 +743,6 @@ Setting3.Test=Value4 } } - Context 'File exists and search text can be found but does not match replace string' { # verifiable (should be called) mocks Mock ` From 6cc21a4608db222ec21d44d89bea40367cd61176 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Tue, 2 Oct 2018 23:10:36 -0400 Subject: [PATCH 18/46] split line in change log, added validate set --- CHANGELOG.md | 3 ++- .../DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 | 12 +++++++++--- DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 | 12 +++++++++--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c62f2ac..cb26d49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,8 @@ ## Unreleased - Added .VSCode settings for applying DSC PSSA rules - fixes [Issue #25](https://github.com/PlagueHO/FileContentDsc/issues/25). -- Added an Encoding parameter to the KeyValuePairFile and ReplaceText resources - fixes [Issue #5](https://github.com/PlagueHO/FileContentDsc/issues/5) +- Added an Encoding parameter to the KeyValuePairFile and ReplaceText + resources - fixes [Issue #5](https://github.com/PlagueHO/FileContentDsc/issues/5). ## 1.1.0.0 diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 index 1a35f6d..d52b3ce 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 @@ -133,7 +133,8 @@ function Get-TargetResource Defaults to $False. .PARAMETER Encoding - Specifies the file encoding. The default value is ASCII. + Specifies the file encoding. The default value is ASCII. Accepts the following values: + "ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown" #> function Set-TargetResource { @@ -180,6 +181,7 @@ function Set-TargetResource $IgnoreValueCase = $false, [Parameter()] + [ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown")] [String] $Encoding = 'Default' ) @@ -309,7 +311,8 @@ function Set-TargetResource Defaults to $False. .PARAMETER Encoding - Specifies the file encoding. The default value is ASCII. + Specifies the file encoding. The default value is ASCII. Accepts the following values: + "ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown" #> function Test-TargetResource { @@ -355,6 +358,7 @@ function Test-TargetResource $IgnoreValueCase = $false, [Parameter()] + [ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown")] [String] $Encoding ) @@ -489,7 +493,8 @@ function Test-TargetResource Defaults to $False. .PARAMETER Encoding - Specifies the file encoding. The default value is ASCII. + Specifies the file encoding. The default value is ASCII. Accepts the following values: + "ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown" #> function Assert-ParametersValid { @@ -534,6 +539,7 @@ function Assert-ParametersValid $IgnoreValueCase = $false, [Parameter()] + [ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown")] [String] $Encoding ) diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 index d1091f6..78cefef 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 @@ -110,7 +110,8 @@ function Get-TargetResource Specifies to append text to the file being modified. Adds the ability to add a configuration entry. .PARAMETER Encoding - Specifies the file encoding. The default value is ASCII. + Specifies the file encoding. The default value is ASCII. Accepts the following values: + "ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown" #> function Set-TargetResource { @@ -148,6 +149,7 @@ function Set-TargetResource $AllowAppend = $false, [Parameter()] + [ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown")] [String] $Encoding = 'Default' ) @@ -218,7 +220,8 @@ function Set-TargetResource Specifies to append text to the file being modified. Adds the ability to add a configuration entry. .PARAMETER Encoding - Specifies the file encoding. The default value is ASCII. + Specifies the file encoding. The default value is ASCII. Accepts the following values: + "ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown" #> function Test-TargetResource { @@ -255,6 +258,7 @@ function Test-TargetResource $AllowAppend = $false, [Parameter()] + [ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown")] [String] $Encoding = 'ASCII' ) @@ -357,7 +361,8 @@ function Test-TargetResource Only used when Type is set to 'Secret'. .PARAMETER Encoding - Specifies the file encoding. The default value is ASCII. + Specifies the file encoding. The default value is ASCII. Accepts the following values: + "ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown" #> function Assert-ParametersValid { @@ -393,6 +398,7 @@ function Assert-ParametersValid $AllowAppend = $false, [Parameter()] + [ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown")] [String] $Encoding ) From 052cd6a1614f078ef140db3a5c440c6c07ff9fa5 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Tue, 2 Oct 2018 23:22:22 -0400 Subject: [PATCH 19/46] test fix --- Tests/Unit/DSR_ReplaceText.Tests.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/Tests/Unit/DSR_ReplaceText.Tests.ps1 b/Tests/Unit/DSR_ReplaceText.Tests.ps1 index c306b67..1e70b76 100644 --- a/Tests/Unit/DSR_ReplaceText.Tests.ps1 +++ b/Tests/Unit/DSR_ReplaceText.Tests.ps1 @@ -602,7 +602,6 @@ Setting3.Test=Value4 -Search $script:testSearchNoFind ` -Text $script:testTextReplace ` -AllowAppend $true ` - -Encoding $script:fileEncodingParameters ` -Verbose } | Should -Not -Throw } From a80483cc635a95b19d3208ae25f43d04bbab99ee Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Wed, 3 Oct 2018 12:03:37 -0400 Subject: [PATCH 20/46] fix formatting --- .../DSR_KeyValuePairFile.psm1 | 51 +++++++++---------- .../DSR_KeyValuePairFile.schema.mof | 2 +- .../en-US/DSR_KeyValuePairFile.strings.psd1 | 2 +- .../DSR_ReplaceText/DSR_ReplaceText.psm1 | 47 ++++++++--------- .../DSR_ReplaceText.schema.mof | 2 +- .../en-US/DSR_ReplaceText.strings.psd1 | 2 +- .../FileContentDsc.Common.psm1 | 16 +++--- 7 files changed, 58 insertions(+), 64 deletions(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 index d52b3ce..99800b1 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 @@ -40,12 +40,12 @@ function Get-TargetResource ( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [String] + [System.String] $Path, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [String] + [System.String] $Name ) @@ -133,8 +133,7 @@ function Get-TargetResource Defaults to $False. .PARAMETER Encoding - Specifies the file encoding. The default value is ASCII. Accepts the following values: - "ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown" + Specifies the file encoding. The default value is ASCII. #> function Set-TargetResource { @@ -145,26 +144,26 @@ function Set-TargetResource ( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [String] + [System.String] $Path, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [String] + [System.String] $Name, [Parameter()] [ValidateSet('Present', 'Absent')] - [String] + [System.String] $Ensure = 'Present', [Parameter()] [ValidateSet('Text', 'Secret')] - [String] + [System.String] $Type = 'Text', [Parameter()] - [String] + [System.String] $Text, [Parameter()] @@ -182,7 +181,7 @@ function Set-TargetResource [Parameter()] [ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown")] - [String] + [System.String] $Encoding = 'Default' ) @@ -311,8 +310,7 @@ function Set-TargetResource Defaults to $False. .PARAMETER Encoding - Specifies the file encoding. The default value is ASCII. Accepts the following values: - "ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown" + Specifies the file encoding. The default value is ASCII. #> function Test-TargetResource { @@ -322,26 +320,26 @@ function Test-TargetResource ( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [String] + [System.String] $Path, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [String] + [System.String] $Name, [Parameter()] [ValidateSet('Present', 'Absent')] - [String] + [System.String] $Ensure = 'Present', [Parameter()] [ValidateSet('Text', 'Secret')] - [String] + [System.String] $Type = 'Text', [Parameter()] - [String] + [System.String] $Text, [Parameter()] @@ -359,7 +357,7 @@ function Test-TargetResource [Parameter()] [ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown")] - [String] + [System.String] $Encoding ) @@ -407,7 +405,7 @@ function Test-TargetResource if ($Encoding -eq $fileEncoding) { Write-Verbose -Message ($localizedData.KeyNotFoundAndShouldNotExistMessage -f ` - $Path, $Name) + $Path, $Name) } } # if } @@ -493,8 +491,7 @@ function Test-TargetResource Defaults to $False. .PARAMETER Encoding - Specifies the file encoding. The default value is ASCII. Accepts the following values: - "ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown" + Specifies the file encoding. The default value is ASCII. #> function Assert-ParametersValid { @@ -503,26 +500,26 @@ function Assert-ParametersValid ( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [String] + [System.String] $Path, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [String] + [System.String] $Name, [Parameter()] [ValidateSet('Present', 'Absent')] - [String] + [System.String] $Ensure = 'Present', [Parameter()] [ValidateSet('Text', 'Secret')] - [String] + [System.String] $Type = 'Text', [Parameter()] - [String] + [System.String] $Text, [Parameter()] @@ -540,7 +537,7 @@ function Assert-ParametersValid [Parameter()] [ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown")] - [String] + [System.String] $Encoding ) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof index b620485..3cb8e50 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof @@ -9,5 +9,5 @@ class DSR_KeyValuePairFile : OMI_BaseResource [write, Description("The secret text to replace the value with in the identified key. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret; [Write, Description("Ignore the case of the name of the key. Defaults to $False.")] Boolean IgnoreNameCase; [Write, Description("Ignore the case of any text or secret when determining if it they need to be updated. Defaults to $False.")] Boolean IgnoreValueCase; - [Write, Description("Specifies the file encoding."),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown"}] String Encoding; + [Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown"}] String Encoding; }; diff --git a/DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1 b/DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1 index 8436c2e..5eafcdd 100644 --- a/DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1 +++ b/DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1 @@ -15,5 +15,5 @@ ConvertFrom-StringData @' KeyFoundReplacementRequiredMessage = Key '{1}' found in file '{0}' and should exist but value(s) are not correct. Change required. KeyFoundButShouldNotExistMessage = Key '{1}' found in file '{0}' but should not exist. Change required. FileParentNotFoundError = File parent path '{0}' not found. - FileEncodingNotInDesiredState = File encoding is set to {0} but should be set to {1}, Change required. + FileEncodingNotInDesiredState = File encoding is set to '{0}' but should be set to '{1}', Change required. '@ diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 index 78cefef..7a5ff67 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 @@ -40,12 +40,12 @@ function Get-TargetResource ( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [String] + [System.String] $Path, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [String] + [System.String] $Search ) @@ -110,8 +110,7 @@ function Get-TargetResource Specifies to append text to the file being modified. Adds the ability to add a configuration entry. .PARAMETER Encoding - Specifies the file encoding. The default value is ASCII. Accepts the following values: - "ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown" + Specifies the file encoding. The default value is ASCII. #> function Set-TargetResource { @@ -122,21 +121,21 @@ function Set-TargetResource ( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [String] + [System.String] $Path, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [String] + [System.String] $Search, [Parameter()] [ValidateSet('Text', 'Secret')] - [String] + [System.String] $Type = 'Text', [Parameter()] - [String] + [System.String] $Text, [Parameter()] @@ -150,7 +149,7 @@ function Set-TargetResource [Parameter()] [ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown")] - [String] + [System.String] $Encoding = 'Default' ) @@ -220,8 +219,7 @@ function Set-TargetResource Specifies to append text to the file being modified. Adds the ability to add a configuration entry. .PARAMETER Encoding - Specifies the file encoding. The default value is ASCII. Accepts the following values: - "ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown" + Specifies the file encoding. The default value is ASCII. #> function Test-TargetResource { @@ -231,21 +229,21 @@ function Test-TargetResource ( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [String] + [System.String] $Path, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [String] + [System.String] $Search, [Parameter()] [ValidateSet('Text', 'Secret')] - [String] + [System.String] $Type = 'Text', [Parameter()] - [String] + [System.String] $Text, [Parameter()] @@ -259,7 +257,7 @@ function Test-TargetResource [Parameter()] [ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown")] - [String] + [System.String] $Encoding = 'ASCII' ) @@ -361,8 +359,7 @@ function Test-TargetResource Only used when Type is set to 'Secret'. .PARAMETER Encoding - Specifies the file encoding. The default value is ASCII. Accepts the following values: - "ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown" + Specifies the file encoding. The default value is ASCII. #> function Assert-ParametersValid { @@ -371,21 +368,21 @@ function Assert-ParametersValid ( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [String] + [System.String] $Path, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [String] + [System.String] $Search, [Parameter()] [ValidateSet('Text', 'Secret')] - [String] + [System.String] $Type = 'Text', [Parameter()] - [String] + [System.String] $Text, [Parameter()] @@ -399,7 +396,7 @@ function Assert-ParametersValid [Parameter()] [ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown")] - [String] + [System.String] $Encoding ) @@ -430,11 +427,11 @@ function Add-ConfigurationEntry param ( [Parameter(Mandatory = $true)] - [String] + [System.String] $FileContent, [Parameter(Mandatory = $true)] - [String] + [System.String] $Text ) diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof index ab2f0f5..f36e4eb 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof @@ -7,5 +7,5 @@ class DSR_ReplaceText : OMI_BaseResource [Write, Description("The text to replace the text identified by the RegEx. Only used when Type is set to 'Text'.")] String Text; [Write, Description("The secret text to replace the text identified by the RegEx. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret; [Write, Description("Specifies to append text to the file being modified. Adds the ability to add a configuration entry.")] Boolean AllowAppend; - [Write, Description("Specifies the file encoding."),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown"}] String Encoding; + [Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown"}] String Encoding; }; diff --git a/DSCResources/DSR_ReplaceText/en-US/DSR_ReplaceText.strings.psd1 b/DSCResources/DSR_ReplaceText/en-US/DSR_ReplaceText.strings.psd1 index 64a37cf..c99f0d9 100644 --- a/DSCResources/DSR_ReplaceText/en-US/DSR_ReplaceText.strings.psd1 +++ b/DSCResources/DSR_ReplaceText/en-US/DSR_ReplaceText.strings.psd1 @@ -10,5 +10,5 @@ ConvertFrom-StringData @' StringReplaceTextMessage = String replaced by '{1}' in file '{0}'. StringReplaceSecretMessage = String replaced by secret text in file '{0}'. FileParentNotFoundError = File parent path '{0}' not found. - FileEncodingNotInDesiredState = File encoding is set to {0} but should be set to {1}, Change required. + FileEncodingNotInDesiredState = File encoding is set to '{0}' but should be set to '{1}', Change required. '@ diff --git a/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 b/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 index 71c35be..f4dedfd 100644 --- a/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 +++ b/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 @@ -166,34 +166,34 @@ function Get-IniSettingFileValue function Get-FileEncoding { [CmdletBinding()] - Param + param ( [Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)] - [string] + [System.String] $Path ) - [byte[]]$byte = get-content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $Path + [byte[]]$byte = Get-Content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $Path if ( $byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf ) { - Write-Output 'UTF8' + return 'UTF8' } elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff) { - Write-Output 'Unicode' + return 'Unicode' } elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xfe -and $byte[3] -eq 0xff) { - Write-Output 'UTF32' + return 'UTF32' } elseif ($byte[0] -eq 0x2b -and $byte[1] -eq 0x2f -and $byte[2] -eq 0x76) { - Write-Output 'UTF7' + return 'UTF7' } else { - Write-Output 'ASCII' + return 'ASCII' } } From 3094b8f4510fdd90d79e19234fbeab3d343ae023 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Thu, 4 Oct 2018 08:27:50 -0400 Subject: [PATCH 21/46] update accepted encoding types --- DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 | 6 +++--- DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 | 6 +++--- DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof | 2 +- Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 | 4 ---- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 index 99800b1..faed834 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 @@ -180,7 +180,7 @@ function Set-TargetResource $IgnoreValueCase = $false, [Parameter()] - [ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown")] + [ValidateSet("ASCII", "Unicode", "UTF7", "UTF8")] [System.String] $Encoding = 'Default' ) @@ -356,7 +356,7 @@ function Test-TargetResource $IgnoreValueCase = $false, [Parameter()] - [ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown")] + [ValidateSet("ASCII", "Unicode", "UTF7", "UTF8")] [System.String] $Encoding ) @@ -536,7 +536,7 @@ function Assert-ParametersValid $IgnoreValueCase = $false, [Parameter()] - [ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown")] + [ValidateSet("ASCII", "Unicode", "UTF7", "UTF8")] [System.String] $Encoding ) diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 index 7a5ff67..49c5277 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 @@ -148,7 +148,7 @@ function Set-TargetResource $AllowAppend = $false, [Parameter()] - [ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown")] + [ValidateSet("ASCII", "Unicode", "UTF7", "UTF8")] [System.String] $Encoding = 'Default' ) @@ -256,7 +256,7 @@ function Test-TargetResource $AllowAppend = $false, [Parameter()] - [ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown")] + [ValidateSet("ASCII", "Unicode", "UTF7", "UTF8")] [System.String] $Encoding = 'ASCII' ) @@ -395,7 +395,7 @@ function Assert-ParametersValid $AllowAppend = $false, [Parameter()] - [ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown")] + [ValidateSet("ASCII", "Unicode", "UTF7", "UTF8")] [System.String] $Encoding ) diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof index f36e4eb..f58cd78 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof @@ -7,5 +7,5 @@ class DSR_ReplaceText : OMI_BaseResource [Write, Description("The text to replace the text identified by the RegEx. Only used when Type is set to 'Text'.")] String Text; [Write, Description("The secret text to replace the text identified by the RegEx. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret; [Write, Description("Specifies to append text to the file being modified. Adds the ability to add a configuration entry.")] Boolean AllowAppend; - [Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown"}] String Encoding; + [Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "Unicode", "UTF7", "UTF8"},Values{"ASCII", "Unicode", "UTF7", "UTF8"}] String Encoding; }; diff --git a/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 b/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 index f4dedfd..65c44d7 100644 --- a/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 +++ b/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 @@ -183,10 +183,6 @@ function Get-FileEncoding { return 'Unicode' } - elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xfe -and $byte[3] -eq 0xff) - { - return 'UTF32' - } elseif ($byte[0] -eq 0x2b -and $byte[1] -eq 0x2f -and $byte[2] -eq 0x76) { return 'UTF7' From a96e2c7f2f111eb0c732fbb3ffa0478dfe696b73 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Thu, 4 Oct 2018 09:56:05 -0400 Subject: [PATCH 22/46] no longer sets default encoding --- .../DSR_KeyValuePairFile.psm1 | 44 ++++++++++------- .../DSR_ReplaceText/DSR_ReplaceText.psm1 | 48 ++++++++++++------- 2 files changed, 57 insertions(+), 35 deletions(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 index faed834..0112994 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 @@ -182,7 +182,7 @@ function Set-TargetResource [Parameter()] [ValidateSet("ASCII", "Unicode", "UTF7", "UTF8")] [System.String] - $Encoding = 'Default' + $Encoding ) Assert-ParametersValid @PSBoundParameters @@ -244,15 +244,27 @@ function Set-TargetResource { if ($results.Count -eq 0) { - if ($Encoding -eq $fileEncoding) - { - # The Key does not exists and should not, and encoding is in the desired state, so don't do anything - return - } - else + if ($PSBoundParameters.ContainsKey('Encoding')) { - Write-Verbose -Message ($localizedData.FileEncodingNotInDesiredState -f ` - $fileEncoding, $Encoding) + if ($Encoding -eq $fileEncoding) + { + # The Key does not exists and should not, and encoding is in the desired state, so don't do anything + return + } + else + { + Write-Verbose -Message ($localizedData.FileEncodingNotInDesiredState -f ` + $fileEncoding, $Encoding) + + Set-Content ` + -Path $Path ` + -Value $fileContent ` + -Encoding $Encoding ` + -NoNewline ` + -Force + + return + } } } else @@ -273,7 +285,6 @@ function Set-TargetResource Set-Content ` -Path $Path ` -Value $fileContent ` - -Encoding $Encoding ` -NoNewline ` -Force } @@ -401,12 +412,9 @@ function Test-TargetResource } else { - # The key value pairs should exist and do and encoding is in desired state - if ($Encoding -eq $fileEncoding) - { - Write-Verbose -Message ($localizedData.KeyNotFoundAndShouldNotExistMessage -f ` - $Path, $Name) - } + # The key value pairs should exist and do + Write-Verbose -Message ($localizedData.KeyNotFoundAndShouldNotExistMessage -f ` + $Path, $Name) } # if } else @@ -446,11 +454,11 @@ function Test-TargetResource } # if } # if - if ($Encoding -ne $fileEncoding) + if ($PSBoundParameters.ContainsKey('Encoding') -and ($Encoding -ne $fileEncoding)) { # File encoding is not in desired state Write-Verbose -Message ($localizedData.FileEncodingNotInDesiredState -f ` - $fileEncoding, $Encoding) + $fileEncoding, $Encoding) $desiredConfigurationMatch = $false } diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 index 49c5277..c144c54 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 @@ -150,7 +150,7 @@ function Set-TargetResource [Parameter()] [ValidateSet("ASCII", "Unicode", "UTF7", "UTF8")] [System.String] - $Encoding = 'Default' + $Encoding ) Assert-ParametersValid @PSBoundParameters @@ -186,12 +186,23 @@ function Set-TargetResource $fileContent = $fileContent -Replace $Search, $Text } - Set-Content ` + if ($PSBoundParameters.ContainsKey('Encoding')) + { + Set-Content ` -Path $Path ` -Value $fileContent ` -Encoding $Encoding ` -NoNewline ` -Force + } + else + { + Set-Content ` + -Path $Path ` + -Value $fileContent ` + -NoNewline ` + -Force + } } <# @@ -258,7 +269,7 @@ function Test-TargetResource [Parameter()] [ValidateSet("ASCII", "Unicode", "UTF7", "UTF8")] [System.String] - $Encoding = 'ASCII' + $Encoding ) Assert-ParametersValid @PSBoundParameters @@ -288,21 +299,24 @@ function Test-TargetResource return $false } - if ($Encoding -eq $fileEncoding) - { - # No matches found and encoding is in desired state - Write-Verbose -Message ($localizedData.StringNotFoundMessage -f ` - $Path, $Search) - - return $true - } - else + if ($PSBoundParameters.ContainsKey('Encoding')) { - # No matches found and encoding is in desired state - Write-Verbose -Message ($localizedData.FileEncodingNotInDesiredState -f ` - $fileEncoding, $Encoding) - - return $false + if ($Encoding -eq $fileEncoding) + { + # No matches found and encoding is in desired state + Write-Verbose -Message ($localizedData.StringNotFoundMessage -f ` + $Path, $Search) + + return $true + } + else + { + # No matches found and encoding is in desired state + Write-Verbose -Message ($localizedData.FileEncodingNotInDesiredState -f ` + $fileEncoding, $Encoding) + + return $false + } } } From 47399f6f76b75341d2e595086dfc8bd02084fb70 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Thu, 4 Oct 2018 10:00:33 -0400 Subject: [PATCH 23/46] removed encoding default references --- DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 | 6 +++--- .../DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof | 2 +- DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 | 6 +++--- DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 index 0112994..468998b 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 @@ -133,7 +133,7 @@ function Get-TargetResource Defaults to $False. .PARAMETER Encoding - Specifies the file encoding. The default value is ASCII. + Specifies the file encoding. #> function Set-TargetResource { @@ -321,7 +321,7 @@ function Set-TargetResource Defaults to $False. .PARAMETER Encoding - Specifies the file encoding. The default value is ASCII. + Specifies the file encoding. #> function Test-TargetResource { @@ -499,7 +499,7 @@ function Test-TargetResource Defaults to $False. .PARAMETER Encoding - Specifies the file encoding. The default value is ASCII. + Specifies the file encoding. #> function Assert-ParametersValid { diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof index 3cb8e50..b620485 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof @@ -9,5 +9,5 @@ class DSR_KeyValuePairFile : OMI_BaseResource [write, Description("The secret text to replace the value with in the identified key. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret; [Write, Description("Ignore the case of the name of the key. Defaults to $False.")] Boolean IgnoreNameCase; [Write, Description("Ignore the case of any text or secret when determining if it they need to be updated. Defaults to $False.")] Boolean IgnoreValueCase; - [Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown"}] String Encoding; + [Write, Description("Specifies the file encoding."),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown"}] String Encoding; }; diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 index c144c54..d498569 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 @@ -110,7 +110,7 @@ function Get-TargetResource Specifies to append text to the file being modified. Adds the ability to add a configuration entry. .PARAMETER Encoding - Specifies the file encoding. The default value is ASCII. + Specifies the file encoding. #> function Set-TargetResource { @@ -230,7 +230,7 @@ function Set-TargetResource Specifies to append text to the file being modified. Adds the ability to add a configuration entry. .PARAMETER Encoding - Specifies the file encoding. The default value is ASCII. + Specifies the file encoding. #> function Test-TargetResource { @@ -373,7 +373,7 @@ function Test-TargetResource Only used when Type is set to 'Secret'. .PARAMETER Encoding - Specifies the file encoding. The default value is ASCII. + Specifies the file encoding. #> function Assert-ParametersValid { diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof index f58cd78..260fa6d 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof @@ -7,5 +7,5 @@ class DSR_ReplaceText : OMI_BaseResource [Write, Description("The text to replace the text identified by the RegEx. Only used when Type is set to 'Text'.")] String Text; [Write, Description("The secret text to replace the text identified by the RegEx. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret; [Write, Description("Specifies to append text to the file being modified. Adds the ability to add a configuration entry.")] Boolean AllowAppend; - [Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "Unicode", "UTF7", "UTF8"},Values{"ASCII", "Unicode", "UTF7", "UTF8"}] String Encoding; + [Write, Description("Specifies the file encoding."),ValueMap{"ASCII", "Unicode", "UTF7", "UTF8"},Values{"ASCII", "Unicode", "UTF7", "UTF8"}] String Encoding; }; From 62a329c3f440964dd72fb9a7fd6bf1e4dc70a15b Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Thu, 4 Oct 2018 15:27:31 -0400 Subject: [PATCH 24/46] valuemap update --- .../DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof index b620485..9e55910 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof @@ -9,5 +9,5 @@ class DSR_KeyValuePairFile : OMI_BaseResource [write, Description("The secret text to replace the value with in the identified key. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret; [Write, Description("Ignore the case of the name of the key. Defaults to $False.")] Boolean IgnoreNameCase; [Write, Description("Ignore the case of any text or secret when determining if it they need to be updated. Defaults to $False.")] Boolean IgnoreValueCase; - [Write, Description("Specifies the file encoding."),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "Default", "Byte", "String", "Unicode", "UTF7", "UTF8", "Unknown"}] String Encoding; + [Write, Description("Specifies the file encoding."),ValueMap{"ASCII", "Unicode", "UTF7", "UTF8"},Values{"ASCII", "Unicode", "UTF7", "UTF8"}] String Encoding; }; From 45dd581f82764c7eb3c500b93c1e8c79b4b79918 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Fri, 5 Oct 2018 10:46:51 -0400 Subject: [PATCH 25/46] test added for get-fileencoding --- .../FileContentDsc.Common.psm1 | 10 ++++--- Tests/Unit/FileContentDsc.Common.tests.ps1 | 27 +++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 b/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 index 65c44d7..b798e5d 100644 --- a/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 +++ b/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 @@ -179,13 +179,17 @@ function Get-FileEncoding { return 'UTF8' } + elseif ($byte[0] -eq 0xff -and $byte[1] -eq 0xfe) + { + return 'UTF32' + } elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff) { - return 'Unicode' + return 'BigEndianUnicode' } - elseif ($byte[0] -eq 0x2b -and $byte[1] -eq 0x2f -and $byte[2] -eq 0x76) + elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xfe -and $byte[3] -eq 0xff) { - return 'UTF7' + return 'BigEndianUTF32' } else { diff --git a/Tests/Unit/FileContentDsc.Common.tests.ps1 b/Tests/Unit/FileContentDsc.Common.tests.ps1 index c3d002f..bc94c05 100644 --- a/Tests/Unit/FileContentDsc.Common.tests.ps1 +++ b/Tests/Unit/FileContentDsc.Common.tests.ps1 @@ -55,6 +55,33 @@ try } } } + + Describe "$($script:ModuleName)\Get-FileEncoding" { + + $testTextFile = "TestDrive:\TestFile.txt" + $value = 'testText' + + $fileEncoding = @( + 'ASCII', + 'BigEndianUnicode', + 'BigEndianUTF32', + 'UTF8', + 'UTF32' + ) + + Context 'Check file encoding' { + + foreach ($file in $fileEncoding) + { + $encoding = $file + Set-Content $testTextFile -Value $value -Encoding $encoding + + It "should return $encoding" { + Get-FileEncoding -Path $testTextFile | Should -Be $encoding + } + } + } + } #endregion } finally From 5d8535698cc9fbaeab1fd44e09445aef94de9a43 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Fri, 5 Oct 2018 10:55:54 -0400 Subject: [PATCH 26/46] updates to reflect encoding support --- .../DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 | 12 ++++++------ .../DSR_KeyValuePairFile.schema.mof | 2 +- DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 | 12 ++++++------ .../DSR_ReplaceText/DSR_ReplaceText.schema.mof | 2 +- .../FileContentDsc.Common/FileContentDsc.Common.psm1 | 2 +- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 index 468998b..e6e064a 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 @@ -133,7 +133,7 @@ function Get-TargetResource Defaults to $False. .PARAMETER Encoding - Specifies the file encoding. + Specifies the file encoding. Defauts to ASCII. #> function Set-TargetResource { @@ -180,7 +180,7 @@ function Set-TargetResource $IgnoreValueCase = $false, [Parameter()] - [ValidateSet("ASCII", "Unicode", "UTF7", "UTF8")] + [ValidateSet("ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32")] [System.String] $Encoding ) @@ -321,7 +321,7 @@ function Set-TargetResource Defaults to $False. .PARAMETER Encoding - Specifies the file encoding. + Specifies the file encoding. Defauts to ASCII. #> function Test-TargetResource { @@ -367,7 +367,7 @@ function Test-TargetResource $IgnoreValueCase = $false, [Parameter()] - [ValidateSet("ASCII", "Unicode", "UTF7", "UTF8")] + [ValidateSet("ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32")] [System.String] $Encoding ) @@ -499,7 +499,7 @@ function Test-TargetResource Defaults to $False. .PARAMETER Encoding - Specifies the file encoding. + Specifies the file encoding. Defauts to ASCII. #> function Assert-ParametersValid { @@ -544,7 +544,7 @@ function Assert-ParametersValid $IgnoreValueCase = $false, [Parameter()] - [ValidateSet("ASCII", "Unicode", "UTF7", "UTF8")] + [ValidateSet("ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32")] [System.String] $Encoding ) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof index 9e55910..3a0ff79 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof @@ -9,5 +9,5 @@ class DSR_KeyValuePairFile : OMI_BaseResource [write, Description("The secret text to replace the value with in the identified key. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret; [Write, Description("Ignore the case of the name of the key. Defaults to $False.")] Boolean IgnoreNameCase; [Write, Description("Ignore the case of any text or secret when determining if it they need to be updated. Defaults to $False.")] Boolean IgnoreValueCase; - [Write, Description("Specifies the file encoding."),ValueMap{"ASCII", "Unicode", "UTF7", "UTF8"},Values{"ASCII", "Unicode", "UTF7", "UTF8"}] String Encoding; + [Write, Description("Specifies the file encoding."),ValueMap{"ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32"},Values{"ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32"}] String Encoding; }; diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 index d498569..fe9d238 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 @@ -110,7 +110,7 @@ function Get-TargetResource Specifies to append text to the file being modified. Adds the ability to add a configuration entry. .PARAMETER Encoding - Specifies the file encoding. + Specifies the file encoding. Defauts to ASCII. #> function Set-TargetResource { @@ -148,7 +148,7 @@ function Set-TargetResource $AllowAppend = $false, [Parameter()] - [ValidateSet("ASCII", "Unicode", "UTF7", "UTF8")] + [ValidateSet("ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32")] [System.String] $Encoding ) @@ -230,7 +230,7 @@ function Set-TargetResource Specifies to append text to the file being modified. Adds the ability to add a configuration entry. .PARAMETER Encoding - Specifies the file encoding. + Specifies the file encoding. Defauts to ASCII. #> function Test-TargetResource { @@ -267,7 +267,7 @@ function Test-TargetResource $AllowAppend = $false, [Parameter()] - [ValidateSet("ASCII", "Unicode", "UTF7", "UTF8")] + [ValidateSet("ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32")] [System.String] $Encoding ) @@ -373,7 +373,7 @@ function Test-TargetResource Only used when Type is set to 'Secret'. .PARAMETER Encoding - Specifies the file encoding. + Specifies the file encoding. Defauts to ASCII. #> function Assert-ParametersValid { @@ -409,7 +409,7 @@ function Assert-ParametersValid $AllowAppend = $false, [Parameter()] - [ValidateSet("ASCII", "Unicode", "UTF7", "UTF8")] + [ValidateSet("ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32")] [System.String] $Encoding ) diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof index 260fa6d..88180db 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof @@ -7,5 +7,5 @@ class DSR_ReplaceText : OMI_BaseResource [Write, Description("The text to replace the text identified by the RegEx. Only used when Type is set to 'Text'.")] String Text; [Write, Description("The secret text to replace the text identified by the RegEx. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret; [Write, Description("Specifies to append text to the file being modified. Adds the ability to add a configuration entry.")] Boolean AllowAppend; - [Write, Description("Specifies the file encoding."),ValueMap{"ASCII", "Unicode", "UTF7", "UTF8"},Values{"ASCII", "Unicode", "UTF7", "UTF8"}] String Encoding; + [Write, Description("Specifies the file encoding."),ValueMap{"ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32"},Values{"ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32"}] String Encoding; }; diff --git a/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 b/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 index b798e5d..b895ce1 100644 --- a/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 +++ b/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 @@ -148,7 +148,7 @@ function Get-IniSettingFileValue <# .SYNOPSIS - Gets file encoding. + Gets file encoding. Defaults to ASCII. .DESCRIPTION The Get-FileEncoding function determines encoding by looking at Byte Order Mark (BOM). From 4ed7eef85f65179dd25b9cc46941a07d4b7aec35 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Fri, 5 Oct 2018 12:15:14 -0400 Subject: [PATCH 27/46] fix typos --- DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 | 6 +++--- .../DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof | 2 +- DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 | 6 +++--- DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 index e6e064a..55f6ded 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 @@ -133,7 +133,7 @@ function Get-TargetResource Defaults to $False. .PARAMETER Encoding - Specifies the file encoding. Defauts to ASCII. + Specifies the file encoding. Defaults to ASCII. #> function Set-TargetResource { @@ -321,7 +321,7 @@ function Set-TargetResource Defaults to $False. .PARAMETER Encoding - Specifies the file encoding. Defauts to ASCII. + Specifies the file encoding. Defaults to ASCII. #> function Test-TargetResource { @@ -499,7 +499,7 @@ function Test-TargetResource Defaults to $False. .PARAMETER Encoding - Specifies the file encoding. Defauts to ASCII. + Specifies the file encoding. Defaults to ASCII. #> function Assert-ParametersValid { diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof index 3a0ff79..2e75108 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof @@ -9,5 +9,5 @@ class DSR_KeyValuePairFile : OMI_BaseResource [write, Description("The secret text to replace the value with in the identified key. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret; [Write, Description("Ignore the case of the name of the key. Defaults to $False.")] Boolean IgnoreNameCase; [Write, Description("Ignore the case of any text or secret when determining if it they need to be updated. Defaults to $False.")] Boolean IgnoreValueCase; - [Write, Description("Specifies the file encoding."),ValueMap{"ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32"},Values{"ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32"}] String Encoding; + [Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32"},Values{"ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32"}] String Encoding; }; diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 index fe9d238..ccec4a7 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 @@ -110,7 +110,7 @@ function Get-TargetResource Specifies to append text to the file being modified. Adds the ability to add a configuration entry. .PARAMETER Encoding - Specifies the file encoding. Defauts to ASCII. + Specifies the file encoding. Defaults to ASCII. #> function Set-TargetResource { @@ -230,7 +230,7 @@ function Set-TargetResource Specifies to append text to the file being modified. Adds the ability to add a configuration entry. .PARAMETER Encoding - Specifies the file encoding. Defauts to ASCII. + Specifies the file encoding. Defaults to ASCII. #> function Test-TargetResource { @@ -373,7 +373,7 @@ function Test-TargetResource Only used when Type is set to 'Secret'. .PARAMETER Encoding - Specifies the file encoding. Defauts to ASCII. + Specifies the file encoding. Defaults to ASCII. #> function Assert-ParametersValid { diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof index 88180db..3f2d00e 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof @@ -7,5 +7,5 @@ class DSR_ReplaceText : OMI_BaseResource [Write, Description("The text to replace the text identified by the RegEx. Only used when Type is set to 'Text'.")] String Text; [Write, Description("The secret text to replace the text identified by the RegEx. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret; [Write, Description("Specifies to append text to the file being modified. Adds the ability to add a configuration entry.")] Boolean AllowAppend; - [Write, Description("Specifies the file encoding."),ValueMap{"ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32"},Values{"ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32"}] String Encoding; + [Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32"},Values{"ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32"}] String Encoding; }; From 3ac02585e0026e6f7a66b6aae134871aeead0bbe Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Fri, 5 Oct 2018 12:26:16 -0400 Subject: [PATCH 28/46] fix typos --- .../DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof | 2 +- DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof index 2e75108..e5d63b1 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof @@ -9,5 +9,5 @@ class DSR_KeyValuePairFile : OMI_BaseResource [write, Description("The secret text to replace the value with in the identified key. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret; [Write, Description("Ignore the case of the name of the key. Defaults to $False.")] Boolean IgnoreNameCase; [Write, Description("Ignore the case of any text or secret when determining if it they need to be updated. Defaults to $False.")] Boolean IgnoreValueCase; - [Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32"},Values{"ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32"}] String Encoding; + [Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigIndianUnicode", "BigIndianUTF32", "UTF8", "UTF32"},Values{"ASCII", "BigIndianUnicode", "BigIndianUTF32", "UTF8", "UTF32"}] String Encoding; }; diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof index 3f2d00e..e852f36 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof @@ -7,5 +7,5 @@ class DSR_ReplaceText : OMI_BaseResource [Write, Description("The text to replace the text identified by the RegEx. Only used when Type is set to 'Text'.")] String Text; [Write, Description("The secret text to replace the text identified by the RegEx. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret; [Write, Description("Specifies to append text to the file being modified. Adds the ability to add a configuration entry.")] Boolean AllowAppend; - [Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32"},Values{"ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32"}] String Encoding; + [Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigIndianUnicode", "BigIndianUTF32", "UTF8", "UTF32"},Values{"ASCII", "BigIndianUnicode", "BigIndianUTF32", "UTF8", "UTF32"}] String Encoding; }; From bbff238940ce38b8f3b08be16367db95bea283d9 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Fri, 5 Oct 2018 12:35:14 -0400 Subject: [PATCH 29/46] typos --- DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 | 6 +++--- DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 index 55f6ded..a76c7d1 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 @@ -180,7 +180,7 @@ function Set-TargetResource $IgnoreValueCase = $false, [Parameter()] - [ValidateSet("ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32")] + [ValidateSet("ASCII", "BigIndianUnicode", "BigIndianUTF32", "UTF8", "UTF32")] [System.String] $Encoding ) @@ -367,7 +367,7 @@ function Test-TargetResource $IgnoreValueCase = $false, [Parameter()] - [ValidateSet("ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32")] + [ValidateSet("ASCII", "BigIndianUnicode", "BigIndianUTF32", "UTF8", "UTF32")] [System.String] $Encoding ) @@ -544,7 +544,7 @@ function Assert-ParametersValid $IgnoreValueCase = $false, [Parameter()] - [ValidateSet("ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32")] + [ValidateSet("ASCII", "BigIndianUnicode", "BigIndianUTF32", "UTF8", "UTF32")] [System.String] $Encoding ) diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 index ccec4a7..2aaaace 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 @@ -148,7 +148,7 @@ function Set-TargetResource $AllowAppend = $false, [Parameter()] - [ValidateSet("ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32")] + [ValidateSet("ASCII", "BigIndianUnicode", "BigIndianUTF32", "UTF8", "UTF32")] [System.String] $Encoding ) @@ -267,7 +267,7 @@ function Test-TargetResource $AllowAppend = $false, [Parameter()] - [ValidateSet("ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32")] + [ValidateSet("ASCII", "BigIndianUnicode", "BigIndianUTF32", "UTF8", "UTF32")] [System.String] $Encoding ) @@ -409,7 +409,7 @@ function Assert-ParametersValid $AllowAppend = $false, [Parameter()] - [ValidateSet("ASCII", "BigIndianUnicode", "BigIndianUnicode", "UTF8", "UTF32")] + [ValidateSet("ASCII", "BigIndianUnicode", "BigIndianUTF32", "UTF8", "UTF32")] [System.String] $Encoding ) From 71287c482e45818c08a66b03e62b2cf936aee2fe Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Mon, 8 Oct 2018 10:10:25 -0400 Subject: [PATCH 30/46] fix typos --- .../DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof | 2 +- DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof index e5d63b1..10a6110 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.schema.mof @@ -9,5 +9,5 @@ class DSR_KeyValuePairFile : OMI_BaseResource [write, Description("The secret text to replace the value with in the identified key. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret; [Write, Description("Ignore the case of the name of the key. Defaults to $False.")] Boolean IgnoreNameCase; [Write, Description("Ignore the case of any text or secret when determining if it they need to be updated. Defaults to $False.")] Boolean IgnoreValueCase; - [Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigIndianUnicode", "BigIndianUTF32", "UTF8", "UTF32"},Values{"ASCII", "BigIndianUnicode", "BigIndianUTF32", "UTF8", "UTF32"}] String Encoding; + [Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32"}] String Encoding; }; diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof index e852f36..c1ebb23 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.schema.mof @@ -7,5 +7,5 @@ class DSR_ReplaceText : OMI_BaseResource [Write, Description("The text to replace the text identified by the RegEx. Only used when Type is set to 'Text'.")] String Text; [Write, Description("The secret text to replace the text identified by the RegEx. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret; [Write, Description("Specifies to append text to the file being modified. Adds the ability to add a configuration entry.")] Boolean AllowAppend; - [Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigIndianUnicode", "BigIndianUTF32", "UTF8", "UTF32"},Values{"ASCII", "BigIndianUnicode", "BigIndianUTF32", "UTF8", "UTF32"}] String Encoding; + [Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32"}] String Encoding; }; From c9f8eeb6405b7abebe6ae4c509c3492a623ab1d1 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Mon, 8 Oct 2018 10:11:17 -0400 Subject: [PATCH 31/46] logic rework, add splat --- .../DSR_KeyValuePairFile.psm1 | 30 +++++------ .../DSR_ReplaceText/DSR_ReplaceText.psm1 | 52 ++++++++++--------- Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 | 1 - Tests/Unit/DSR_ReplaceText.Tests.ps1 | 40 +++++++++++--- 4 files changed, 75 insertions(+), 48 deletions(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 index a76c7d1..cde3afb 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 @@ -180,7 +180,7 @@ function Set-TargetResource $IgnoreValueCase = $false, [Parameter()] - [ValidateSet("ASCII", "BigIndianUnicode", "BigIndianUTF32", "UTF8", "UTF32")] + [ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32")] [System.String] $Encoding ) @@ -190,6 +190,12 @@ function Set-TargetResource $fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue $fileEncoding = Get-FileEncoding -Path $Path + $FileProperties = @{ + Path = $Path + NoNewline = $true + Force = $true + } + Write-Verbose -Message ($localizedData.SearchForKeyMessage -f ` $Path, $Name) @@ -256,14 +262,8 @@ function Set-TargetResource Write-Verbose -Message ($localizedData.FileEncodingNotInDesiredState -f ` $fileEncoding, $Encoding) - Set-Content ` - -Path $Path ` - -Value $fileContent ` - -Encoding $Encoding ` - -NoNewline ` - -Force - - return + # Add encoding parameter and value to the hashtable + $FileProperties.add('Encoding', $Encoding) } } } @@ -282,11 +282,9 @@ function Set-TargetResource $fileContent = '{0}={1}' -f $Name, $Text } # if - Set-Content ` - -Path $Path ` - -Value $fileContent ` - -NoNewline ` - -Force + $FileProperties.Add('Value', $fileContent) + + Set-Content @FileProperties } <# @@ -367,7 +365,7 @@ function Test-TargetResource $IgnoreValueCase = $false, [Parameter()] - [ValidateSet("ASCII", "BigIndianUnicode", "BigIndianUTF32", "UTF8", "UTF32")] + [ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32")] [System.String] $Encoding ) @@ -544,7 +542,7 @@ function Assert-ParametersValid $IgnoreValueCase = $false, [Parameter()] - [ValidateSet("ASCII", "BigIndianUnicode", "BigIndianUTF32", "UTF8", "UTF32")] + [ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32")] [System.String] $Encoding ) diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 index 2aaaace..5667a8f 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 @@ -148,7 +148,7 @@ function Set-TargetResource $AllowAppend = $false, [Parameter()] - [ValidateSet("ASCII", "BigIndianUnicode", "BigIndianUTF32", "UTF8", "UTF32")] + [ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32")] [System.String] $Encoding ) @@ -156,6 +156,13 @@ function Set-TargetResource Assert-ParametersValid @PSBoundParameters $fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue + $fileEncoding = Get-FileEncoding $Path + + $FileProperties = @{ + Path = $Path + NoNewline = $true + Force = $true + } if ($Type -eq 'Secret') { @@ -164,11 +171,22 @@ function Set-TargetResource $Text = $Secret.GetNetworkCredential().Password } - else + elseif ($PSBoundParameters.ContainsKey('Encoding')) { - Write-Verbose -Message ($localizedData.StringReplaceTextMessage -f ` + if ($Encoding -eq $fileEncoding) + { + Write-Verbose -Message ($localizedData.StringReplaceTextMessage -f ` + $Path, $Text) + } + else + { + Write-Verbose -Message ($localizedData.StringReplaceTextMessage -f ` $Path, $Text) - } # if + + # Add encoding parameter and value to the hashtable + $FileProperties.Add('Encoding', $Encoding) + } + } if ($null -eq $fileContent) { @@ -186,23 +204,9 @@ function Set-TargetResource $fileContent = $fileContent -Replace $Search, $Text } - if ($PSBoundParameters.ContainsKey('Encoding')) - { - Set-Content ` - -Path $Path ` - -Value $fileContent ` - -Encoding $Encoding ` - -NoNewline ` - -Force - } - else - { - Set-Content ` - -Path $Path ` - -Value $fileContent ` - -NoNewline ` - -Force - } + $FileProperties.Add('Value', $fileContent) + + Set-Content @FileProperties } <# @@ -267,7 +271,7 @@ function Test-TargetResource $AllowAppend = $false, [Parameter()] - [ValidateSet("ASCII", "BigIndianUnicode", "BigIndianUTF32", "UTF8", "UTF32")] + [ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32")] [System.String] $Encoding ) @@ -311,7 +315,7 @@ function Test-TargetResource } else { - # No matches found and encoding is in desired state + # No matches found but encoding is not in desired state Write-Verbose -Message ($localizedData.FileEncodingNotInDesiredState -f ` $fileEncoding, $Encoding) @@ -409,7 +413,7 @@ function Assert-ParametersValid $AllowAppend = $false, [Parameter()] - [ValidateSet("ASCII", "BigIndianUnicode", "BigIndianUTF32", "UTF8", "UTF32")] + [ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32")] [System.String] $Encoding ) diff --git a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 index 7688204..a61b107 100644 --- a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 +++ b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 @@ -651,7 +651,6 @@ $($script:testAddedName)=$($script:testText) -Path $script:testTextFile ` -Name $script:testName.ToUpper() ` -Encoding $script:fileEncodingParameters.Encoding ` - -Ensure 'Absent' ` -Verbose } | Should -Not -Throw } diff --git a/Tests/Unit/DSR_ReplaceText.Tests.ps1 b/Tests/Unit/DSR_ReplaceText.Tests.ps1 index 1e70b76..8c95831 100644 --- a/Tests/Unit/DSR_ReplaceText.Tests.ps1 +++ b/Tests/Unit/DSR_ReplaceText.Tests.ps1 @@ -326,13 +326,18 @@ Setting3.Test=Value4 -MockWith { $script:testFileContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileContent } ` + -Verifiable + Mock ` -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` ($value -eq $script:testFileExpectedTextContent) - } ` - -MockWith { $script:fileEncodingParameters.Encoding } ` + } ` -Verifiable It 'Should not throw an exception' { @@ -340,7 +345,6 @@ Setting3.Test=Value4 -Path $script:testTextFile ` -Search $script:testSearch ` -Text $script:testTextReplace ` - -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -359,7 +363,7 @@ Setting3.Test=Value4 -ParameterFilter { ($path -eq $script:testTextFile) -and ` ($value -eq $script:testFileExpectedTextContent) - } ` + } ` -Exactly 1 } } @@ -377,6 +381,12 @@ Setting3.Test=Value4 -MockWith { $script:testFileContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileContent } ` + -Verifiable + Mock ` -CommandName Set-Content ` -ParameterFilter { @@ -391,7 +401,6 @@ Setting3.Test=Value4 -Search $script:testSearchNoFind ` -Text $script:testTextReplaceNoFind ` -AllowAppend $true ` - -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -427,6 +436,12 @@ Setting3.Test=Value4 -MockWith { $script:testFileContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileContent } ` + -Verifiable + Mock ` -CommandName Set-Content ` -ParameterFilter { @@ -441,7 +456,6 @@ Setting3.Test=Value4 -Search $script:testSearchNoFind ` -Text $script:testTextReplaceNoFind ` -AllowAppend $false ` - -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -478,6 +492,12 @@ Setting3.Test=Value4 -MockWith { $script:testFileContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileContent } ` + -Verifiable + Mock ` -CommandName Set-Content ` -ParameterFilter { @@ -492,7 +512,6 @@ Setting3.Test=Value4 -Search $script:testSearch ` -Type 'Secret' ` -Secret $script:testSecretCredential ` - -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -529,6 +548,12 @@ Setting3.Test=Value4 -MockWith { $null } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileContent } ` + -Verifiable + Mock ` -CommandName Set-Content ` -ParameterFilter { @@ -564,6 +589,7 @@ Setting3.Test=Value4 -Exactly 1 } } + } #endregion From 55cbae07d62372759cc55f9a65393022eea2d6da Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Mon, 8 Oct 2018 11:35:32 -0400 Subject: [PATCH 32/46] minor fix --- Tests/Unit/DSR_ReplaceText.Tests.ps1 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Tests/Unit/DSR_ReplaceText.Tests.ps1 b/Tests/Unit/DSR_ReplaceText.Tests.ps1 index 8c95831..f0b4594 100644 --- a/Tests/Unit/DSR_ReplaceText.Tests.ps1 +++ b/Tests/Unit/DSR_ReplaceText.Tests.ps1 @@ -329,7 +329,7 @@ Setting3.Test=Value4 Mock ` -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileContent } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable Mock ` @@ -384,7 +384,7 @@ Setting3.Test=Value4 Mock ` -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileContent } ` + -MockWith { $script:testCompliantEncoding.encoding } ` -Verifiable Mock ` @@ -439,7 +439,7 @@ Setting3.Test=Value4 Mock ` -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileContent } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable Mock ` @@ -495,7 +495,7 @@ Setting3.Test=Value4 Mock ` -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileContent } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable Mock ` @@ -551,7 +551,7 @@ Setting3.Test=Value4 Mock ` -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileContent } ` + -MockWith { $script:testCompliantEncoding.encoding } ` -Verifiable Mock ` From 9a25d81487de552a686d8084e904d6dd1ec8a67c Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Mon, 8 Oct 2018 12:58:11 -0400 Subject: [PATCH 33/46] keyvalue pair test updates --- Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 | 454 +++++++++++----------- 1 file changed, 220 insertions(+), 234 deletions(-) diff --git a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 index a61b107..9c4a94c 100644 --- a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 +++ b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 @@ -111,7 +111,7 @@ $($script:testAddedName)=$($script:testText) #region Function Get-TargetResource Describe 'DSR_KeyValuePairFile\Get-TargetResource' { - Context 'File exists and contains matching key and encoding is in desired state' { + Context 'File exists and contains matching key' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -141,12 +141,11 @@ $($script:testAddedName)=$($script:testText) } It 'Should return expected values' { - $script:result.Path | Should -Be $script:testTextFile - $script:result.Name | Should -Be $script:testName - $script:result.Ensure | Should -Be 'Present' - $script:result.Type | Should -Be 'Text' - $script:result.Text | Should -Be "$($script:testText),$($script:testText),$($script:testText)" - $script:result.Encoding | Should -Be $script:fileEncodingParameters.Encoding + $script:result.Path | Should -Be $script:testTextFile + $script:result.Name | Should -Be $script:testName + $script:result.Ensure | Should -Be 'Present' + $script:result.Type | Should -Be 'Text' + $script:result.Text | Should -Be "$($script:testText),$($script:testText),$($script:testText)" } It 'Should call the expected mocks' { @@ -157,15 +156,10 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 } } - Context 'File exists and contains matching key but encoding is not in desired state' { + Context 'File exists and does not contain matching key' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -181,7 +175,7 @@ $($script:testAddedName)=$($script:testText) Mock ` -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable $script:result = $null @@ -189,18 +183,17 @@ $($script:testAddedName)=$($script:testText) It 'Should not throw an exception' { { $script:result = Get-TargetResource ` -Path $script:testTextFile ` - -Name $script:testName ` + -Name $script:testName.ToUpper() ` -Verbose } | Should -Not -Throw } It 'Should return expected values' { - $script:result.Path | Should -Be $script:testTextFile - $script:result.Name | Should -Be $script:testName - $script:result.Ensure | Should -Be 'Present' - $script:result.Type | Should -Be 'Text' - $script:result.Text | Should -Be "$($script:testText),$($script:testText),$($script:testText)" - $script:result.Encoding | Should -Be $script:testNonCompliantEncoding.Encoding + $script:result.Path | Should -Be $script:testTextFile + $script:result.Name | Should -Be $script:testName + $script:result.Ensure | Should -Be 'Absent' + $script:result.Type | Should -Be 'Text' + $script:result.Text | Should -BeNullOrEmpty } It 'Should call the expected mocks' { @@ -211,15 +204,14 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 } } + } + #endregion - Context 'File exists and does not contain matching key and encoding is in desired state' { + #region Function Set-TargetResource + Describe 'DSR_KeyValuePairFile\Set-TargetResource' { + Context 'File does not exist' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -229,7 +221,7 @@ $($script:testAddedName)=$($script:testText) Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileExpectedTextContent } ` + -MockWith { $null } ` -Verifiable Mock ` @@ -238,79 +230,24 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable - $script:result = $null - - It 'Should not throw an exception' { - { $script:result = Get-TargetResource ` - -Path $script:testTextFile ` - -Name $script:testName.ToUpper() ` - -Verbose - } | Should -Not -Throw - } - - It 'Should return expected values' { - $script:result.Path | Should -Be $script:testTextFile - $script:result.Name | Should -Be $script:testName - $script:result.Ensure | Should -Be 'Absent' - $script:result.Type | Should -Be 'Text' - $script:result.Text | Should -BeNullOrEmpty - $script:result.Encoding | Should -Be $script:fileEncodingParameters.Encoding - } - - It 'Should call the expected mocks' { - Assert-VerifiableMock - Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - } - } - - Context 'File exists and does not contain matching key and encoding is not in desired state' { - # verifiable (should be called) mocks - Mock ` - -CommandName Assert-ParametersValid ` - -ModuleName 'DSR_KeyValuePairFile' ` - -Verifiable - - Mock ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileExpectedTextContent } ` - -Verifiable - Mock ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -CommandName Set-Content ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($value -eq "$script:testName=$script:testText") + } ` -Verifiable - $script:result = $null - It 'Should not throw an exception' { - { $script:result = Get-TargetResource ` + { Set-TargetResource ` -Path $script:testTextFile ` - -Name $script:testName.ToUpper() ` + -Name $script:testName ` + -Ensure 'Present' ` + -Text $script:testText ` -Verbose } | Should -Not -Throw } - It 'Should return expected values' { - $script:result.Path | Should -Be $script:testTextFile - $script:result.Name | Should -Be $script:testName - $script:result.Ensure | Should -Be 'Absent' - $script:result.Type | Should -Be 'Text' - $script:result.Text | Should -BeNullOrEmpty - $script:result.Encoding | Should -Be $script:testNonCompliantEncoding.Encoding - } - It 'Should call the expected mocks' { Assert-VerifiableMock Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 @@ -321,17 +258,16 @@ $($script:testAddedName)=$($script:testText) -Exactly 1 Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` + -CommandName Set-Content ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($value -eq "$script:testName=$script:testText") + } ` -Exactly 1 } } - } - #endregion - #region Function Set-TargetResource - Describe 'DSR_KeyValuePairFile\Set-TargetResource' { - Context 'File does not exist' { + Context 'File exists and contains matching key that should exist' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -341,50 +277,20 @@ $($script:testAddedName)=$($script:testText) Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $null } ` + -MockWith { $script:testFileContent } ` -Verifiable Mock ` -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $null } ` - -Verifiable - - It 'Should not throw an exception' { - { Set-TargetResource ` - -Path $script:testTextFile ` - -Name $script:testName ` - -Ensure 'Present' ` - -Text $script:testText ` - -Encoding $script:fileEncodingParameters.Encoding ` - -Verbose - } | Should -Not -Throw - } - - It 'Should call the expected mocks' { - Assert-VerifiableMock - Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 - } - } - - Context 'File exists and contains matching key that should exist' { - # verifiable (should be called) mocks - Mock ` - -CommandName Assert-ParametersValid ` - -ModuleName 'DSR_KeyValuePairFile' ` - -Verifiable - - Mock ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileContent } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable Mock ` -CommandName Set-Content ` -ParameterFilter { ($path -eq $script:testTextFile) -and ` - ($value -eq $script:testFileExpectedTextContent) ` + ($value -eq $script:testFileExpectedTextContent) } ` -Verifiable @@ -430,6 +336,12 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + Mock ` -CommandName Set-Content ` -ParameterFilter { @@ -468,7 +380,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists and does not contain matching key but key should exist' { + Context 'File exists does not contain matching key but key should exist' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -481,6 +393,12 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + Mock ` -CommandName Set-Content ` -ParameterFilter { @@ -531,6 +449,12 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + Mock ` -CommandName Set-Content ` -ParameterFilter { @@ -569,7 +493,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists and does not contain key with matching case and should not and encoding is in desired state' { + Context 'File exists and does not contain key with matching case and should not' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -596,7 +520,6 @@ $($script:testAddedName)=$($script:testText) { Set-TargetResource ` -Path $script:testTextFile ` -Name $script:testName.ToUpper() ` - -Encoding $script:fileEncodingParameters.Encoding ` -Ensure 'Absent' ` -Verbose } | Should -Not -Throw @@ -613,16 +536,11 @@ $($script:testAddedName)=$($script:testText) Assert-MockCalled ` -CommandName Set-Content ` - -Exactly 0 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 } } - Context 'File exists and does not contain key with matching case and should not but encoding in not in desired state' { + Context 'File exists and contains key with a different case but should not and IgnoreNameCase is True' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -638,54 +556,7 @@ $($script:testAddedName)=$($script:testText) Mock ` -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testNonCompliantEncoding.Encoding - } ` - -Verifiable - - # non-verifiable mocks - Mock ` - -CommandName Set-Content - - It 'Should not throw an exception' { - { Set-TargetResource ` - -Path $script:testTextFile ` - -Name $script:testName.ToUpper() ` - -Encoding $script:fileEncodingParameters.Encoding ` - -Verbose - } | Should -Not -Throw - } - - It 'Should call the expected mocks' { - Assert-VerifiableMock - Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - - Assert-MockCalled ` - -CommandName Set-Content ` - -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 - } - } - - Context 'File exists and contains key with a different case but should not and IgnoreNameCase is True' { - # verifiable (should be called) mocks - Mock ` - -CommandName Assert-ParametersValid ` - -ModuleName 'DSR_KeyValuePairFile' ` - -Verifiable - - Mock ` - -CommandName Get-Content ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileContent } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable Mock ` @@ -748,6 +619,12 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` @@ -773,7 +650,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists and does not contain matching key and should not and encoding is in desired state ' { + Context 'File exists and does not contain matching key but should not' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -820,11 +697,6 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 } } @@ -857,13 +729,13 @@ $($script:testAddedName)=$($script:testText) { $script:result = Test-TargetResource ` -Path $script:testTextFile ` -Name $script:testName.ToUpper() ` - -Ensure 'Absent' ` -Encoding $script:fileEncodingParameters.Encoding ` + -Ensure 'Absent' ` -Verbose } | Should -Not -Throw } - It 'Should return true' { + It 'Should return false' { $script:result | Should -Be $false } @@ -875,11 +747,6 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 } } @@ -914,7 +781,6 @@ $($script:testAddedName)=$($script:testText) -Name $script:testName ` -Ensure 'Present' ` -Text $script:testText ` - -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -934,6 +800,57 @@ $($script:testAddedName)=$($script:testText) } } + Context 'File exists and contains matching key that should exist and values match but encoding is not in desired state' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_KeyValuePairFile' ` + -Verifiable + + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_KeyValuePairFile' ` + -MockWith { $true } ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileExpectedTextContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -Verifiable + + It 'Should not throw an exception' { + { $script:result = Test-TargetResource ` + -Path $script:testTextFile ` + -Name $script:testName ` + -Ensure 'Present' ` + -Text $script:testText ` + -Encoding $script:fileEncodingParameters.Encoding ` + -Verbose + } | Should -Not -Throw + } + + It 'Should return false' { + $script:result | Should -Be $false + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + Context 'File exists and contains matching key that should exist and values do not match secret text' { # verifiable (should be called) mocks Mock ` @@ -953,6 +870,12 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` @@ -1011,7 +934,6 @@ $($script:testAddedName)=$($script:testText) -Ensure 'Present' ` -Type 'Secret' ` -Secret $script:testSecretCredential ` - -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -1028,15 +950,10 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 } } - Context 'File exists and contains matching key that should exist and values match but are different case and IgnoreValueCase is False' { + Context 'File exists and contains matching key that should exist and values match secret text but encoding is not in desired state' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -1052,7 +969,13 @@ $($script:testAddedName)=$($script:testText) Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testFileExpectedTextContent } ` + -MockWith { $script:testFileExpectedSecretContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testNonCompliantEncoding.Encoding } ` -Verifiable It 'Should not throw an exception' { @@ -1060,7 +983,9 @@ $($script:testAddedName)=$($script:testText) -Path $script:testTextFile ` -Name $script:testName ` -Ensure 'Present' ` - -Text $script:testText.ToUpper() ` + -Type 'Secret' ` + -Secret $script:testSecretCredential ` + -Encoding $script:fileEncodingParameters.Encoding ` -Verbose } | Should -Not -Throw } @@ -1080,7 +1005,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists and contains matching key that should exist and values match but are different case and IgnoreValueCase is True' { + Context 'File exists and contains key with different case that should exist and values match and IgnoreNameCase is True' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -1107,13 +1032,12 @@ $($script:testAddedName)=$($script:testText) It 'Should not throw an exception' { { $script:result = Test-TargetResource ` - -Path $script:testTextFile ` - -Name $script:testName ` - -Ensure 'Present' ` - -Text $script:testText.ToUpper() ` - -Encoding $script:fileEncodingParameters.Encoding ` - -IgnoreValueCase:$true ` - -Verbose + -Path $script:testTextFile ` + -Name $script:testName.ToUpper() ` + -Ensure 'Present' ` + -Text $script:testText ` + -IgnoreNameCase:$true ` + -Verbose } | Should -Not -Throw } @@ -1129,15 +1053,10 @@ $($script:testAddedName)=$($script:testText) -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 - - Assert-MockCalled ` - -CommandName Get-FileEncoding ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -Exactly 1 } } - Context 'File exists and contains matching key that should not exist' { + Context 'File exists and contains matching key that should exist and values match but are different case and IgnoreValueCase is False' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` @@ -1156,12 +1075,18 @@ $($script:testAddedName)=$($script:testText) -MockWith { $script:testFileExpectedTextContent } ` -Verifiable + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable + It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` -Name $script:testName ` - -Ensure 'Absent' ` - -Text $script:testText ` + -Ensure 'Present' ` + -Text $script:testText.ToUpper() ` -Verbose } | Should -Not -Throw } @@ -1181,26 +1106,88 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists and encoding is not in desired state' { + Context 'File exists and contains matching key that should exist and values match but are different case and IgnoreValueCase is True' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_KeyValuePairFile' ` + -MockWith { $true } ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileExpectedTextContent } ` + -Verifiable + Mock ` -CommandName Get-FileEncoding ` -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testNonCompliantEncoding.Encoding } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` -Verifiable - $script:result = $null + It 'Should not throw an exception' { + { $script:result = Test-TargetResource ` + -Path $script:testTextFile ` + -Name $script:testName ` + -Ensure 'Present' ` + -Text $script:testText.ToUpper() ` + -IgnoreValueCase:$true ` + -Verbose + } | Should -Not -Throw + } + + It 'Should return true' { + $script:result | Should -Be $true + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + } + } + + Context 'File exists and contains matching key that should not exist' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_KeyValuePairFile' ` + -Verifiable + + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_KeyValuePairFile' ` + -MockWith { $true } ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileExpectedTextContent } ` + -Verifiable + + Mock ` + -CommandName Get-FileEncoding ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testCompliantEncoding.Encoding } ` + -Verifiable It 'Should not throw an exception' { { $script:result = Test-TargetResource ` -Path $script:testTextFile ` -Name $script:testName ` - -Encoding $script:fileEncodingParameters.Encoding ` + -Ensure 'Absent' ` + -Text $script:testText ` -Verbose } | Should -Not -Throw } @@ -1214,13 +1201,12 @@ $($script:testAddedName)=$($script:testText) Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 Assert-MockCalled ` - -CommandName Get-FileEncoding ` + -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` -Exactly 1 } } } - #endregion #region Function Assert-ParametersValid From df69f300eeed7ee32366edec160f2917dab2c005 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Fri, 12 Oct 2018 12:14:16 -0400 Subject: [PATCH 34/46] fix formatting --- .../DSR_KeyValuePairFile.psm1 | 6 +- .../FileContentDsc.Common.psm1 | 2 +- Tests/Unit/FileContentDsc.Common.tests.ps1 | 89 +++++++++---------- 3 files changed, 47 insertions(+), 50 deletions(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 index cde3afb..661a370 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 @@ -190,7 +190,7 @@ function Set-TargetResource $fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue $fileEncoding = Get-FileEncoding -Path $Path - $FileProperties = @{ + $fileProperties = @{ Path = $Path NoNewline = $true Force = $true @@ -263,7 +263,7 @@ function Set-TargetResource $fileEncoding, $Encoding) # Add encoding parameter and value to the hashtable - $FileProperties.add('Encoding', $Encoding) + $fileProperties.add('Encoding', $Encoding) } } } @@ -282,7 +282,7 @@ function Set-TargetResource $fileContent = '{0}={1}' -f $Name, $Text } # if - $FileProperties.Add('Value', $fileContent) + $fileProperties.Add('Value', $fileContent) Set-Content @FileProperties } diff --git a/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 b/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 index b895ce1..c90f986 100644 --- a/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 +++ b/Modules/FileContentDsc.Common/FileContentDsc.Common.psm1 @@ -175,7 +175,7 @@ function Get-FileEncoding [byte[]]$byte = Get-Content -Encoding byte -ReadCount 4 -TotalCount 4 -Path $Path - if ( $byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf ) + if ($byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf) { return 'UTF8' } diff --git a/Tests/Unit/FileContentDsc.Common.tests.ps1 b/Tests/Unit/FileContentDsc.Common.tests.ps1 index bc94c05..ad99b97 100644 --- a/Tests/Unit/FileContentDsc.Common.tests.ps1 +++ b/Tests/Unit/FileContentDsc.Common.tests.ps1 @@ -24,64 +24,61 @@ try $LocalizedData } - Describe "$($script:ModuleName)\Get-TextEolCharacter" { - - $textNoNewLine = 'NoNewLine' - $textCRLFOnly = "CRLFOnly`r`n" - $textCROnly = "CROnly`r" - $textBoth = "CRLFLine`r`nCRLine`r" - - Context 'text with no new line' { - It 'should return CRLF' { - Get-TextEolCharacter -Text $textNoNewLine | Should -Be "`r`n" - } + Describe "$($script:ModuleName)\Get-TextEolCharacter" { + $textNoNewLine = 'NoNewLine' + $textCRLFOnly = "CRLFOnly`r`n" + $textCROnly = "CROnly`r" + $textBoth = "CRLFLine`r`nCRLine`r" + + Context 'text with no new line' { + It 'should return CRLF' { + Get-TextEolCharacter -Text $textNoNewLine | Should -Be "`r`n" } + } - Context 'text with CRLF only' { - It 'should return CRLF' { - Get-TextEolCharacter -Text $textCRLFOnly | Should -Be "`r`n" - } + Context 'text with CRLF only' { + It 'should return CRLF' { + Get-TextEolCharacter -Text $textCRLFOnly | Should -Be "`r`n" } + } - Context 'text with CR only' { - It 'should return CR' { - Get-TextEolCharacter -Text $textCROnly | Should -Be "`r" - } + Context 'text with CR only' { + It 'should return CR' { + Get-TextEolCharacter -Text $textCROnly | Should -Be "`r" } + } - Context 'text with both CR and CRLF' { - It 'should return CRLF' { - Get-TextEolCharacter -Text $textBoth | Should -Be "`r`n" - } + Context 'text with both CR and CRLF' { + It 'should return CRLF' { + Get-TextEolCharacter -Text $textBoth | Should -Be "`r`n" } } + } - Describe "$($script:ModuleName)\Get-FileEncoding" { - - $testTextFile = "TestDrive:\TestFile.txt" - $value = 'testText' - - $fileEncoding = @( - 'ASCII', - 'BigEndianUnicode', - 'BigEndianUTF32', - 'UTF8', - 'UTF32' - ) - - Context 'Check file encoding' { - - foreach ($file in $fileEncoding) - { - $encoding = $file - Set-Content $testTextFile -Value $value -Encoding $encoding - - It "should return $encoding" { - Get-FileEncoding -Path $testTextFile | Should -Be $encoding - } + Describe "$($script:ModuleName)\Get-FileEncoding" { + $testTextFile = "TestDrive:\TestFile.txt" + $value = 'testText' + + $fileEncoding = @( + 'ASCII', + 'BigEndianUnicode', + 'BigEndianUTF32', + 'UTF8', + 'UTF32' + ) + + Context 'When checking file encoding' { + foreach ($file in $fileEncoding) + { + $encoding = $file + Set-Content $testTextFile -Value $value -Encoding $encoding + + It "should return $encoding" { + Get-FileEncoding -Path $testTextFile | Should -Be $encoding } } } + } #endregion } finally From 1e80495a46ae8dcf887785e768abf0fd0fd50a72 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Fri, 12 Oct 2018 21:20:05 -0400 Subject: [PATCH 35/46] using -TestCases instead of foreach --- Tests/Unit/FileContentDsc.Common.tests.ps1 | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/Tests/Unit/FileContentDsc.Common.tests.ps1 b/Tests/Unit/FileContentDsc.Common.tests.ps1 index ad99b97..5124cf4 100644 --- a/Tests/Unit/FileContentDsc.Common.tests.ps1 +++ b/Tests/Unit/FileContentDsc.Common.tests.ps1 @@ -58,24 +58,13 @@ try Describe "$($script:ModuleName)\Get-FileEncoding" { $testTextFile = "TestDrive:\TestFile.txt" $value = 'testText' - - $fileEncoding = @( - 'ASCII', - 'BigEndianUnicode', - 'BigEndianUTF32', - 'UTF8', - 'UTF32' - ) + $encoding = @{encoding = 'ASCII'}, @{encoding = 'BigEndianUnicode'}, @{encoding = 'BigEndianUTF32'}, @{encoding = 'UTF8'}, @{encoding = 'UTF32'} Context 'When checking file encoding' { - foreach ($file in $fileEncoding) - { - $encoding = $file + It "encoding is and should return " -TestCases $encoding { + param($encoding) Set-Content $testTextFile -Value $value -Encoding $encoding - - It "should return $encoding" { - Get-FileEncoding -Path $testTextFile | Should -Be $encoding - } + Get-FileEncoding -Path $testTextFile | Should -Be $encoding } } } From e675a97c3d57ae2c5adf7b313862ffd4351e239b Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Mon, 15 Oct 2018 16:10:05 -0400 Subject: [PATCH 36/46] bug fix to ensure encoding gets set --- DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 index 661a370..332e011 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 @@ -224,6 +224,10 @@ function Set-TargetResource # The key value pair should exist $keyValuePair = '{0}={1}{2}' -f $Name, $Text, $eolChars + if ($PSBoundParameters.ContainsKey('Encoding') -and ($Encoding -ne $fileEncoding)) + { + $fileProperties.Add('Encoding', $Encoding) + } if ($results.Count -eq 0) { # The key value pair was not found so add it to the end of the file @@ -244,7 +248,7 @@ function Set-TargetResource Write-Verbose -Message ($localizedData.KeyUpdateMessage -f ` $Path, $Name) - } # if + }#> # if } else { From 0ffde9b3b92fad962ded3ad0e6bf8b8d10d36109 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Mon, 15 Oct 2018 16:10:47 -0400 Subject: [PATCH 37/46] fix formatting --- .../DSR_ReplaceText/DSR_ReplaceText.psm1 | 8 ++++---- Tests/Unit/FileContentDsc.Common.tests.ps1 | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 index 5667a8f..f766831 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 @@ -158,7 +158,7 @@ function Set-TargetResource $fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue $fileEncoding = Get-FileEncoding $Path - $FileProperties = @{ + $fileproperties = @{ Path = $Path NoNewline = $true Force = $true @@ -184,7 +184,7 @@ function Set-TargetResource $Path, $Text) # Add encoding parameter and value to the hashtable - $FileProperties.Add('Encoding', $Encoding) + $fileproperties.Add('Encoding', $Encoding) } } @@ -204,9 +204,9 @@ function Set-TargetResource $fileContent = $fileContent -Replace $Search, $Text } - $FileProperties.Add('Value', $fileContent) + $fileproperties.Add('Value', $fileContent) - Set-Content @FileProperties + Set-Content @fileproperties } <# diff --git a/Tests/Unit/FileContentDsc.Common.tests.ps1 b/Tests/Unit/FileContentDsc.Common.tests.ps1 index 5124cf4..5c02df5 100644 --- a/Tests/Unit/FileContentDsc.Common.tests.ps1 +++ b/Tests/Unit/FileContentDsc.Common.tests.ps1 @@ -58,7 +58,23 @@ try Describe "$($script:ModuleName)\Get-FileEncoding" { $testTextFile = "TestDrive:\TestFile.txt" $value = 'testText' - $encoding = @{encoding = 'ASCII'}, @{encoding = 'BigEndianUnicode'}, @{encoding = 'BigEndianUTF32'}, @{encoding = 'UTF8'}, @{encoding = 'UTF32'} + $encoding = @( + @{ + encoding = 'ASCII' + }, + @{ + encoding = 'BigEndianUnicode' + }, + @{ + encoding = 'BigEndianUTF32' + }, + @{ + encoding = 'UTF8' + }, + @{ + encoding = 'UTF32' + } + ) Context 'When checking file encoding' { It "encoding is and should return " -TestCases $encoding { From 7508d814ab7c4474bef62bf3abb09d0efaba50df Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Mon, 15 Oct 2018 16:11:18 -0400 Subject: [PATCH 38/46] integration test updates --- ...DSR_KeyValuePairFile.Integration.Tests.ps1 | 81 +++++++++++++++++++ .../DSR_KeyValuePairFile.config.ps1 | 12 +++ .../DSR_ReplaceText.Integration.Tests.ps1 | 79 ++++++++++++++++++ Tests/Integration/DSR_ReplaceText.config.ps1 | 11 +++ 4 files changed, 183 insertions(+) diff --git a/Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 b/Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 index cb7b640..bc0963b 100644 --- a/Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 +++ b/Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 @@ -16,6 +16,7 @@ if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCR Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force Import-Module (Join-Path -Path $script:moduleRoot -ChildPath "$($script:DSCModuleName).psd1") -Force +Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'Modules\FileContentDsc.Common') -Force $TestEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:DSCModuleName ` -DSCResourceName $script:DSCResourceName ` @@ -34,6 +35,16 @@ try $script:testSecureSecret = ConvertTo-SecureString -String $script:testSecret -AsPlainText -Force $script:testSecretCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ('Dummy', $script:testSecureSecret) + $script:fileEncodingParameters = @{ + Path = $script:testTextFile + Encoding = 'ASCII' + } + + $script:testNonCompliantEncoding = @{ + Path = $script:fileEncodingParameters.Path + Encoding = 'UTF8' + } + $script:testFileContent = @" Setting1=Value1 $($script:testName)=Value 2 @@ -269,6 +280,76 @@ Setting3.Test=Value4 } } } + + Context 'A text file that requires encoding be changed' { + BeforeAll { + # Create the text file to use for testing + Set-Content ` + -Path $script:testTextFile ` + -Value $script:testFileContent ` + -Encoding $script:testNonCompliantEncoding.Encoding ` + -NoNewline ` + -Force + } + + #region DEFAULT TESTS + It 'Should compile and apply the MOF without throwing' { + { + $configData = @{ + AllNodes = @( + @{ + NodeName = 'localhost' + Path = $script:testTextFile + Name = $script:testName + Ensure = 'Present' + Type = 'Text' + Text = $script:testText + Encoding = $script:fileEncodingParameters.Encoding + } + ) + } + + & $script:configurationName ` + -OutputPath $TestDrive ` + -ConfigurationData $configData + + Start-DscConfiguration ` + -Path $TestDrive ` + -ComputerName localhost ` + -Wait ` + -Verbose ` + -Force ` + -ErrorAction Stop + } | Should -Not -Throw + } + + It 'Should be able to call Get-DscConfiguration without throwing' { + { $script:currentDscConfig = Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -throw + } + + It 'Should have set the resource and all the parameters should match' { + $script:current = $script:currentDscConfig | Where-Object { + $_.ConfigurationName -eq $script:configurationName + } + $current.Path | Should -Be $script:testTextFile + $current.Name | Should -Be $script:testName + $current.Ensure | Should -Be 'Present' + $current.Type | Should -Be 'Text' + $current.Text | Should -Be "$($script:testText),$($script:testText),$($script:testText)" + $current.Encoding | Should -Be $script:fileEncodingParameters.Encoding + } + + It 'Should convert file encoding to the expected type' { + Get-FileEncoding -Path $script:testTextFile | Should -Be $script:fileEncodingParameters.Encoding + } + + AfterAll { + if (Test-Path -Path $script:testTextFile) + { + Remove-Item -Path $script:testTextFile -Force + } + } + } } } finally diff --git a/Tests/Integration/DSR_KeyValuePairFile.config.ps1 b/Tests/Integration/DSR_KeyValuePairFile.config.ps1 index a30af36..b7db524 100644 --- a/Tests/Integration/DSR_KeyValuePairFile.config.ps1 +++ b/Tests/Integration/DSR_KeyValuePairFile.config.ps1 @@ -30,6 +30,18 @@ Configuration $ConfigurationName Text = $Node.Text } } + elseif (($Node.Type -eq 'Text') -and ($Node.Encoding -eq 'ASCII')) + { + KeyValuePairFile KeyValuePairFileIntegrationTest + { + Path = $Node.Path + Name = $Node.Name + Ensure = $Node.Ensure + Type = $Node.Type + Text = $Node.Text + Encoding = $Node.Encoding + } + } else { KeyValuePairFile KeyValuePairFileIntegrationTest diff --git a/Tests/Integration/DSR_ReplaceText.Integration.Tests.ps1 b/Tests/Integration/DSR_ReplaceText.Integration.Tests.ps1 index acf9be0..75331bd 100644 --- a/Tests/Integration/DSR_ReplaceText.Integration.Tests.ps1 +++ b/Tests/Integration/DSR_ReplaceText.Integration.Tests.ps1 @@ -16,6 +16,7 @@ if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCR Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force Import-Module (Join-Path -Path $script:moduleRoot -ChildPath "$($script:DSCModuleName).psd1") -Force +Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'Modules\FileContentDsc.Common') -Force $TestEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:DSCModuleName ` -DSCResourceName $script:DSCResourceName ` @@ -36,6 +37,16 @@ try $script:testSecureSecretReplace = ConvertTo-SecureString -String $script:testSecretReplace -AsPlainText -Force $script:testSecretCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ('Dummy', $script:testSecureSecretReplace) + $script:fileEncodingParameters = @{ + Path = $script:testTextFile + Encoding = 'ASCII' + } + + $script:testNonCompliantEncoding = @{ + Path = $script:fileEncodingParameters.Path + Encoding = 'UTF8' + } + $script:testFileContent = @" Setting1=Value1 Setting.Two='Value2' @@ -196,6 +207,74 @@ Setting3.Test=Value4 } } } + + Context 'A text file that requires encoding be changed' { + BeforeAll { + # Create the text file to use for testing + Set-Content ` + -Path $script:testTextFile ` + -Value $script:testFileContent ` + -Encoding $script:testNonCompliantEncoding.Encoding ` + -NoNewline ` + -Force + } + + #region DEFAULT TESTS + It 'Should compile and apply the MOF without throwing' { + { + $configData = @{ + AllNodes = @( + @{ + NodeName = 'localhost' + Path = $script:testTextFile + Search = $script:testSearch + Type = 'Text' + Text = $script:testTextReplace + Encoding = $script:fileEncodingParameters.Encoding + } + ) + } + + & $script:configurationName ` + -OutputPath $TestDrive ` + -ConfigurationData $configData + + Start-DscConfiguration ` + -Path $TestDrive ` + -ComputerName localhost ` + -Wait ` + -Verbose ` + -Force ` + -ErrorAction Stop + } | Should -Not -Throw + } + + It 'Should be able to call Get-DscConfiguration without throwing' { + { $script:currentDscConfig = Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -throw + } + + It 'Should have set the resource and all the parameters should match' { + $script:current = $script:currentDscConfig | Where-Object { + $_.ConfigurationName -eq $script:configurationName + } + $current.Path | Should -Be $script:testTextFile + $current.Search | Should -Be $script:testSearch + $current.Type | Should -Be 'Text' + $current.Text | Should -Be "$($script:testTextReplace),$($script:testTextReplace),$($script:testTextReplace)" + $current.Encoding | Should -Be $script:fileEncodingParameters.Encoding + } + + It 'Should convert file encoding to the expected type' { + Get-FileEncoding -Path $script:testTextFile | Should -Be $script:fileEncodingParameters.Encoding + } + + AfterAll { + if (Test-Path -Path $script:testTextFile) + { + Remove-Item -Path $script:testTextFile -Force + } + } + } } } finally diff --git a/Tests/Integration/DSR_ReplaceText.config.ps1 b/Tests/Integration/DSR_ReplaceText.config.ps1 index 4639099..4fc4141 100644 --- a/Tests/Integration/DSR_ReplaceText.config.ps1 +++ b/Tests/Integration/DSR_ReplaceText.config.ps1 @@ -20,6 +20,17 @@ Configuration $ConfigurationName Text = $Node.Text } } + elseif (($Node.Type -eq 'Text') -and ($Node.Encoding -eq 'ASCII')) + { + ReplaceText ReplaceTextIntegrationTest + { + Path = $Node.Path + Search = $Node.Search + Type = $Node.Type + Text = $Node.Text + Encoding = $Node.Encoding + } + } else { ReplaceText ReplaceTextIntegrationTest From 676f3f16395ba3445a6a3fe250e540f9b4fca70a Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Mon, 15 Oct 2018 16:15:14 -0400 Subject: [PATCH 39/46] fix formatting --- .../DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 | 2 +- DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 index 332e011..fc7166b 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 @@ -288,7 +288,7 @@ function Set-TargetResource $fileProperties.Add('Value', $fileContent) - Set-Content @FileProperties + Set-Content @fileProperties } <# diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 index f766831..269ff4b 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 @@ -158,7 +158,7 @@ function Set-TargetResource $fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue $fileEncoding = Get-FileEncoding $Path - $fileproperties = @{ + $fileProperties = @{ Path = $Path NoNewline = $true Force = $true @@ -184,7 +184,7 @@ function Set-TargetResource $Path, $Text) # Add encoding parameter and value to the hashtable - $fileproperties.Add('Encoding', $Encoding) + $fileProperties.Add('Encoding', $Encoding) } } @@ -204,9 +204,9 @@ function Set-TargetResource $fileContent = $fileContent -Replace $Search, $Text } - $fileproperties.Add('Value', $fileContent) + $fileProperties.Add('Value', $fileContent) - Set-Content @fileproperties + Set-Content @fileProperties } <# From 9bfb63cb5b455f877fbf5f8d21aa9c9a04f2cd4b Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Mon, 15 Oct 2018 16:28:01 -0400 Subject: [PATCH 40/46] formatting --- Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 | 2 +- Tests/Integration/DSR_ReplaceText.Integration.Tests.ps1 | 2 +- Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 | 2 +- Tests/Unit/DSR_ReplaceText.Tests.ps1 | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 b/Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 index bc0963b..32f492b 100644 --- a/Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 +++ b/Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 @@ -36,7 +36,7 @@ try $script:testSecretCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ('Dummy', $script:testSecureSecret) $script:fileEncodingParameters = @{ - Path = $script:testTextFile + Path = $script:testTextFile Encoding = 'ASCII' } diff --git a/Tests/Integration/DSR_ReplaceText.Integration.Tests.ps1 b/Tests/Integration/DSR_ReplaceText.Integration.Tests.ps1 index 75331bd..a0ad97f 100644 --- a/Tests/Integration/DSR_ReplaceText.Integration.Tests.ps1 +++ b/Tests/Integration/DSR_ReplaceText.Integration.Tests.ps1 @@ -38,7 +38,7 @@ try $script:testSecretCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ('Dummy', $script:testSecureSecretReplace) $script:fileEncodingParameters = @{ - Path = $script:testTextFile + Path = $script:testTextFile Encoding = 'ASCII' } diff --git a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 index 9c4a94c..c847fef 100644 --- a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 +++ b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 @@ -41,7 +41,7 @@ try $script:testSecretCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ('Dummy', $script:testSecureSecret) $script:fileEncodingParameters = @{ - Path = $script:testTextFile + Path = $script:testTextFile Encoding = 'ASCII' } diff --git a/Tests/Unit/DSR_ReplaceText.Tests.ps1 b/Tests/Unit/DSR_ReplaceText.Tests.ps1 index f0b4594..83ddbfc 100644 --- a/Tests/Unit/DSR_ReplaceText.Tests.ps1 +++ b/Tests/Unit/DSR_ReplaceText.Tests.ps1 @@ -43,7 +43,7 @@ try $script:testSecretCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ('Dummy', $script:testSecureSecretReplace) $script:fileEncodingParameters = @{ - Path = $script:testTextFile + Path = $script:testTextFile Encoding = 'ASCII' } From 8e21388d0eb3a0306b528d2833aa4baab2c2856c Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Tue, 16 Oct 2018 08:04:23 -0400 Subject: [PATCH 41/46] removing module import --- Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 | 1 - Tests/Integration/DSR_ReplaceText.Integration.Tests.ps1 | 1 - 2 files changed, 2 deletions(-) diff --git a/Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 b/Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 index 32f492b..ea5f853 100644 --- a/Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 +++ b/Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 @@ -16,7 +16,6 @@ if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCR Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force Import-Module (Join-Path -Path $script:moduleRoot -ChildPath "$($script:DSCModuleName).psd1") -Force -Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'Modules\FileContentDsc.Common') -Force $TestEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:DSCModuleName ` -DSCResourceName $script:DSCResourceName ` diff --git a/Tests/Integration/DSR_ReplaceText.Integration.Tests.ps1 b/Tests/Integration/DSR_ReplaceText.Integration.Tests.ps1 index a0ad97f..c45b0b1 100644 --- a/Tests/Integration/DSR_ReplaceText.Integration.Tests.ps1 +++ b/Tests/Integration/DSR_ReplaceText.Integration.Tests.ps1 @@ -16,7 +16,6 @@ if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCR Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force Import-Module (Join-Path -Path $script:moduleRoot -ChildPath "$($script:DSCModuleName).psd1") -Force -Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'Modules\FileContentDsc.Common') -Force $TestEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:DSCModuleName ` -DSCResourceName $script:DSCResourceName ` From 448038d46199e4fc61f04de9233fab6eb56a0fe4 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Tue, 16 Oct 2018 15:55:20 -0400 Subject: [PATCH 42/46] readding module import --- Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 | 1 + Tests/Integration/DSR_ReplaceText.Integration.Tests.ps1 | 1 + 2 files changed, 2 insertions(+) diff --git a/Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 b/Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 index ea5f853..32f492b 100644 --- a/Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 +++ b/Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 @@ -16,6 +16,7 @@ if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCR Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force Import-Module (Join-Path -Path $script:moduleRoot -ChildPath "$($script:DSCModuleName).psd1") -Force +Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'Modules\FileContentDsc.Common') -Force $TestEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:DSCModuleName ` -DSCResourceName $script:DSCResourceName ` diff --git a/Tests/Integration/DSR_ReplaceText.Integration.Tests.ps1 b/Tests/Integration/DSR_ReplaceText.Integration.Tests.ps1 index c45b0b1..a0ad97f 100644 --- a/Tests/Integration/DSR_ReplaceText.Integration.Tests.ps1 +++ b/Tests/Integration/DSR_ReplaceText.Integration.Tests.ps1 @@ -16,6 +16,7 @@ if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCR Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force Import-Module (Join-Path -Path $script:moduleRoot -ChildPath "$($script:DSCModuleName).psd1") -Force +Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'Modules\FileContentDsc.Common') -Force $TestEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:DSCModuleName ` -DSCResourceName $script:DSCResourceName ` From 472a94f1b8259f1d35ba484ec22c9ca3fd0bf174 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Thu, 25 Oct 2018 09:08:07 -0400 Subject: [PATCH 43/46] minor formatting, logic, and test updates --- .../DSR_KeyValuePairFile.psm1 | 24 +++++++++---------- ...DSR_KeyValuePairFile.Integration.Tests.ps1 | 1 + Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 | 5 ++-- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 index fc7166b..43ad799 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 @@ -228,6 +228,7 @@ function Set-TargetResource { $fileProperties.Add('Encoding', $Encoding) } + if ($results.Count -eq 0) { # The key value pair was not found so add it to the end of the file @@ -248,27 +249,24 @@ function Set-TargetResource Write-Verbose -Message ($localizedData.KeyUpdateMessage -f ` $Path, $Name) - }#> # if + } # if } else { if ($results.Count -eq 0) { - if ($PSBoundParameters.ContainsKey('Encoding')) + if ($PSBoundParameters.ContainsKey('Encoding') -and ($Encoding -eq $fileEncoding)) { - if ($Encoding -eq $fileEncoding) - { # The Key does not exists and should not, and encoding is in the desired state, so don't do anything return - } - else - { - Write-Verbose -Message ($localizedData.FileEncodingNotInDesiredState -f ` - $fileEncoding, $Encoding) - - # Add encoding parameter and value to the hashtable - $fileProperties.add('Encoding', $Encoding) - } + } + else + { + Write-Verbose -Message ($localizedData.FileEncodingNotInDesiredState -f ` + $fileEncoding, $Encoding) + + # Add encoding parameter and value to the hashtable + $fileProperties.add('Encoding', $Encoding) } } else diff --git a/Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 b/Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 index 32f492b..4755df3 100644 --- a/Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 +++ b/Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 @@ -16,6 +16,7 @@ if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCR Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force Import-Module (Join-Path -Path $script:moduleRoot -ChildPath "$($script:DSCModuleName).psd1") -Force +# Import the helper module for the Get-FileEncoding function Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'Modules\FileContentDsc.Common') -Force $TestEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:DSCModuleName ` diff --git a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 index c847fef..c9f1dc8 100644 --- a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 +++ b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 @@ -520,6 +520,7 @@ $($script:testAddedName)=$($script:testText) { Set-TargetResource ` -Path $script:testTextFile ` -Name $script:testName.ToUpper() ` + -Encoding $script:fileEncodingParameters.Encoding ` -Ensure 'Absent' ` -Verbose } | Should -Not -Throw @@ -536,7 +537,7 @@ $($script:testAddedName)=$($script:testText) Assert-MockCalled ` -CommandName Set-Content ` - -Exactly 1 + -Exactly 0 } } @@ -650,7 +651,7 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File exists and does not contain matching key but should not' { + Context 'File exists and does not contain matching key and should not' { # verifiable (should be called) mocks Mock ` -CommandName Assert-ParametersValid ` From 294d9d0969091215680f3714f78d1c5e43ed7df9 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Thu, 25 Oct 2018 09:14:46 -0400 Subject: [PATCH 44/46] comment update --- Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 b/Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 index 4755df3..ce658ea 100644 --- a/Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 +++ b/Tests/Integration/DSR_KeyValuePairFile.Integration.Tests.ps1 @@ -16,7 +16,7 @@ if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCR Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force Import-Module (Join-Path -Path $script:moduleRoot -ChildPath "$($script:DSCModuleName).psd1") -Force -# Import the helper module for the Get-FileEncoding function +# Helper module import required for the Get-FileEncoding function Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'Modules\FileContentDsc.Common') -Force $TestEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:DSCModuleName ` From b411148cca1b65f72e196558b836ba4fbcf760b5 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Thu, 25 Oct 2018 12:23:16 -0400 Subject: [PATCH 45/46] add logic to address ASCII default --- .../DSR_KeyValuePairFile.psm1 | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 index 43ad799..2ac1f28 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 @@ -224,11 +224,6 @@ function Set-TargetResource # The key value pair should exist $keyValuePair = '{0}={1}{2}' -f $Name, $Text, $eolChars - if ($PSBoundParameters.ContainsKey('Encoding') -and ($Encoding -ne $fileEncoding)) - { - $fileProperties.Add('Encoding', $Encoding) - } - if ($results.Count -eq 0) { # The key value pair was not found so add it to the end of the file @@ -264,9 +259,6 @@ function Set-TargetResource { Write-Verbose -Message ($localizedData.FileEncodingNotInDesiredState -f ` $fileEncoding, $Encoding) - - # Add encoding parameter and value to the hashtable - $fileProperties.add('Encoding', $Encoding) } } else @@ -286,6 +278,13 @@ function Set-TargetResource $fileProperties.Add('Value', $fileContent) + # Verify encoding is not set to the passed parameter or the default of ASCII + if ($PSBoundParameters.ContainsKey('Encoding') -and ($Encoding -ne ($fileEncoding -or 'ASCII'))) + { + # Add encoding parameter and value to the hashtable + $fileProperties.Add('Encoding', $Encoding) + } + Set-Content @fileProperties } From 2f586f2e64b4a7311f6c160a9540c0553e618e29 Mon Sep 17 00:00:00 2001 From: nehrua <36175222+nehrua@users.noreply.github.com> Date: Fri, 26 Oct 2018 09:42:55 -0400 Subject: [PATCH 46/46] added comment --- Tests/Integration/DSR_ReplaceText.Integration.Tests.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/Tests/Integration/DSR_ReplaceText.Integration.Tests.ps1 b/Tests/Integration/DSR_ReplaceText.Integration.Tests.ps1 index a0ad97f..ebfdc2f 100644 --- a/Tests/Integration/DSR_ReplaceText.Integration.Tests.ps1 +++ b/Tests/Integration/DSR_ReplaceText.Integration.Tests.ps1 @@ -16,6 +16,7 @@ if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCR Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force Import-Module (Join-Path -Path $script:moduleRoot -ChildPath "$($script:DSCModuleName).psd1") -Force +# Helper module import required for the Get-FileEncoding function Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'Modules\FileContentDsc.Common') -Force $TestEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:DSCModuleName `