Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ internal static IList<string> BuildPropertyNames(PSObject source, IList<string>
/// <returns>Converted string.</returns>
internal string ConvertPropertyNamesCSV(IList<string> propertyNames)
{
ArgumentNullException.ThrowIfNull(propertyNames);
ArgumentNullException.ThrowIfNull(propertyNames);

_outputString.Clear();
bool first = true;
Expand Down Expand Up @@ -994,7 +994,7 @@ internal string ConvertPropertyNamesCSV(IList<string> propertyNames)
AppendStringWithEscapeAlways(_outputString, propertyName);
break;
case BaseCsvWritingCommand.QuoteKind.AsNeeded:

if (propertyName.AsSpan().IndexOfAny(_delimiter, '\n', '"') != -1)
{
AppendStringWithEscapeAlways(_outputString, propertyName);
Expand Down Expand Up @@ -1023,7 +1023,7 @@ internal string ConvertPropertyNamesCSV(IList<string> propertyNames)
/// <returns></returns>
internal string ConvertPSObjectToCSV(PSObject mshObject, IList<string> propertyNames)
{
ArgumentNullException.ThrowIfNull(propertyNames);
ArgumentNullException.ThrowIfNull(propertyNames);

_outputString.Clear();
bool first = true;
Expand Down Expand Up @@ -1109,7 +1109,7 @@ internal string ConvertPSObjectToCSV(PSObject mshObject, IList<string> propertyN
/// <returns>ToString() value.</returns>
internal static string GetToStringValueForProperty(PSPropertyInfo property)
{
ArgumentNullException.ThrowIfNull(property);
ArgumentNullException.ThrowIfNull(property);

string value = null;
try
Expand Down Expand Up @@ -1271,7 +1271,7 @@ internal class ImportCsvHelper

internal ImportCsvHelper(PSCmdlet cmdlet, char delimiter, IList<string> header, string typeName, StreamReader streamReader)
{
ArgumentNullException.ThrowIfNull(cmdlet);
ArgumentNullException.ThrowIfNull(cmdlet);
ArgumentNullException.ThrowIfNull(streamReader);

_cmdlet = cmdlet;
Expand Down Expand Up @@ -1380,15 +1380,12 @@ internal void Import(ref bool alreadyWriteOutWarning)
var prevalidated = false;
var values = new List<string>(ValueCountGuestimate);
var builder = new StringBuilder(LineLengthGuestimate);
while (true)
while (!EOF)
{
ParseNextRecord(values, builder);
if (values.Count == 0)
break;

if (values.Count == 1 && string.IsNullOrEmpty(values[0]))
{
// skip the blank lines
// Skip blank line in the input.
continue;
}

Expand Down Expand Up @@ -1531,11 +1528,13 @@ private void ParseNextRecord(List<string> result, StringBuilder current)
// ->"foo bar" <- is read as ->foo bar<-
// ->"foo bar" ab <- is read as ->"foo bar" ab <-
bool endofRecord = false;
ReadTillNextDelimiter(current, ref endofRecord, true);
ReadTillNextDelimiter(current, ref endofRecord, eatTrailingBlanks: true);
result.Add(current.ToString());
current.Remove(0, current.Length);
if (endofRecord)
break;
{
return;
}
}
}
else if (current.Length == 0)
Expand All @@ -1554,11 +1553,13 @@ private void ParseNextRecord(List<string> result, StringBuilder current)
// Basically we read till next delimiter
bool endOfRecord = false;
current.Append(ch);
ReadTillNextDelimiter(current, ref endOfRecord, false);
ReadTillNextDelimiter(current, ref endOfRecord, eatTrailingBlanks: false);
result.Add(current.ToString());
current.Remove(0, current.Length);
if (endOfRecord)
break;
{
return;
}
}
}
else if (ch == ' ' || ch == '\t')
Expand Down Expand Up @@ -1605,11 +1606,14 @@ private void ParseNextRecord(List<string> result, StringBuilder current)
}
else
{
result.Add(current.ToString());
current.Remove(0, current.Length);
if (current.Length != 0 || result.Count != 0)
{
result.Add(current.ToString());
current.Remove(0, current.Length);
}

// New line outside quote is end of word and end of record
break;
return;
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ a,b,c

Describe "ConvertFrom-Csv DRT Unit Tests" -Tags "CI" {
It "Test ConvertFrom-Csv with pipelined InputObject and Header" {
$inputObject = [pscustomobject]@{ First = 1; Second = 2 }
$inputObject = [PSCustomObject]@{ First = 1; Second = 2 }
$res = $inputObject | ConvertTo-Csv
$result = $res | ConvertFrom-Csv -Header "Header1","Header2"

Expand All @@ -80,3 +80,117 @@ Describe "ConvertFrom-Csv DRT Unit Tests" -Tags "CI" {
$result[1].Header2 | Should -BeExactly "2"
}
}

Describe "ConvertFrom-Csv with empty and null values" -Tags "CI" {
It 'ConvertFrom-Csv correctly deserializes input CSV - <Test>' -TestCases @(
@{
Test = '1a'
Expected = [PSCustomObject] @{ P1 = '' }
InputCsv = @'
"P1"
""
'@
}
@{
Test = '1b'
Expected = [PSCustomObject] @{ P1 = '' }, [PSCustomObject] @{ P1 = '' }, [PSCustomObject] @{ P1 = '' }
InputCsv = @'
"P1"
""
""
""
'@
}
@{
Test = '2a'
Expected = [PSCustomObject] @{ P1 = ''; P2 = $null }
InputCsv = @'
"P1","P2"
"",
'@
}
@{
Test = '2b'
Expected = [PSCustomObject] @{ P1 = ''; P2 = '' }, [PSCustomObject] @{ P1 = ''; P2 = $null }
InputCsv = @'
"P1","P2"
"",
"",
'@
}
@{
Test = '3a'
Expected = [PSCustomObject] @{ P1 = ''; P2 = $null }
InputCsv = @'
"P1","P2"
,
'@
}
@{
Test = '3b'
Expected = [PSCustomObject] @{ P1 = ''; P2 = '' }, [PSCustomObject] @{ P1 = ''; P2 = $null }
InputCsv = @'
"P1","P2"
,
,
'@
}
@{
Test = '4a'
Expected = [PSCustomObject] @{ P1 = ''; P2 = '' }
InputCsv = @'
"P1","P2"
,""
'@
}
@{
Test = '4b'
Expected = [PSCustomObject] @{ P1 = ''; P2 = '' }, [PSCustomObject] @{ P1 = ''; P2 = '' }
InputCsv = @'
"P1","P2"
,""
,""
'@
}
@{
Test = '5a'
Expected = [PSCustomObject] @{ P1 = ''; P2 = '' }
InputCsv = @'
"P1","P2"
"",""
'@
}
@{
Test = '5b'
Expected = [PSCustomObject] @{ P1 = ''; P2 = '' }, [PSCustomObject] @{ P1 = ''; P2 = '' }
InputCsv = @'
"P1","P2"
"",""
"",""
'@
}
@{
Test = '6a'
Expected = [PSCustomObject] @{ P1 = ''; P2 = ''; P3 = $null }
InputCsv = @'
"P1","P2","P3"
,,
'@
}
@{
Test = '6b'
Expected = [PSCustomObject] @{ P1 = ''; P2 = ''; P3 = '' }, [PSCustomObject] @{ P1 = ''; P2 = ''; P3 = $null }
InputCsv = @'
"P1","P2","P3"
,,
,,
'@
}
) {
param($Test, $Expected, $InputCsv)
$expectedResult = $Expected | ConvertTo-Csv
$actualResult = $InputCsv | ConvertFrom-Csv | ConvertTo-Csv

$actualResult | Should -BeExactly $expectedResult
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ Describe "Import-Csv with different newlines" -Tags "CI" {
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
"h1,h2,h3$($newline)11,12,13$($newline)$($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
Expand Down