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

Add "UTF8BOM" and "UTF8NoBOM" to the acceptable values for Encoding parameter. #59

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
coverage - Fixes [Issue #50](https://github.com/dsccommunity/FileContentDsc/issues/50).
- Automatically publish documentation to GitHub Wiki - Fixes [Issue #51](https://github.com/dsccommunity/FileContentDsc/issues/51).
- Renamed `master` branch to `main` - Fixes [Issue #53](https://github.com/dsccommunity/FileContentDsc/issues/53).
- Added `UTF8BOM` and `UTF8NoBOM` options to the Encoding parameter of the KeyValuePairFile and ReplaceText - Fixes [Issue #56](https://github.com/dsccommunity/FileContentDsc/issues/56).
- Updated `GitVersion.yml` to latest pattern - Fixes [Issue #57](https://github.com/dsccommunity/FileContentDsc/issues/57).
- Updated build to use `Sampler.GitHubTasks` - Fixes [Issue #60](https://github.com/dsccommunity/FileContentDsc/issues/60).
- Added support for publishing code coverage to `CodeCov.io` and
Expand All @@ -37,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- CI Pipeline
- Updated pipeline files to match current DSC Community patterns - fixes [Issue #71](https://github.com/dsccommunity/FileContentDsc/issues/71).
- Updated HQRM and build steps to use windows-latest image.
- Keep GitVersion.Tool as v5

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ stages:
vmImage: 'windows-latest'
steps:
- pwsh: |
dotnet tool install --global GitVersion.Tool
dotnet tool install --global GitVersion.Tool --version 5.*
$gitVersionObject = dotnet-gitversion | ConvertFrom-Json
$gitVersionObject.PSObject.Properties.ForEach{
Write-Host -Object "Setting Task Variable '$($_.Name)' with value '$($_.Value)'."
Expand Down
51 changes: 33 additions & 18 deletions source/DSCResources/DSC_KeyValuePairFile/DSC_KeyValuePairFile.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@

if (Test-Path -Path $Path)
{
$fileContent = Get-Content -Path $Path -Raw
$fileEncoding = Get-FileEncoding -Path $Path
$fileEncoding = Get-FileEncoding $Path -ErrorAction SilentlyContinue
$fileContent = Get-FileContent -Path $Path -Encoding $fileEncoding

if ($null -ne $fileContent)
if (-not [string]::IsNullOrEmpty($fileContent))
{
Write-Verbose -Message ($script:localizedData.SearchForKeyMessage -f $Path, $Name)

Expand Down Expand Up @@ -180,15 +180,15 @@
$IgnoreValueCase = $false,

[Parameter()]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF32')]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
[System.String]
$Encoding
)

Assert-ParametersValid @PSBoundParameters

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

$fileProperties = @{
Path = $Path
Expand Down Expand Up @@ -248,10 +248,12 @@
{
if ($results.Count -eq 0)
{
if ($PSBoundParameters.ContainsKey('Encoding') -and ($Encoding -eq $fileEncoding))
if ($PSBoundParameters.ContainsKey('Encoding') -and `
(Test-FileEncodingEqual -FileEncoding $fileEncoding -ExpectedEncoding $Encoding)
)
{
# The Key does not exists and should not, and encoding is in the desired state, so don't do anything
return
# The Key does not exists and should not, and encoding is in the desired state, so don't do anything
return
}
else
{
Expand Down Expand Up @@ -281,7 +283,7 @@
$fileProperties.Add('Encoding', $Encoding)
}

Set-Content @fileProperties
Set-TextContent @fileProperties
}

<#
Expand Down Expand Up @@ -362,7 +364,7 @@
$IgnoreValueCase = $false,

[Parameter()]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF32')]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
[System.String]
$Encoding
)
Expand All @@ -380,17 +382,17 @@
return ($Ensure -eq 'Absent')
}

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

if ($null -eq $fileContent)
if ([string]::IsNullOrEmpty($fileContent))
{
Write-Verbose -Message ($script:localizedData.KeyValuePairFileIsEmpty -f $Path)

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)
Expand Down Expand Up @@ -456,10 +458,23 @@

if ($PSBoundParameters.ContainsKey('Encoding') -and ($Encoding -ne $fileEncoding))
{
# File encoding is not in desired state
Write-Verbose -Message ($script:localizedData.FileEncodingNotInDesiredState -f $fileEncoding, $Encoding)
if ($Encoding -eq 'UTF8' -and $fileEncoding -like 'UTF8*')
{
#If the Encoding specified as UTF8, Either UTF8NoBOM or UTF8BOM is acceptable
$desiredConfigurationMatch = $true

Check warning on line 464 in source/DSCResources/DSC_KeyValuePairFile/DSC_KeyValuePairFile.psm1

View check run for this annotation

Codecov / codecov/patch

source/DSCResources/DSC_KeyValuePairFile/DSC_KeyValuePairFile.psm1#L464

Added line #L464 was not covered by tests
}
elseif ($Encoding -eq 'UTF8NoBOM' -and $fileEncoding -eq 'ASCII')
{
#If the Encoding specified as UTF8NoBOM, Either UTF8NoBOM or ASCII is acceptable
$desiredConfigurationMatch = $true

Check warning on line 469 in source/DSCResources/DSC_KeyValuePairFile/DSC_KeyValuePairFile.psm1

View check run for this annotation

Codecov / codecov/patch

source/DSCResources/DSC_KeyValuePairFile/DSC_KeyValuePairFile.psm1#L469

Added line #L469 was not covered by tests
}
else
{
# File encoding is not in desired state
Write-Verbose -Message ($script:localizedData.FileEncodingNotInDesiredState -f $fileEncoding, $Encoding)

$desiredConfigurationMatch = $false
$desiredConfigurationMatch = $false
}
}

return $desiredConfigurationMatch
Expand Down Expand Up @@ -543,7 +558,7 @@
$IgnoreValueCase = $false,

[Parameter()]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF32')]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
[System.String]
$Encoding
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ class DSC_KeyValuePairFile : OMI_BaseResource
[write, Description("The secret text to replace the value with in the identified key. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret;
[Write, Description("Ignore the case of the name of the key. Defaults to $False.")] Boolean IgnoreNameCase;
[Write, Description("Ignore the case of any text or secret when determining if it they need to be updated. Defaults to $False.")] Boolean IgnoreValueCase;
[Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32"}] String Encoding;
[Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF8BOM", "UTF8NoBOM", "UTF32"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF8BOM", "UTF8NoBOM", "UTF32"}] String Encoding;
};
56 changes: 24 additions & 32 deletions source/DSCResources/DSC_ReplaceText/DSC_ReplaceText.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@

Assert-ParametersValid @PSBoundParameters

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

Write-Verbose -Message ($script:localizedData.SearchForTextMessage -f `
$Path, $Search)
$Path, $Search)

$text = ''

Expand All @@ -56,14 +56,14 @@
{
# No matches found - already in state
Write-Verbose -Message ($script:localizedData.StringNotFoundMessage -f `
$Path, $Search)
$Path, $Search)
}
else
{
$text = ($results.Value -join ',')

Write-Verbose -Message ($script:localizedData.StringMatchFoundMessage -f `
$Path, $Search, $text)
$Path, $Search, $text)
} # if

return @{
Expand Down Expand Up @@ -136,15 +136,15 @@
$AllowAppend = $false,

[Parameter()]
[ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32")]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
[System.String]
$Encoding
)

Assert-ParametersValid @PSBoundParameters

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

$fileProperties = @{
Path = $Path
Expand All @@ -155,25 +155,17 @@
if ($Type -eq 'Secret')
{
Write-Verbose -Message ($script:localizedData.StringReplaceSecretMessage -f `
$Path)
$Path)

$Text = $Secret.GetNetworkCredential().Password
}
elseif ($PSBoundParameters.ContainsKey('Encoding'))
{
if ($Encoding -eq $fileEncoding)
{
Write-Verbose -Message ($script:localizedData.StringReplaceTextMessage -f `
$Path, $Text)
}
else
{
Write-Verbose -Message ($script:localizedData.StringReplaceTextMessage -f `
$Path, $Text)
Write-Verbose -Message ($script:localizedData.StringReplaceTextMessage -f `

Check warning on line 164 in source/DSCResources/DSC_ReplaceText/DSC_ReplaceText.psm1

View check run for this annotation

Codecov / codecov/patch

source/DSCResources/DSC_ReplaceText/DSC_ReplaceText.psm1#L164

Added line #L164 was not covered by tests
$Path, $Text)

# Add encoding parameter and value to the hashtable
$fileProperties.Add('Encoding', $Encoding)
}
# Add encoding parameter and value to the hashtable
$fileProperties.Add('Encoding', $Encoding)

Check warning on line 168 in source/DSCResources/DSC_ReplaceText/DSC_ReplaceText.psm1

View check run for this annotation

Codecov / codecov/patch

source/DSCResources/DSC_ReplaceText/DSC_ReplaceText.psm1#L168

Added line #L168 was not covered by tests
}

if ($null -eq $fileContent)
Expand All @@ -194,7 +186,7 @@

$fileProperties.Add('Value', $fileContent)

Set-Content @fileProperties
Set-TextContent @fileProperties
}

<#
Expand Down Expand Up @@ -259,7 +251,7 @@
$AllowAppend = $false,

[Parameter()]
[ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32")]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
[System.String]
$Encoding
)
Expand All @@ -272,11 +264,11 @@
return $false
}

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

Write-Verbose -Message ($script:localizedData.SearchForTextMessage -f `
$Path, $Search)
$Path, $Search)

# Search the file content for any matches
$results = [regex]::Matches($fileContent, $Search)
Expand All @@ -293,19 +285,19 @@
}
if ($PSBoundParameters.ContainsKey('Encoding'))
{
if ($Encoding -eq $fileEncoding)
if (Test-FileEncodingEqual -FileEncoding $fileEncoding -ExpectedEncoding $Encoding)
{
# No matches found and encoding is in desired state
Write-Verbose -Message ($script:localizedData.StringNotFoundMessage -f `
$Path, $Search)
$Path, $Search)

return $true
}
else
{
# No matches found but encoding is not in desired state
Write-Verbose -Message ($script:localizedData.FileEncodingNotInDesiredState -f `
$fileEncoding, $Encoding)
$fileEncoding, $Encoding)

return $false
}
Expand All @@ -331,12 +323,12 @@
if ($desiredConfigurationMatch)
{
Write-Verbose -Message ($script:localizedData.StringNoReplacementMessage -f `
$Path, $Search)
$Path, $Search)
}
else
{
Write-Verbose -Message ($script:localizedData.StringReplacementRequiredMessage -f `
$Path, $Search)
$Path, $Search)
} # if

return $desiredConfigurationMatch
Expand Down Expand Up @@ -401,7 +393,7 @@
$AllowAppend = $false,

[Parameter()]
[ValidateSet("ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32")]
[ValidateSet('ASCII', 'BigEndianUnicode', 'BigEndianUTF32', 'UTF8', 'UTF8BOM', 'UTF8NoBOM', 'UTF32')]
[System.String]
$Encoding
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ class DSC_ReplaceText : OMI_BaseResource
[Write, Description("The text to replace the text identified by the RegEx. Only used when Type is set to 'Text'.")] String Text;
[Write, Description("The secret text to replace the text identified by the RegEx. Only used when Type is set to 'Secret'."),EmbeddedInstance("MSFT_Credential")] String Secret;
[Write, Description("Specifies to append text to the file being modified. Adds the ability to add a configuration entry.")] Boolean AllowAppend;
[Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF32"}] String Encoding;
[Write, Description("Specifies the file encoding. Defaults to ASCII"),ValueMap{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF8BOM", "UTF8NoBOM", "UTF32"},Values{"ASCII", "BigEndianUnicode", "BigEndianUTF32", "UTF8", "UTF8BOM", "UTF8NoBOM", "UTF32"}] String Encoding;
};
Loading