Skip to content

Added tests for Get-Content - #4723

Merged
Mike Richmond (mirichmo) merged 8 commits into
PowerShell:masterfrom
sarithsutha:master
Sep 18, 2017
Merged

Mike Richmond (mirichmo) merged 8 commits into
PowerShell:masterfrom
sarithsutha:master

Conversation

@sarithsutha

@sarithsutha Sarith Sutha (sarithsutha) commented Aug 31, 2017 •

Copy link
Copy Markdown
Contributor

Closes #4150

Added more unit tests for Get-Content, mostly around the -Tail switch to increase the coverage for this cmdlet.

@msftclas

@sarithsutha,
Thanks for your contribution.
To ensure that the project team has proper rights to use your work, please complete the Contribution License Agreement at https://cla.microsoft.com.

It will cover your contributions to all Microsoft-managed open source projects.
Thanks,
Microsoft Pull Request Bot

@iSazonov

Copy link
Copy Markdown
Collaborator

Sarith Sutha (@sarithsutha) Thanks for your contribution! Please add PR description and motivations. Also you should sign CLA before we can review and merge.

@msftclas

Sarith Sutha (@sarithsutha), thanks for signing the contribution license agreement. We will now validate the agreement and then the pull request.

Thanks, Microsoft Pull Request Bot

@sarithsutha Sarith Sutha (sarithsutha) changed the title Add tests for Get-Content -Tail #4150 Added tests for Get-Content Aug 31, 2017
}
catch {
$_.FullyQualifiedErrorId | should be "TailAndHeadCannotCoexist,Microsoft.PowerShell.Commands.GetContentCommand"
}

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.


It "Should throw TailAndHeadCannotCoexist when both -Tail and -TotalCount switches are used" {
try {
Get-Content -path $testPath -Tail 1 -TotalCount 1 -ea stop

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 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:

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 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

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.

Why UTF7? Maybe Utf8?

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.

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"

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.

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]}

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 can use Compare-Object to exclude Should and the cycle.
Below too.

@sarithsutha

Copy link
Copy Markdown
Contributor Author

Thanks for the suggestions Ilya (@iSazonov). I have incorporated them.

popd
Push-Location env:
{
Get-Content PATH -Tail 1 -ErrorAction Stop

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.

Maybe replace PATH with something like UnsupportedProvider.

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.

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

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 use space -ReadCount:-1 -> -ReadCount -1

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 clarify why you use UTF7?

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.

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

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 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

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 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

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 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

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 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

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 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

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 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

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.

You don't use space in other places after semi. Please remove extra space.

@sarithsutha

Copy link
Copy Markdown
Contributor Author

Removed the extra spaces.

And here is the clarification on UTF7 usage:
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.

@iSazonov

Copy link
Copy Markdown
Collaborator

James Truher (@JamesWTruher) Can you approve using UTF7 in the tests?

@mirichmo Mike Richmond (mirichmo) left a comment

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.

Thanks for the contribution. It looks close to being ready.

$secondLine = "Hello2,World2"
$thirdLine = "Hello3,World3"
$fourthLine = "Hello4,World4"
$fileContent = $firstLine,$secondLine,$thirdLine,$fourthLine

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.

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>
} 

@sarithsutha

Copy link
Copy Markdown
Contributor Author

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.

@iSazonov

Copy link
Copy Markdown
Collaborator

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

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.

Can we use one line $expected = $secondLine,$thirdLine,$fourthLine ?

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.

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

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.

The same.

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.

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

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.

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.

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.

Sure, that makes sense. I've made the changes...

@iSazonov

Copy link
Copy Markdown
Collaborator

Mike Richmond (@mirichmo) Could you please take another look?

@mirichmo
Mike Richmond (mirichmo) merged commit af798fb into PowerShell:master Sep 18, 2017
@iSazonov

Copy link
Copy Markdown
Collaborator

Sarith Sutha (@sarithsutha) Thanks for your contribution!

Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
* 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
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