Skip to content

Commit 396098a

Browse files
Respect explicit false for CSV UseCulture (PowerShell#26513)
Fix ConvertTo-Csv/Export-Csv/Import-Csv/ConvertFrom-Csv delimiter selection when -UseCulture:$false is passed explicitly. Treat explicit false the same as omitting the switch and use the default comma delimiter. Fixes PowerShell#26513
1 parent f1aaf15 commit 396098a

5 files changed

Lines changed: 92 additions & 6 deletions

File tree

‎src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,12 +1787,9 @@ internal static char SetDelimiter(PSCmdlet cmdlet, string parameterSetName, char
17871787
case "UseCulture":
17881788
case "CulturePath":
17891789
case "CultureLiteralPath":
1790-
if (useCulture)
1791-
{
1792-
// ListSeparator is apparently always a character even though the property returns a string, checked via:
1793-
// [CultureInfo]::GetCultures("AllCultures") | % { ([CultureInfo]($_.Name)).TextInfo.ListSeparator } | ? Length -ne 1
1794-
delimiter = CultureInfo.CurrentCulture.TextInfo.ListSeparator[0];
1795-
}
1790+
// ListSeparator is apparently always a character even though the property returns a string, checked via:
1791+
// [CultureInfo]::GetCultures("AllCultures") | % { ([CultureInfo]($_.Name)).TextInfo.ListSeparator } | ? Length -ne 1
1792+
delimiter = useCulture ? CultureInfo.CurrentCulture.TextInfo.ListSeparator[0] : ImportExportCSVHelper.CSVDelimiter;
17961793

17971794
break;
17981795
default:

‎test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertFrom-Csv.Tests.ps1‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,27 @@ a,b,c
6060
$actualLength | Should -Be 3
6161
}
6262

63+
It "Uses the default delimiter when -UseCulture is explicitly false" {
64+
$originalCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture
65+
$originalUICulture = [System.Threading.Thread]::CurrentThread.CurrentUICulture
66+
67+
try {
68+
[System.Threading.Thread]::CurrentThread.CurrentCulture = [CultureInfo]'de-DE'
69+
[System.Threading.Thread]::CurrentThread.CurrentUICulture = [CultureInfo]'de-DE'
70+
[CultureInfo]::CurrentCulture.TextInfo.ListSeparator | Should -Not -BeExactly ','
71+
72+
$actualData = $testColumns | ConvertFrom-Csv -UseCulture:$false
73+
74+
$actualData.a | Should -Be 1
75+
$actualData.b | Should -Be 2
76+
$actualData.c | Should -Be 3
77+
}
78+
finally {
79+
[System.Threading.Thread]::CurrentThread.CurrentCulture = $originalCulture
80+
[System.Threading.Thread]::CurrentThread.CurrentUICulture = $originalUICulture
81+
}
82+
}
83+
6384
It "Should Contain the Imported Type data" {
6485
$actualData = $testTypeData | ConvertFrom-Csv
6586
$actualData.PSObject.TypeNames.Count | Should -Be 2

‎test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Csv.Tests.ps1‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,27 @@ Describe "ConvertTo-Csv DRT Unit Tests" -Tags "CI" {
2727
$returnObject[2] | Should -BeExactly "`"1`"$($delimiter)`"2`""
2828
}
2929

30+
It "Uses the default delimiter when -UseCulture is explicitly false" {
31+
$originalCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture
32+
$originalUICulture = [System.Threading.Thread]::CurrentThread.CurrentUICulture
33+
34+
try {
35+
[System.Threading.Thread]::CurrentThread.CurrentCulture = [CultureInfo]'de-DE'
36+
[System.Threading.Thread]::CurrentThread.CurrentUICulture = [CultureInfo]'de-DE'
37+
[CultureInfo]::CurrentCulture.TextInfo.ListSeparator | Should -Not -BeExactly ','
38+
39+
$returnObject = $inputObject | ConvertTo-Csv -UseCulture:$false -IncludeTypeInformation
40+
$returnObject.Count | Should -Be 3
41+
$returnObject[0] | Should -BeExactly "#TYPE System.Management.Automation.PSCustomObject"
42+
$returnObject[1] | Should -BeExactly "`"First`",`"Second`""
43+
$returnObject[2] | Should -BeExactly "`"1`",`"2`""
44+
}
45+
finally {
46+
[System.Threading.Thread]::CurrentThread.CurrentCulture = $originalCulture
47+
[System.Threading.Thread]::CurrentThread.CurrentUICulture = $originalUICulture
48+
}
49+
}
50+
3051
It "Test convertto-csv with Delimiter" {
3152
$returnObject = $inputObject | ConvertTo-Csv -Delimiter ";" -IncludeTypeInformation
3253
$returnObject.Count | Should -Be 3

‎test/powershell/Modules/Microsoft.PowerShell.Utility/Export-Csv.Tests.ps1‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,33 @@ Describe "Export-Csv" -Tags "CI" {
9191
$results[0] | Should -BeExactly "#TYPE System.String"
9292
}
9393

94+
It "Uses the default delimiter when -UseCulture is explicitly false" {
95+
$originalCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture
96+
$originalUICulture = [System.Threading.Thread]::CurrentThread.CurrentUICulture
97+
98+
try {
99+
[System.Threading.Thread]::CurrentThread.CurrentCulture = [CultureInfo]'de-DE'
100+
[System.Threading.Thread]::CurrentThread.CurrentUICulture = [CultureInfo]'de-DE'
101+
[CultureInfo]::CurrentCulture.TextInfo.ListSeparator | Should -Not -BeExactly ','
102+
103+
$P1 | Export-Csv -Path $testCsv -UseCulture:$false
104+
$results = Get-Content -Path $testCsv
105+
106+
$results[0] | Should -BeExactly '"P1"'
107+
$results[1] | Should -BeExactly '"first"'
108+
109+
[pscustomobject]@{ H1 = 'V1'; H2 = 'V2' } | Export-Csv -Path $testCsv -UseCulture:$false
110+
$results = Get-Content -Path $testCsv
111+
112+
$results[0] | Should -BeExactly '"H1","H2"'
113+
$results[1] | Should -BeExactly '"V1","V2"'
114+
}
115+
finally {
116+
[System.Threading.Thread]::CurrentThread.CurrentCulture = $originalCulture
117+
[System.Threading.Thread]::CurrentThread.CurrentUICulture = $originalUICulture
118+
}
119+
}
120+
94121
It "Should support -LiteralPath parameter" {
95122
$testObject | Export-Csv -LiteralPath $testCsv
96123
$results = Import-Csv -Path $testCsv

‎test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Csv.Tests.ps1‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,26 @@ Describe "Import-Csv DRT Unit Tests" -Tags "CI" {
2020
$returnObject.First | Should -Be 1
2121
$returnObject.Second | Should -Be 2
2222
}
23+
24+
It "Uses the default delimiter when -UseCulture is explicitly false" {
25+
$originalCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture
26+
$originalUICulture = [System.Threading.Thread]::CurrentThread.CurrentUICulture
27+
28+
try {
29+
[System.Threading.Thread]::CurrentThread.CurrentCulture = [CultureInfo]'de-DE'
30+
[System.Threading.Thread]::CurrentThread.CurrentUICulture = [CultureInfo]'de-DE'
31+
[CultureInfo]::CurrentCulture.TextInfo.ListSeparator | Should -Not -BeExactly ','
32+
33+
$psObject | Export-Csv -Path $fileToGenerate
34+
$returnObject = Import-Csv -Path $fileToGenerate -UseCulture:$false
35+
$returnObject.First | Should -Be 1
36+
$returnObject.Second | Should -Be 2
37+
}
38+
finally {
39+
[System.Threading.Thread]::CurrentThread.CurrentCulture = $originalCulture
40+
[System.Threading.Thread]::CurrentThread.CurrentUICulture = $originalUICulture
41+
}
42+
}
2343
}
2444

2545
Describe "Import-Csv Double Quote Delimiter" -Tags "CI" {

0 commit comments

Comments
 (0)