Repository navigation
Added tests for Get-Content - #4723
Conversation
|
@sarithsutha, It will cover your contributions to all Microsoft-managed open source projects. |
|
Sarith Sutha (@sarithsutha) Thanks for your contribution! Please add PR description and motivations. Also you should sign CLA before we can review and merge. |
|
Sarith Sutha (@sarithsutha), thanks for signing the contribution license agreement. We will now validate the agreement and then the pull request. |
| } | ||
| catch { | ||
| $_.FullyQualifiedErrorId | should be "TailAndHeadCannotCoexist,Microsoft.PowerShell.Commands.GetContentCommand" | ||
| } |
There was a problem hiding this comment.
Please use 'ShouldBeErrorId' - see https://github.com/PowerShell/PowerShell/blob/master/docs/testing-guidelines/WritingPesterTests.md
|
|
||
| It "Should throw TailAndHeadCannotCoexist when both -Tail and -TotalCount switches are used" { | ||
| try { | ||
| Get-Content -path $testPath -Tail 1 -TotalCount 1 -ea stop |
There was a problem hiding this comment.
Please fix formatting "-path" -> "-Path", "stop" -> "Stop".
Please use full name for cmdlets, parameters and values. "-ea" -> "ErrorAction".
Below too.
| } | ||
|
|
||
| It "Should throw TailNotSupported when -Tail used with an unsupported provider" { | ||
| pushd env: |
There was a problem hiding this comment.
Please don't use aliases.
Below too.
|
|
||
| It "Should Get-Content containing multi-byte chars for a variety of -Tail and -ReadCount values" { | ||
| Set-content -path $testPath "Hello,World","Hello2,World2","Hello3,World3","Hello4,World4" | ||
| $result = Get-Content -path $testPath -ReadCount:-1 -Tail 5 -Encoding UTF7 |
There was a problem hiding this comment.
Why UTF7? Maybe Utf8?
There was a problem hiding this comment.
This test is for the scenario when SeekPositionForTail fails when using a FileSystemContentReaderWriter. FileSystemContentReaderWriter treats UTF7 and UTF8 differently when determining the starting byte . The exception BackReaderEncodingNotSupportedException is not thrown for UTF8.
| } | ||
|
|
||
| It "Should Get-Content containing multi-byte chars for a variety of -Tail and -ReadCount values" { | ||
| Set-content -path $testPath "Hello,World","Hello2,World2","Hello3,World3","Hello4,World4" |
There was a problem hiding this comment.
If we use a string some times we should put in a variable.
Below too.
| $result = Get-Content -path $testPath -ReadCount:-1 -Tail 5 -Encoding UTF7 | ||
| $result.Length | Should Be 4 | ||
| $expected = "Hello,World","Hello2,World2","Hello3,World3","Hello4,World4" | ||
| for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} |
There was a problem hiding this comment.
I believe we can use Compare-Object to exclude Should and the cycle.
Below too.
|
Thanks for the suggestions Ilya (@iSazonov). I have incorporated them. |
| popd | ||
| Push-Location env: | ||
| { | ||
| Get-Content PATH -Tail 1 -ErrorAction Stop |
There was a problem hiding this comment.
Maybe replace PATH with something like UnsupportedProvider.
There was a problem hiding this comment.
UnsupportedProvider would not be a valid environment variable and the ErrorId would be different i.e. PathNotFound instead of TailNotSupported.
Should I create one with that name before the test and then remove it?
| $fourthLine = "Hello4,World4" | ||
| $fileContent = $firstLine,$secondLine,$thirdLine,$fourthLine | ||
| Set-content -Path $testPath $fileContent | ||
| $result = Get-Content -Path $testPath -ReadCount:-1 -Tail 5 -Encoding UTF7 |
There was a problem hiding this comment.
Please use space -ReadCount:-1 -> -ReadCount -1
There was a problem hiding this comment.
Please clarify why you use UTF7?
There was a problem hiding this comment.
This test is for the scenario when SeekPositionForTail fails when using a FileSystemContentReaderWriter. FileSystemContentReaderWriter treats UTF7 and UTF8 differently when determining the starting byte . The exception BackReaderEncodingNotSupportedException is not thrown for UTF8.
| $expected = "Hello2,World2","Hello3,World3","Hello4,World4" | ||
| for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} | ||
| $result = Get-Content -Path $testPath -ReadCount 0 -Tail 3 -Encoding UTF7 | ||
| $result.Length | Should Be 3 |
There was a problem hiding this comment.
Please remove extra spaces.
| $expected = "Hello2,World2","Hello3,World3","Hello4,World4" | ||
| for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} | ||
| $result = Get-Content -Path $testPath -ReadCount 1 -Tail 3 -Encoding UTF7 | ||
| $result.Length | Should Be 3 |
There was a problem hiding this comment.
Please remove extra spaces.
| $expected = "Hello2,World2","Hello3,World3","Hello4,World4" | ||
| for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} | ||
| $result = Get-Content -Path $testPath -ReadCount 99999 -Tail 3 -Encoding UTF7 | ||
| $result.Length | Should Be 3 |
There was a problem hiding this comment.
Please remove extra spaces.
| $expected = $expected,"Hello4,World4" | ||
| for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} | ||
| $result = Get-Content -Path $testPath -ReadCount 2 -Tail 3 -Encoding UTF7 | ||
| $result.Length | Should Be 2 |
There was a problem hiding this comment.
Please remove extra spaces.
| $result=Get-Content -path $testPath -TotalCount 0 -ReadCount 1 -Encoding UTF7 | ||
| $result.Length | Should Be 0 | ||
| $result = Get-Content -Path $testPath -TotalCount 0 -ReadCount 1 -Encoding UTF7 | ||
| $result.Length | Should Be 0 |
There was a problem hiding this comment.
Please remove extra spaces.
| $expected = "Hello,World","Hello2,World2" | ||
| $expected = $expected, "Hello3,World3" | ||
| $result = Get-Content -Path $testPath -TotalCount 3 -ReadCount 2 -Encoding UTF7 | ||
| $result.Length | Should Be 2 |
There was a problem hiding this comment.
Please remove extra spaces.
| $result = Get-Content -Path $testPath -TotalCount 3 -ReadCount 2 -Encoding UTF7 | ||
| $result.Length | Should Be 2 | ||
| $expected = $firstLine,$secondLine | ||
| $expected = $expected, $thirdLine |
There was a problem hiding this comment.
You don't use space in other places after semi. Please remove extra space.
|
Removed the extra spaces. And here is the clarification on UTF7 usage: |
|
James Truher (@JamesWTruher) Can you approve using UTF7 in the tests? |
Mike Richmond (mirichmo)
left a comment
There was a problem hiding this comment.
Thanks for the contribution. It looks close to being ready.
| $secondLine = "Hello2,World2" | ||
| $thirdLine = "Hello3,World3" | ||
| $fourthLine = "Hello4,World4" | ||
| $fileContent = $firstLine,$secondLine,$thirdLine,$fourthLine |
There was a problem hiding this comment.
I'd like to see this test refactored into a Context and each set of tests become a separate It within the Context. There are too many Should checks within this It.
Something like:
Context "Check Get-Content containing multi-byte chars" {
BeforeAll {
$firstLine = "Hello,World"
$secondLine = "Hello2,World2"
$thirdLine = "Hello3,World3"
$fourthLine = "Hello4,World4"
$fileContent = $firstLine,$secondLine,$thirdLine,$fourthLine
}
It "Some descriptive title" {
Set-content -Path $testPath $fileContent
$result = Get-Content -Path $testPath -ReadCount -1 -Tail 5 -Encoding UTF7
$result.Length | Should Be 4
}
<Additional It blocks, one for each group of checks>
} |
Mike Richmond (@mirichmo), thanks for the review. I've refactored the tests as suggested. Does this look okay? if not, please let me know the changes that are needed. |
|
Sarith Sutha (@sarithsutha) Please resolve the merge conflict. |
| $result = Get-Content -Path $testPath -ReadCount 2 -Tail 3 -Encoding UTF7 | ||
| $result.Length | Should Be 2 | ||
| $expected = $secondLine,$thirdLine | ||
| $expected = $expected,$fourthLine |
There was a problem hiding this comment.
Can we use one line $expected = $secondLine,$thirdLine,$fourthLine ?
There was a problem hiding this comment.
The result will be a jagged Array, the first element is an array containing two strings (second and third lines) and the second element is a plain string (fourth line) and $expected has to be the same to compare.
| $result = Get-Content -Path $testPath -TotalCount 3 -ReadCount 2 -Encoding UTF7 | ||
| $result.Length | Should Be 2 | ||
| $expected = $firstLine,$secondLine | ||
| $expected = $expected,$thirdLine |
There was a problem hiding this comment.
Similar case here. For comparing with result , $expected is supposed to be a jagged Array.
$expected[0] to contain first line and second line
$expected[1] to contain third line
There was a problem hiding this comment.
I see what you are doing, but the code obscures the intent a bit. How about making it syntactically more obvious what is happening. Your explanation makes it much more clear and takes the same number of lines.
There was a problem hiding this comment.
Sure, that makes sense. I've made the changes...
|
Mike Richmond (@mirichmo) Could you please take another look? |
|
Sarith Sutha (@sarithsutha) Thanks for your contribution! |
* Add tests for Get-Content -Tail PowerShell#4150 * Incorporated the review comments * renamed couple of tests * removed extra spaces * Tests refactored into a Context and the checks broken down into separate tests * Made the indents consistent with the other existing tests * Incorporated the review comments
Closes #4150
Added more unit tests for Get-Content, mostly around the -Tail switch to increase the coverage for this cmdlet.