From 5ba78ae53ff70877fd079051751ea4ecc977b71f Mon Sep 17 00:00:00 2001 From: Paal Braathen Date: Fri, 26 Oct 2018 14:13:01 +0200 Subject: [PATCH 1/4] Avoid file deletion in Start-Transcript when not appending --- .../host/msh/StartTranscriptCmdlet.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/StartTranscriptCmdlet.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/StartTranscriptCmdlet.cs index 5453731ba69..ae58c3b65f0 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/StartTranscriptCmdlet.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/StartTranscriptCmdlet.cs @@ -212,10 +212,10 @@ protected override void BeginProcessing() } } - // If they didn't specify -Append, delete the file + // If they didn't specify -Append, empty the file if (!_shouldAppend) { - System.IO.File.Delete(effectiveFilePath); + System.IO.File.WriteAllText(effectiveFilePath, String.Empty); } } From a5c12d449909c50a2163221cefe6a928bca4280b Mon Sep 17 00:00:00 2001 From: Paal Braathen Date: Fri, 26 Oct 2018 18:28:12 +0200 Subject: [PATCH 2/4] Use the built-in type alias 'string' rather than String or System.String. --- .../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 ae58c3b65f0..52ff019906c 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/StartTranscriptCmdlet.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/StartTranscriptCmdlet.cs @@ -215,7 +215,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); } } From 64e2b82006da7d40c0dd46e97d3a31ebd56024d8 Mon Sep 17 00:00:00 2001 From: Paal Braathen Date: Sat, 3 Nov 2018 17:20:10 +0100 Subject: [PATCH 3/4] Add file deletion test in Start-Transcript.Tests --- .../Start-Transcript.Tests.ps1 | 19 +++++++++++++++++++ 1 file changed, 19 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 423c4176eaa..358c6cb0c4f 100644 --- a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 @@ -108,6 +108,25 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { $expectedError = "CannotStartTranscription,Microsoft.PowerShell.Commands.StartTranscriptCommand" ValidateTranscription -scriptToExecute $script -outputFilePath $null -expectedError $expectedError } + It "Should not delete the file if it already exist" { + # Create an existing file + $transcriptFilePath = Join-Path $TestDrive ([System.IO.Path]::GetRandomFileName()) + Out-File $transcriptFilePath + + $FileSystemWatcher = [System.IO.FileSystemWatcher]::new((Split-Path -Parent $transcriptFilePath), (Split-Path -Leaf $transcriptFilePath)) + + $Job = Register-ObjectEvent -InputObject $FileSystemWatcher -EventName "Deleted" -SourceIdentifier "FileDeleted" -Action { + return "FileDeleted" + } + + Start-Transcript -Path $transcriptFilePath + Stop-Transcript + + Unregister-Event -SourceIdentifier "FileDeleted" + + # Nothing should have been returned by the FileSystemWatcher + Receive-Job $job | Should -Be $null + } It "Transcription should remain active if other runspace in the host get closed" { try { $ps = [powershell]::Create() From 5e31313e693edc475eab749a2d17e1df20ef4860 Mon Sep 17 00:00:00 2001 From: Paal Braathen Date: Tue, 6 Nov 2018 22:26:58 +0100 Subject: [PATCH 4/4] Wrap Start-Transcript part in try-catch-finally in Start-Transcript.Tests --- .../Start-Transcript.Tests.ps1 | 10 ++++++---- 1 file changed, 6 insertions(+), 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 358c6cb0c4f..ade669dcc96 100644 --- a/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 @@ -119,10 +119,12 @@ Describe "Start-Transcript, Stop-Transcript tests" -tags "CI" { return "FileDeleted" } - Start-Transcript -Path $transcriptFilePath - Stop-Transcript - - Unregister-Event -SourceIdentifier "FileDeleted" + try { + Start-Transcript -Path $transcriptFilePath + Stop-Transcript + } finally { + Unregister-Event -SourceIdentifier "FileDeleted" + } # Nothing should have been returned by the FileSystemWatcher Receive-Job $job | Should -Be $null