Enable Add-Content to share read access to other tools while writing content (#8091)

This commit is contained in:
Steve Lee 2018-10-23 16:27:53 -07:00 committed by Aditya Patwardhan
parent bf2667352a
commit 082c3b0bae
2 changed files with 24 additions and 4 deletions

View file

@ -6530,7 +6530,7 @@ namespace Microsoft.PowerShell.Commands
return stream;
}
stream = new FileSystemContentReaderWriter(path, streamName, filemode, FileAccess.Write, FileShare.Write, encoding, usingByteEncoding, false, this, false, suppressNewline);
stream = new FileSystemContentReaderWriter(path, streamName, filemode, FileAccess.Write, FileShare.ReadWrite, encoding, usingByteEncoding, false, this, false, suppressNewline);
}
catch (PathTooLongException pathTooLong)
{

View file

@ -1,8 +1,11 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
Describe "Add-Content cmdlet tests" -Tags "CI" {
$file1 = "file1.txt"
Setup -File "$file1"
BeforeAll {
$file1 = "file1.txt"
Setup -File "$file1"
}
Context "Add-Content should actually add content" {
It "should Add-Content to TestDrive:\$file1" {
@ -40,7 +43,7 @@ Describe "Add-Content cmdlet tests" -Tags "CI" {
{ Add-Content -Path $() -Value "ShouldNotWorkBecausePathIsInvalid" -ErrorAction Stop } | Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentCommand"
}
It "Should throw an error on a directory" {
It "Should throw an error on a directory" {
{ Add-Content -Path . -Value "WriteContainerContentException" -ErrorAction Stop } | Should -Throw -ErrorId "WriteContainerContentException,Microsoft.PowerShell.Commands.AddContentCommand"
}
@ -57,5 +60,22 @@ Describe "Add-Content cmdlet tests" -Tags "CI" {
$result[0] | Should -BeExactly "hello"
$result[1] | Should -BeExactly "world"
}
It "Should not block reads while writing" {
$logpath = Join-Path $testdrive "test.log"
Set-Content $logpath -Value "hello"
$f = [System.IO.FileStream]::new($logpath, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::ReadWrite)
Add-Content $logpath -Value "world"
$f.Close()
$content = Get-Content $logpath
$content | Should -HaveCount 2
$content[0] | Should -BeExactly "hello"
$content[1] | Should -BeExactly "world"
}
}
}