When using Start-Transcript and file exists, empty file rather than deleting (#8131)

This commit is contained in:
Paal Braathen
2018-11-07 08:34:42 +05:00
committed by Ilya
parent e702119177
commit 2e89efa1bc
2 changed files with 23 additions and 2 deletions
@@ -212,10 +212,10 @@ namespace Microsoft.PowerShell.Commands
}
}
// 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);
}
}
@@ -108,6 +108,27 @@ 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"
}
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
}
It "Transcription should remain active if other runspace in the host get closed" {
try {
$ps = [powershell]::Create()