From 20b2e3df3710adb4abbfb196cb4244425d192408 Mon Sep 17 00:00:00 2001 From: Topping Date: Fri, 5 Oct 2018 06:15:56 +0200 Subject: [PATCH 1/2] No longer skips a column without name if double quote delimiter is used in Import-Csv (#7899) --- .../commands/utility/CSVCommands.cs | 7 ++-- .../Import-Csv.Tests.ps1 | 32 +++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CSVCommands.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CSVCommands.cs index a37c8f9d00b..2f86c324e6b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CSVCommands.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CSVCommands.cs @@ -1610,9 +1610,12 @@ private void { 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)) { 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 e707cb3a0f0..3c2316fa113 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Csv.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Csv.Tests.ps1 @@ -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 " -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 From 09f54713466f6c2ada2741d15c84984d24581383 Mon Sep 17 00:00:00 2001 From: Ilya Date: Fri, 5 Oct 2018 09:31:32 +0500 Subject: [PATCH 2/2] Use params in ImportPSModule (#7933) --- .../engine/InitialSessionState.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/InitialSessionState.cs b/src/System.Management.Automation/engine/InitialSessionState.cs index 701d9f0a856..8c4cc0d0e21 100644 --- a/src/System.Management.Automation/engine/InitialSessionState.cs +++ b/src/System.Management.Automation/engine/InitialSessionState.cs @@ -1842,10 +1842,13 @@ public Microsoft.PowerShell.ExecutionPolicy ExecutionPolicy /// /// The modules to add /// - public void ImportPSModule(string[] name) + public void ImportPSModule(params string[] name) { if (name == null) + { throw new ArgumentNullException("name"); + } + foreach (string n in name) { ModuleSpecificationsToImport.Add(new ModuleSpecification(n));