Skip to content
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
4 changes: 4 additions & 0 deletions Source/Private/SetModuleContent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ function SetModuleContent {
)
begin {
Write-Debug "SetModuleContent WorkingDirectory $WorkingDirectory"
# Windows PowerShell's Set-Content has no UTF8Bom encoding, but its UTF8 encoding writes a BOM
if (-not $IsCoreCLR -and $Encoding -eq "UTF8Bom") {
$Encoding = "UTF8"
}
Write-Debug "Encoding $Encoding"
Push-Location $WorkingDirectory -StackName SetModuleContent
$ContentStarted = $false # There has been no content yet
Expand Down
4 changes: 4 additions & 0 deletions Source/Public/ConvertTo-Script.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ function ConvertTo-Script {
}
}

# Windows PowerShell's Set-Content has no UTF8Bom encoding, but its UTF8 encoding writes a BOM
if (-not $IsCoreCLR -and $Encoding -eq "UTF8Bom") {
$Encoding = "UTF8"
}
$SetContentCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Management\Set-Content', [System.Management.Automation.CommandTypes]::Cmdlet)
Write-Debug " EXIT: ConvertTo-Script BEGIN"
}
Expand Down
4 changes: 4 additions & 0 deletions Source/Public/Invoke-ScriptGenerator.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ function Invoke-ScriptGenerator {
$null = $AstParam.Remove("Encoding")
$ParseResults = ConvertToAst @AstParam
[StringBuilder]$Builder = $ParseResults.Ast.Extent.Text
# Windows PowerShell's Set-Content has no UTF8Bom encoding, but its UTF8 encoding writes a BOM
if (-not $IsCoreCLR -and $Encoding -eq "UTF8Bom") {
$Encoding = "UTF8"
}
$SetContentCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Management\Set-Content', [System.Management.Automation.CommandTypes]::Cmdlet)
}
process {
Expand Down
32 changes: 32 additions & 0 deletions Tests/Integration/Source1.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,35 @@ Describe "Regression test for #125: SourceDirectories supports wildcards" -Tag I
$Metadata.FunctionsToExport | Should -Be @("Get-Source", "Set-Source")
}
}

Describe "Regression test for UTF8Bom encoding on Windows PowerShell" -Tag Integration, Regression {
BeforeAll {
# The non-ASCII string returned by Source1's Set-Source
$NonAscii = "sto$([char]0x35E)o$([char]0x2032)p$([char]0x12D)d"

Copy-Item $PSScriptRoot\Source1 TestDrive:\Utf8BomParameter -Recurse
$ParameterOutput = Build-Module TestDrive:\Utf8BomParameter -Encoding UTF8Bom -OutputDirectory "$TestDrive\Utf8BomParameterOutput" -Passthru -ErrorAction Stop
$ParameterModule = [IO.Path]::ChangeExtension($ParameterOutput.Path, "psm1")

# Values from build.psd1 are not checked by the Encoding parameter's ValidateSet
Copy-Item $PSScriptRoot\Source1 TestDrive:\Utf8BomManifest -Recurse
$metadata = Import-Metadata TestDrive:\Utf8BomManifest\build.psd1
$metadata["Encoding"] = "UTF8Bom"
$metadata["OutputDirectory"] = "$TestDrive\Utf8BomManifestOutput"
$metadata | Export-Metadata TestDrive:\Utf8BomManifest\build.psd1
$ManifestOutput = Build-Module TestDrive:\Utf8BomManifest -Passthru -ErrorAction Stop
$ManifestModule = [IO.Path]::ChangeExtension($ManifestOutput.Path, "psm1")
}

It "Writes a BOM and UTF-8 content when UTF8Bom is passed as a parameter" {
$Bytes = [IO.File]::ReadAllBytes($ParameterModule)
$Bytes[0..2] | Should -Be @(0xEF, 0xBB, 0xBF)
[IO.File]::ReadAllText($ParameterModule, [Text.Encoding]::UTF8) | Should -Match ([regex]::Escape($NonAscii))
}

It "Writes a BOM and UTF-8 content when UTF8Bom is set in build.psd1" {
$Bytes = [IO.File]::ReadAllBytes($ManifestModule)
$Bytes[0..2] | Should -Be @(0xEF, 0xBB, 0xBF)
[IO.File]::ReadAllText($ManifestModule, [Text.Encoding]::UTF8) | Should -Match ([regex]::Escape($NonAscii))
}
}
7 changes: 7 additions & 0 deletions Tests/Public/ConvertTo-Script.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,12 @@ Describe "ConvertTo-Script" {
$ScriptFileCommand = Get-Command $ScriptFile
$ScriptFileCommand.Parameters.Keys | Sort-Object | Should -Be ($BuildModuleCommand.Parameters.Keys | Sort-Object)
}

It "Accepts UTF8Bom encoding on every edition" {
Remove-Item $ScriptFile -ErrorAction Ignore
$null = Invoke-ScriptGenerator -Path $ModuleBuilder.Path -Generator "ConvertTo-Script" -Parameters @{ FunctionName = "Build-Module"; Encoding = "UTF8Bom" } -ErrorAction Stop

$ScriptFile | Should -Exist
}
}
}