Files
PowerShell-PowerShell/test/powershell/engine/Basic/Encoding.Tests.ps1
T
James Truher [MSFT] be700729d6 Unify cmdlets with parameter 'Encoding' to be of type System.Text.Encoding (#5080)
This unifies file encoding across the inbox cmdlets to be UTF-8 without a BOM for all platforms. This is a breaking change as cmdlets on windows have a number of different encodings. This supports better interoperability with tradition Linux shells as we are using the same encoding.

Validate that files are created with UTF-8 encoding without BOM
Update tests to validate Encoding parameter to new type and create new tests for
parameter type validation.

[Breaking Change] The '-Encoding Byte' has been removed from the filesystem provider cmdlets. A new parameter '-AsByteStream' is now added to indicate that a byte stream is required as input, or output will be a stream of bytes.
2017-10-23 19:46:27 -07:00

74 lines
3.5 KiB
PowerShell

Describe "File encoding tests" -Tag CI {
Context "ParameterType for parameter 'Encoding' should be 'Encoding'" {
BeforeAll {
$testCases = Get-Command -Module Microsoft.PowerShell.* |
Where-Object { $_.Parameters -and $_.Parameters['Encoding'] } |
ForEach-Object { @{ Command = $_ } }
}
It "Encoding parameter of command '<Command>' is type 'Encoding'" -Testcase $testCases {
param ( $command )
$command.Parameters['Encoding'].ParameterType.FullName | Should BeExactly "System.Text.Encoding"
}
}
Context "File contents are UTF8 without BOM" {
BeforeAll {
$testStr = "t" + ([char]233) + "st"
$nl = [environment]::newline
$utf8Bytes = 116,195,169,115,116
$nlBytes = [byte[]][char[]]$nl
$ExpectedWithNewline = $( $utf8Bytes ; $nlBytes )
$outputFile = "${TESTDRIVE}/file.txt"
$utf8Preamble = [text.encoding]::UTF8.GetPreamble()
$simpleTestCases = @(
# New-Item does not add CR/NL
@{ Command = "New-Item"; Parameters = @{ type = "file";value = $testStr; Path = $outputFile }; Expected = $utf8Bytes ; Operator = "be" }
# the following commands add a CR/NL
@{ Command = "Set-Content"; Parameters = @{ value = $testStr; Path = $outputFile }; Expected = $ExpectedWithNewline ; Operator = "be" }
@{ Command = "Add-Content"; Parameters = @{ value = $testStr; Path = $outputFile }; Expected = $ExpectedWithNewline ; Operator = "be" }
@{ Command = "Out-File"; Parameters = @{ InputObject = $testStr; Path = $outputFile }; Expected = $ExpectedWithNewline ; Operator = "be" }
# Redirection
@{ Command = { $testStr > $outputFile } ; Expected = $ExpectedWithNewline ; Operator = "be" }
)
function Get-FileBytes ( $path ) {
[io.file]::ReadAllBytes($path)
}
}
AfterEach {
if ( Test-Path $outputFile ) {
Remove-Item -Force $outputFile
}
}
It "<command> produces correct content '<Expected>'" -Testcases $simpleTestCases {
param ( $Command, $parameters, $Expected, $Operator)
& $command @parameters
$bytes = Get-FileBytes $outputFile
$bytes -join "-" | should ${Operator} ($Expected -join "-")
}
It "Export-CSV creates file with UTF-8 encoding without BOM" {
[pscustomobject]@{ Key = $testStr } | Export-Csv $outputFile
$bytes = Get-FileBytes $outputFile
$bytes[0,1,2] -join "-" | should not be ($utf8Preamble -join "-")
$bytes -join "-" | should match ($utf8bytes -join "-")
}
It "Export-CliXml creates file with UTF-8 encoding without BOM" {
[pscustomobject]@{ Key = $testStr } | Export-Clixml $outputFile
$bytes = Get-FileBytes $outputFile
$bytes[0,1,2] -join "-" | should not be ($utf8Preamble -join "-")
$bytes -join "-" | should match ($utf8bytes -join "-")
}
It "Appends correctly on non-Windows systems" -Skip:$IsWindows {
bash -c "echo '${testStr}' > $outputFile"
${testStr} >> $outputFile
$bytes = Get-FileBytes $outputFile
$Expected = $( $ExpectedWithNewline; $ExpectedWithNewline )
$bytes -join "-" | should be ($Expected -join "-")
}
}
}