Skip to content

Commit 7c38512

Browse files
committed
More code fixes
1 parent ee69b94 commit 7c38512

File tree

4 files changed

+71
-15
lines changed

4 files changed

+71
-15
lines changed

DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ function Get-TargetResource
103103
.PARAMETER Secret
104104
The secret text to replace the text identified by the RegEx.
105105
Only used when Type is set to 'Secret'.
106+
107+
.PARAMETER AllowAppend
108+
Specifies to append text to the file being modified. Adds the ability to add a configuration entry.
106109
#>
107110
function Set-TargetResource
108111
{
@@ -200,6 +203,9 @@ function Set-TargetResource
200203
.PARAMETER Secret
201204
The secret text to replace the text identified by the RegEx.
202205
Only used when Type is set to 'Secret'.
206+
207+
.PARAMETER AllowAppend
208+
Specifies to append text to the file being modified. Adds the ability to add a configuration entry.
203209
#>
204210
function Test-TargetResource
205211
{
@@ -374,7 +380,7 @@ function Assert-ParametersValid
374380
The existing file content of the configuration file.
375381
376382
.PARAMETER Text
377-
The text to replace the text identifed by the RegEx.
383+
The text to append to the end of the FileContent.
378384
#>
379385
function Add-ConfigurationEntry
380386
{
@@ -391,17 +397,22 @@ function Add-ConfigurationEntry
391397
$Text
392398
)
393399

394-
$stringBuilder = New-Object -TypeName System.Text.StringBuilder
395-
396-
$fileContentArray = $FileContent.Trim() -split '\n'
397-
398-
foreach ($line in $fileContentArray)
400+
if ($FileContent -match '\n$' -and $FileContent -notmatch '\r\n$')
399401
{
400-
# Lets remove the return character since AppendLine() always adds one
401-
$null = $stringBuilder.AppendLine($line -replace '\r')
402+
# default Linux line ending
403+
$detectedNewLineFormat = "`n"
402404
}
405+
else
406+
{
407+
# default Windows line ending
408+
$detectedNewLineFormat = "`r`n"
409+
}
410+
411+
$stringBuilder = New-Object -TypeName System.Text.StringBuilder
403412

404-
$null = $stringBuilder.AppendLine($Text)
413+
$null = $stringBuilder.Append($FileContent)
414+
$null = $stringBuilder.Append($Text)
415+
$null = $stringBuilder.Append($detectedNewLineFormat)
405416

406417
return $stringBuilder.ToString()
407418
}

TestFile.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Setting1=Value1
2+
Setting.Two='Value2'
3+
Setting.Two='Value3'
4+
Setting.Two='TestText'
5+
Setting3.Test=Value4
6+
7+
Setting.NotExist='TestText'Setting1=Value1
8+
Setting.Two='Value2'
9+
Setting.Two='Value3'
10+
Setting.Two='TestText'
11+
Setting3.Test=Value4
12+
13+
Setting.NotExist='TestText'Setting1=Value1
14+
Setting.Two='Value2'
15+
Setting.Two='Value3'
16+
Setting.Two='TestText'
17+
Setting3.Test=Value4
18+
19+
Setting.NotExist='TestText'Setting1=Value1
20+
Setting.Two='Value2'
21+
Setting.Two='Value3'
22+
Setting.Two='TestText'
23+
Setting3.Test=Value4
24+
25+
Setting.NotExist='TestText'

Tests/Unit/DSR_ReplaceText.Tests.ps1

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,32 @@ Setting3.Test=Value4
814814
$result | Should -Be $script:testFileExpectedTextContentNewKey
815815
}
816816
}
817+
818+
Context 'Apply a LF (default Linux)' {
819+
$linuxString = "Line1`nLine2`n"
820+
821+
$result = Add-ConfigurationEntry `
822+
-Text 'Line3' `
823+
-FileContent $linuxString
824+
825+
It 'Should end with a LF' {
826+
$result -match '\n$' | Should -BeTrue
827+
$result -match '\b\r\n$' | Should -BeFalse
828+
}
829+
}
830+
831+
Context 'Apply a CRLF (default Windows)' {
832+
$windowsString = "Line1`r`nLine2`r`n"
833+
834+
$result = Add-ConfigurationEntry `
835+
-Text 'Line3' `
836+
-FileContent $windowsString
837+
838+
It 'Should match a CRLF line ending' {
839+
$result -match '\r\n$' | Should -BeTrue
840+
$result -match '\b\n$' | Should -BeFalse
841+
}
842+
}
817843
}
818844
}
819845
}

Tests/Unit/TestFile.txt

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)