From 082c3b0baefcb14d77906ea49e5437470cfb97a6 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 23 Oct 2018 16:27:53 -0700 Subject: [PATCH] Enable `Add-Content` to share read access to other tools while writing content (#8091) --- .../namespaces/FileSystemProvider.cs | 2 +- .../Add-Content.Tests.ps1 | 26 ++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 62ccb423a7..1c918ea8c2 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -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) { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Add-Content.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Add-Content.Tests.ps1 index 53ad2499bd..0157e47faf 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Add-Content.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Add-Content.Tests.ps1 @@ -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" + } } }