Skip to content

Commit 205056b

Browse files
committed
Updated KeyValuePairFile
If file does not exist Test-TargetResouce will return $false Updated tests.
1 parent e4a289c commit 205056b

3 files changed

Lines changed: 184 additions & 56 deletions

File tree

.vscode/launch.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "PowerShell",
9+
"request": "launch",
10+
"name": "PowerShell Launch Current File",
11+
"script": "${file}",
12+
"args": [],
13+
"cwd": "${file}"
14+
},
15+
{
16+
"type": "PowerShell",
17+
"request": "launch",
18+
"name": "PowerShell Launch Current File in Temporary Console",
19+
"script": "${file}",
20+
"args": [],
21+
"cwd": "${file}",
22+
"createTemporaryIntegratedConsole": true
23+
},
24+
{
25+
"type": "PowerShell",
26+
"request": "launch",
27+
"name": "PowerShell Launch Current File w/Args Prompt",
28+
"script": "${file}",
29+
"args": [
30+
"${command:SpecifyScriptArgs}"
31+
],
32+
"cwd": "${file}"
33+
},
34+
{
35+
"type": "PowerShell",
36+
"request": "attach",
37+
"name": "PowerShell Attach to Host Process",
38+
"processId": "${command:PickPSHostProcess}",
39+
"runspaceId": 1
40+
},
41+
{
42+
"type": "PowerShell",
43+
"request": "launch",
44+
"name": "PowerShell Interactive Session",
45+
"cwd": ""
46+
}
47+
]
48+
}

DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ function Set-TargetResource
177177

178178
Assert-ParametersValid @PSBoundParameters
179179

180-
$fileContent = Get-Content -Path $Path -Raw
180+
$fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue
181181

182182
Write-Verbose -Message ($localizedData.SearchForKeyMessage -f `
183183
$Path, $Name)
@@ -187,61 +187,68 @@ function Set-TargetResource
187187
$Text = $Secret.GetNetworkCredential().Password
188188
} # if
189189

190-
# Determine the EOL characters used in the file
191-
$eolChars = Get-TextEolCharacter -Text $fileContent
192-
193-
# Setup the Regex Options that will be used
194-
$regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline
195-
if ($IgnoreNameCase)
190+
if ($null -ne $fileContent)
196191
{
197-
$regExOptions += [System.Text.RegularExpressions.RegexOptions]::IgnoreCase
198-
}
192+
# Determine the EOL characters used in the file
193+
$eolChars = Get-TextEolCharacter -Text $fileContent
199194

200-
# Search the key that matches the requested key
201-
$results = [regex]::Matches($fileContent, "^[\s]*$Name=([^\n\r]*)", $regExOptions)
195+
# Setup the Regex Options that will be used
196+
$regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline
197+
if ($IgnoreNameCase)
198+
{
199+
$regExOptions += [System.Text.RegularExpressions.RegexOptions]::IgnoreCase
200+
}
202201

203-
if ($Ensure -eq 'Present')
204-
{
205-
# The key value pair should exist
206-
$keyValuePair = '{0}={1}{2}' -f $Name, $Text, $eolChars
202+
# Search the key that matches the requested key
203+
$results = [regex]::Matches($fileContent, "^[\s]*$Name=([^\n\r]*)", $regExOptions)
207204

208-
if ($results.Count -eq 0)
205+
if ($Ensure -eq 'Present')
209206
{
210-
# The key value pair was not found so add it to the end of the file
211-
if (-not $fileContent.EndsWith($eolChars))
207+
# The key value pair should exist
208+
$keyValuePair = '{0}={1}{2}' -f $Name, $Text, $eolChars
209+
210+
if ($results.Count -eq 0)
212211
{
213-
$fileContent += $eolChars
214-
} # if
212+
# The key value pair was not found so add it to the end of the file
213+
if (-not $fileContent.EndsWith($eolChars))
214+
{
215+
$fileContent += $eolChars
216+
} # if
215217

216-
$fileContent += $keyValuePair
218+
$fileContent += $keyValuePair
217219

218-
Write-Verbose -Message ($localizedData.KeyAddMessage -f `
219-
$Path, $Name)
220+
Write-Verbose -Message ($localizedData.KeyAddMessage -f `
221+
$Path, $Name)
222+
}
223+
else
224+
{
225+
# The key value pair was found so update it
226+
$fileContent = [regex]::Replace($fileContent, "^[\s]*$Name=(.*)($eolChars*)", $keyValuePair, $regExOptions)
227+
228+
Write-Verbose -Message ($localizedData.KeyUpdateMessage -f `
229+
$Path, $Name)
230+
} # if
220231
}
221232
else
222233
{
223-
# The key value pair was found so update it
224-
$fileContent = [regex]::Replace($fileContent, "^[\s]*$Name=(.*)($eolChars*)", $keyValuePair, $regExOptions)
234+
if ($results.Count -eq 0)
235+
{
236+
# The Key does not exists and should not so don't do anything
237+
return
238+
}
239+
else
240+
{
241+
# The Key exists in the file but should not so remove it
242+
$fileContent = [regex]::Replace($fileContent, "^[\s]*$Name=(.*)$eolChars", '', $regExOptions)
225243

226-
Write-Verbose -Message ($localizedData.KeyUpdateMessage -f `
227-
$Path, $Name)
244+
Write-Verbose -Message ($localizedData.KeyRemoveMessage -f `
245+
$Path, $Name)
246+
}
228247
} # if
229248
}
230249
else
231250
{
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
238-
{
239-
# The Key exists in the file but should not so remove it
240-
$fileContent = [regex]::Replace($fileContent, "^[\s]*$Name=(.*)$eolChars", '', $regExOptions)
241-
242-
Write-Verbose -Message ($localizedData.KeyRemoveMessage -f `
243-
$Path, $Name)
244-
}
251+
$fileContent = '{0}={1}' -f $Name, $Text
245252
} # if
246253

247254
Set-Content `
@@ -331,6 +338,12 @@ function Test-TargetResource
331338
# Flag to signal whether settings are correct
332339
[Boolean] $desiredConfigurationMatch = $true
333340

341+
# Check if file being managed exists. If not return $False.
342+
if (-not (Test-Path -Path $Path))
343+
{
344+
return $false
345+
}
346+
334347
$fileContent = Get-Content -Path $Path -Raw
335348

336349
Write-Verbose -Message ($localizedData.SearchForKeyMessage -f `
@@ -484,8 +497,9 @@ function Assert-ParametersValid
484497
$IgnoreValueCase = $false
485498
)
486499

487-
# Does the file in path exist?
488-
if (-not (Test-Path -Path $Path))
500+
# Does the file in parent path exist?
501+
$parentPath = Split-Path -Path $Path -Parent
502+
if (-not (Test-Path -Path $parentPath))
489503
{
490504
New-InvalidArgumentException `
491505
-Message ($localizedData.FileNotFoundError -f $Path) `

Tests/Unit/DSR_KeyValuePairFile.Tests.ps1

Lines changed: 80 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,12 @@ $($script:testAddedName)=$($script:testText)
488488
-ModuleName 'DSR_KeyValuePairFile' `
489489
-Verifiable
490490

491+
Mock `
492+
-CommandName Test-Path `
493+
-ModuleName 'DSR_KeyValuePairFile' `
494+
-MockWith { $true } `
495+
-Verifiable
496+
491497
Mock `
492498
-CommandName Get-Content `
493499
-ParameterFilter { $path -eq $script:testTextFile } `
@@ -526,6 +532,12 @@ $($script:testAddedName)=$($script:testText)
526532
-ModuleName 'DSR_KeyValuePairFile' `
527533
-Verifiable
528534

535+
Mock `
536+
-CommandName Test-Path `
537+
-ModuleName 'DSR_KeyValuePairFile' `
538+
-MockWith { $true } `
539+
-Verifiable
540+
529541
Mock `
530542
-CommandName Get-Content `
531543
-ParameterFilter { $path -eq $script:testTextFile } `
@@ -563,6 +575,12 @@ $($script:testAddedName)=$($script:testText)
563575
-ModuleName 'DSR_KeyValuePairFile' `
564576
-Verifiable
565577

578+
Mock `
579+
-CommandName Test-Path `
580+
-ModuleName 'DSR_KeyValuePairFile' `
581+
-MockWith { $true } `
582+
-Verifiable
583+
566584
Mock `
567585
-CommandName Get-Content `
568586
-ParameterFilter { $path -eq $script:testTextFile } `
@@ -601,6 +619,12 @@ $($script:testAddedName)=$($script:testText)
601619
-ModuleName 'DSR_KeyValuePairFile' `
602620
-Verifiable
603621

622+
Mock `
623+
-CommandName Test-Path `
624+
-ModuleName 'DSR_KeyValuePairFile' `
625+
-MockWith { $true } `
626+
-Verifiable
627+
604628
Mock `
605629
-CommandName Get-Content `
606630
-ParameterFilter { $path -eq $script:testTextFile } `
@@ -640,6 +664,12 @@ $($script:testAddedName)=$($script:testText)
640664
-ModuleName 'DSR_KeyValuePairFile' `
641665
-Verifiable
642666

667+
Mock `
668+
-CommandName Test-Path `
669+
-ModuleName 'DSR_KeyValuePairFile' `
670+
-MockWith { $true } `
671+
-Verifiable
672+
643673
Mock `
644674
-CommandName Get-Content `
645675
-ParameterFilter { $path -eq $script:testTextFile } `
@@ -679,6 +709,12 @@ $($script:testAddedName)=$($script:testText)
679709
-ModuleName 'DSR_KeyValuePairFile' `
680710
-Verifiable
681711

712+
Mock `
713+
-CommandName Test-Path `
714+
-ModuleName 'DSR_KeyValuePairFile' `
715+
-MockWith { $true } `
716+
-Verifiable
717+
682718
Mock `
683719
-CommandName Get-Content `
684720
-ParameterFilter { $path -eq $script:testTextFile } `
@@ -718,6 +754,12 @@ $($script:testAddedName)=$($script:testText)
718754
-ModuleName 'DSR_KeyValuePairFile' `
719755
-Verifiable
720756

757+
Mock `
758+
-CommandName Test-Path `
759+
-ModuleName 'DSR_KeyValuePairFile' `
760+
-MockWith { $true } `
761+
-Verifiable
762+
721763
Mock `
722764
-CommandName Get-Content `
723765
-ParameterFilter { $path -eq $script:testTextFile } `
@@ -756,6 +798,12 @@ $($script:testAddedName)=$($script:testText)
756798
-ModuleName 'DSR_KeyValuePairFile' `
757799
-Verifiable
758800

801+
Mock `
802+
-CommandName Test-Path `
803+
-ModuleName 'DSR_KeyValuePairFile' `
804+
-MockWith { $true } `
805+
-Verifiable
806+
759807
Mock `
760808
-CommandName Get-Content `
761809
-ParameterFilter { $path -eq $script:testTextFile } `
@@ -764,12 +812,12 @@ $($script:testAddedName)=$($script:testText)
764812

765813
It 'Should not throw an exception' {
766814
{ $script:result = Test-TargetResource `
767-
-Path $script:testTextFile `
768-
-Name $script:testName `
769-
-Ensure 'Present' `
770-
-Text $script:testText.ToUpper() `
771-
-IgnoreValueCase:$true `
772-
-Verbose
815+
-Path $script:testTextFile `
816+
-Name $script:testName `
817+
-Ensure 'Present' `
818+
-Text $script:testText.ToUpper() `
819+
-IgnoreValueCase:$true `
820+
-Verbose
773821
} | Should -Not -Throw
774822
}
775823

@@ -795,6 +843,12 @@ $($script:testAddedName)=$($script:testText)
795843
-ModuleName 'DSR_KeyValuePairFile' `
796844
-Verifiable
797845

846+
Mock `
847+
-CommandName Test-Path `
848+
-ModuleName 'DSR_KeyValuePairFile' `
849+
-MockWith { $true } `
850+
-Verifiable
851+
798852
Mock `
799853
-CommandName Get-Content `
800854
-ParameterFilter { $path -eq $script:testTextFile } `
@@ -830,8 +884,14 @@ $($script:testAddedName)=$($script:testText)
830884

831885
#region Function Assert-ParametersValid
832886
Describe 'DSR_KeyValuePairFile\Assert-ParametersValid' {
833-
Context 'File exists' {
887+
Context 'File parent path exists' {
834888
# verifiable (should be called) mocks
889+
Mock `
890+
-CommandName Split-Path `
891+
-ParameterFilter { $path -eq $script:testTextFile } `
892+
-MockWith { $script:testTextFile } `
893+
-Verifiable
894+
835895
Mock `
836896
-CommandName Test-Path `
837897
-ParameterFilter { $path -eq $script:testTextFile } `
@@ -840,9 +900,9 @@ $($script:testAddedName)=$($script:testText)
840900

841901
It 'Should not throw an exception' {
842902
{ Assert-ParametersValid `
843-
-Path $script:testTextFile `
844-
-Name $script:testName `
845-
-Verbose
903+
-Path $script:testTextFile `
904+
-Name $script:testName `
905+
-Verbose
846906
} | Should -Not -Throw
847907
}
848908

@@ -852,8 +912,14 @@ $($script:testAddedName)=$($script:testText)
852912
}
853913
}
854914

855-
Context 'File does not exist' {
915+
Context 'File parent path does not exist' {
856916
# verifiable (should be called) mocks
917+
Mock `
918+
-CommandName Split-Path `
919+
-ParameterFilter { $path -eq $script:testTextFile } `
920+
-MockWith { $script:testTextFile } `
921+
-Verifiable
922+
857923
Mock `
858924
-CommandName Test-Path `
859925
-ParameterFilter { $path -eq $script:testTextFile } `
@@ -866,9 +932,9 @@ $($script:testAddedName)=$($script:testText)
866932

867933
It 'Should throw expected exception' {
868934
{ Assert-ParametersValid `
869-
-Path $script:testTextFile `
870-
-Name $script:testName `
871-
-Verbose
935+
-Path $script:testTextFile `
936+
-Name $script:testName `
937+
-Verbose
872938
} | Should -Throw $errorRecord
873939
}
874940

0 commit comments

Comments
 (0)