From b56f7c012a433e50862e7ae8a3dac9521c3851fb Mon Sep 17 00:00:00 2001 From: Peter Schneider Date: Mon, 16 Mar 2020 19:44:04 +0100 Subject: [PATCH 1/5] Use Pester special drive for temp files and remove testfile in BeforeAll Pester block --- .../commands/utility/Tee-Object.cs | 13 +++++++++ .../Tee-Object.Tests.ps1 | 28 ++++++++++++++----- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Tee-Object.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Tee-Object.cs index 3866c1202ad..64f84c14b13 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Tee-Object.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/Tee-Object.cs @@ -3,6 +3,7 @@ using System; using System.Management.Automation; +using System.Text; using Microsoft.PowerShell.Commands.Internal.Format; @@ -72,6 +73,16 @@ public SwitchParameter Append private bool _append; + /// + /// Gets or sets the Encoding. + /// + [Parameter(ParameterSetName = "File")] + [Parameter(ParameterSetName = "LiteralFile")] + [ArgumentToEncodingTransformationAttribute] + [ArgumentEncodingCompletionsAttribute] + [ValidateNotNullOrEmpty] + public Encoding Encoding { get; set; } = ClrFacade.GetDefaultEncoding(); + /// /// Variable parameter. /// @@ -95,12 +106,14 @@ protected override void BeginProcessing() _commandWrapper.Initialize(Context, "out-file", typeof(OutFileCommand)); _commandWrapper.AddNamedParameter("filepath", _fileName); _commandWrapper.AddNamedParameter("append", _append); + _commandWrapper.AddNamedParameter("encoding", Encoding); } else if (string.Equals(ParameterSetName, "LiteralFile", StringComparison.OrdinalIgnoreCase)) { _commandWrapper.Initialize(Context, "out-file", typeof(OutFileCommand)); _commandWrapper.AddNamedParameter("LiteralPath", _fileName); _commandWrapper.AddNamedParameter("append", _append); + _commandWrapper.AddNamedParameter("encoding", Encoding); } else { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 index daa72e8e634..b472daf0184 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 @@ -4,21 +4,35 @@ Describe "Tee-Object" -Tags "CI" { Context "Validate Tee-Object is correctly forking output" { - $testfile = Join-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath assets) -ChildPath testfile.txt + BeforeAll { + $testfile = Join-Path -Path (Join-Path -Path $TestDrive -ChildPath assets) -ChildPath testfile.txt + $testvalue = [char](244) + if ($IsWindows) { + $expectedBytes = 244,13,10 -join "-" + } else { + $expectedBytes = 244,10 -join "-" + } + Remove-Item -Path $teefile -ErrorAction SilentlyContinue -Force + } It "Should return the output to the screen and to the variable" { - $teefile = $testfile - Write-Output teeobjecttest1 | Tee-Object -variable teeresults - $teeresults | Should -BeExactly "teeobjecttest1" - Remove-Item $teefile -ErrorAction SilentlyContinue + Write-Output teeobjecttest1 | Tee-Object -Variable teeresults + $teeresults | Should -BeExactly "teeobjecttest1" } It "Should tee the output to a file" { $teefile = $testfile Write-Output teeobjecttest3 | Tee-Object $teefile Get-Content $teefile | Should -BeExactly "teeobjecttest3" - Remove-Item $teefile -ErrorAction SilentlyContinue - } + } + + 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 + } } } From b08faa46d3052b42ad584f972f975a5dbb5c038f Mon Sep 17 00:00:00 2001 From: Peter Schneider Date: Sat, 21 Mar 2020 17:14:50 +0100 Subject: [PATCH 2/5] Delte testfile in BeforeEach Pester block --- .../Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 index b472daf0184..323c7c45d42 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 @@ -5,13 +5,16 @@ Describe "Tee-Object" -Tags "CI" { Context "Validate Tee-Object is correctly forking output" { BeforeAll { - $testfile = Join-Path -Path (Join-Path -Path $TestDrive -ChildPath assets) -ChildPath testfile.txt + $testfile = Join-Path $TestDrive -ChildPath testfile.txt $testvalue = [char](244) if ($IsWindows) { $expectedBytes = 244,13,10 -join "-" } else { $expectedBytes = 244,10 -join "-" } + } + + BeforeEach { Remove-Item -Path $teefile -ErrorAction SilentlyContinue -Force } From 3af62f435f17c193e844e1ee7765179a6d9af563 Mon Sep 17 00:00:00 2001 From: Peter Schneider Date: Sat, 21 Mar 2020 18:44:53 +0100 Subject: [PATCH 3/5] Fix CI Co-Authored-By: Ilya --- .../Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 index 323c7c45d42..70fc1b4ae98 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 @@ -15,7 +15,7 @@ Describe "Tee-Object" -Tags "CI" { } BeforeEach { - Remove-Item -Path $teefile -ErrorAction SilentlyContinue -Force + Remove-Item -Path $testfile -ErrorAction SilentlyContinue -Force } It "Should return the output to the screen and to the variable" { From bace4cba71d9168612c449eaf67a32664e9b95b4 Mon Sep 17 00:00:00 2001 From: Peter Schneider Date: Sun, 22 Mar 2020 22:15:28 +0100 Subject: [PATCH 4/5] =?UTF-8?q?Changed=20testValue=20to=20=D1=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 index 70fc1b4ae98..08ac519d039 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 @@ -5,9 +5,10 @@ Describe "Tee-Object" -Tags "CI" { Context "Validate Tee-Object is correctly forking output" { BeforeAll { - $testfile = Join-Path $TestDrive -ChildPath testfile.txt - $testvalue = [char](244) + $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 "-" From 9ce687be1a30ce4c84f79fe4f0dd317f10bf0c23 Mon Sep 17 00:00:00 2001 From: Peter Schneider Date: Thu, 28 May 2020 17:08:18 +0200 Subject: [PATCH 5/5] Address CI failure --- .../Tee-Object.Tests.ps1 | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 index dfb4396c966..7483c4d8004 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Tee-Object.Tests.ps1 @@ -19,16 +19,15 @@ Describe "Tee-Object" -Tags "CI" { 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" - Remove-Item $teefile -ErrorAction SilentlyContinue - } + It "Should return the output to the screen and to the variable" { + Write-Output teeobjecttest1 | Tee-Object -Variable teeresults + $teeresults | Should -BeExactly "teeobjecttest1" + } - It "Should tee the output to a file" { - $teefile = $testfile - Write-Output teeobjecttest3 | Tee-Object $teefile - Get-Content $teefile | Should -BeExactly "teeobjecttest3" + It "Should tee the output to a file" { + $teefile = $testfile + Write-Output teeobjecttest3 | Tee-Object $teefile + Get-Content $teefile | Should -BeExactly "teeobjecttest3" } It "Parameter 'Encoding' should accept encoding" {