diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs index cb455417531..3c408811242 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs @@ -1787,12 +1787,9 @@ internal static char SetDelimiter(PSCmdlet cmdlet, string parameterSetName, char case "UseCulture": case "CulturePath": case "CultureLiteralPath": - if (useCulture) - { - // ListSeparator is apparently always a character even though the property returns a string, checked via: - // [CultureInfo]::GetCultures("AllCultures") | % { ([CultureInfo]($_.Name)).TextInfo.ListSeparator } | ? Length -ne 1 - delimiter = CultureInfo.CurrentCulture.TextInfo.ListSeparator[0]; - } + // ListSeparator is apparently always a character even though the property returns a string, checked via: + // [CultureInfo]::GetCultures("AllCultures") | % { ([CultureInfo]($_.Name)).TextInfo.ListSeparator } | ? Length -ne 1 + delimiter = useCulture ? CultureInfo.CurrentCulture.TextInfo.ListSeparator[0] : ImportExportCSVHelper.CSVDelimiter; break; default: diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Csv.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Csv.Tests.ps1 index b5573609fbf..787e488b833 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Csv.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Csv.Tests.ps1 @@ -60,6 +60,27 @@ a,b,c $actualLength | Should -Be 3 } + It "Uses the default delimiter when -UseCulture is explicitly false" { + $originalCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture + $originalUICulture = [System.Threading.Thread]::CurrentThread.CurrentUICulture + + try { + [System.Threading.Thread]::CurrentThread.CurrentCulture = [CultureInfo]'de-DE' + [System.Threading.Thread]::CurrentThread.CurrentUICulture = [CultureInfo]'de-DE' + [CultureInfo]::CurrentCulture.TextInfo.ListSeparator | Should -Not -BeExactly ',' + + $actualData = $testColumns | ConvertFrom-Csv -UseCulture:$false + + $actualData.a | Should -Be 1 + $actualData.b | Should -Be 2 + $actualData.c | Should -Be 3 + } + finally { + [System.Threading.Thread]::CurrentThread.CurrentCulture = $originalCulture + [System.Threading.Thread]::CurrentThread.CurrentUICulture = $originalUICulture + } + } + It "Should Contain the Imported Type data" { $actualData = $testTypeData | ConvertFrom-Csv $actualData.PSObject.TypeNames.Count | Should -Be 2 diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Csv.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Csv.Tests.ps1 index bfba7718272..39751311baf 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Csv.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Csv.Tests.ps1 @@ -27,6 +27,27 @@ Describe "ConvertTo-Csv DRT Unit Tests" -Tags "CI" { $returnObject[2] | Should -BeExactly "`"1`"$($delimiter)`"2`"" } + It "Uses the default delimiter when -UseCulture is explicitly false" { + $originalCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture + $originalUICulture = [System.Threading.Thread]::CurrentThread.CurrentUICulture + + try { + [System.Threading.Thread]::CurrentThread.CurrentCulture = [CultureInfo]'de-DE' + [System.Threading.Thread]::CurrentThread.CurrentUICulture = [CultureInfo]'de-DE' + [CultureInfo]::CurrentCulture.TextInfo.ListSeparator | Should -Not -BeExactly ',' + + $returnObject = $inputObject | ConvertTo-Csv -UseCulture:$false -IncludeTypeInformation + $returnObject.Count | Should -Be 3 + $returnObject[0] | Should -BeExactly "#TYPE System.Management.Automation.PSCustomObject" + $returnObject[1] | Should -BeExactly "`"First`",`"Second`"" + $returnObject[2] | Should -BeExactly "`"1`",`"2`"" + } + finally { + [System.Threading.Thread]::CurrentThread.CurrentCulture = $originalCulture + [System.Threading.Thread]::CurrentThread.CurrentUICulture = $originalUICulture + } + } + It "Test convertto-csv with Delimiter" { $returnObject = $inputObject | ConvertTo-Csv -Delimiter ";" -IncludeTypeInformation $returnObject.Count | Should -Be 3 diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-Csv.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-Csv.Tests.ps1 index aae457f2f82..afe951b833c 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-Csv.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-Csv.Tests.ps1 @@ -91,6 +91,33 @@ Describe "Export-Csv" -Tags "CI" { $results[0] | Should -BeExactly "#TYPE System.String" } + It "Uses the default delimiter when -UseCulture is explicitly false" { + $originalCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture + $originalUICulture = [System.Threading.Thread]::CurrentThread.CurrentUICulture + + try { + [System.Threading.Thread]::CurrentThread.CurrentCulture = [CultureInfo]'de-DE' + [System.Threading.Thread]::CurrentThread.CurrentUICulture = [CultureInfo]'de-DE' + [CultureInfo]::CurrentCulture.TextInfo.ListSeparator | Should -Not -BeExactly ',' + + $P1 | Export-Csv -Path $testCsv -UseCulture:$false + $results = Get-Content -Path $testCsv + + $results[0] | Should -BeExactly '"P1"' + $results[1] | Should -BeExactly '"first"' + + [pscustomobject]@{ H1 = 'V1'; H2 = 'V2' } | Export-Csv -Path $testCsv -UseCulture:$false + $results = Get-Content -Path $testCsv + + $results[0] | Should -BeExactly '"H1","H2"' + $results[1] | Should -BeExactly '"V1","V2"' + } + finally { + [System.Threading.Thread]::CurrentThread.CurrentCulture = $originalCulture + [System.Threading.Thread]::CurrentThread.CurrentUICulture = $originalUICulture + } + } + It "Should support -LiteralPath parameter" { $testObject | Export-Csv -LiteralPath $testCsv $results = Import-Csv -Path $testCsv 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 debd3f3001b..4a08164a384 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Csv.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Csv.Tests.ps1 @@ -20,6 +20,26 @@ Describe "Import-Csv DRT Unit Tests" -Tags "CI" { $returnObject.First | Should -Be 1 $returnObject.Second | Should -Be 2 } + + It "Uses the default delimiter when -UseCulture is explicitly false" { + $originalCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture + $originalUICulture = [System.Threading.Thread]::CurrentThread.CurrentUICulture + + try { + [System.Threading.Thread]::CurrentThread.CurrentCulture = [CultureInfo]'de-DE' + [System.Threading.Thread]::CurrentThread.CurrentUICulture = [CultureInfo]'de-DE' + [CultureInfo]::CurrentCulture.TextInfo.ListSeparator | Should -Not -BeExactly ',' + + $psObject | Export-Csv -Path $fileToGenerate + $returnObject = Import-Csv -Path $fileToGenerate -UseCulture:$false + $returnObject.First | Should -Be 1 + $returnObject.Second | Should -Be 2 + } + finally { + [System.Threading.Thread]::CurrentThread.CurrentCulture = $originalCulture + [System.Threading.Thread]::CurrentThread.CurrentUICulture = $originalUICulture + } + } } Describe "Import-Csv Double Quote Delimiter" -Tags "CI" {