mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Add PSTypeName Support for Import-Csv and ConvertFrom-Csv (#5389)
closes #5134 Adds PSTypeName preservation on Import-Csv and ConvertFrom-Csv
This commit is contained in:
@@ -1336,10 +1336,6 @@ namespace Microsoft.PowerShell.Commands
|
||||
{
|
||||
type = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
type = ImportExportCSVHelper.CSVTypePrefix + type;
|
||||
}
|
||||
}
|
||||
}
|
||||
return type;
|
||||
@@ -1606,11 +1602,6 @@ namespace Microsoft.PowerShell.Commands
|
||||
PSObject result = new PSObject();
|
||||
char delimiterlocal = delimiter;
|
||||
int unspecifiedNameIndex = 1;
|
||||
if (type != null && type.Length > 0)
|
||||
{
|
||||
result.TypeNames.Clear();
|
||||
result.TypeNames.Add(type);
|
||||
}
|
||||
for (int i = 0; i <= names.Count - 1; i++)
|
||||
{
|
||||
string name = names[i];
|
||||
@@ -1638,6 +1629,13 @@ namespace Microsoft.PowerShell.Commands
|
||||
_alreadyWarnedUnspecifiedName = true;
|
||||
}
|
||||
|
||||
if (type != null && type.Length > 0)
|
||||
{
|
||||
result.TypeNames.Clear();
|
||||
result.TypeNames.Add(type);
|
||||
result.TypeNames.Add(ImportExportCSVHelper.CSVTypePrefix + type);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,11 @@ Describe "ConvertFrom-Csv" -Tags "CI" {
|
||||
$testColumns = @"
|
||||
a,b,c
|
||||
1,2,3
|
||||
"@
|
||||
$testTypeData = @"
|
||||
#TYPE My.Custom.Object
|
||||
a,b,c
|
||||
1,2,3
|
||||
"@
|
||||
}
|
||||
|
||||
@@ -53,6 +58,13 @@ a,b,c
|
||||
|
||||
$actualLength | Should Be 3
|
||||
}
|
||||
|
||||
It "Should Contain the Imported Type data" {
|
||||
$actualData = $testTypeData | ConvertFrom-Csv
|
||||
$actualData.PSObject.TypeNames.Count | Should Be 2
|
||||
$actualData.PSObject.TypeNames[0] | Should Be "My.Custom.Object"
|
||||
$actualData.PSObject.TypeNames[1] | Should Be "CSV:My.Custom.Object"
|
||||
}
|
||||
}
|
||||
|
||||
Describe "ConvertFrom-Csv DRT Unit Tests" -Tags "CI" {
|
||||
|
||||
@@ -80,16 +80,17 @@ Describe "Import-Csv #Type Tests" -Tags "CI" {
|
||||
Remove-Item -Path $testfile -Force -ErrorAction SilentlyContinue
|
||||
$processlist = (Get-Process)[0..1]
|
||||
$processlist | Export-Csv -Path $testfile -Force -IncludeTypeInformation
|
||||
# Import-Csv add "CSV:" before actual type
|
||||
$expectedProcessType = "CSV:System.Diagnostics.Process"
|
||||
$expectedProcessTypes = "System.Diagnostics.Process","CSV:System.Diagnostics.Process"
|
||||
}
|
||||
|
||||
It "Test import-csv import Object" {
|
||||
$importObjectList = Import-Csv -Path $testfile
|
||||
$processlist.Count | Should Be $importObjectList.Count
|
||||
|
||||
$importType = $importObjectList[0].psobject.TypeNames[0]
|
||||
$importType | Should Be $expectedProcessType
|
||||
$importTypes = $importObjectList[0].psobject.TypeNames
|
||||
$importTypes.Count | Should Be $expectedProcessTypes.Count
|
||||
$importTypes[0] | Should Be $expectedProcessTypes[0]
|
||||
$importTypes[1] | Should Be $expectedProcessTypes[1]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user