From 205056badefd8a0dc182a4a4dba4a1c78ea4ac58 Mon Sep 17 00:00:00 2001 From: Jason Walker Date: Mon, 30 Jul 2018 21:40:40 -0400 Subject: [PATCH 1/6] Updated KeyValuePairFile If file does not exist Test-TargetResouce will return $false Updated tests. --- .vscode/launch.json | 48 +++++++++ .../DSR_KeyValuePairFile.psm1 | 98 +++++++++++-------- Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 | 94 +++++++++++++++--- 3 files changed, 184 insertions(+), 56 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..96266b0 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,48 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "PowerShell", + "request": "launch", + "name": "PowerShell Launch Current File", + "script": "${file}", + "args": [], + "cwd": "${file}" + }, + { + "type": "PowerShell", + "request": "launch", + "name": "PowerShell Launch Current File in Temporary Console", + "script": "${file}", + "args": [], + "cwd": "${file}", + "createTemporaryIntegratedConsole": true + }, + { + "type": "PowerShell", + "request": "launch", + "name": "PowerShell Launch Current File w/Args Prompt", + "script": "${file}", + "args": [ + "${command:SpecifyScriptArgs}" + ], + "cwd": "${file}" + }, + { + "type": "PowerShell", + "request": "attach", + "name": "PowerShell Attach to Host Process", + "processId": "${command:PickPSHostProcess}", + "runspaceId": 1 + }, + { + "type": "PowerShell", + "request": "launch", + "name": "PowerShell Interactive Session", + "cwd": "" + } + ] +} \ No newline at end of file diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 index 3e4d670..7085731 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 @@ -177,7 +177,7 @@ function Set-TargetResource Assert-ParametersValid @PSBoundParameters - $fileContent = Get-Content -Path $Path -Raw + $fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue Write-Verbose -Message ($localizedData.SearchForKeyMessage -f ` $Path, $Name) @@ -187,61 +187,68 @@ function Set-TargetResource $Text = $Secret.GetNetworkCredential().Password } # if - # Determine the EOL characters used in the file - $eolChars = Get-TextEolCharacter -Text $fileContent - - # Setup the Regex Options that will be used - $regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline - if ($IgnoreNameCase) + if ($null -ne $fileContent) { - $regExOptions += [System.Text.RegularExpressions.RegexOptions]::IgnoreCase - } + # Determine the EOL characters used in the file + $eolChars = Get-TextEolCharacter -Text $fileContent - # Search the key that matches the requested key - $results = [regex]::Matches($fileContent, "^[\s]*$Name=([^\n\r]*)", $regExOptions) + # Setup the Regex Options that will be used + $regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline + if ($IgnoreNameCase) + { + $regExOptions += [System.Text.RegularExpressions.RegexOptions]::IgnoreCase + } - if ($Ensure -eq 'Present') - { - # The key value pair should exist - $keyValuePair = '{0}={1}{2}' -f $Name, $Text, $eolChars + # Search the key that matches the requested key + $results = [regex]::Matches($fileContent, "^[\s]*$Name=([^\n\r]*)", $regExOptions) - if ($results.Count -eq 0) + if ($Ensure -eq 'Present') { - # The key value pair was not found so add it to the end of the file - if (-not $fileContent.EndsWith($eolChars)) + # The key value pair should exist + $keyValuePair = '{0}={1}{2}' -f $Name, $Text, $eolChars + + if ($results.Count -eq 0) { - $fileContent += $eolChars - } # if + # The key value pair was not found so add it to the end of the file + if (-not $fileContent.EndsWith($eolChars)) + { + $fileContent += $eolChars + } # if - $fileContent += $keyValuePair + $fileContent += $keyValuePair - Write-Verbose -Message ($localizedData.KeyAddMessage -f ` - $Path, $Name) + Write-Verbose -Message ($localizedData.KeyAddMessage -f ` + $Path, $Name) + } + else + { + # The key value pair was found so update it + $fileContent = [regex]::Replace($fileContent, "^[\s]*$Name=(.*)($eolChars*)", $keyValuePair, $regExOptions) + + Write-Verbose -Message ($localizedData.KeyUpdateMessage -f ` + $Path, $Name) + } # if } else { - # The key value pair was found so update it - $fileContent = [regex]::Replace($fileContent, "^[\s]*$Name=(.*)($eolChars*)", $keyValuePair, $regExOptions) + if ($results.Count -eq 0) + { + # The Key does not exists and should not so don't do anything + return + } + else + { + # The Key exists in the file but should not so remove it + $fileContent = [regex]::Replace($fileContent, "^[\s]*$Name=(.*)$eolChars", '', $regExOptions) - Write-Verbose -Message ($localizedData.KeyUpdateMessage -f ` - $Path, $Name) + Write-Verbose -Message ($localizedData.KeyRemoveMessage -f ` + $Path, $Name) + } } # if } else { - if ($results.Count -eq 0) - { - # The Key does not exists and should not so don't do anything - return - } - else - { - # The Key exists in the file but should not so remove it - $fileContent = [regex]::Replace($fileContent, "^[\s]*$Name=(.*)$eolChars", '', $regExOptions) - - Write-Verbose -Message ($localizedData.KeyRemoveMessage -f ` - $Path, $Name) - } + $fileContent = '{0}={1}' -f $Name, $Text } # if Set-Content ` @@ -331,6 +338,12 @@ function Test-TargetResource # Flag to signal whether settings are correct [Boolean] $desiredConfigurationMatch = $true + # Check if file being managed exists. If not return $False. + if (-not (Test-Path -Path $Path)) + { + return $false + } + $fileContent = Get-Content -Path $Path -Raw Write-Verbose -Message ($localizedData.SearchForKeyMessage -f ` @@ -484,8 +497,9 @@ function Assert-ParametersValid $IgnoreValueCase = $false ) - # Does the file in path exist? - if (-not (Test-Path -Path $Path)) + # Does the file in parent path exist? + $parentPath = Split-Path -Path $Path -Parent + if (-not (Test-Path -Path $parentPath)) { New-InvalidArgumentException ` -Message ($localizedData.FileNotFoundError -f $Path) ` diff --git a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 index 9927bcf..f0ec2d9 100644 --- a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 +++ b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 @@ -488,6 +488,12 @@ $($script:testAddedName)=$($script:testText) -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_KeyValuePairFile' ` + -MockWith { $true } ` + -Verifiable + Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` @@ -526,6 +532,12 @@ $($script:testAddedName)=$($script:testText) -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_KeyValuePairFile' ` + -MockWith { $true } ` + -Verifiable + Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` @@ -563,6 +575,12 @@ $($script:testAddedName)=$($script:testText) -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_KeyValuePairFile' ` + -MockWith { $true } ` + -Verifiable + Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` @@ -601,6 +619,12 @@ $($script:testAddedName)=$($script:testText) -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_KeyValuePairFile' ` + -MockWith { $true } ` + -Verifiable + Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` @@ -640,6 +664,12 @@ $($script:testAddedName)=$($script:testText) -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_KeyValuePairFile' ` + -MockWith { $true } ` + -Verifiable + Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` @@ -679,6 +709,12 @@ $($script:testAddedName)=$($script:testText) -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_KeyValuePairFile' ` + -MockWith { $true } ` + -Verifiable + Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` @@ -718,6 +754,12 @@ $($script:testAddedName)=$($script:testText) -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_KeyValuePairFile' ` + -MockWith { $true } ` + -Verifiable + Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` @@ -756,6 +798,12 @@ $($script:testAddedName)=$($script:testText) -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_KeyValuePairFile' ` + -MockWith { $true } ` + -Verifiable + Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` @@ -764,12 +812,12 @@ $($script:testAddedName)=$($script:testText) It 'Should not throw an exception' { { $script:result = Test-TargetResource ` - -Path $script:testTextFile ` - -Name $script:testName ` - -Ensure 'Present' ` - -Text $script:testText.ToUpper() ` - -IgnoreValueCase:$true ` - -Verbose + -Path $script:testTextFile ` + -Name $script:testName ` + -Ensure 'Present' ` + -Text $script:testText.ToUpper() ` + -IgnoreValueCase:$true ` + -Verbose } | Should -Not -Throw } @@ -795,6 +843,12 @@ $($script:testAddedName)=$($script:testText) -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_KeyValuePairFile' ` + -MockWith { $true } ` + -Verifiable + Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` @@ -830,8 +884,14 @@ $($script:testAddedName)=$($script:testText) #region Function Assert-ParametersValid Describe 'DSR_KeyValuePairFile\Assert-ParametersValid' { - Context 'File exists' { + Context 'File parent path exists' { # verifiable (should be called) mocks + Mock ` + -CommandName Split-Path ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testTextFile } ` + -Verifiable + Mock ` -CommandName Test-Path ` -ParameterFilter { $path -eq $script:testTextFile } ` @@ -840,9 +900,9 @@ $($script:testAddedName)=$($script:testText) It 'Should not throw an exception' { { Assert-ParametersValid ` - -Path $script:testTextFile ` - -Name $script:testName ` - -Verbose + -Path $script:testTextFile ` + -Name $script:testName ` + -Verbose } | Should -Not -Throw } @@ -852,8 +912,14 @@ $($script:testAddedName)=$($script:testText) } } - Context 'File does not exist' { + Context 'File parent path does not exist' { # verifiable (should be called) mocks + Mock ` + -CommandName Split-Path ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testTextFile } ` + -Verifiable + Mock ` -CommandName Test-Path ` -ParameterFilter { $path -eq $script:testTextFile } ` @@ -866,9 +932,9 @@ $($script:testAddedName)=$($script:testText) It 'Should throw expected exception' { { Assert-ParametersValid ` - -Path $script:testTextFile ` - -Name $script:testName ` - -Verbose + -Path $script:testTextFile ` + -Name $script:testName ` + -Verbose } | Should -Throw $errorRecord } From be9e15586651a6c12328481bdca603d1bc697fcd Mon Sep 17 00:00:00 2001 From: Jason Walker Date: Tue, 31 Jul 2018 19:03:15 -0400 Subject: [PATCH 2/6] End of day push --- .../DSR_IniSettingsFile/DSR_IniSettingsFile.psm1 | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/DSCResources/DSR_IniSettingsFile/DSR_IniSettingsFile.psm1 b/DSCResources/DSR_IniSettingsFile/DSR_IniSettingsFile.psm1 index 7960b9a..1fbdcb9 100644 --- a/DSCResources/DSR_IniSettingsFile/DSR_IniSettingsFile.psm1 +++ b/DSCResources/DSR_IniSettingsFile/DSR_IniSettingsFile.psm1 @@ -220,6 +220,12 @@ function Test-TargetResource Assert-ParametersValid @PSBoundParameters + # Check if file being managed exists. If not return $False. + if (-not (Test-Path -Path $Path)) + { + return $false + } + if ($Type -eq 'Secret') { $Text = $Secret.GetNetworkCredential().Password @@ -306,8 +312,9 @@ function Assert-ParametersValid $Secret ) - # Does the file in path exist? - if (-not (Test-Path -Path $Path)) + # Does the file in parent path exist? + $parentPath = Split-Path -Path $Path -Parent + if (-not (Test-Path -Path $parentPath)) { New-InvalidArgumentException ` -Message ($localizedData.FileNotFoundError -f $Path) ` From 8a0b31ce079c5f1325f34187fb7749a6c2b14f0f Mon Sep 17 00:00:00 2001 From: Jason Walker Date: Tue, 31 Jul 2018 20:57:24 -0400 Subject: [PATCH 3/6] Updated IniSettingsFile/ReplaceText So Test-TargetResource return $false if file being managed doesn't exist --- .../DSR_IniSettingsFile.psm1 | 9 +++- .../en-US/DSR_IniSettingsFile.strings.psd1 | 2 +- .../DSR_KeyValuePairFile.psm1 | 4 +- .../en-US/DSR_KeyValuePairFile.strings.psd1 | 2 +- .../DSR_ReplaceText/DSR_ReplaceText.psm1 | 24 ++++++++-- .../en-US/DSR_ReplaceText.strings.psd1 | 2 +- Tests/Unit/DSR_IniSettingsFile.Tests.ps1 | 16 ++++++- Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 | 2 +- Tests/Unit/DSR_ReplaceText.Tests.ps1 | 46 ++++++++++++++++++- 9 files changed, 90 insertions(+), 17 deletions(-) diff --git a/DSCResources/DSR_IniSettingsFile/DSR_IniSettingsFile.psm1 b/DSCResources/DSR_IniSettingsFile/DSR_IniSettingsFile.psm1 index 1fbdcb9..8a2eb31 100644 --- a/DSCResources/DSR_IniSettingsFile/DSR_IniSettingsFile.psm1 +++ b/DSCResources/DSR_IniSettingsFile/DSR_IniSettingsFile.psm1 @@ -136,6 +136,11 @@ function Set-TargetResource Assert-ParametersValid @PSBoundParameters + if (-not (Test-Path -Path $Path)) + { + Out-File -FilePath $Path -Force + } + if ($Type -eq 'Secret') { Write-Verbose -Message ($localizedData.SetIniSettingSecretMessage -f ` @@ -312,12 +317,12 @@ function Assert-ParametersValid $Secret ) - # Does the file in parent path exist? + # Does the file's parent path exist? $parentPath = Split-Path -Path $Path -Parent if (-not (Test-Path -Path $parentPath)) { New-InvalidArgumentException ` - -Message ($localizedData.FileNotFoundError -f $Path) ` + -Message ($localizedData.FileParentNotFoundError -f $parentPath) ` -ArgumentName 'Path' } # if } diff --git a/DSCResources/DSR_IniSettingsFile/en-US/DSR_IniSettingsFile.strings.psd1 b/DSCResources/DSR_IniSettingsFile/en-US/DSR_IniSettingsFile.strings.psd1 index 4959348..e714c5e 100644 --- a/DSCResources/DSR_IniSettingsFile/en-US/DSR_IniSettingsFile.strings.psd1 +++ b/DSCResources/DSR_IniSettingsFile/en-US/DSR_IniSettingsFile.strings.psd1 @@ -6,5 +6,5 @@ ConvertFrom-StringData @' SetIniSettingSecretMessage = Setting the entry '{1}' key '{2}' to secret text in INI settings file '{0}'. IniSettingMatchesMessage = The entry '{1}' key '{2}' in INI settings file '{0}' is in the correct state. Change not required. IniSettingMismatchMessage = The entry '{1}' key '{2}' in INI settings file '{0}' is not in the correct state. Change required. - FileNotFoundError = File '{0}' not found. + FileParentNotFoundError = File parent path '{0}' not found. '@ diff --git a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 index 7085731..edf9d4d 100644 --- a/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 +++ b/DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1 @@ -497,12 +497,12 @@ function Assert-ParametersValid $IgnoreValueCase = $false ) - # Does the file in parent path exist? + # Does the file's parent path exist? $parentPath = Split-Path -Path $Path -Parent if (-not (Test-Path -Path $parentPath)) { New-InvalidArgumentException ` - -Message ($localizedData.FileNotFoundError -f $Path) ` + -Message ($localizedData.FileParentNotFoundError -f $Path) ` -ArgumentName 'Path' } # if } diff --git a/DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1 b/DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1 index 56477d5..7134505 100644 --- a/DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1 +++ b/DSCResources/DSR_KeyValuePairFile/en-US/DSR_KeyValuePairFile.strings.psd1 @@ -14,5 +14,5 @@ ConvertFrom-StringData @' KeyFoundButNoReplacementMessage = Key '{1}' found in file '{0}' and should exist and value(s) are correct. Change not required. KeyFoundReplacementRequiredMessage = Key '{1}' found in file '{0}' and should exist but value(s) are not correct. Change required. KeyFoundButShouldNotExistMessage = Key '{1}' found in file '{0}' but should not exist. Change required. - FileNotFoundError = File '{0}' not found. + FileParentNotFoundError = File parent path '{0}' not found. '@ diff --git a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 index b0c005b..e39958d 100644 --- a/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 +++ b/DSCResources/DSR_ReplaceText/DSR_ReplaceText.psm1 @@ -138,7 +138,7 @@ function Set-TargetResource Assert-ParametersValid @PSBoundParameters - $fileContent = Get-Content -Path $Path -Raw + $fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue if ($Type -eq 'Secret') { @@ -153,7 +153,14 @@ function Set-TargetResource $Path, $Text) } # if - $fileContent = $fileContent -Replace $Search, $Text + if ($null -eq $fileContent) + { + $fileContent = $Text + } + else + { + $fileContent = $fileContent -Replace $Search, $Text + } Set-Content ` -Path $Path ` @@ -216,6 +223,12 @@ function Test-TargetResource Assert-ParametersValid @PSBoundParameters + # Check if file being managed exists. If not return $False. + if (-not (Test-Path -Path $Path)) + { + return $false + } + $fileContent = Get-Content -Path $Path -Raw Write-Verbose -Message ($localizedData.SearchForTextMessage -f ` @@ -315,11 +328,12 @@ function Assert-ParametersValid $Secret ) - # Does the file in path exist? - if (-not (Test-Path -Path $Path)) + # Does the file's parent path exist? + $parentPath = Split-Path -Path $Path -Parent + if (-not (Test-Path -Path $parentPath)) { New-InvalidArgumentException ` - -Message ($localizedData.FileNotFoundError -f $Path) ` + -Message ($localizedData.FileParentNotFoundError -f $parentPath) ` -ArgumentName 'Path' } # if } diff --git a/DSCResources/DSR_ReplaceText/en-US/DSR_ReplaceText.strings.psd1 b/DSCResources/DSR_ReplaceText/en-US/DSR_ReplaceText.strings.psd1 index 1670035..85dca92 100644 --- a/DSCResources/DSR_ReplaceText/en-US/DSR_ReplaceText.strings.psd1 +++ b/DSCResources/DSR_ReplaceText/en-US/DSR_ReplaceText.strings.psd1 @@ -8,5 +8,5 @@ ConvertFrom-StringData @' StringNoReplacementMessage = String found using RegEx '{1}' in file '{0}', no replacement required. StringReplaceTextMessage = String replaced by '{1}' in file '{0}'. StringReplaceSecretMessage = String replaced by secret text in file '{0}'. - FileNotFoundError = File '{0}' not found. + FileParentNotFoundError = File parent path '{0}' not found. '@ diff --git a/Tests/Unit/DSR_IniSettingsFile.Tests.ps1 b/Tests/Unit/DSR_IniSettingsFile.Tests.ps1 index 1f2f000..0a29278 100644 --- a/Tests/Unit/DSR_IniSettingsFile.Tests.ps1 +++ b/Tests/Unit/DSR_IniSettingsFile.Tests.ps1 @@ -440,6 +440,12 @@ try Describe 'DSR_IniSettingsFile\Assert-ParametersValid' { Context 'File exists' { # verifiable (should be called) mocks + Mock ` + -CommandName Split-Path ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testTextFile } ` + -Verifiable + Mock ` -CommandName Test-Path ` -ParameterFilter { $path -eq $script:testTextFile } ` @@ -461,8 +467,14 @@ try } } - Context 'File does not exist' { + Context 'File parent does not exist' { # verifiable (should be called) mocks + Mock ` + -CommandName Split-Path ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testTextFile } ` + -Verifiable + Mock ` -CommandName Test-Path ` -ParameterFilter { $path -eq $script:testTextFile } ` @@ -470,7 +482,7 @@ try -Verifiable $errorRecord = Get-InvalidArgumentRecord ` - -Message ($localizedData.FileNotFoundError -f $script:testTextFile) ` + -Message ($localizedData.FileParentNotFoundError -f $script:testTextFile) ` -ArgumentName 'Path' It 'Should throw expected exception' { diff --git a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 index f0ec2d9..7b4c720 100644 --- a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 +++ b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 @@ -927,7 +927,7 @@ $($script:testAddedName)=$($script:testText) -Verifiable $errorRecord = Get-InvalidArgumentRecord ` - -Message ($localizedData.FileNotFoundError -f $script:testTextFile) ` + -Message ($localizedData.FileParentNotFoundError -f $script:testTextFile) ` -ArgumentName 'Path' It 'Should throw expected exception' { diff --git a/Tests/Unit/DSR_ReplaceText.Tests.ps1 b/Tests/Unit/DSR_ReplaceText.Tests.ps1 index 20435a3..c49062e 100644 --- a/Tests/Unit/DSR_ReplaceText.Tests.ps1 +++ b/Tests/Unit/DSR_ReplaceText.Tests.ps1 @@ -267,6 +267,12 @@ Setting3.Test=Value4 -ModuleName 'DSR_ReplaceText' ` -Verifiable + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_ReplaceText' ` + -MockWith { $true } ` + -Verifiable + Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` @@ -306,6 +312,12 @@ Setting3.Test=Value4 -ModuleName 'DSR_ReplaceText' ` -Verifiable + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_ReplaceText' ` + -MockWith { $true } ` + -Verifiable + Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` @@ -345,6 +357,12 @@ Setting3.Test=Value4 -ModuleName 'DSR_ReplaceText' ` -Verifiable + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_ReplaceText' ` + -MockWith { $true } ` + -Verifiable + Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` @@ -384,6 +402,12 @@ Setting3.Test=Value4 -ModuleName 'DSR_ReplaceText' ` -Verifiable + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_ReplaceText' ` + -MockWith { $true } ` + -Verifiable + Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` @@ -424,6 +448,12 @@ Setting3.Test=Value4 -ModuleName 'DSR_ReplaceText' ` -Verifiable + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_ReplaceText' ` + -MockWith { $true } ` + -Verifiable + Mock ` -CommandName Get-Content ` -ParameterFilter { $path -eq $script:testTextFile } ` @@ -463,6 +493,12 @@ Setting3.Test=Value4 Describe 'DSR_ReplaceText\Assert-ParametersValid' { Context 'File exists' { # verifiable (should be called) mocks + Mock ` + -CommandName Split-Path ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testTextFile } ` + -Verifiable + Mock ` -CommandName Test-Path ` -ParameterFilter { $path -eq $script:testTextFile } ` @@ -483,8 +519,14 @@ Setting3.Test=Value4 } } - Context 'File does not exist' { + Context 'File parent does not exist' { # verifiable (should be called) mocks + Mock ` + -CommandName Split-Path ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testTextFile } ` + -Verifiable + Mock ` -CommandName Test-Path ` -ParameterFilter { $path -eq $script:testTextFile } ` @@ -492,7 +534,7 @@ Setting3.Test=Value4 -Verifiable $errorRecord = Get-InvalidArgumentRecord ` - -Message ($localizedData.FileNotFoundError -f $script:testTextFile) ` + -Message ($localizedData.FileParentNotFoundError -f $script:testTextFile) ` -ArgumentName 'Path' It 'Should throw expected exception' { From 614e4870694cf878b7a0c759bff9107e75f00d5d Mon Sep 17 00:00:00 2001 From: Jason Walker Date: Tue, 31 Jul 2018 21:12:02 -0400 Subject: [PATCH 4/6] Update changeLog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c419164..ea36a36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ to root folder of repository and remove test harness - Fixes [Issue #11](https://github.com/PlagueHO/FileContentDsc/issues/11). - Converted Examples to support format for publishing to PowerShell Gallery. +- Refactor Test-TargetResource to return $false in all DSC resource -Fixes [Issue #12](https://github.com/PlagueHO/FileContentDsc/issues/13) ## 1.0.0.0 From 617610232293c40b3cbcbf0b88690a87f8ac0235 Mon Sep 17 00:00:00 2001 From: Jason Walker Date: Thu, 2 Aug 2018 07:54:50 -0400 Subject: [PATCH 5/6] Remove luanch.json and it to gitignore to it isn't subject to meta.tests --- .gitignore | 1 + .vscode/launch.json | 48 --------------------------------------------- 2 files changed, 1 insertion(+), 48 deletions(-) delete mode 100644 .vscode/launch.json diff --git a/.gitignore b/.gitignore index 6aa8421..d4462da 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ node_modules node_modules/* markdownissues.txt TestResults.xml +launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 96266b0..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,48 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "type": "PowerShell", - "request": "launch", - "name": "PowerShell Launch Current File", - "script": "${file}", - "args": [], - "cwd": "${file}" - }, - { - "type": "PowerShell", - "request": "launch", - "name": "PowerShell Launch Current File in Temporary Console", - "script": "${file}", - "args": [], - "cwd": "${file}", - "createTemporaryIntegratedConsole": true - }, - { - "type": "PowerShell", - "request": "launch", - "name": "PowerShell Launch Current File w/Args Prompt", - "script": "${file}", - "args": [ - "${command:SpecifyScriptArgs}" - ], - "cwd": "${file}" - }, - { - "type": "PowerShell", - "request": "attach", - "name": "PowerShell Attach to Host Process", - "processId": "${command:PickPSHostProcess}", - "runspaceId": 1 - }, - { - "type": "PowerShell", - "request": "launch", - "name": "PowerShell Interactive Session", - "cwd": "" - } - ] -} \ No newline at end of file From 83eaab06bb7eb28394e944d684f143990b90856b Mon Sep 17 00:00:00 2001 From: Jason Walker Date: Thu, 2 Aug 2018 21:38:10 -0400 Subject: [PATCH 6/6] Finished unit tests --- Tests/Unit/DSR_IniSettingsFile.Tests.ps1 | 97 +++++++++++++++++-- Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 | 108 +++++++++++++++++++--- Tests/Unit/DSR_ReplaceText.Tests.ps1 | 93 +++++++++++++++++++ 3 files changed, 278 insertions(+), 20 deletions(-) diff --git a/Tests/Unit/DSR_IniSettingsFile.Tests.ps1 b/Tests/Unit/DSR_IniSettingsFile.Tests.ps1 index 0a29278..969a7eb 100644 --- a/Tests/Unit/DSR_IniSettingsFile.Tests.ps1 +++ b/Tests/Unit/DSR_IniSettingsFile.Tests.ps1 @@ -235,6 +235,60 @@ try -Exactly 1 } } + + Context 'File does not exist' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_IniSettingsFile' ` + -Verifiable + + Mock ` + -CommandName Set-IniSettingFileValue ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($section -eq $script:testSection) -and ` + ($key -eq $script:testKey) -and ` + ($value -eq $script:testText) + } ` + -Verifiable + + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_IniSettingsFile' ` + -MockWith { $false } ` + -Verifiable + + Mock ` + -CommandName Out-File ` + -ModuleName 'DSR_IniSettingsFile' ` + -Verifiable + + It 'Should not throw an exception' { + { Set-TargetResource ` + -Path $script:testTextFile ` + -Section $script:testSection ` + -Key $script:testKey ` + -Text $script:testText ` + -Verbose + } | Should -Not -Throw + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Set-IniSettingFileValue ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($section -eq $script:testSection) -and ` + ($key -eq $script:testKey) -and ` + ($value -eq $script:testText) + } ` + -Exactly 1 + } + } } #endregion @@ -433,6 +487,33 @@ try -Exactly 1 } } + + Context 'File does not exist' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_IniSettingsFile' ` + -Verifiable + + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_IniSettingsFile' ` + -MockWith { $false } + + It 'Should return false' { + $result = Test-TargetResource ` + -Path $script:testTextFile ` + -Section $script:testSection ` + -Key $script:testKey ` + -Text $script:testText + + $result | Should -Be $false + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + } + } } #endregion @@ -441,10 +522,10 @@ try Context 'File exists' { # verifiable (should be called) mocks Mock ` - -CommandName Split-Path ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testTextFile } ` - -Verifiable + -CommandName Split-Path ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testTextFile } ` + -Verifiable Mock ` -CommandName Test-Path ` @@ -470,10 +551,10 @@ try Context 'File parent does not exist' { # verifiable (should be called) mocks Mock ` - -CommandName Split-Path ` - -ParameterFilter { $path -eq $script:testTextFile } ` - -MockWith { $script:testTextFile } ` - -Verifiable + -CommandName Split-Path ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testTextFile } ` + -Verifiable Mock ` -CommandName Test-Path ` diff --git a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 index 7b4c720..f700346 100644 --- a/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 +++ b/Tests/Unit/DSR_KeyValuePairFile.Tests.ps1 @@ -184,6 +184,56 @@ $($script:testAddedName)=$($script:testText) #region Function Set-TargetResource Describe 'DSR_KeyValuePairFile\Set-TargetResource' { + Context 'File does not exist' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_KeyValuePairFile' ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $null } ` + -Verifiable + + Mock ` + -CommandName Set-Content ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($value -eq "$script:testName=$script:testText") + } ` + -Verifiable + + It 'Should not throw an exception' { + { Set-TargetResource ` + -Path $script:testTextFile ` + -Name $script:testName ` + -Ensure 'Present' ` + -Text $script:testText ` + -Verbose + } | Should -Not -Throw + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Set-Content ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($value -eq "$script:testName=$script:testText") + } ` + -Exactly 1 + } + } + Context 'File exists and contains matching key that should exist' { # verifiable (should be called) mocks Mock ` @@ -488,7 +538,7 @@ $($script:testAddedName)=$($script:testText) -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable - Mock ` + Mock ` -CommandName Test-Path ` -ModuleName 'DSR_KeyValuePairFile' ` -MockWith { $true } ` @@ -533,10 +583,10 @@ $($script:testAddedName)=$($script:testText) -Verifiable Mock ` - -CommandName Test-Path ` - -ModuleName 'DSR_KeyValuePairFile' ` - -MockWith { $true } ` - -Verifiable + -CommandName Test-Path ` + -ModuleName 'DSR_KeyValuePairFile' ` + -MockWith { $true } ` + -Verifiable Mock ` -CommandName Get-Content ` @@ -575,7 +625,7 @@ $($script:testAddedName)=$($script:testText) -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable - Mock ` + Mock ` -CommandName Test-Path ` -ModuleName 'DSR_KeyValuePairFile' ` -MockWith { $true } ` @@ -619,7 +669,7 @@ $($script:testAddedName)=$($script:testText) -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable - Mock ` + Mock ` -CommandName Test-Path ` -ModuleName 'DSR_KeyValuePairFile' ` -MockWith { $true } ` @@ -664,7 +714,7 @@ $($script:testAddedName)=$($script:testText) -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable - Mock ` + Mock ` -CommandName Test-Path ` -ModuleName 'DSR_KeyValuePairFile' ` -MockWith { $true } ` @@ -709,7 +759,7 @@ $($script:testAddedName)=$($script:testText) -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable - Mock ` + Mock ` -CommandName Test-Path ` -ModuleName 'DSR_KeyValuePairFile' ` -MockWith { $true } ` @@ -754,7 +804,7 @@ $($script:testAddedName)=$($script:testText) -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable - Mock ` + Mock ` -CommandName Test-Path ` -ModuleName 'DSR_KeyValuePairFile' ` -MockWith { $true } ` @@ -798,7 +848,7 @@ $($script:testAddedName)=$($script:testText) -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable - Mock ` + Mock ` -CommandName Test-Path ` -ModuleName 'DSR_KeyValuePairFile' ` -MockWith { $true } ` @@ -843,7 +893,7 @@ $($script:testAddedName)=$($script:testText) -ModuleName 'DSR_KeyValuePairFile' ` -Verifiable - Mock ` + Mock ` -CommandName Test-Path ` -ModuleName 'DSR_KeyValuePairFile' ` -MockWith { $true } ` @@ -879,6 +929,40 @@ $($script:testAddedName)=$($script:testText) -Exactly 1 } } + + Context 'File exists and does not contain matching key but it should' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_KeyValuePairFile' ` + -Verifiable + + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_KeyValuePairFile' ` + -MockWith { $false } ` + -Verifiable + + It 'Should not throw an exception' { + { $script:result = Test-TargetResource ` + -Path $script:testTextFile ` + -Name $script:testName.ToUpper() ` + -Ensure 'Present' ` + -Text $script:testText ` + -Verbose + } | Should -Not -Throw + } + + It 'Should return false' { + $script:result | Should -Be $false + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + } + } + } #endregion diff --git a/Tests/Unit/DSR_ReplaceText.Tests.ps1 b/Tests/Unit/DSR_ReplaceText.Tests.ps1 index c49062e..b993dee 100644 --- a/Tests/Unit/DSR_ReplaceText.Tests.ps1 +++ b/Tests/Unit/DSR_ReplaceText.Tests.ps1 @@ -255,6 +255,56 @@ Setting3.Test=Value4 -Exactly 1 } } + + Context 'File does not exist' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_ReplaceText' ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $null } ` + -Verifiable + + Mock ` + -CommandName Set-Content ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($value -eq $script:testTextReplace) + } ` + -Verifiable + + It 'Should not throw an exception' { + { Set-TargetResource ` + -Path $script:testTextFile ` + -Search $script:testSearch ` + -Text $script:testTextReplace ` + -Verbose + } | Should -Not -Throw + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -Exactly 1 + + Assert-MockCalled ` + -CommandName Set-Content ` + -ParameterFilter { + ($path -eq $script:testTextFile) -and ` + ($value -eq $script:testTextReplace) + } ` + -Exactly 1 + } + } + } #endregion @@ -486,6 +536,49 @@ Setting3.Test=Value4 -Exactly 1 } } + + Context 'File does not exist' { + # verifiable (should be called) mocks + Mock ` + -CommandName Assert-ParametersValid ` + -ModuleName 'DSR_ReplaceText' ` + -Verifiable + + Mock ` + -CommandName Test-Path ` + -ModuleName 'DSR_ReplaceText' ` + -MockWith { $false } ` + -Verifiable + + Mock ` + -CommandName Get-Content ` + -ParameterFilter { $path -eq $script:testTextFile } ` + -MockWith { $script:testFileContent } + + $script:result = $null + + It 'Should not throw an exception' { + { $script:result = Test-TargetResource ` + -Path $script:testTextFile ` + -Search $script:testSearchNoFind ` + -Text $script:testTextReplace ` + -Verbose + } | Should -Not -Throw + } + + It 'Should return true' { + $script:result | Should -Be $false + } + + It 'Should call the expected mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Assert-ParametersValid -Exactly 1 + + Assert-MockCalled ` + -CommandName Get-Content ` + -Exactly 0 + } + } } #endregion