mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
When using Start-Transcript and file exists, empty file rather than deleting (#8131)
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user