Skip to content

Commit 8a0b31c

Browse files
committed
Updated IniSettingsFile/ReplaceText
So Test-TargetResource return $false if file being managed doesn't exist
1 parent be9e155 commit 8a0b31c

9 files changed

Lines changed: 90 additions & 17 deletions

File tree

DSCResources/DSR_IniSettingsFile/DSR_IniSettingsFile.psm1

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ function Set-TargetResource
136136

137137
Assert-ParametersValid @PSBoundParameters
138138

139+
if (-not (Test-Path -Path $Path))
140+
{
141+
Out-File -FilePath $Path -Force
142+
}
143+
139144
if ($Type -eq 'Secret')
140145
{
141146
Write-Verbose -Message ($localizedData.SetIniSettingSecretMessage -f `
@@ -312,12 +317,12 @@ function Assert-ParametersValid
312317
$Secret
313318
)
314319

315-
# Does the file in parent path exist?
320+
# Does the file's parent path exist?
316321
$parentPath = Split-Path -Path $Path -Parent
317322
if (-not (Test-Path -Path $parentPath))
318323
{
319324
New-InvalidArgumentException `
320-
-Message ($localizedData.FileNotFoundError -f $Path) `
325+
-Message ($localizedData.FileParentNotFoundError -f $parentPath) `
321326
-ArgumentName 'Path'
322327
} # if
323328
}

DSCResources/DSR_IniSettingsFile/en-US/DSR_IniSettingsFile.strings.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ ConvertFrom-StringData @'
66
SetIniSettingSecretMessage = Setting the entry '{1}' key '{2}' to secret text in INI settings file '{0}'.
77
IniSettingMatchesMessage = The entry '{1}' key '{2}' in INI settings file '{0}' is in the correct state. Change not required.
88
IniSettingMismatchMessage = The entry '{1}' key '{2}' in INI settings file '{0}' is not in the correct state. Change required.
9-
FileNotFoundError = File '{0}' not found.
9+
FileParentNotFoundError = File parent path '{0}' not found.
1010
'@

DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,12 +497,12 @@ function Assert-ParametersValid
497497
$IgnoreValueCase = $false
498498
)
499499

500-
# Does the file in parent path exist?
500+
# Does the file's parent path exist?
501501
$parentPath = Split-Path -Path $Path -Parent
502502
if (-not (Test-Path -Path $parentPath))
503503
{
504504
New-InvalidArgumentException `
505-
-Message ($localizedData.FileNotFoundError -f $Path) `
505+
-Message ($localizedData.FileParentNotFoundError -f $Path) `
506506
-ArgumentName 'Path'
507507
} # if
508508
}

DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ ConvertFrom-StringData @'
1414
KeyFoundButNoReplacementMessage = Key '{1}' found in file '{0}' and should exist and value(s) are correct. Change not required.
1515
KeyFoundReplacementRequiredMessage = Key '{1}' found in file '{0}' and should exist but value(s) are not correct. Change required.
1616
KeyFoundButShouldNotExistMessage = Key '{1}' found in file '{0}' but should not exist. Change required.
17-
FileNotFoundError = File '{0}' not found.
17+
FileParentNotFoundError = File parent path '{0}' not found.
1818
'@

DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ function Set-TargetResource
138138

139139
Assert-ParametersValid @PSBoundParameters
140140

141-
$fileContent = Get-Content -Path $Path -Raw
141+
$fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue
142142

143143
if ($Type -eq 'Secret')
144144
{
@@ -153,7 +153,14 @@ function Set-TargetResource
153153
$Path, $Text)
154154
} # if
155155

156-
$fileContent = $fileContent -Replace $Search, $Text
156+
if ($null -eq $fileContent)
157+
{
158+
$fileContent = $Text
159+
}
160+
else
161+
{
162+
$fileContent = $fileContent -Replace $Search, $Text
163+
}
157164

158165
Set-Content `
159166
-Path $Path `
@@ -216,6 +223,12 @@ function Test-TargetResource
216223

217224
Assert-ParametersValid @PSBoundParameters
218225

226+
# Check if file being managed exists. If not return $False.
227+
if (-not (Test-Path -Path $Path))
228+
{
229+
return $false
230+
}
231+
219232
$fileContent = Get-Content -Path $Path -Raw
220233

221234
Write-Verbose -Message ($localizedData.SearchForTextMessage -f `
@@ -315,11 +328,12 @@ function Assert-ParametersValid
315328
$Secret
316329
)
317330

318-
# Does the file in path exist?
319-
if (-not (Test-Path -Path $Path))
331+
# Does the file's parent path exist?
332+
$parentPath = Split-Path -Path $Path -Parent
333+
if (-not (Test-Path -Path $parentPath))
320334
{
321335
New-InvalidArgumentException `
322-
-Message ($localizedData.FileNotFoundError -f $Path) `
336+
-Message ($localizedData.FileParentNotFoundError -f $parentPath) `
323337
-ArgumentName 'Path'
324338
} # if
325339
}

DSCResources/DSR_ReplaceText/en-US/DSR_ReplaceText.strings.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ ConvertFrom-StringData @'
88
StringNoReplacementMessage = String found using RegEx '{1}' in file '{0}', no replacement required.
99
StringReplaceTextMessage = String replaced by '{1}' in file '{0}'.
1010
StringReplaceSecretMessage = String replaced by secret text in file '{0}'.
11-
FileNotFoundError = File '{0}' not found.
11+
FileParentNotFoundError = File parent path '{0}' not found.
1212
'@

Tests/Unit/DSR_IniSettingsFile.Tests.ps1

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,12 @@ try
440440
Describe 'DSR_IniSettingsFile\Assert-ParametersValid' {
441441
Context 'File exists' {
442442
# verifiable (should be called) mocks
443+
Mock `
444+
-CommandName Split-Path `
445+
-ParameterFilter { $path -eq $script:testTextFile } `
446+
-MockWith { $script:testTextFile } `
447+
-Verifiable
448+
443449
Mock `
444450
-CommandName Test-Path `
445451
-ParameterFilter { $path -eq $script:testTextFile } `
@@ -461,16 +467,22 @@ try
461467
}
462468
}
463469

464-
Context 'File does not exist' {
470+
Context 'File parent does not exist' {
465471
# verifiable (should be called) mocks
472+
Mock `
473+
-CommandName Split-Path `
474+
-ParameterFilter { $path -eq $script:testTextFile } `
475+
-MockWith { $script:testTextFile } `
476+
-Verifiable
477+
466478
Mock `
467479
-CommandName Test-Path `
468480
-ParameterFilter { $path -eq $script:testTextFile } `
469481
-MockWith { $false } `
470482
-Verifiable
471483

472484
$errorRecord = Get-InvalidArgumentRecord `
473-
-Message ($localizedData.FileNotFoundError -f $script:testTextFile) `
485+
-Message ($localizedData.FileParentNotFoundError -f $script:testTextFile) `
474486
-ArgumentName 'Path'
475487

476488
It 'Should throw expected exception' {

Tests/Unit/DSR_KeyValuePairFile.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ $($script:testAddedName)=$($script:testText)
927927
-Verifiable
928928

929929
$errorRecord = Get-InvalidArgumentRecord `
930-
-Message ($localizedData.FileNotFoundError -f $script:testTextFile) `
930+
-Message ($localizedData.FileParentNotFoundError -f $script:testTextFile) `
931931
-ArgumentName 'Path'
932932

933933
It 'Should throw expected exception' {

Tests/Unit/DSR_ReplaceText.Tests.ps1

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,12 @@ Setting3.Test=Value4
267267
-ModuleName 'DSR_ReplaceText' `
268268
-Verifiable
269269

270+
Mock `
271+
-CommandName Test-Path `
272+
-ModuleName 'DSR_ReplaceText' `
273+
-MockWith { $true } `
274+
-Verifiable
275+
270276
Mock `
271277
-CommandName Get-Content `
272278
-ParameterFilter { $path -eq $script:testTextFile } `
@@ -306,6 +312,12 @@ Setting3.Test=Value4
306312
-ModuleName 'DSR_ReplaceText' `
307313
-Verifiable
308314

315+
Mock `
316+
-CommandName Test-Path `
317+
-ModuleName 'DSR_ReplaceText' `
318+
-MockWith { $true } `
319+
-Verifiable
320+
309321
Mock `
310322
-CommandName Get-Content `
311323
-ParameterFilter { $path -eq $script:testTextFile } `
@@ -345,6 +357,12 @@ Setting3.Test=Value4
345357
-ModuleName 'DSR_ReplaceText' `
346358
-Verifiable
347359

360+
Mock `
361+
-CommandName Test-Path `
362+
-ModuleName 'DSR_ReplaceText' `
363+
-MockWith { $true } `
364+
-Verifiable
365+
348366
Mock `
349367
-CommandName Get-Content `
350368
-ParameterFilter { $path -eq $script:testTextFile } `
@@ -384,6 +402,12 @@ Setting3.Test=Value4
384402
-ModuleName 'DSR_ReplaceText' `
385403
-Verifiable
386404

405+
Mock `
406+
-CommandName Test-Path `
407+
-ModuleName 'DSR_ReplaceText' `
408+
-MockWith { $true } `
409+
-Verifiable
410+
387411
Mock `
388412
-CommandName Get-Content `
389413
-ParameterFilter { $path -eq $script:testTextFile } `
@@ -424,6 +448,12 @@ Setting3.Test=Value4
424448
-ModuleName 'DSR_ReplaceText' `
425449
-Verifiable
426450

451+
Mock `
452+
-CommandName Test-Path `
453+
-ModuleName 'DSR_ReplaceText' `
454+
-MockWith { $true } `
455+
-Verifiable
456+
427457
Mock `
428458
-CommandName Get-Content `
429459
-ParameterFilter { $path -eq $script:testTextFile } `
@@ -463,6 +493,12 @@ Setting3.Test=Value4
463493
Describe 'DSR_ReplaceText\Assert-ParametersValid' {
464494
Context 'File exists' {
465495
# verifiable (should be called) mocks
496+
Mock `
497+
-CommandName Split-Path `
498+
-ParameterFilter { $path -eq $script:testTextFile } `
499+
-MockWith { $script:testTextFile } `
500+
-Verifiable
501+
466502
Mock `
467503
-CommandName Test-Path `
468504
-ParameterFilter { $path -eq $script:testTextFile } `
@@ -483,16 +519,22 @@ Setting3.Test=Value4
483519
}
484520
}
485521

486-
Context 'File does not exist' {
522+
Context 'File parent does not exist' {
487523
# verifiable (should be called) mocks
524+
Mock `
525+
-CommandName Split-Path `
526+
-ParameterFilter { $path -eq $script:testTextFile } `
527+
-MockWith { $script:testTextFile } `
528+
-Verifiable
529+
488530
Mock `
489531
-CommandName Test-Path `
490532
-ParameterFilter { $path -eq $script:testTextFile } `
491533
-MockWith { $false } `
492534
-Verifiable
493535

494536
$errorRecord = Get-InvalidArgumentRecord `
495-
-Message ($localizedData.FileNotFoundError -f $script:testTextFile) `
537+
-Message ($localizedData.FileParentNotFoundError -f $script:testTextFile) `
496538
-ArgumentName 'Path'
497539

498540
It 'Should throw expected exception' {

0 commit comments

Comments
 (0)