Skip to content

Commit 47f4e87

Browse files
committed
Updated unit tests
1 parent a506e6e commit 47f4e87

3 files changed

Lines changed: 105 additions & 3 deletions

File tree

DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ function Set-TargetResource
161161
elseif ( [regex]::Matches($fileContent, $Search).Count -eq 0 )
162162
{
163163
# configuration file exists but Text does not exist so lets add it
164-
$fileContent = "{0}`n`r{1}" -f $fileContent, $Text
164+
$fileContent = Add-ConfigurationEntry -FileContent $fileContent -Text $Text
165165
}
166166
else
167167
{
@@ -172,9 +172,8 @@ function Set-TargetResource
172172
Set-Content `
173173
-Path $Path `
174174
-Value $fileContent `
175+
-NoNewline `
175176
-Force
176-
#-NoNewline `
177-
178177
}
179178

180179
<#
@@ -346,4 +345,41 @@ function Assert-ParametersValid
346345
} # if
347346
}
348347

348+
<#
349+
.SYNOPSIS
350+
Uses the stringBuilder class to append a configuration entry to the existing file content.
351+
352+
.PARAMETER FileContent
353+
The existing file content of the configuration file.
354+
355+
.PARAMETER Text
356+
The text to replace the text identifed by the RegEx.
357+
#>
358+
function Add-ConfigurationEntry
359+
{
360+
param
361+
(
362+
[Parameter()]
363+
[string]
364+
$FileContent,
365+
366+
[Parameter()]
367+
[string]
368+
$Text
369+
)
370+
371+
$stringBuilder = New-Object System.Text.StringBuilder
372+
373+
$fileContentArray = $FileContent.Trim() -split '\n'
374+
375+
foreach ($line in $fileContentArray)
376+
{
377+
$null = $stringBuilder.AppendLine($line.Trim())
378+
}
379+
380+
$null = $stringBuilder.AppendLine($Text)
381+
382+
return $stringBuilder.ToString()
383+
}
384+
349385
Export-ModuleMember -Function *-TargetResource

Tests/Unit/DSR_ReplaceText.Tests.ps1

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ try
3636
$script:testSecret = 'TestSecret'
3737
$script:testSearch = "Setting\.Two='(.)*'"
3838
$script:testSearchNoFind = "Setting.NotExist='(.)*'"
39+
$script:testTextReplaceNoFind = "Setting.NotExist='$($script:testText)'"
3940
$script:testTextReplace = "Setting.Two='$($script:testText)'"
4041
$script:testSecretReplace = "Setting.Two='$($script:testSecret)'"
4142
$script:testSecureSecretReplace = ConvertTo-SecureString -String $script:testSecretReplace -AsPlainText -Force
@@ -57,6 +58,16 @@ Setting.Two='$($script:testText)'
5758
Setting.Two='$($script:testText)'
5859
Setting3.Test=Value4
5960
61+
"@
62+
63+
$script:testFileExpectedTextContentNewKey = @"
64+
Setting1=Value1
65+
Setting.Two='Value2'
66+
Setting.Two='Value3'
67+
Setting.Two='$($script:testText)'
68+
Setting3.Test=Value4
69+
Setting.NotExist='$($script:testText)'
70+
6071
"@
6172

6273
$script:testFileExpectedSecretContent = @"
@@ -206,6 +217,55 @@ Setting3.Test=Value4
206217
}
207218
}
208219

220+
Context 'File exists and search text can not be found'{
221+
# verifiable (should be called) mocks
222+
Mock `
223+
-CommandName Assert-ParametersValid `
224+
-ModuleName 'DSR_ReplaceText' `
225+
-Verifiable
226+
227+
Mock `
228+
-CommandName Get-Content `
229+
-ParameterFilter { $path -eq $script:testTextFile } `
230+
-MockWith { $script:testFileContent } `
231+
-Verifiable
232+
233+
Mock `
234+
-CommandName Set-Content `
235+
-ParameterFilter {
236+
($path -eq $script:testTextFile) -and `
237+
($value -eq $script:testFileExpectedTextContentNewKey)
238+
} `
239+
-Verifiable
240+
241+
It 'Should not throw an exception' {
242+
{ $script:result = Set-TargetResource `
243+
-Path $script:testTextFile `
244+
-Search $script:testSearchNoFind `
245+
-Text $script:testTextReplaceNoFind `
246+
-Verbose
247+
} | Should -Not -Throw
248+
}
249+
250+
It 'Should call the expected mocks' {
251+
Assert-VerifiableMock
252+
Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1
253+
254+
Assert-MockCalled `
255+
-CommandName Get-Content `
256+
-ParameterFilter { $path -eq $script:testTextFile } `
257+
-Exactly 1
258+
259+
Assert-MockCalled `
260+
-CommandName Set-Content `
261+
-ParameterFilter {
262+
($path -eq $script:testTextFile) -and `
263+
($value -eq $script:testFileExpectedTextContentNewKey)
264+
} `
265+
-Exactly 1
266+
}
267+
}
268+
209269
Context 'File exists and search secret can be found' {
210270
# verifiable (should be called) mocks
211271
Mock `

Tests/Unit/TestFile.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Setting1=Value1
2+
Setting.Two='Value2'
3+
Setting.Two='Value3'
4+
Setting.Two='TestText'
5+
Setting3.Test=Value4Setting.NotExist='TestText'
6+

0 commit comments

Comments
 (0)