Skip to content

Improve code coverage in Export-Csv.Tests.ps1. - #6795

Merged
Aditya Patwardhan (adityapatwardhan) merged 6 commits into
PowerShell:masterfrom
sethvs:ExportCsvTests
May 4, 2018
Merged

Improve code coverage in Export-Csv.Tests.ps1.#6795
Aditya Patwardhan (adityapatwardhan) merged 6 commits into
PowerShell:masterfrom
sethvs:ExportCsvTests

Conversation

@sethvs

@sethvs Sergey Vasin (sethvs) commented May 2, 2018

Copy link
Copy Markdown
Contributor

PR Summary

Improve code coverage in Export-Csv.Tests.ps1.

PR Checklist

BeforeAll {
$filePath = Join-Path $TestDrive -ChildPath "test.csv"
$newLine = [environment]::NewLine
It "Should be able to use -LiteralPath parameter" {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be able to use -> Should support

$results = Import-Csv -Path $testCsv

$results.P2 | Should -BeExactly "second"
$property = $results | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | ForEach-Object { $_.Name }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps:

$property = $results.PSObject.Properties.Name

$results = Import-Csv -Path $testCsv

$results.P1 | Should -BeExactly "first"
$property = $results | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | ForEach-Object { $_.Name }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above.

$results = Import-Csv -Path $testCsv

$results.P1 | Should -BeExactly "first"
$property = $results | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | ForEach-Object { $_.Name }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

$results = Import-Csv -Path $testCsv

$results.P2 | Should -BeExactly "second"
$property = $results | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | ForEach-Object { $_.Name }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above


$results.P1 | Should -BeExactly "first"
$property = $results | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | ForEach-Object { $_.Name }
$property | Should -BeExactly P1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 should be quoted


$results.P2 | Should -BeExactly "second"
$property = $results | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | ForEach-Object { $_.Name }
$property | Should -BeExactly P2

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 should be quoted

$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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 should be quoted


$results[0].P1 | Should -BeExactly "eleventh"
$property = $results | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | ForEach-Object { $_.Name }
$property | Should -BeExactly P1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 should be quoted


$results[0].P1 | Should -BeExactly "first"
$property = $results | Get-Member | Where-Object { $_.MemberType -eq "NoteProperty" } | ForEach-Object { $_.Name }
$property | Should -BeExactly P1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 should be quoted

@sethvs

Copy link
Copy Markdown
Contributor Author

Fixed.


$results.P2 | Should -BeExactly "second"
$property = $results.PSObject.Properties.Name
$property | Should -BeExactly "P2"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems it is a duplicate line 91 and should be removed- the line failed if we haven't "P2" property and we never reach line 93. Below too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe better remove 92 and 93, since line 91 covers both property name and value?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

$property | Should -BeExactly "P1"
}

It "Should append to empty file if -Append parameter specified" {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we should remove the tests - it repeats the previous test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a branch in code that this test covers.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the comment that it does not duplicate the previous test and checks another branch of the code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closed.


$contents.Count | Should -Be 2
$contents[0].Contains($delimiter) | Should -BeTrue
$contents[1].Contains($delimiter) | Should -BeTrue

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use Should -Contain

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. If the delimiter is comma, Should treats input as array, so $contents[0] | Should -Contain $delimiter will fail.

@iSazonov Ilya (iSazonov) May 2, 2018

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$contents[0] is a string not a language operator.
I think it is passed:
"," | Should -Contain ","

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It "Test export-csv with a useculture flag" {
    $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
    $contents = Get-Content -Path $fileToGenerate

    $contents.Count | Should -Be 2
    $contents[0] | Should -Contain $delimiter
}


  [-] Test export-csv with a useculture flag 144ms
    Expected ',' to be found in collection "PSPath","PSParentPath","PSChildName","PSDrive","PSProvider","PSIsContainer","Mode","BaseName","Target","LinkType","Name","Parent","Exists","Root","FullName","Extension","CreationTime","CreationTimeUtc","LastAccessTime","LastAccessTimeUtc","LastWriteTime","LastWriteTimeUtc","Attributes", but it was not found.
    186:         $contents[0] | Should -Contain $delimiter

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the repo! This is my misconception. Closed.


# 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New-Item … > $null

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@sethvs Sergey Vasin (sethvs) changed the title Improve code coverage in Export-Csv.Tests.ps1. WIP: Improve code coverage in Export-Csv.Tests.ps1. May 3, 2018
@sethvs Sergey Vasin (sethvs) changed the title WIP: Improve code coverage in Export-Csv.Tests.ps1. Improve code coverage in Export-Csv.Tests.ps1. May 3, 2018
@adityapatwardhan
Aditya Patwardhan (adityapatwardhan) merged commit 1be0594 into PowerShell:master May 4, 2018
@adityapatwardhan

Copy link
Copy Markdown
Member

Sergey Vasin (@sethvs) Thanks for your contribution!

@sethvs
Sergey Vasin (sethvs) deleted the ExportCsvTests branch May 21, 2018 16:25
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
* Improve code coverage in Export-Csv.Tests.ps1.

* Change test logic.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants