Skip to content
Merged
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 @@ -1013,7 +1013,7 @@ internal string ConvertPSObjectToCSV(PSObject mshObject, IList<string> propertyN
AppendStringWithEscapeAlways(_outputString, value);
break;
case BaseCsvWritingCommand.QuoteKind.AsNeeded:
if (value.Contains(_delimiter))
if (value != null && value.Contains(_delimiter))
{
AppendStringWithEscapeAlways(_outputString, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Describe "ConvertTo-Csv" -Tags "CI" {
BeforeAll {
$Name = "Hello"; $Data = "World";
$testObject = [pscustomobject]@{ FirstColumn = $Name; SecondColumn = $Data }
$testNullObject = [pscustomobject]@{ FirstColumn = $Name; SecondColumn = $null }
}

It "Should Be able to be called without error" {
Expand Down Expand Up @@ -121,6 +122,11 @@ Describe "ConvertTo-Csv" -Tags "CI" {

$result[0] | Should -BeExactly "`"FirstColumn`",`"SecondColumn`""
$result[1] | Should -BeExactly "`"Hello`",`"World`""

$result = $testNullObject | ConvertTo-Csv -UseQuotes Always -Delimiter ','

$result[0] | Should -BeExactly "`"FirstColumn`",`"SecondColumn`""
$result[1] | Should -BeExactly "`"Hello`","
}

It "UseQuotes Always is default" {
Expand All @@ -135,13 +141,23 @@ Describe "ConvertTo-Csv" -Tags "CI" {

$result[0] | Should -BeExactly "FirstColumn,SecondColumn"
$result[1] | Should -BeExactly "Hello,World"

$result = $testNullObject | ConvertTo-Csv -UseQuotes Never -Delimiter ','

$result[0] | Should -BeExactly "FirstColumn,SecondColumn"
$result[1] | Should -BeExactly "Hello,"
}

It "UseQuotes AsNeeded" {
$result = $testObject | ConvertTo-Csv -UseQuotes AsNeeded -Delimiter 'r'

$result[0] | Should -BeExactly "`"FirstColumn`"rSecondColumn"
$result[1] | Should -BeExactly "Hellor`"World`""

$result = $testNullObject | ConvertTo-Csv -UseQuotes AsNeeded -Delimiter 'r'

$result[0] | Should -BeExactly "`"FirstColumn`"rSecondColumn"
$result[1] | Should -BeExactly "Hellor"
}
}
}