diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CSVCommands.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CSVCommands.cs index e1fe1bcf0..77b877704 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CSVCommands.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CSVCommands.cs @@ -1483,21 +1483,12 @@ namespace Microsoft.PowerShell.Commands break; } } - else if (IsNewLine(ch)) + else if (IsNewLine(ch, out string newLine)) { - if (ch == '\r') - { - ReadChar(); - } - if (seenBeginQuote) { //newline inside quote are valid - current.Append(ch); - if (ch == '\r') - { - current.Append('\n'); - } + current.Append(newLine); } else { @@ -1520,23 +1511,30 @@ namespace Microsoft.PowerShell.Commands return result; } + // If we detect a newline we return it as a string "\r", "\n" or "\r\n" private bool - IsNewLine(char ch) + IsNewLine(char ch, out string newLine) { - bool newLine = false; - if (ch == '\n') - { - newLine = true; - } - else if (ch == '\r') + newLine = ""; + if (ch == '\r') { if (PeekNextChar('\n')) { - newLine = true; + ReadChar(); + newLine = "\r\n"; + } + else + { + newLine = "\r"; } } - return newLine; + else if (ch == '\n') + { + newLine = "\n"; + } + + return newLine != ""; } /// @@ -1574,13 +1572,9 @@ namespace Microsoft.PowerShell.Commands { break; } - else if (IsNewLine(ch)) + else if (IsNewLine(ch, out string newLine)) { endOfRecord = true; - if (ch == '\r') - { - ReadChar(); - } break; } else diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Csv.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Csv.Tests.ps1 index fc3144ad7..c1c5aebf7 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Csv.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Csv.Tests.ps1 @@ -92,3 +92,24 @@ Describe "Import-Csv #Type Tests" -Tags "CI" { $importType | Should Be $expectedProcessType } } + +Describe "Import-Csv with different newlines" -Tags "CI" { + It "Test import-csv with '' newline" -TestCases @( + @{ name = "CR"; newline = "`r" } + @{ name = "LF"; newline = "`n" } + @{ name = "CRLF"; newline = "`r`n" } + ) { + param($newline) + $csvFile = Join-Path $TestDrive -ChildPath $((New-Guid).Guid) + $delimiter = ',' + "h1,h2,h3$($newline)11,12,13$($newline)21,22,23$($newline)" | Out-File -FilePath $csvFile + $returnObject = Import-Csv -Path $csvFile -Delimiter $delimiter + $returnObject.Count | Should Be 2 + $returnObject[0].h1 | Should Be 11 + $returnObject[0].h2 | Should Be 12 + $returnObject[0].h3 | Should Be 13 + $returnObject[1].h1 | Should Be 21 + $returnObject[1].h2 | Should Be 22 + $returnObject[1].h3 | Should Be 23 + } +}