mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Fix NRE in csv commands (#12281)
This commit is contained in:
@@ -1013,7 +1013,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
AppendStringWithEscapeAlways(_outputString, value);
|
||||
break;
|
||||
case BaseCsvWritingCommand.QuoteKind.AsNeeded:
|
||||
if (value.Contains(_delimiter))
|
||||
if (value != null && value.Contains(_delimiter))
|
||||
{
|
||||
AppendStringWithEscapeAlways(_outputString, value);
|
||||
}
|
||||
|
||||
@@ -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" {
|
||||
@@ -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" {
|
||||
@@ -135,6 +141,11 @@ 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" {
|
||||
@@ -142,6 +153,11 @@ Describe "ConvertTo-Csv" -Tags "CI" {
|
||||
|
||||
$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"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user