2016-03-04 23:48:30 +01:00
|
|
|
Describe "Get-Content" {
|
2015-09-16 00:33:52 +02:00
|
|
|
$testString = "This is a test content for a file"
|
2015-11-30 23:42:23 +01:00
|
|
|
$nl = [Environment]::NewLine
|
2015-09-18 00:35:38 +02:00
|
|
|
|
|
|
|
$firstline = "Here's a first line "
|
|
|
|
$secondline = " here's a second line"
|
|
|
|
$thirdline = "more text"
|
|
|
|
$fourthline = "just to make sure"
|
|
|
|
$fifthline = "there's plenty to work with"
|
|
|
|
|
|
|
|
$testString2 = $firstline + $nl + $secondline + $nl + $thirdline + $nl + $fourthline + $nl + $fifthline
|
|
|
|
|
2016-02-05 17:53:44 +01:00
|
|
|
$testPath = Join-Path -Path $TestDrive -ChildPath testfile1
|
|
|
|
$testPath2 = Join-Path -Path $TestDrive -ChildPath testfile2
|
2015-09-16 00:33:52 +02:00
|
|
|
|
|
|
|
BeforeEach {
|
2016-03-04 23:52:27 +01:00
|
|
|
New-Item -Path $testPath -ItemType file -Force -Value $testString
|
|
|
|
New-Item -Path $testPath2 -ItemType file -Force -Value $testString2
|
2015-09-16 00:33:52 +02:00
|
|
|
}
|
|
|
|
|
2016-02-04 17:43:51 +01:00
|
|
|
AfterEach {
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Item -Path $testPath -Force
|
|
|
|
Remove-Item -Path $testPath2 -Force
|
2016-02-04 17:43:51 +01:00
|
|
|
}
|
|
|
|
|
2015-07-15 19:15:39 +02:00
|
|
|
It "Should throw an error on a directory " {
|
2016-03-04 23:52:27 +01:00
|
|
|
# also tests that -erroraction SilentlyContinue will work.
|
|
|
|
Get-Content . -ErrorAction SilentlyContinue | Should Throw
|
2015-09-16 00:33:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should return an Object when listing only a single line" {
|
2016-03-04 23:52:27 +01:00
|
|
|
(Get-Content -Path $testPath).GetType().BaseType.Name | Should Be "Object"
|
2015-09-16 00:33:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should deliver an array object when listing a file with multiple lines" {
|
2016-03-04 23:52:27 +01:00
|
|
|
(Get-Content -Path $testPath2).GetType().BaseType.Name | Should Be "Array"
|
2015-09-16 00:33:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should return the correct information from a file" {
|
2016-03-04 23:52:27 +01:00
|
|
|
(Get-Content -Path $testPath) | Should Be $testString
|
2015-09-16 00:33:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to call using the cat alias" {
|
2016-03-04 23:52:27 +01:00
|
|
|
{ cat -Path $testPath } | Should Not Throw
|
2015-09-16 00:33:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to call using the gc alias" {
|
2016-03-04 23:52:27 +01:00
|
|
|
{ gc -Path $testPath } | Should Not Throw
|
2015-09-16 00:33:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to call using the type alias" {
|
2016-03-04 23:52:27 +01:00
|
|
|
{ type -Path $testPath } | Should Not Throw
|
2015-09-16 00:33:52 +02:00
|
|
|
}
|
|
|
|
|
2015-09-18 00:35:38 +02:00
|
|
|
It "Should return the same values for aliases" {
|
2016-03-04 23:52:27 +01:00
|
|
|
$getContentAlias = Get-Content -Path $testPath
|
|
|
|
$gcAlias = gc -Path $testPath
|
|
|
|
$catAlias = cat -Path $testPath
|
|
|
|
$typeAlias = type -Path $testPath
|
2015-09-18 00:35:38 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$getContentAlias | Should Be $gcAlias
|
|
|
|
$getContentAlias | Should Be $catAlias
|
|
|
|
$getContentAlias | Should Be $typeAlias
|
2015-09-18 00:35:38 +02:00
|
|
|
}
|
|
|
|
|
2015-09-16 00:33:52 +02:00
|
|
|
It "Should be able to return a specific line from a file" {
|
2016-03-04 23:52:27 +01:00
|
|
|
(Get-Content -Path $testPath2)[1] | Should be $secondline
|
2015-09-16 00:33:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to specify the number of lines to get the content of using the TotalCount switch" {
|
2016-03-04 23:52:27 +01:00
|
|
|
$returnArray = (cat -Path $testPath2 -TotalCount 2)
|
2015-09-16 00:33:52 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$returnArray[0] | Should Be $firstline
|
|
|
|
$returnArray[1] | Should Be $secondline
|
2015-09-16 00:33:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to specify the number of lines to get the content of using the Head switch" {
|
2016-03-04 23:52:27 +01:00
|
|
|
$returnArray = (cat -Path $testPath2 -Head 2)
|
2015-07-15 19:15:39 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$returnArray[0] | Should Be $firstline
|
|
|
|
$returnArray[1] | Should Be $secondline
|
2015-07-15 19:15:39 +02:00
|
|
|
}
|
|
|
|
|
2015-09-16 00:33:52 +02:00
|
|
|
It "Should be able to specify the number of lines to get the content of using the First switch" {
|
2016-03-04 23:52:27 +01:00
|
|
|
$returnArray = (cat -Path $testPath2 -First 2)
|
2015-09-16 00:33:52 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$returnArray[0] | Should Be $firstline
|
|
|
|
$returnArray[1] | Should Be $secondline
|
2015-09-16 00:33:52 +02:00
|
|
|
}
|
2015-07-16 01:27:53 +02:00
|
|
|
|
2015-09-16 00:33:52 +02:00
|
|
|
It "Should return the last line of a file using the Tail switch" {
|
2016-03-04 23:52:27 +01:00
|
|
|
Get-Content -Path $testPath -Tail 1 | Should Be $testString
|
2015-09-16 00:33:52 +02:00
|
|
|
}
|
|
|
|
|
2015-09-19 03:32:21 +02:00
|
|
|
|
2015-09-16 00:33:52 +02:00
|
|
|
It "Should return the last lines of a file using the Last alias" {
|
2016-03-04 23:52:27 +01:00
|
|
|
Get-Content -Path $testPath2 -Last 1 | Should Be $fifthline
|
2015-09-16 00:33:52 +02:00
|
|
|
}
|
2015-07-16 01:27:53 +02:00
|
|
|
|
2015-09-16 00:33:52 +02:00
|
|
|
It "Should be able to get content within a different drive" {
|
2016-03-04 23:52:27 +01:00
|
|
|
pushd env:
|
|
|
|
$expectedoutput = [Environment]::GetEnvironmentVariable("PATH");
|
2015-07-16 01:27:53 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
{ Get-Content PATH } | Should Not Throw
|
|
|
|
Get-Content PATH | Should Be $expectedoutput
|
2015-07-15 19:15:39 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
popd
|
2015-07-15 19:15:39 +02:00
|
|
|
}
|
2015-07-16 01:27:53 +02:00
|
|
|
}
|