2018-02-13 18:23:53 +01:00
|
|
|
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
# Licensed under the MIT License.
|
2017-09-30 01:28:15 +02:00
|
|
|
Describe "Redirection operator now supports encoding changes" -Tags "CI" {
|
|
|
|
BeforeAll {
|
|
|
|
$asciiString = "abc"
|
|
|
|
|
|
|
|
if ( $IsWindows ) {
|
|
|
|
$asciiCR = "`r`n"
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$asciiCR = [string][char]10
|
|
|
|
}
|
|
|
|
|
2017-10-24 04:46:27 +02:00
|
|
|
# If Out-File -Encoding happens to have a default, be sure to
|
2017-09-30 01:28:15 +02:00
|
|
|
# save it away
|
|
|
|
$SavedValue = $null
|
|
|
|
$oldDefaultParameterValues = $psDefaultParameterValues.Clone()
|
|
|
|
$psDefaultParameterValues = @{}
|
|
|
|
}
|
|
|
|
AfterAll {
|
|
|
|
# be sure to tidy up afterwards
|
|
|
|
$global:psDefaultParameterValues = $oldDefaultParameterValues
|
|
|
|
}
|
|
|
|
BeforeEach {
|
|
|
|
# start each test with a clean plate!
|
2017-10-24 04:46:27 +02:00
|
|
|
$psdefaultParameterValues.Remove("Out-File:Encoding")
|
2017-09-30 01:28:15 +02:00
|
|
|
}
|
|
|
|
AfterEach {
|
|
|
|
# end each test with a clean plate!
|
2017-10-24 04:46:27 +02:00
|
|
|
$psdefaultParameterValues.Remove("Out-File:Encoding")
|
2017-09-30 01:28:15 +02:00
|
|
|
}
|
|
|
|
|
2017-10-24 04:46:27 +02:00
|
|
|
It "If encoding is unset, redirection should be UTF8 without bom" {
|
2017-09-30 01:28:15 +02:00
|
|
|
$asciiString > TESTDRIVE:\file.txt
|
2017-10-24 04:46:27 +02:00
|
|
|
$bytes = Get-Content -AsByteStream TESTDRIVE:\file.txt
|
|
|
|
# create the expected - utf8 encoding without a bom
|
|
|
|
$encoding = [Text.UTF8Encoding]::new($false)
|
|
|
|
# we know that there will be no preamble, so don't provide any bytes
|
|
|
|
$TXT = $encoding.GetBytes($asciiString)
|
|
|
|
$CR = $encoding.GetBytes($asciiCR)
|
|
|
|
$expectedBytes = .{ $TXT; $CR }
|
2018-03-21 18:47:08 +01:00
|
|
|
$bytes.Count | Should -Be $expectedBytes.count
|
2017-09-30 01:28:15 +02:00
|
|
|
for($i = 0; $i -lt $bytes.count; $i++) {
|
2018-03-21 18:47:08 +01:00
|
|
|
$bytes[$i] | Should -Be $expectedBytes[$i]
|
2017-09-30 01:28:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# $availableEncodings = "unknown","string","unicode","bigendianunicode","utf8","utf7", "utf32","ascii","default","oem"
|
2017-10-24 04:46:27 +02:00
|
|
|
$availableEncodings = (Get-Command Out-File).Parameters["Encoding"].Attributes.ValidValues
|
2017-09-30 01:28:15 +02:00
|
|
|
|
|
|
|
foreach($encoding in $availableEncodings) {
|
|
|
|
$skipTest = $false
|
|
|
|
if ($encoding -eq "default") {
|
|
|
|
# [System.Text.Encoding]::Default is exposed by 'System.Private.CoreLib.dll' at
|
|
|
|
# runtime via reflection. However,it isn't exposed in the reference contract of
|
|
|
|
# 'System.Text.Encoding', and therefore we cannot use 'Encoding.Default' in our
|
|
|
|
# code. So we need to skip this encoding in the test.
|
|
|
|
$skipTest = $true
|
|
|
|
}
|
|
|
|
|
2017-10-24 04:46:27 +02:00
|
|
|
# some of the encodings accepted by Out-File aren't real,
|
|
|
|
# and Out-File has its own translation, so we'll
|
2017-09-30 01:28:15 +02:00
|
|
|
# not do that logic here, but simply ignore those encodings
|
|
|
|
# as they eventually are translated to "real" encoding
|
2017-10-24 04:46:27 +02:00
|
|
|
$enc = [System.Text.Encoding]::$encoding
|
2017-09-30 01:28:15 +02:00
|
|
|
if ( $enc )
|
|
|
|
{
|
2017-10-24 04:46:27 +02:00
|
|
|
$msg = "Overriding encoding for Out-File is respected for $encoding"
|
2017-09-30 01:28:15 +02:00
|
|
|
$BOM = $enc.GetPreamble()
|
|
|
|
$TXT = $enc.GetBytes($asciiString)
|
|
|
|
$CR = $enc.GetBytes($asciiCR)
|
|
|
|
$expectedBytes = .{ $BOM; $TXT; $CR }
|
2017-10-24 04:46:27 +02:00
|
|
|
$psdefaultparameterValues["Out-File:Encoding"] = "$encoding"
|
2017-09-30 01:28:15 +02:00
|
|
|
$asciiString > TESTDRIVE:/file.txt
|
2017-10-24 04:46:27 +02:00
|
|
|
$observedBytes = Get-Content -AsByteStream TESTDRIVE:/file.txt
|
2017-09-30 01:28:15 +02:00
|
|
|
# THE TEST
|
|
|
|
It $msg -Skip:$skipTest {
|
2018-03-21 18:47:08 +01:00
|
|
|
$observedBytes.Count | Should -Be $expectedBytes.Count
|
2017-09-30 01:28:15 +02:00
|
|
|
for($i = 0;$i -lt $observedBytes.Count; $i++) {
|
2018-03-21 18:47:08 +01:00
|
|
|
$observedBytes[$i] | Should -Be $expectedBytes[$i]
|
2017-09-30 01:28:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Describe "File redirection mixed with Out-Null" -Tags CI {
|
|
|
|
It "File redirection before Out-Null should work" {
|
|
|
|
"some text" > $TestDrive\out.txt | Out-Null
|
2018-03-21 18:47:08 +01:00
|
|
|
Get-Content $TestDrive\out.txt | Should -BeExactly "some text"
|
2017-09-30 01:28:15 +02:00
|
|
|
|
2018-12-28 09:48:23 +01:00
|
|
|
Write-Output "some more text" > $TestDrive\out.txt | Out-Null
|
2018-03-21 18:47:08 +01:00
|
|
|
Get-Content $TestDrive\out.txt | Should -BeExactly "some more text"
|
2017-09-30 01:28:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Describe "File redirection should have 'DoComplete' called on the underlying pipeline processor" -Tags CI {
|
|
|
|
It "File redirection should result in the same file as Out-File" {
|
|
|
|
$object = [pscustomobject] @{ one = 1 }
|
|
|
|
$redirectFile = Join-Path $TestDrive fileRedirect.txt
|
|
|
|
$outFile = Join-Path $TestDrive outFile.txt
|
|
|
|
|
|
|
|
$object > $redirectFile
|
|
|
|
$object | Out-File $outFile
|
|
|
|
|
|
|
|
$redirectFileContent = Get-Content $redirectFile -Raw
|
|
|
|
$outFileContent = Get-Content $outFile -Raw
|
2018-03-21 18:47:08 +01:00
|
|
|
$redirectFileContent | Should -BeExactly $outFileContent
|
2017-09-30 01:28:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "File redirection should not mess up the original pipe" {
|
|
|
|
$outputFile = Join-Path $TestDrive output.txt
|
|
|
|
$otherStreamFile = Join-Path $TestDrive otherstream.txt
|
|
|
|
|
|
|
|
$result = & { $(Get-Command NonExist; 1234) > $outputFile *> $otherStreamFile; "Hello" }
|
2018-03-21 18:47:08 +01:00
|
|
|
$result | Should -BeExactly "Hello"
|
2017-09-30 01:28:15 +02:00
|
|
|
|
|
|
|
$outputContent = Get-Content $outputFile -Raw
|
2018-03-21 18:47:08 +01:00
|
|
|
$outputContent.Trim() | Should -BeExactly '1234'
|
2017-09-30 01:28:15 +02:00
|
|
|
|
|
|
|
$errorContent = Get-Content $otherStreamFile | ForEach-Object { $_.Trim() }
|
|
|
|
$errorContent = $errorContent -join ""
|
2018-03-21 18:47:08 +01:00
|
|
|
$errorContent | Should -Match "CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand"
|
2017-09-30 01:28:15 +02:00
|
|
|
}
|
|
|
|
}
|