No longer skips a column without name if double quote delimiter is used in Import-Csv (#7899)

This commit is contained in:
Topping
2018-10-05 09:15:56 +05:00
committed by Ilya
parent 49826a7a29
commit 20b2e3df37
2 changed files with 37 additions and 2 deletions
@@ -1610,9 +1610,12 @@ namespace Microsoft.PowerShell.Commands
{
string name = names[i];
string value = null;
////if name is null and delimiter is '"', continue
////if name is null and delimiter is '"', use a default property name 'UnspecifiedName'
if (name.Length == 0 && delimiterlocal == '"')
continue;
{
name = UnspecifiedName + unspecifiedNameIndex;
unspecifiedNameIndex++;
}
////if name is null and delimiter is not '"', use a default property name 'UnspecifiedName'
if (string.IsNullOrEmpty(name))
{
@@ -22,6 +22,38 @@ Describe "Import-Csv DRT Unit Tests" -Tags "CI" {
}
}
Describe "Import-Csv Double Quote Delimiter" -Tags "CI" {
BeforeAll {
$empyValueCsv = @'
a1""a3
v1"v2"v3
'@
$withValueCsv = @'
a1"a2"a3
v1"v2"v3
'@
$quotedCharacterCsv = @'
a1,a2,a3
v1,"v2",v3
'@
}
It "Should handle <name>" -TestCases @(
@{ name = "quote with empty value" ; expectedHeader = "a1,H1,a3"; file = "EmptyValue.csv" ; content = $empyValueCsv ; delimiter = '"' }
@{ name = "quote with value" ; expectedHeader = "a1,a2,a3"; file = "WithValue.csv" ; content = $withValueCsv ; delimiter = '"' }
@{ name = "value enclosed in quote" ; expectedHeader = "a1,a2,a3"; file = "QuotedCharacter.csv" ; content = $quotedCharacterCsv ; delimiter = ',' }
){
param($expectedHeader, $file, $content, $delimiter)
Set-Content testdrive:/$file -Value $content
$returnObject = Import-Csv -Path testdrive:/$file -Delimiter $delimiter
$actualHeader = $returnObject[0].psobject.Properties.name -join ','
$actualHeader | Should -Be $expectedHeader
}
}
Describe "Import-Csv File Format Tests" -Tags "CI" {
BeforeAll {
# The file is w/o header