Export-Csv Test Improvements (#5150)

This commit is contained in:
Roma Marusyk
2017-10-26 08:56:07 +04:00
committed by Ilya
parent 00dde3d8f2
commit 282deb7ee1
@@ -1,67 +1,48 @@
Describe "Export-Csv" -Tags "CI" {
$testObject = @("test","object","array")
$testCsv = Join-Path -Path $TestDrive -ChildPath "output.csv"
BeforeAll {
$testObject = @("test","object","array")
$testCsv = Join-Path -Path $TestDrive -ChildPath "output.csv"
}
AfterEach {
Remove-Item $testCsv -Force -ErrorAction SilentlyContinue
Remove-Item $testCsv -Force -ErrorAction SilentlyContinue
}
It "Should be able to be called without error" {
{ $testObject | Export-Csv $testCsv } | Should Not Throw
{ $testObject | Export-Csv $testCsv -ErrorAction Stop } | Should Not Throw
}
It "Should throw if an output file isn't specified" {
{ $testObject | Export-Csv -ErrorAction SilentlyContinue } | Should Throw
{ $testObject | Export-Csv -ErrorAction Stop } | ShouldBeErrorId "CannotSpecifyPathAndLiteralPath,Microsoft.PowerShell.Commands.ExportCsvCommand"
}
It "Should be a string when exporting via pipe" {
$testObject | Export-Csv $testCsv -IncludeTypeInformation
$testObject | Export-Csv $testCsv -IncludeTypeInformation
$piped = Get-Content $testCsv
$piped = Get-Content $testCsv
$piped[0] | Should Match ".String"
$piped[0] | Should BeExactly "#TYPE System.String"
}
It "Should be an object when exporting via the inputObject switch" {
Export-Csv -InputObject $testObject -Path $testCsv -IncludeTypeInformation
Export-Csv -InputObject $testObject -Path $testCsv -IncludeTypeInformation
$switch = Get-Content $testCsv
$switch = Get-Content $testCsv
$switch[0] | Should Match ".Object"
$switch[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" {
$testObject | Export-Csv -Path $testCsv -IncludeTypeInformation
$testObject | Export-Csv -Path $testCsv -IncludeTypeInformation
$first = "`"" + $testObject[0].Length.ToString() + "`""
$second = "`"" + $testObject[1].Length.ToString() + "`""
$third = "`"" + $testObject[2].Length.ToString() + "`""
$expected = @("#TYPE System.String", "`"Length`"", $first , $second, $third)
$first = "`"" + $testObject[0].Length.ToString() + "`""
$second = "`"" + $testObject[1].Length.ToString() + "`""
$third = "`"" + $testObject[2].Length.ToString() + "`""
$expected = @("#TYPE System.String", "`"Length`"", $first , $second, $third)
for ( $i = 0; $i -lt $testCsv.Length; $i++)
{
$(Get-Content $testCsv)[$i] | Should Be $expected[$i]
}
}
It "Should be able to use the epcsv alias without error" {
{ $testObject | Export-Csv -Path $testCsv } | Should Not Throw
}
It "Should have the same information when using the alias vs the cmdlet" {
$testObject | Export-Csv -Path $testCsv
$aliasObject = Join-Path -Path $TestDrive -ChildPath "alias.csv"
$testObject | epcsv -Path $aliasObject
for ( $i = 0; $i -lt $testCsv.Length; $i++)
{
$(Get-Content $testCsv)[$i] | Should Be $(Get-Content $aliasObject)[$i]
}
# Clean up after yourself
Remove-Item $aliasObject -Force
for ($i = 0; $i -lt $expected.Count; $i++) {
$(Get-Content $testCsv)[$i] | Should Be $expected[$i]
}
}
It "Does not include type information by default" {
@@ -91,8 +72,11 @@ Describe "Export-Csv" -Tags "CI" {
}
Describe "Export-Csv DRT Unit Tests" -Tags "CI" {
$filePath = Join-Path $TestDrive -ChildPath "test.csv"
$newLine = [environment]::NewLine
BeforeAll {
$filePath = Join-Path $TestDrive -ChildPath "test.csv"
$newLine = [environment]::NewLine
}
It "Test basic function works well" {
$input = [pscustomobject]@{ "P1" = "V11"; "P2" = "V12"; "P3" = "V13" }
$input | Export-Csv -Path $filePath -NoTypeInformation