Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error when KeyValuePair file is empty or does not exist - Fixes #34 #35

Merged
merged 2 commits into from
Jul 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
- Removing uneccesary `#region` blocks.
- Conversion of double quotes to single quotes where possible.
- Replace variables with string litterals in `describe` block description.
- KeyValuePairFile:
- Improve unit tests to simplify and cover additional test cases.
- Fix error occuring when file is empty or does not exist - fixes [Issue #34](https://github.com/PlagueHO/FileContentDsc/issues/34).

## 1.2.0.0

Expand Down
129 changes: 70 additions & 59 deletions DSCResources/DSR_KeyValuePairFile/DSR_KeyValuePairFile.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -39,43 +39,55 @@ function Get-TargetResource

Assert-ParametersValid @PSBoundParameters

$fileContent = Get-Content -Path $Path -Raw
$fileEncoding = Get-FileEncoding -Path $Path

Write-Verbose -Message ($script:localizedData.SearchForKeyMessage -f `
$Path, $Name)

# Setup the Regex Options that will be used
$regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline

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

$ensure = 'Absent'
$text = $null
$fileEncoding = 'ASCII'

if ($results.Count -eq 0)
if (Test-Path -Path $Path)
{
# No matches found
Write-Verbose -Message ($script:localizedData.KeyNotFoundMessage -f `
$Path, $Name)
}
else
{
# One of more key value pairs were found
$ensure = 'Present'
$textValues = @()
$fileContent = Get-Content -Path $Path -Raw
$fileEncoding = Get-FileEncoding -Path $Path

foreach ($match in $results)
if ($null -ne $fileContent)
{
$textValues += $match.Groups[1].Value
}
Write-Verbose -Message ($script:localizedData.SearchForKeyMessage -f $Path, $Name)

$text = ($textValues -join ',')
# Setup the Regex Options that will be used
$regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline

Write-Verbose -Message ($script:localizedData.KeyFoundMessage -f `
$Path, $Name, $text)
} # if
# Search the key that matches the requested key
$results = [regex]::Matches($fileContent, "^[\s]*$Name=([^\n\r]*)", $regExOptions)

if ($results.Count -eq 0)
{
# No matches found
Write-Verbose -Message ($script:localizedData.KeyNotFoundMessage -f $Path, $Name)
}
else
{
# One of more key value pairs were found
$ensure = 'Present'
$textValues = @()

foreach ($match in $results)
{
$textValues += $match.Groups[1].Value
}

$text = ($textValues -join ',')

Write-Verbose -Message ($script:localizedData.KeyFoundMessage -f $Path, $Name, $text)
} # if
}
else
{
Write-Verbose -Message ($script:localizedData.KeyValuePairFileIsEmpty -f $Path)
}
}
else
{
Write-Verbose -Message ($script:localizedData.KeyValuePairFileNotFound -f $Path)
}

return @{
Path = $Path
Expand Down Expand Up @@ -174,16 +186,15 @@ function Set-TargetResource
Assert-ParametersValid @PSBoundParameters

$fileContent = Get-Content -Path $Path -Raw -ErrorAction SilentlyContinue
$fileEncoding = Get-FileEncoding -Path $Path
$fileEncoding = Get-FileEncoding -Path $Path -ErrorAction SilentlyContinue

$fileProperties = @{
Path = $Path
NoNewline = $true
Force = $true
}

Write-Verbose -Message ($script:localizedData.SearchForKeyMessage -f `
$Path, $Name)
Write-Verbose -Message ($script:localizedData.SearchForKeyMessage -f $Path, $Name)

if ($Type -eq 'Secret')
{
Expand Down Expand Up @@ -221,16 +232,14 @@ function Set-TargetResource

$fileContent += $keyValuePair

Write-Verbose -Message ($script:localizedData.KeyAddMessage -f `
$Path, $Name)
Write-Verbose -Message ($script: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 ($script:localizedData.KeyUpdateMessage -f `
$Path, $Name)
Write-Verbose -Message ($script:localizedData.KeyUpdateMessage -f $Path, $Name)
} # if
}
else
Expand All @@ -244,17 +253,15 @@ function Set-TargetResource
}
else
{
Write-Verbose -Message ($script:localizedData.FileEncodingNotInDesiredState -f `
$fileEncoding, $Encoding)
Write-Verbose -Message ($script:localizedData.FileEncodingNotInDesiredState -f $fileEncoding, $Encoding)
}
}
else
{
# The Key exists in the file but should not so remove it
$fileContent = [regex]::Replace($fileContent, "^[\s]*$Name=(.*)$eolChars", '', $regExOptions)

Write-Verbose -Message ($script:localizedData.KeyRemoveMessage -f `
$Path, $Name)
Write-Verbose -Message ($script:localizedData.KeyRemoveMessage -f $Path, $Name)
}
} # if
}
Expand Down Expand Up @@ -360,23 +367,32 @@ function Test-TargetResource

Assert-ParametersValid @PSBoundParameters

# Flag to signal whether settings are correct
[System.Boolean] $desiredConfigurationMatch = $true

# Check if file being managed exists. If not return $False.
<#
If the file being managed does not exist then return true if
the key should be absent or false if it should be present.
#>
if (-not (Test-Path -Path $Path))
{
return $false
Write-Verbose -Message ($script:localizedData.KeyValuePairFileNotFound -f $Path)

return ($Ensure -eq 'Absent')
}

$fileContent = Get-Content -Path $Path -Raw
$fileEncoding = Get-FileEncoding -Path $Path

Write-Verbose -Message ($script:localizedData.SearchForKeyMessage -f `
$Path, $Name)
if ($null -eq $fileContent)
{
Write-Verbose -Message ($script:localizedData.KeyValuePairFileIsEmpty -f $Path)

# Setup the Regex Options that will be used
return ($Ensure -eq 'Absent')
}

$desiredConfigurationMatch = $true
$fileEncoding = Get-FileEncoding -Path $Path
$regExOptions = [System.Text.RegularExpressions.RegexOptions]::Multiline

Write-Verbose -Message ($script:localizedData.SearchForKeyMessage -f $Path, $Name)

if ($IgnoreNameCase)
{
$regExOptions += [System.Text.RegularExpressions.RegexOptions]::IgnoreCase
Expand All @@ -391,16 +407,14 @@ function Test-TargetResource
if ($Ensure -eq 'Present')
{
# The key value pairs should exist but do not
Write-Verbose -Message ($script:localizedData.KeyNotFoundButShouldExistMessage -f `
$Path, $Name)
Write-Verbose -Message ($script:localizedData.KeyNotFoundButShouldExistMessage -f $Path, $Name)

$desiredConfigurationMatch = $false
}
else
{
# The key value pairs should exist and do
Write-Verbose -Message ($script:localizedData.KeyNotFoundAndShouldNotExistMessage -f `
$Path, $Name)
Write-Verbose -Message ($script:localizedData.KeyNotFoundAndShouldNotExistMessage -f $Path, $Name)
} # if
}
else
Expand All @@ -426,15 +440,13 @@ function Test-TargetResource

if ($desiredConfigurationMatch)
{
Write-Verbose -Message ($script:localizedData.KeyFoundButNoReplacementMessage -f `
$Path, $Name)
Write-Verbose -Message ($script:localizedData.KeyFoundButNoReplacementMessage -f $Path, $Name)
}
}
else
{
# The key value pairs should not exist
Write-Verbose -Message ($script:localizedData.KeyFoundButShouldNotExistMessage -f `
$Path, $Name)
Write-Verbose -Message ($script:localizedData.KeyFoundButShouldNotExistMessage -f $Path, $Name)

$desiredConfigurationMatch = $false
} # if
Expand All @@ -443,8 +455,7 @@ function Test-TargetResource
if ($PSBoundParameters.ContainsKey('Encoding') -and ($Encoding -ne $fileEncoding))
{
# File encoding is not in desired state
Write-Verbose -Message ($script:localizedData.FileEncodingNotInDesiredState -f `
$fileEncoding, $Encoding)
Write-Verbose -Message ($script:localizedData.FileEncodingNotInDesiredState -f $fileEncoding, $Encoding)

$desiredConfigurationMatch = $false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ ConvertFrom-StringData @'
KeyNotFoundAndShouldNotExistMessage = Key '{1}' not found in file '{0}' and should not exist. Change not required.
KeyFoundButNoReplacementMessage = Key '{1}' found in file '{0}' and should exist and value(s) are correct. Change not required.
KeyFoundButShouldNotExistMessage = Key '{1}' found in file '{0}' but should not exist. Change required.
KeyValuePairFileNotFound = Key Value Pair file '{0}' not found.
KeyValuePairFileIsEmpty = Key Value Pair file '{0}' is empty.
FileParentNotFoundError = File parent path '{0}' not found.
FileEncodingNotInDesiredState = File encoding is set to '{0}' but should be set to '{1}', Change required.
'@
Loading