Skip to content

Get-Content -Raw neglects to read the last character, if it is a LF #4980

@mklement0

Description

@mklement0

Steps to reproduce

Save the following to t.Tests.ps1 and invoke with ./t.Tests.ps1:

Describe "Get-Content -Raw test" {
    BeforeAll {
        Push-Location TestDrive:/
        $lfTextUnterminated = "a`nb"
        $lfText = "a`nb`n"
        $crlfTextUnterminated = "a`r`nb"
        $crlfText = "a`r`nb`r`n"
        Set-Content -Encoding Ascii -NoNewline 'lf.txt' -value $lfText
        Set-Content -Encoding Ascii -NoNewline 'crlf.txt' -value $crlfText
        Set-Content -Encoding Ascii -NoNewline 'lf-nt.txt' -value $lfTextUnterminated
        Set-Content -Encoding Ascii -NoNewline 'crlf-nt.txt' -value $crlfTextUnterminated
    }
    It "Reads LF-terminated files in full." {
        Get-Content -Raw 'lf.txt' | Should BeExactly $lfText
    }
    It "Reads CRLF-terminated files in full." {
        Get-Content -Raw 'crlf.txt' | Should BeExactly $crlfText
    }
    It "Reads LF-separated files without trailing newline in full." {
        Get-Content -Raw 'lf-nt.txt' | Should BeExactly $lfTextUnterminated
    }
    It "Reads CRLF-separated files without trailing newline in full." {
        Get-Content -Raw 'crlf-nt.txt' | Should BeExactly $crlfTextUnterminated
    }
    AfterAll {
        Remove-Item -LiteralPath 'lf.txt', 'lf-nt.txt', 'crlf.txt', 'crlf-nt.txt'
        Pop-Location
    }
}

Expected behavior

All tests should pass.

Actual behavior

The files ending in an LF character are read without that last character.
Note that Windows Powershell is not affected.

...
 [-] Reads LF-terminated files in full. 47ms
   Expected string length 4 but was 3. Strings differ at index 3.
   Expected: {a\nb\n}
   But was:  {a\nb}
   ---------------^
   at line: 18 in /Users/mklement/Desktop/pg/t.Tests.ps1
   18:         Get-Content -Raw 'lf.txt' | Should BeExactly $lfText
 [-] Reads CRLF-terminated files in full. 20ms
   Expected string length 6 but was 5. Strings differ at index 5.
   Expected: {a\r\nb\r\n}
   But was:  {a\r\nb\r}
   -------------------^
   at line: 21 in /Users/mklement/Desktop/pg/t.Tests.ps1
   21:         Get-Content -Raw 'crlf.txt' | Should BeExactly $crlfText
...

Environment data

PowerShell Core v6.0.0-beta.7 on macOS 10.12.6
PowerShell Core v6.0.0-beta.7 on Ubuntu 16.04.3 LTS
PowerShell Core v6.0.0-beta.7 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)

Metadata

Metadata

Assignees

Labels

Issue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.WG-Engine-Providersbuilt-in PowerShell providers such as FileSystem, Certificates, Registry, etc.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions