Files
PowerShell/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1
T

95 lines
3.6 KiB
PowerShell
Raw Normal View History

# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
2016-07-25 14:01:41 -07:00
Describe "Tee-Object" -Tags "CI" {
2015-11-19 06:23:45 +00:00
Context "Validate Tee-Object is correctly forking output" {
2021-07-22 06:26:07 +02:00
BeforeAll {
$testfile = Join-Path $TestDrive -ChildPath "testfile.txt"
$testvalue = "ф"
if ($IsWindows) {
# Expected bytes: 244 - 'ф', 13 - '`r', 10 - '`n'.
$expectedBytes = 244,13,10 -join "-"
} else {
$expectedBytes = 244,10 -join "-"
}
}
BeforeEach {
Remove-Item -Path $testfile -ErrorAction SilentlyContinue -Force
}
It "Should return the output to the screen and to the variable" {
Write-Output teeobjecttest1 | Tee-Object -Variable teeresults
$teeresults | Should -BeExactly "teeobjecttest1"
}
2015-11-19 06:23:45 +00:00
It "Should tee the output to a file" {
$teefile = $testfile
Write-Output teeobjecttest3 | Tee-Object $teefile
Get-Content $teefile | Should -BeExactly "teeobjecttest3"
}
$unicodeTestString = "A1£ę௸🤔"
It "Should tee output to file using <encoding> encoding when selected" -TestCases @(
@{ Encoding = "ascii"; Content = "teeobjecttest1"},
@{ Encoding = "bigendianunicode"; Content = $unicodeTestString },
@{ Encoding = "default"; Content = $unicodeTestString },
@{ Encoding = "latin1"; Content = "téèõbjêcttëst1" },
@{ Encoding = "unicode"; Content = $unicodeTestString },
@{ Encoding = "utf32"; Content = $unicodeTestString },
@{ Encoding = "utf7"; Content = $unicodeTestString},
@{ Encoding = "utf8"; Content = $unicodeTestString}
) {
param($Encoding, $Content)
$teefile = $testfile
Write-Output -InputObject $content | Tee-Object -FilePath $teefile -Encoding $Encoding
Get-Content -Path $teefile -Encoding $Encoding | Should -BeExactly $content
Remove-Item -Path $teefile -ErrorAction SilentlyContinue
2021-07-22 06:26:07 +02:00
}
It "Parameter 'Encoding' should accept encoding" {
$teefile = $testfile
$encoding = 1251
$testvalue | Tee-Object -Encoding $encoding $teefile
Get-Content $teefile -Encoding $encoding | Should -BeExactly $testvalue
(Get-Content $teefile -AsByteStream) -join "-" | Should -BeExactly $expectedBytes
}
2015-11-19 06:23:45 +00:00
}
}
2016-05-30 02:35:04 -07:00
2016-07-25 14:01:41 -07:00
Describe "Tee-Object DRT Unit Tests" -Tags "CI" {
2016-06-01 19:44:02 -07:00
BeforeAll {
$tempFile = Join-Path $TestDrive -ChildPath "TeeObjectTestsTempFile"
}
2016-05-30 02:35:04 -07:00
It "Positive File Test" {
$expected = "1", "2", "3"
2016-06-01 19:44:02 -07:00
$results = $expected | Tee-Object -FilePath $tempFile
$results.Length | Should -Be 3
$results | Should -Be $expected
2016-06-01 19:44:02 -07:00
$content = Get-Content $tempFile
$content | Should -Be $expected
2016-05-30 02:35:04 -07:00
}
It "Positive File Test with Path parameter alias" {
$expected = "1", "2", "3"
$results = $expected | Tee-Object -Path $tempFile
$results.Length | Should -Be 3
$results | Should -Be $expected
$content = Get-Content $tempFile
$content | Should -Be $expected
}
2016-06-01 19:44:02 -07:00
It "Positive Variable Test" {
2017-01-16 13:31:14 -08:00
$expected = "1", "2", "3"
2016-05-30 02:35:04 -07:00
$varName = "teeObjectTestVar"
$results = $expected | Tee-Object -Variable $varName
$results.Length | Should -Be 3
$results | Should -Be $expected
2017-01-16 13:31:14 -08:00
2016-05-30 02:35:04 -07:00
$results = Get-Variable -Name $varName -ValueOnly
$results.Length | Should -Be 3
$results | Should -Be $expected
2016-05-30 02:35:04 -07:00
}
2016-07-25 14:01:41 -07:00
}