From d528bf5d3df65655a027cdb46d6734f91518bc7b Mon Sep 17 00:00:00 2001 From: Eugene Samoylov Date: Tue, 15 Oct 2019 00:32:37 +0000 Subject: [PATCH] Fix for `Get-Content -ReadCount 0` behavior when `-TotalCount` is set (#10749) --- .../commands/management/GetContentCommand.cs | 2 +- .../Microsoft.PowerShell.Management/Get-Content.Tests.ps1 | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/GetContentCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/GetContentCommand.cs index bc1e87042..bf7d6037b 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/GetContentCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/GetContentCommand.cs @@ -206,7 +206,7 @@ namespace Microsoft.PowerShell.Commands // I am using TotalCount - countToRead so that I don't // have to worry about overflow - if ((TotalCount > 0) && (TotalCount - countToRead < countRead)) + if ((TotalCount > 0) && (countToRead == 0 || (TotalCount - countToRead < countRead))) { countToRead = TotalCount - countRead; } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Content.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Content.Tests.ps1 index 4c4e9337f..ffb198daa 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Content.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Get-Content.Tests.ps1 @@ -342,6 +342,12 @@ baz $expected[1] = $thirdLine Compare-Object -ReferenceObject $expected -DifferenceObject $result | Should -BeNullOrEmpty } + It "Should return the same number of first lines as set in -TotalCount at one time for -ReadCount 0" { + $result = Get-Content -Path $testPath -TotalCount 3 -ReadCount 0 + $result.Length | Should -Be 3 + $expected = $firstLine,$secondLine,$thirdLine + Compare-Object -ReferenceObject $expected -DifferenceObject $result | Should -BeNullOrEmpty + } It "A warning should be emitted if both -AsByteStream and -Encoding are used together" { [byte[]][char[]]"test" | Set-Content -Encoding Unicode -AsByteStream "${TESTDRIVE}\bfile.txt" -WarningVariable contentWarning *> $null $contentWarning.Message | Should -Match "-AsByteStream"