From 48943672c17e21c94e4e35c35ce1a6a42f08db32 Mon Sep 17 00:00:00 2001 From: sethvs Date: Wed, 2 May 2018 10:43:05 +0300 Subject: [PATCH 1/6] Improve code coverage in Export-Csv.Tests.ps1. --- .../Export-Csv.Tests.ps1 | 167 +++++++++++++----- 1 file changed, 121 insertions(+), 46 deletions(-) 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 6dc8c328dfc..867c1a5787d 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-Csv.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-Csv.Tests.ps1 @@ -4,34 +4,36 @@ Describe "Export-Csv" -Tags "CI" { BeforeAll { $testObject = @("test","object","array") $testCsv = Join-Path -Path $TestDrive -ChildPath "output.csv" + $newLine = [environment]::NewLine + $P1 = [pscustomobject]@{"P1" = "first"} + $P2 = [pscustomobject]@{"P2" = "second"} + $P11 = [pscustomobject]@{"P1" = "eleventh"} } AfterEach { - Remove-Item $testCsv -Force -ErrorAction SilentlyContinue + Remove-Item -Path $testCsv -Force -ErrorAction SilentlyContinue } It "Should be able to be called without error" { - { $testObject | Export-Csv $testCsv -ErrorAction Stop } | Should -Not -Throw + { $testObject | Export-Csv -Path $testCsv -ErrorAction Stop } | Should -Not -Throw } It "Should throw if an output file isn't specified" { - { $testObject | Export-Csv -ErrorAction Stop } | ShouldBeErrorId "CannotSpecifyPathAndLiteralPath,Microsoft.PowerShell.Commands.ExportCsvCommand" + { $testObject | Export-Csv -ErrorAction Stop } | Should -Throw -ErrorId "CannotSpecifyPathAndLiteralPath,Microsoft.PowerShell.Commands.ExportCsvCommand" } It "Should be a string when exporting via pipe" { - $testObject | Export-Csv $testCsv -IncludeTypeInformation - - $piped = Get-Content $testCsv + $testObject | Export-Csv -Path $testCsv -IncludeTypeInformation + $results = Get-Content -Path $testCsv - $piped[0] | Should -BeExactly "#TYPE System.String" + $results[0] | Should -BeExactly "#TYPE System.String" } It "Should be an object when exporting via the inputObject switch" { Export-Csv -InputObject $testObject -Path $testCsv -IncludeTypeInformation + $results = Get-Content -Path $testCsv - $switch = Get-Content $testCsv - - $switch[0] | Should -BeExactly "#TYPE System.Object[]" + $results[0] | Should -BeExactly "#TYPE System.Object[]" } It "Should output a csv file containing a string of all the lengths of each element when piped input is used" { @@ -43,46 +45,130 @@ Describe "Export-Csv" -Tags "CI" { $expected = @("#TYPE System.String", "`"Length`"", $first , $second, $third) for ($i = 0; $i -lt $expected.Count; $i++) { - $(Get-Content $testCsv)[$i] | Should -Be $expected[$i] + $(Get-Content -Path $testCsv)[$i] | Should -Be $expected[$i] } } It "Does not include type information by default" { $testObject | Export-Csv -Path $testCsv + $results = Get-Content -Path $testCsv - $(Get-Content $testCsv)[0] | Should -Not -Match ([regex]::Escape("System.String")) - $(Get-Content $testCsv)[0] | Should -Not -Match ([regex]::Escape("#TYPE")) + $results[0] | Should -Not -Match ([regex]::Escape("System.String")) + $results[0] | Should -Not -Match ([regex]::Escape("#TYPE")) } It "Does not include type information with -NoTypeInformation" { $testObject | Export-Csv -Path $testCsv -NoTypeInformation + $results = Get-Content -Path $testCsv - $(Get-Content $testCsv)[0] | Should -Not -Match ([regex]::Escape("System.String")) - $(Get-Content $testCsv)[0] | Should -Not -Match ([regex]::Escape("#TYPE")) + $results[0] | Should -Not -Match ([regex]::Escape("System.String")) + $results[0] | Should -Not -Match ([regex]::Escape("#TYPE")) } It "Includes type information when -IncludeTypeInformation is supplied" { $testObject | Export-Csv -Path $testCsv -IncludeTypeInformation + $results = Get-Content -Path $testCsv - $(Get-Content $testCsv)[0] | Should -BeExactly "#TYPE System.String" + $results[0] | Should -BeExactly "#TYPE System.String" } It "Does not support -IncludeTypeInformation and -NoTypeInformation at the same time" { - { $testObject | Export-Csv -Path $testCsv -IncludeTypeInformation -NoTypeInformation } | - ShouldBeErrorId "CannotSpecifyIncludeTypeInformationAndNoTypeInformation,Microsoft.PowerShell.Commands.ExportCsvCommand" + { $testObject | Export-Csv -Path $testCsv -IncludeTypeInformation -NoTypeInformation } | Should -Throw -ErrorId "CannotSpecifyIncludeTypeInformationAndNoTypeInformation,Microsoft.PowerShell.Commands.ExportCsvCommand" } -} -Describe "Export-Csv DRT Unit Tests" -Tags "CI" { - BeforeAll { - $filePath = Join-Path $TestDrive -ChildPath "test.csv" - $newLine = [environment]::NewLine + It "Should be able to use -LiteralPath parameter" { + $testObject | Export-Csv -LiteralPath $testCsv + $results = Import-Csv -Path $testCsv + + $results | Should -HaveCount 3 + } + + It "Should overwrite file without -NoClobber parameter" { + $P1 | Export-Csv -Path $testCsv + $P2 | Export-Csv -Path $testCsv + $results = Import-Csv -Path $testCsv + + $results.P2 | Should -BeExactly "second" + $property = $results | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | ForEach-Object { $_.Name } + $property | Should -BeExactly P2 + } + + It "Should not overwrite file with -NoClobber parameter" { + $P1 | Export-Csv -Path $testCsv + { $P2 | Export-Csv -Path $testCsv -NoClobber} | Should -Throw -ErrorId "NoClobber,Microsoft.PowerShell.Commands.ExportCsvCommand" + $results = Import-Csv -Path $testCsv + + $results.P1 | Should -BeExactly "first" + $property = $results | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | ForEach-Object { $_.Name } + $property | Should -BeExactly P1 + } + + It "Should not overwrite read-only file without -Force parameter" { + $P1 | Export-Csv -Path $testCsv + Set-ItemProperty -Path $testCsv -Name IsReadOnly -Value $true + + { $P2 | Export-Csv -Path $testCsv } | Should -Throw -ErrorId "FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand" + $results = Import-Csv -Path $testCsv + + $results.P1 | Should -BeExactly "first" + $property = $results | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | ForEach-Object { $_.Name } + $property | Should -BeExactly P1 + } + + It "Should overwrite read-only file with -Force parameter" { + $P1 | Export-Csv -Path $testCsv + Set-ItemProperty -Path $testCsv -Name IsReadOnly -Value $true + + $P2 | Export-Csv -Path $testCsv -Force + $results = Import-Csv -Path $testCsv + + $results.P2 | Should -BeExactly "second" + $property = $results | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | ForEach-Object { $_.Name } + $property | Should -BeExactly P2 + } + + It "Should not export to file if -WhatIf parameter specified" { + $P1 | Export-Csv -Path $testCsv -WhatIf + $testCsv | Should -Not -Exist + } + + It "Should append to file if -Append parameter specified" { + $P1 | Export-Csv -Path $testCsv + $P11 | Export-Csv -Path $testCsv -Append + $results = Import-Csv -Path $testCsv + + $results[0].P1 | Should -BeExactly "first" + $results[1].P1 | Should -BeExactly "eleventh" + $property = $results | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | ForEach-Object { $_.Name } + $property | Should -BeExactly P1 + } + + It "Should append to empty file if -Append parameter specified" { + New-Item -Path $testCsv -ItemType File + + $P11 | Export-Csv -Path $testCsv -Append + $results = Import-Csv -Path $testCsv + + $results[0].P1 | Should -BeExactly "eleventh" + $property = $results | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | ForEach-Object { $_.Name } + $property | Should -BeExactly P1 + } + + It "Should throw when appended property does not exits in existing .csv file" { + $P1 | Export-Csv -Path $testCsv + { $P2 | Export-Csv -Path $testCsv -Append -ErrorAction Stop } | Should -Throw -ErrorId "CannotAppendCsvWithMismatchedPropertyNames,Microsoft.PowerShell.Commands.ExportCsvCommand" + $results = Import-Csv -Path $testCsv + + $results[0].P1 | Should -BeExactly "first" + $property = $results | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | ForEach-Object { $_.Name } + $property | Should -BeExactly P1 } It "Test basic function works well" { - $input = [pscustomobject]@{ "P1" = "V11"; "P2" = "V12"; "P3" = "V13" } - $input | Export-Csv -Path $filePath -NoTypeInformation - $results = Import-Csv $filePath + $in = [pscustomobject]@{ "P1" = "V11"; "P2" = "V12"; "P3" = "V13" } + $in | Export-Csv -Path $testCsv -NoTypeInformation + $results = Import-Csv -Path $testCsv + $results.P1 | Should -BeExactly "V11" $results.P2 | Should -BeExactly "V12" $results.P3 | Should -BeExactly "V13" @@ -90,34 +176,23 @@ Describe "Export-Csv DRT Unit Tests" -Tags "CI" { It "Test if it works with special character" { $v3 = "abc" + $newLine + "foo" - $input = [pscustomobject]@{ "P1" = " "; "P2" = "abc,foo"; "P3" = $v3} - $input | Export-Csv -Path $filePath -NoTypeInformation - $results = Import-Csv $filePath + $in = [pscustomobject]@{ "P1" = " "; "P2" = "abc,foo"; "P3" = $v3} + $in | Export-Csv -Path $testCsv -NoTypeInformation + $results = Import-Csv -Path $testCsv + $results.P1 | Should -BeExactly " " $results.P2 | Should -BeExactly "abc,foo" - $results.P3 | Should -Be $v3 - } - - It "Test force switch works well" { - $input = [pscustomobject]@{ "P1" = "first" } - $input | Export-Csv -Path $filePath - - $input = [pscustomobject]@{ "P2" = "second" } - $input | Export-Csv -Path $filePath -Force - $results = Import-Csv $filePath - - $results.P2 | Should -BeExactly "second" - $property = $results | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | ForEach-Object { $_.Name } - $property | Should -Not -Be P1 + $results.P3 | Should -BeExactly $v3 } It "Test export-csv with a useculture flag" { - $outputFilesDir = Join-Path $TestDrive -ChildPath "Monad" - $fileToGenerate = Join-Path $outputFilesDir -ChildPath "CSVTests.csv" + $outputFilesDir = Join-Path -Path $TestDrive -ChildPath "Monad" + $fileToGenerate = Join-Path -Path $outputFilesDir -ChildPath "CSVTests.csv" $delimiter = (Get-Culture).TextInfo.ListSeparator New-Item -Path $outputFilesDir -ItemType Directory -Force - Get-Item -Path $outputFilesDir| Export-Csv -Path $fileToGenerate -UseCulture -NoTypeInformation + Get-Item -Path $outputFilesDir | Export-Csv -Path $fileToGenerate -UseCulture -NoTypeInformation $contents = Get-Content -Path $fileToGenerate + $contents.Count | Should -Be 2 $contents[0].Contains($delimiter) | Should -BeTrue $contents[1].Contains($delimiter) | Should -BeTrue From 64defc581a3bc565a5094ecbd67e08fed87835c2 Mon Sep 17 00:00:00 2001 From: sethvs Date: Wed, 2 May 2018 12:25:13 +0300 Subject: [PATCH 2/6] Implement requested changes. --- .../Export-Csv.Tests.ps1 | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) 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 867c1a5787d..58b6767bd95 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-Csv.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-Csv.Tests.ps1 @@ -76,7 +76,7 @@ Describe "Export-Csv" -Tags "CI" { { $testObject | Export-Csv -Path $testCsv -IncludeTypeInformation -NoTypeInformation } | Should -Throw -ErrorId "CannotSpecifyIncludeTypeInformationAndNoTypeInformation,Microsoft.PowerShell.Commands.ExportCsvCommand" } - It "Should be able to use -LiteralPath parameter" { + It "Should support -LiteralPath parameter" { $testObject | Export-Csv -LiteralPath $testCsv $results = Import-Csv -Path $testCsv @@ -89,8 +89,8 @@ Describe "Export-Csv" -Tags "CI" { $results = Import-Csv -Path $testCsv $results.P2 | Should -BeExactly "second" - $property = $results | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | ForEach-Object { $_.Name } - $property | Should -BeExactly P2 + $property = $results.PSObject.Properties.Name + $property | Should -BeExactly "P2" } It "Should not overwrite file with -NoClobber parameter" { @@ -99,8 +99,8 @@ Describe "Export-Csv" -Tags "CI" { $results = Import-Csv -Path $testCsv $results.P1 | Should -BeExactly "first" - $property = $results | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | ForEach-Object { $_.Name } - $property | Should -BeExactly P1 + $property = $results.PSObject.Properties.Name + $property | Should -BeExactly "P1" } It "Should not overwrite read-only file without -Force parameter" { @@ -111,8 +111,8 @@ Describe "Export-Csv" -Tags "CI" { $results = Import-Csv -Path $testCsv $results.P1 | Should -BeExactly "first" - $property = $results | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | ForEach-Object { $_.Name } - $property | Should -BeExactly P1 + $property = $results.PSObject.Properties.Name + $property | Should -BeExactly "P1" } It "Should overwrite read-only file with -Force parameter" { @@ -123,8 +123,8 @@ Describe "Export-Csv" -Tags "CI" { $results = Import-Csv -Path $testCsv $results.P2 | Should -BeExactly "second" - $property = $results | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | ForEach-Object { $_.Name } - $property | Should -BeExactly P2 + $property = $results.PSObject.Properties.Name + $property | Should -BeExactly "P2" } It "Should not export to file if -WhatIf parameter specified" { @@ -139,8 +139,8 @@ Describe "Export-Csv" -Tags "CI" { $results[0].P1 | Should -BeExactly "first" $results[1].P1 | Should -BeExactly "eleventh" - $property = $results | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | ForEach-Object { $_.Name } - $property | Should -BeExactly P1 + $property = $results[0].PSObject.Properties.Name + $property | Should -BeExactly "P1" } It "Should append to empty file if -Append parameter specified" { @@ -150,18 +150,18 @@ Describe "Export-Csv" -Tags "CI" { $results = Import-Csv -Path $testCsv $results[0].P1 | Should -BeExactly "eleventh" - $property = $results | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | ForEach-Object { $_.Name } - $property | Should -BeExactly P1 + $property = $results.PSObject.Properties.Name + $property | Should -BeExactly "P1" } - It "Should throw when appended property does not exits in existing .csv file" { + It "Should throw when appended property does not exist in existing .csv file" { $P1 | Export-Csv -Path $testCsv { $P2 | Export-Csv -Path $testCsv -Append -ErrorAction Stop } | Should -Throw -ErrorId "CannotAppendCsvWithMismatchedPropertyNames,Microsoft.PowerShell.Commands.ExportCsvCommand" $results = Import-Csv -Path $testCsv $results[0].P1 | Should -BeExactly "first" - $property = $results | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | ForEach-Object { $_.Name } - $property | Should -BeExactly P1 + $property = $results.PSObject.Properties.Name + $property | Should -BeExactly "P1" } It "Test basic function works well" { From 5d5838288bda9aa4e88896ad4803a0be0f9da9fb Mon Sep 17 00:00:00 2001 From: sethvs Date: Wed, 2 May 2018 17:03:37 +0300 Subject: [PATCH 3/6] Implement requested changes 2. --- .../Export-Csv.Tests.ps1 | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) 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 58b6767bd95..909278d7968 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-Csv.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-Csv.Tests.ps1 @@ -89,8 +89,6 @@ Describe "Export-Csv" -Tags "CI" { $results = Import-Csv -Path $testCsv $results.P2 | Should -BeExactly "second" - $property = $results.PSObject.Properties.Name - $property | Should -BeExactly "P2" } It "Should not overwrite file with -NoClobber parameter" { @@ -99,8 +97,6 @@ Describe "Export-Csv" -Tags "CI" { $results = Import-Csv -Path $testCsv $results.P1 | Should -BeExactly "first" - $property = $results.PSObject.Properties.Name - $property | Should -BeExactly "P1" } It "Should not overwrite read-only file without -Force parameter" { @@ -111,8 +107,6 @@ Describe "Export-Csv" -Tags "CI" { $results = Import-Csv -Path $testCsv $results.P1 | Should -BeExactly "first" - $property = $results.PSObject.Properties.Name - $property | Should -BeExactly "P1" } It "Should overwrite read-only file with -Force parameter" { @@ -123,8 +117,6 @@ Describe "Export-Csv" -Tags "CI" { $results = Import-Csv -Path $testCsv $results.P2 | Should -BeExactly "second" - $property = $results.PSObject.Properties.Name - $property | Should -BeExactly "P2" } It "Should not export to file if -WhatIf parameter specified" { @@ -139,10 +131,9 @@ Describe "Export-Csv" -Tags "CI" { $results[0].P1 | Should -BeExactly "first" $results[1].P1 | Should -BeExactly "eleventh" - $property = $results[0].PSObject.Properties.Name - $property | Should -BeExactly "P1" } + # This test is not a duplicate of previous one, since it covers a separate branch in code. It "Should append to empty file if -Append parameter specified" { New-Item -Path $testCsv -ItemType File @@ -150,8 +141,6 @@ Describe "Export-Csv" -Tags "CI" { $results = Import-Csv -Path $testCsv $results[0].P1 | Should -BeExactly "eleventh" - $property = $results.PSObject.Properties.Name - $property | Should -BeExactly "P1" } It "Should throw when appended property does not exist in existing .csv file" { @@ -160,8 +149,6 @@ Describe "Export-Csv" -Tags "CI" { $results = Import-Csv -Path $testCsv $results[0].P1 | Should -BeExactly "first" - $property = $results.PSObject.Properties.Name - $property | Should -BeExactly "P1" } It "Test basic function works well" { From 62ea92d717a4df50d7304fe99c9b0ce7f237d346 Mon Sep 17 00:00:00 2001 From: sethvs Date: Thu, 3 May 2018 08:28:52 +0300 Subject: [PATCH 4/6] Add Out-Null. --- .../Modules/Microsoft.PowerShell.Utility/Export-Csv.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 909278d7968..fba16906216 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-Csv.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-Csv.Tests.ps1 @@ -135,7 +135,7 @@ Describe "Export-Csv" -Tags "CI" { # This test is not a duplicate of previous one, since it covers a separate branch in code. It "Should append to empty file if -Append parameter specified" { - New-Item -Path $testCsv -ItemType File + New-Item -Path $testCsv -ItemType File | Out-Null $P11 | Export-Csv -Path $testCsv -Append $results = Import-Csv -Path $testCsv From b5555a3e61b5fe9df1d5783dad86145c52691a5b Mon Sep 17 00:00:00 2001 From: sethvs Date: Thu, 3 May 2018 12:18:25 +0300 Subject: [PATCH 5/6] Add tests covering ExportCsvHelper. --- .../Export-Csv.Tests.ps1 | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) 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 fba16906216..3ddb6acd215 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-Csv.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-Csv.Tests.ps1 @@ -133,7 +133,7 @@ Describe "Export-Csv" -Tags "CI" { $results[1].P1 | Should -BeExactly "eleventh" } - # This test is not a duplicate of previous one, since it covers a separate branch in code. + # This test is not a duplicate of the previous one, since it covers a separate branch in code. It "Should append to empty file if -Append parameter specified" { New-Item -Path $testCsv -ItemType File | Out-Null @@ -151,6 +151,53 @@ Describe "Export-Csv" -Tags "CI" { $results[0].P1 | Should -BeExactly "first" } + It "Should append existing properties, add missing properties with empty value, and skip extra properties" { + $object1 = [PSCustomObject]@{first = 1; second = 2} + $object2 = [PSCustomObject]@{first = 11; third = 13} + + $object1 | Export-Csv -Path $testCsv + $object2 | Export-Csv -Path $testCsv -Append -Force + + $results = Import-Csv -Path $testCsv + + $results[0].first | Should -BeExactly "1" + $results[0].second | Should -BeExactly "2" + $results[1].first | Should -BeExactly "11" + $results[1].second | Should -BeNullOrEmpty + $results[1].PSObject.properties.Name | Should -Not -Contain 'third' + } + + It "First line should be #TYPE if -IncludeTypeInformation used and pstypenames object property is empty" { + $object = [PSCustomObject]@{first = 1} + $pstypenames = $object.pstypenames | ForEach-Object -Process {$_} + $pstypenames | ForEach-Object -Process {$object.pstypenames.Remove($_)} + $object | Export-Csv -Path $testCsv -IncludeTypeInformation + $content = Get-Content -Path $testCsv + + $content[0] | Should -BeExactly '#TYPE' + } + + It "Shoud remove 'CSV:' from first line when export imported object" { + $P1 | Export-Csv -Path $testCsv -IncludeTypeInformation + $result1 = Import-Csv -Path $testCsv + + # after removing the first element - 'System.Management.Automation.PSCustomObject', + # the remaining - 'CSV:System.Management.Automation.PSCustomObject' is tested. + $result1.pstypenames.Remove('System.Management.Automation.PSCustomObject') + $result1 | Export-Csv -Path $testCsv -IncludeTypeInformation + $result2 = Get-Content -Path $testCsv + + $result2[0] | Should -Not -Match 'CSV' + } + + It "Should escape double quote with another double quote" { + $object = [PSCustomObject]@{first = 'Double quote " in the middle.'} + $object | Export-Csv -Path $testCsv + $result = Get-Content -Path $testCsv + + $result[1] | Should -BeExactly '"Double quote "" in the middle."' + } + It "Test basic function works well" { $in = [pscustomobject]@{ "P1" = "V11"; "P2" = "V12"; "P3" = "V13" } $in | Export-Csv -Path $testCsv -NoTypeInformation From b2b26920ebd7215769fa5ab6a40e4c83192b371b Mon Sep 17 00:00:00 2001 From: sethvs Date: Fri, 4 May 2018 11:50:10 +0300 Subject: [PATCH 6/6] Change test logic. --- .../Export-Csv.Tests.ps1 | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) 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 3ddb6acd215..3f7822be839 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-Csv.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Export-Csv.Tests.ps1 @@ -177,17 +177,15 @@ Describe "Export-Csv" -Tags "CI" { $content[0] | Should -BeExactly '#TYPE' } - It "Shoud remove 'CSV:' from first line when export imported object" { - $P1 | Export-Csv -Path $testCsv -IncludeTypeInformation - $result1 = Import-Csv -Path $testCsv - - # after removing the first element - 'System.Management.Automation.PSCustomObject', - # the remaining - 'CSV:System.Management.Automation.PSCustomObject' is tested. - $result1.pstypenames.Remove('System.Management.Automation.PSCustomObject') - $result1 | Export-Csv -Path $testCsv -IncludeTypeInformation - $result2 = Get-Content -Path $testCsv + # If type starts with CSV: Export-CSV should remove it. This would happen when you export + # an imported object. Import-Csv adds CSV: prefix to the type. + It "Should remove 'CSV:' from the type name" { + $object = [PSCustomObject]@{first = 1} + $object.pstypenames.Insert(0, "CSV:TheType") + $object | Export-Csv -Path $testCsv -IncludeTypeInformation + $result = Get-Content -Path $testCsv - $result2[0] | Should -Not -Match 'CSV' + $result[0] | Should -BeExactly "#TYPE TheType" } It "Should escape double quote with another double quote" {