Skip to content

Commit cd55d9d

Browse files
Unit tests almost completed
1 parent d61e97f commit cd55d9d

5 files changed

Lines changed: 715 additions & 24 deletions

File tree

Modules/FileContentDsc/DSCResources/MSFT_KeyValuePairFile/MSFT_KeyValuePairFile.psm1

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,29 @@ function Get-TargetResource
6060
$regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline
6161

6262
# Search the key that matches the requested key
63-
$results = [regex]::Matches($fileContent, "^[\s]*$Name=(.*)$", $regExOptions)
63+
$results = [regex]::Matches($fileContent, "^[\s]*$Name=([^\n\r]*)", $regExOptions)
64+
65+
$ensure = 'Absent'
66+
$text = $null
6467

6568
if ($results.Count -eq 0)
6669
{
6770
# No matches found
68-
$ensure = 'Absent'
69-
$text = ''
70-
7171
Write-Verbose -Message ($localizedData.KeyNotFoundMessage -f `
7272
$Path, $Name)
7373
}
7474
else
7575
{
7676
# One of more key value pairs were found
7777
$ensure = 'Present'
78+
$textValues = @()
7879

79-
$groups = @()
8080
foreach ($match in $results)
8181
{
82-
Write-Verbose -Verbose -Message ($match.Groups[1])
83-
$groups += $match.Groups[1]
82+
$textValues += $match.Groups[1]
8483
}
8584

86-
$text = ($groups -join ',')
85+
$text = ($textValues -join ',')
8786

8887
Write-Verbose -Message ($localizedData.KeyFoundMessage -f `
8988
$Path, $Name, $text)
@@ -195,11 +194,11 @@ function Set-TargetResource
195194
$regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline
196195
if ($IgnoreNameCase)
197196
{
198-
$regExOptions += [System.Text.RegularExpressions.RegexOptions]::IgnoreNameCase
197+
$regExOptions += [System.Text.RegularExpressions.RegexOptions]::IgnoreCase
199198
}
200199

201200
# Search the key that matches the requested key
202-
$results = [regex]::Matches($fileContent, "^[\s]*$Name=(.*)$", $regExOptions)
201+
$results = [regex]::Matches($fileContent, "^[\s]*$Name=([^\n\r]*)", $regExOptions)
203202

204203
if ($Ensure -eq 'Present')
205204
{
@@ -230,7 +229,12 @@ function Set-TargetResource
230229
}
231230
else
232231
{
233-
if ($results.Count -gt 0)
232+
if ($results.Count -eq 0)
233+
{
234+
# The Key does not exists and should not so don't do anything
235+
return
236+
}
237+
else
234238
{
235239
# The Key exists in the file but should not so remove it
236240
$fileContent = [regex]::Replace($fileContent, "^[\s]*$Name=(.*)$eolChars", '', $regExOptions)
@@ -336,11 +340,11 @@ function Test-TargetResource
336340
$regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline
337341
if ($IgnoreNameCase)
338342
{
339-
$regExOptions += [System.Text.RegularExpressions.RegexOptions]::IgnoreNameCase
343+
$regExOptions += [System.Text.RegularExpressions.RegexOptions]::IgnoreCase
340344
}
341345

342346
# Search the key that matches the requested key
343-
$results = [regex]::Matches($fileContent, "^[\s]*$Name=(.*)$", $regExOptions)
347+
$results = [regex]::Matches($fileContent, "^[\s]*$Name=([^\n\r]*)", $regExOptions)
344348

345349
if ($results.Count -eq 0)
346350
{
@@ -374,8 +378,8 @@ function Test-TargetResource
374378
# Check each found key value pair and check it has the correct value
375379
foreach ($match in $results)
376380
{
377-
if (($IgnoreValueCase -and ($match.Groups[1] -ne $Text)) -or `
378-
(-not $IgnoreValueCase -and ($match.Groups[1] -cne $Text)))
381+
if (($IgnoreValueCase -and ($match.Groups[1].Value -ne $Text)) -or `
382+
(-not $IgnoreValueCase -and ($match.Groups[1].Value -cne $Text)))
379383
{
380384
$desiredConfigurationMatch = $false
381385
} #

Modules/FileContentDsc/DSCResources/MSFT_KeyValuePairFile/en-US/MSFT_KeyValuePairFile.strings.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ConvertFrom-StringData @'
77
KeyReplaceSecretMessage = Key '{1}' found in file '{0}' being updated with secret value.
88
KeyReplaceTextMessage = Key '{1}' found in file '{0}' being updated with '{2}'.
99
KeyRemoveMessage = Key '{1}' found in file '{0}' but has been removed.
10-
KeyAddMessage = Key '{1}' not found in file '{0}' so has being added.
10+
KeyAddMessage = Key '{1}' not found in file '{0}' so has been added.
1111
KeyUpdateMessage = Key '{1}' found in file '{0}' and has been updated.
1212
KeyNotFoundButShouldExistMessage = Key '{1}' not found in file '{0}' but should exist. Change required.
1313
KeyNotFoundAndShouldNotExistMessage = Key '{1}' not found in file '{0}' and should not exist. Change not required.

TestFile.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Setting1=Value1
2+
Setting.Two=Test Secret
3+
Setting.Two=Test Secret
4+
Setting.Two=Test Secret
5+
Setting3.Test=Value4

Tests/Integration/MSFT_KeyValuePairFile.Integration.Tests.ps1

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ try
1515
$script:configurationName = 'KeyValuePairFile'
1616
$script:testTextFile = Join-Path -Path $TestDrive -ChildPath 'TestFile.txt'
1717
$script:testName = 'Setting.Two'
18-
$script:testText = 'TestText'
19-
$script:testSecret = 'TestSecret'
18+
$script:testText = 'Test Text'
19+
$script:testSecret = 'Test Secret'
2020
$script:testSecureSecret = ConvertTo-SecureString -String $script:testSecret -AsPlainText -Force
2121
$script:testSecretCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ('Dummy', $script:testSecureSecret)
2222

2323
$script:testFileContent = @"
2424
Setting1=Value1
25-
$($script:testName)=Value2
26-
$($script:testName)=Value3
25+
$($script:testName)=Value 2
26+
$($script:testName)=Value 3
2727
$($script:testName)=$($script:testText)
2828
Setting3.Test=Value4
2929
@@ -195,10 +195,10 @@ Setting3.Test=Value4
195195
$configData = @{
196196
AllNodes = @(
197197
@{
198-
NodeName = 'localhost'
199-
Path = $script:testTextFile
200-
Name = $script:testName
201-
Ensure = 'Absent'
198+
NodeName = 'localhost'
199+
Path = $script:testTextFile
200+
Name = $script:testName
201+
Ensure = 'Absent'
202202
}
203203
)
204204
}

0 commit comments

Comments
 (0)