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/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 diff --git a/DSCResources/DSR_IniSettingsFile/DSR_IniSettingsFile.psm1 b/DSCResources/DSR_IniSettingsFile/DSR_IniSettingsFile.psm1 index 7960b9a..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 ` @@ -220,6 +225,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,11 +317,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_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 3e4d670..edf9d4d 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,11 +497,12 @@ function Assert-ParametersValid $IgnoreValueCase = $false ) - # 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 $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..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 @@ -440,6 +521,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 +548,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 +563,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 9927bcf..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,6 +538,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 +582,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 +625,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 +669,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 +714,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 +759,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 +804,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 +848,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 +862,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 +893,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 } ` @@ -825,13 +929,53 @@ $($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 #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 +984,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 +996,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 } ` @@ -861,14 +1011,14 @@ $($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' { { Assert-ParametersValid ` - -Path $script:testTextFile ` - -Name $script:testName ` - -Verbose + -Path $script:testTextFile ` + -Name $script:testName ` + -Verbose } | Should -Throw $errorRecord } diff --git a/Tests/Unit/DSR_ReplaceText.Tests.ps1 b/Tests/Unit/DSR_ReplaceText.Tests.ps1 index 20435a3..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 @@ -267,6 +317,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 +362,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 +407,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 +452,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 +498,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 } ` @@ -456,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 @@ -463,6 +586,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 +612,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 +627,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' {