Refactor the Get-Content tests to use -TestCases. (#6082)

This commit is contained in:
Klaudia Algiz 2018-02-08 13:32:10 -08:00 committed by Travis Plunk
parent 40eb3b3e13
commit d6678d8417

View file

@ -9,15 +9,19 @@ Describe "Get-Content" -Tags "CI" {
$testString2 = $firstline + $nl + $secondline + $nl + $thirdline + $nl + $fourthline + $nl + $fifthline
$testPath = Join-Path -Path $TestDrive -ChildPath testfile1
$testPath2 = Join-Path -Path $TestDrive -ChildPath testfile2
$testContent = "AA","BB","CC","DD"
$testDelimiterContent = "Hello1,World1","Hello2,World2","Hello3,World3","Hello4,World4"
BeforeEach {
New-Item -Path $testPath -ItemType file -Force -Value $testString
New-Item -Path $testPath2 -ItemType file -Force -Value $testString2
}
AfterEach {
Remove-Item -Path $testPath -Force
Remove-Item -Path $testPath2 -Force
}
It "Should throw an error on a directory " {
try {
Get-Content . -ErrorAction Stop
@ -27,41 +31,50 @@ Describe "Get-Content" -Tags "CI" {
$_.FullyQualifiedErrorId | should be "GetContentReaderUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetContentCommand"
}
}
It "Should return an Object when listing only a single line and the correct information from a file" {
$content = (Get-Content -Path $testPath)
$content | Should Be $testString
$content.Count | Should Be 1
$content | Should BeOfType "System.String"
}
It "Should deliver an array object when listing a file with multiple lines and the correct information from a file" {
$content = (Get-Content -Path $testPath2)
@(Compare-Object $content $testString2.Split($nl) -SyncWindow 0).Length | Should Be 0
,$content | Should BeOfType "System.Array"
}
It "Should be able to return a specific line from a file" {
(Get-Content -Path $testPath2)[1] | Should be $secondline
}
It "Should be able to specify the number of lines to get the content of using the TotalCount switch" {
$returnArray = (Get-Content -Path $testPath2 -TotalCount 2)
$returnArray[0] | Should Be $firstline
$returnArray[1] | Should Be $secondline
}
It "Should be able to specify the number of lines to get the content of using the Head switch" {
$returnArray = (Get-Content -Path $testPath2 -Head 2)
$returnArray[0] | Should Be $firstline
$returnArray[1] | Should Be $secondline
}
It "Should be able to specify the number of lines to get the content of using the First switch" {
$returnArray = (Get-Content -Path $testPath2 -First 2)
$returnArray[0] | Should Be $firstline
$returnArray[1] | Should Be $secondline
}
It "Should return the last line of a file using the Tail switch" {
Get-Content -Path $testPath -Tail 1 | Should Be $testString
}
It "Should return the last lines of a file using the Last alias" {
Get-Content -Path $testPath2 -Last 1 | Should Be $fifthline
}
It "Should be able to get content within a different drive" {
Push-Location env:
$expectedoutput = [Environment]::GetEnvironmentVariable("PATH");
@ -69,16 +82,20 @@ Describe "Get-Content" -Tags "CI" {
Get-Content PATH | Should Be $expectedoutput
Pop-Location
}
#[BugId(BugDatabase.WindowsOutOfBandReleases, 906022)]
It "should throw 'PSNotSupportedException' when you Set-Content to an unsupported provider" -Skip:($IsLinux -Or $IsMacOS) {
{Get-Content -Path HKLM:\\software\\microsoft -EA stop} | Should Throw "IContentCmdletProvider interface is not implemented"
}
It 'Verifies -Tail reports a TailNotSupported error for unsupported providers' {
{Get-Content -Path Variable:\PSHOME -Tail 1 -ErrorAction Stop} | ShouldBeErrorId 'TailNotSupported,Microsoft.PowerShell.Commands.GetContentCommand'
}
It 'Verifies using -Tail and -TotalCount together reports a TailAndHeadCannotCoexist error' {
{ Get-Content -Path Variable:\PSHOME -Tail 1 -TotalCount 5 -ErrorAction Stop} | ShouldBeErrorId 'TailAndHeadCannotCoexist,Microsoft.PowerShell.Commands.GetContentCommand'
}
It 'Verifies -Tail with content that uses an explicit encoding' -TestCases @(
@{EncodingName = 'String'},
@{EncodingName = 'Unicode'},
@ -109,50 +126,86 @@ baz
$actual.Length | Should Be $tailCount
$actual[0] | Should Be $expected
}
It "should Get-Content with a variety of -Tail and -ReadCount values" {#[DRT]
Set-Content -Path $testPath "Hello,World","Hello2,World2","Hello3,World3","Hello4,World4"
$result=Get-Content -Path $testPath -Readcount:-1 -Tail 5
$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]}
$result=Get-Content -Path $testPath -Readcount 0 -Tail 3
$result.Length | Should Be 3
$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
$result.Length | Should Be 3
$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
$result.Length | Should Be 3
$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 2 -Tail 3
$result.Length | Should Be 2
$expected = "Hello2,World2","Hello3,World3"
$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 2
$result.Length | Should Be 2
$expected = "Hello3,World3","Hello4,World4"
for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]}
$result=Get-Content -Path $testPath -Delimiter "," -Tail 2
$result.Length | Should Be 2
$expected = "World3${nl}Hello4", "World4${nl}"
for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]}
$result=Get-Content -Path $testPath -Delimiter "o" -Tail 3
$result.Length | Should Be 3
$expected = "rld3${nl}Hell", '4,W', "rld4${nl}"
for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]}
$result=Get-Content -Path $testPath -AsByteStream -Tail 10
$result.Length | Should Be 10
if ($IsWindows) {
$expected = 52, 44, 87, 111, 114, 108, 100, 52, 13, 10
} else {
$expected = 111, 52, 44, 87, 111, 114, 108, 100, 52, 10
It "should Get-Content with a variety of -Tail and -ReadCount: <test>" -TestCases @(
@{ test = "negative readcount"
GetContentParams = @{Path = $testPath; Readcount = -1; Tail = 5}
expectedLength = 4
expectedContent = "AA","BB","CC","DD"
}
for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]}
@{ test = "readcount=0"
GetContentParams = @{Path = $testPath; Readcount = 0; Tail = 3}
expectedLength = 3
expectedContent = "BB","CC","DD"
}
@{ test = "readcount=1"
GetContentParams = @{Path = $testPath; Readcount = 1; Tail = 3}
expectedLength = 3
expectedContent = "BB","CC","DD"
}
@{ test = "high readcount"
GetContentParams = @{Path = $testPath; Readcount = 99999; Tail = 3}
expectedLength = 3
expectedContent = "BB","CC","DD"
}
@{ test = "readcount=2 tail=3"
GetContentParams = @{Path = $testPath; Readcount = 2; Tail = 3}
expectedLength = 2
expectedContent = ("BB","CC"), "DD"
}
@{ test = "readcount=2 tail=2"
GetContentParams = @{Path = $testPath; Readcount = 2; Tail = 2}
expectedLength = 2
expectedContent = "CC","DD"
}
) {
param($GetContentParams, $expectedLength, $expectedContent)
Set-Content -Path $testPath $testContent
$result = Get-Content @GetContentParams
$result.Length | Should Be $expectedLength
$result -eq $expectedContent
}
It "should Get-Content with a variety of -Delimiter and -Tail: <test>" -TestCases @(
@{ test = ", as delimiter"
GetContentParams = @{Path = $testPath; Delimiter = ","; Tail = 2}
expectedLength = 2
expectedContent = "World3${nl}Hello4", "World4${nl}"
}
@{ test = "o as delimiter"
GetContentParams = @{Path = $testPath; Delimiter = "o"; Tail = 3}
expectedLength = 3
expectedContent = "rld3${nl}Hell", '4,W', "rld4${nl}"
}
) {
param($GetContentParams, $expectedLength, $expectedContent)
Set-Content -Path $testPath $testDelimiterContent
$result = Get-Content @GetContentParams
$result.Length | Should Be $expectedLength
$result | Should BeExactly $expectedContent
}
It "should Get-Content with a variety of -Tail values and -AsByteStream parameter" -TestCases @(
@{
GetContentParams = @{Path = $testPath; Tail = 10; AsByteStream = $true}
expectedLength = 10
# Byte encoding of \r\nCC\r\nDD\r\n
expectedWindowsContent = 13, 10, 67, 67, 13, 10, 68, 68, 13, 10
# Byte encoding of \nBB\nCC\nDD\n
expectedNotWindowsContent = 10, 66, 66, 10, 67, 67, 10, 68, 68, 10
}
) {
param($GetContentParams, $expectedLength, $expectedWindowsContent, $expectedNotWindowsContent)
Set-Content -Path $testPath $testContent
$result = Get-Content @GetContentParams
$result.Length | Should Be $expectedLength
if ($isWindows) {
$result | Should BeExactly $expectedWindowsContent
} else {
$result | Should BeExactly $expectedNotWindowsContent
}
}
#[BugId(BugDatabase.WindowsOutOfBandReleases, 905829)]
It "should Get-Content that matches the input string"{
Set-Content $testPath "Hello,llllWorlld","Hello2,llllWorlld2"
@ -227,6 +280,7 @@ baz
Get-Content -Path $testPath -Tail 1 -ErrorAction Stop -Raw
} | ShouldBeErrorId "InvalidOperation,Microsoft.PowerShell.Commands.GetContentCommand"
}
Context "Check Get-Content containing multi-byte chars" {
BeforeAll {
$firstLine = "Hello,World"