From 8ca9c6d553fca31494e106f9896e569cdf05c510 Mon Sep 17 00:00:00 2001 From: Xavier Hahn Date: Mon, 26 Oct 2020 22:44:19 +0100 Subject: [PATCH 1/5] Start transcript fix non append encoding When the -Append file has not been set by the user, the file is emptied using WriteAllText. This method seems to keep the encoding of the file if it existed prior. We don't want this behavior and want to be sure that the UTF8NoBom is used. It calls the override with the encoding set. --- .../host/msh/StartTranscriptCmdlet.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/StartTranscriptCmdlet.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/StartTranscriptCmdlet.cs index 3f413df76ef..a58f3fae297 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/StartTranscriptCmdlet.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/StartTranscriptCmdlet.cs @@ -228,7 +228,7 @@ protected override void BeginProcessing() // If they didn't specify -Append, empty the file if (!_shouldAppend) { - System.IO.File.WriteAllText(effectiveFilePath, string.Empty); + System.IO.File.WriteAllText(effectiveFilePath, string.Empty, Utils.utf8NoBom); } } From 4758b70268dc3a0f946eae082bde43fcdf79cac7 Mon Sep 17 00:00:00 2001 From: Xavier Hahn Date: Thu, 29 Oct 2020 22:12:09 +0100 Subject: [PATCH 2/5] Add unit test to check encoding of start-transcript files --- .../Start-Transcript.Tests.ps1 | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 index d350c44d80c..02748828501 100644 --- a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 @@ -47,6 +47,17 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { } ## function ends here + $defaultEncoding = New-Object -TypeName System.Text.UTF8Encoding -ArgumentList $true + function GetFileEncoding { + param ( + [string] $filePath + ) + + $reader = New-Object -TypeName System.IO.StreamReader -ArgumentList $filePath,$defaultEncoding,$true + $reader.Read() | Out-Null + return $reader.CurrentEncoding.BodyName + } + $transcriptFilePath = Join-Path $TestDrive "transcriptdata.txt" Remove-Item $transcriptFilePath -Force -ErrorAction SilentlyContinue } @@ -129,6 +140,35 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { # Nothing should have been returned by the FileSystemWatcher Receive-Job $job | Should -Be $null } + It "Should keep the existing file's encoding if the 'Append' parameter is used"{ + $transcriptFilePath = Join-Path $TestDrive ([System.IO.Path]::GetRandomFileName()) + "Text" | Out-File -Encoding unicode $transcriptFilePath + + Start-Transcript -Append -Path $transcriptFilePath + Get-Date | Out-Null + Stop-Transcript + + GetFileEncoding $transcriptFilePath | Should -Be 'utf-16' + } + It "Should default to utf8 with 'Append' parameter and file doesn't exist"{ + $transcriptFilePath = Join-Path $TestDrive ([System.IO.Path]::GetRandomFileName()) + + Start-Transcript -Append -Path $transcriptFilePath + Get-Date | Out-Null + Stop-Transcript + + GetFileEncoding $transcriptFilePath | Should -Be 'utf-8' + } + It "Should create a new utf8 encoded file if no 'Append' parameter is set regardless of existing file's encoding"{ + $transcriptFilePath = Join-Path $TestDrive ([System.IO.Path]::GetRandomFileName()) + "Text" | Out-File -Encoding unicode $transcriptFilePath + + Start-Transcript -Path $transcriptFilePath + Get-Date | Out-Null + Stop-Transcript + + GetFileEncoding $transcriptFilePath | Should -Be 'utf-8' + } It "Transcription should remain active if other runspace in the host get closed" { try { $ps = [powershell]::Create() From 39b2203a2d1951dc2ebb3f9fb0c4003a8894f96a Mon Sep 17 00:00:00 2001 From: Xavier Hahn Date: Fri, 30 Oct 2020 07:59:34 +0100 Subject: [PATCH 3/5] Use BeExactly to compare strings --- .../Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 index 02748828501..7dea8b61f36 100644 --- a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 @@ -148,7 +148,7 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { Get-Date | Out-Null Stop-Transcript - GetFileEncoding $transcriptFilePath | Should -Be 'utf-16' + GetFileEncoding $transcriptFilePath | Should -BeExactly 'utf-16' } It "Should default to utf8 with 'Append' parameter and file doesn't exist"{ $transcriptFilePath = Join-Path $TestDrive ([System.IO.Path]::GetRandomFileName()) @@ -157,7 +157,7 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { Get-Date | Out-Null Stop-Transcript - GetFileEncoding $transcriptFilePath | Should -Be 'utf-8' + GetFileEncoding $transcriptFilePath | Should -BeExactly 'utf-8' } It "Should create a new utf8 encoded file if no 'Append' parameter is set regardless of existing file's encoding"{ $transcriptFilePath = Join-Path $TestDrive ([System.IO.Path]::GetRandomFileName()) @@ -167,7 +167,7 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { Get-Date | Out-Null Stop-Transcript - GetFileEncoding $transcriptFilePath | Should -Be 'utf-8' + GetFileEncoding $transcriptFilePath | Should -BeExactly 'utf-8' } It "Transcription should remain active if other runspace in the host get closed" { try { From c1e8e82de44df3ad6c75663a2e8448391121e2d8 Mon Sep 17 00:00:00 2001 From: Xavier Hahn Date: Tue, 15 Dec 2020 22:11:31 +0100 Subject: [PATCH 4/5] Change the syntax used to create the UTF8Encoding in unit tests Co-authored-by: Steve Lee --- .../Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 index 7dea8b61f36..d9cd2e53632 100644 --- a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 @@ -47,13 +47,13 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { } ## function ends here - $defaultEncoding = New-Object -TypeName System.Text.UTF8Encoding -ArgumentList $true + $defaultEncoding = [System.Text.UTF8Encoding]::new($true) function GetFileEncoding { param ( [string] $filePath ) - $reader = New-Object -TypeName System.IO.StreamReader -ArgumentList $filePath,$defaultEncoding,$true + $reader = [System.IO.StreamReader]::new($filePath, $defaultEncoding, $true) $reader.Read() | Out-Null return $reader.CurrentEncoding.BodyName } From 353acdb3ec8292235878bfe6068f5a30a3e6c3ac Mon Sep 17 00:00:00 2001 From: Xavier Hahn Date: Thu, 17 Dec 2020 22:14:15 +0100 Subject: [PATCH 5/5] Remove the creation of the random test file path --- .../Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 index d9cd2e53632..00b773608d7 100644 --- a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 @@ -141,7 +141,6 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { Receive-Job $job | Should -Be $null } It "Should keep the existing file's encoding if the 'Append' parameter is used"{ - $transcriptFilePath = Join-Path $TestDrive ([System.IO.Path]::GetRandomFileName()) "Text" | Out-File -Encoding unicode $transcriptFilePath Start-Transcript -Append -Path $transcriptFilePath @@ -151,8 +150,6 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { GetFileEncoding $transcriptFilePath | Should -BeExactly 'utf-16' } It "Should default to utf8 with 'Append' parameter and file doesn't exist"{ - $transcriptFilePath = Join-Path $TestDrive ([System.IO.Path]::GetRandomFileName()) - Start-Transcript -Append -Path $transcriptFilePath Get-Date | Out-Null Stop-Transcript @@ -160,7 +157,6 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { GetFileEncoding $transcriptFilePath | Should -BeExactly 'utf-8' } It "Should create a new utf8 encoded file if no 'Append' parameter is set regardless of existing file's encoding"{ - $transcriptFilePath = Join-Path $TestDrive ([System.IO.Path]::GetRandomFileName()) "Text" | Out-File -Encoding unicode $transcriptFilePath Start-Transcript -Path $transcriptFilePath