|
1 | 1 | Describe "Get-Content" -Tags "CI" {
|
2 |
| - $testString = "This is a test content for a file" |
3 |
| - $nl = [Environment]::NewLine |
4 |
| - $firstline = "Here's a first line " |
5 |
| - $secondline = " here's a second line" |
6 |
| - $thirdline = "more text" |
7 |
| - $fourthline = "just to make sure" |
8 |
| - $fifthline = "there's plenty to work with" |
9 |
| - $testString2 = $firstline + $nl + $secondline + $nl + $thirdline + $nl + $fourthline + $nl + $fifthline |
10 |
| - $testPath = Join-Path -Path $TestDrive -ChildPath testfile1 |
11 |
| - $testPath2 = Join-Path -Path $TestDrive -ChildPath testfile2 |
| 2 | + $testString = "This is a test content for a file" |
| 3 | + $nl = [Environment]::NewLine |
| 4 | + $firstline = "Here's a first line " |
| 5 | + $secondline = " here's a second line" |
| 6 | + $thirdline = "more text" |
| 7 | + $fourthline = "just to make sure" |
| 8 | + $fifthline = "there's plenty to work with" |
| 9 | + $testString2 = $firstline + $nl + $secondline + $nl + $thirdline + $nl + $fourthline + $nl + $fifthline |
| 10 | + $testPath = Join-Path -Path $TestDrive -ChildPath testfile1 |
| 11 | + $testPath2 = Join-Path -Path $TestDrive -ChildPath testfile2 |
12 | 12 |
|
13 |
| - BeforeEach { |
14 |
| - New-Item -Path $testPath -ItemType file -Force -Value $testString |
15 |
| - New-Item -Path $testPath2 -ItemType file -Force -Value $testString2 |
16 |
| - } |
17 |
| - AfterEach { |
18 |
| - Remove-Item -Path $testPath -Force |
19 |
| - Remove-Item -Path $testPath2 -Force |
20 |
| - } |
21 |
| - It "Should throw an error on a directory " { |
| 13 | + BeforeEach { |
| 14 | + New-Item -Path $testPath -ItemType file -Force -Value $testString |
| 15 | + New-Item -Path $testPath2 -ItemType file -Force -Value $testString2 |
| 16 | + } |
| 17 | + AfterEach { |
| 18 | + Remove-Item -Path $testPath -Force |
| 19 | + Remove-Item -Path $testPath2 -Force |
| 20 | + } |
| 21 | + It "Should throw an error on a directory " { |
22 | 22 | try {
|
23 | 23 | Get-Content . -ErrorAction Stop
|
24 | 24 | throw "No Exception!"
|
25 | 25 | }
|
26 | 26 | catch {
|
27 | 27 | $_.FullyQualifiedErrorId | should be "GetContentReaderUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetContentCommand"
|
28 | 28 | }
|
29 |
| - } |
30 |
| - It "Should return an Object when listing only a single line and the correct information from a file" { |
| 29 | + } |
| 30 | + It "Should return an Object when listing only a single line and the correct information from a file" { |
31 | 31 | $content = (Get-Content -Path $testPath)
|
32 | 32 | $content | Should Be $testString
|
33 | 33 | $content.Count | Should Be 1
|
34 | 34 | $content | Should BeOfType "System.String"
|
35 |
| - } |
36 |
| - It "Should deliver an array object when listing a file with multiple lines and the correct information from a file" { |
| 35 | + } |
| 36 | + It "Should deliver an array object when listing a file with multiple lines and the correct information from a file" { |
37 | 37 | $content = (Get-Content -Path $testPath2)
|
38 | 38 | @(Compare-Object $content $testString2.Split($nl) -SyncWindow 0).Length | Should Be 0
|
39 | 39 | ,$content | Should BeOfType "System.Array"
|
40 |
| - } |
41 |
| - It "Should be able to return a specific line from a file" { |
42 |
| - (Get-Content -Path $testPath2)[1] | Should be $secondline |
43 |
| - } |
44 |
| - It "Should be able to specify the number of lines to get the content of using the TotalCount switch" { |
45 |
| - $returnArray = (Get-Content -Path $testPath2 -TotalCount 2) |
46 |
| - $returnArray[0] | Should Be $firstline |
47 |
| - $returnArray[1] | Should Be $secondline |
48 |
| - } |
49 |
| - It "Should be able to specify the number of lines to get the content of using the Head switch" { |
50 |
| - $returnArray = (Get-Content -Path $testPath2 -Head 2) |
51 |
| - $returnArray[0] | Should Be $firstline |
52 |
| - $returnArray[1] | Should Be $secondline |
53 |
| - } |
54 |
| - It "Should be able to specify the number of lines to get the content of using the First switch" { |
55 |
| - $returnArray = (Get-Content -Path $testPath2 -First 2) |
56 |
| - $returnArray[0] | Should Be $firstline |
57 |
| - $returnArray[1] | Should Be $secondline |
58 |
| - } |
59 |
| - It "Should return the last line of a file using the Tail switch" { |
60 |
| - Get-Content -Path $testPath -Tail 1 | Should Be $testString |
61 |
| - } |
62 |
| - It "Should return the last lines of a file using the Last alias" { |
63 |
| - Get-Content -Path $testPath2 -Last 1 | Should Be $fifthline |
64 |
| - } |
65 |
| - It "Should be able to get content within a different drive" { |
66 |
| - Push-Location env: |
67 |
| - $expectedoutput = [Environment]::GetEnvironmentVariable("PATH"); |
68 |
| - { Get-Content PATH } | Should Not Throw |
69 |
| - Get-Content PATH | Should Be $expectedoutput |
70 |
| - Pop-Location |
71 |
| - } |
72 |
| - #[BugId(BugDatabase.WindowsOutOfBandReleases, 906022)] |
73 |
| - It "should throw 'PSNotSupportedException' when you set-content to an unsupported provider" -Skip:($IsLinux -Or $IsOSX) { |
74 |
| - {get-content -path HKLM:\\software\\microsoft -ea stop} | Should Throw "IContentCmdletProvider interface is not implemented" |
75 |
| - } |
76 |
| - It "should Get-Content with a variety of -Tail and -ReadCount values" {#[DRT] |
77 |
| - set-content -path $testPath "Hello,World","Hello2,World2","Hello3,World3","Hello4,World4" |
78 |
| - $result=get-content -path $testPath -readcount:-1 -tail 5 |
79 |
| - $result.Length | Should Be 4 |
80 |
| - $expected = "Hello,World","Hello2,World2","Hello3,World3","Hello4,World4" |
81 |
| - for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} |
82 |
| - $result=get-content -path $testPath -readcount 0 -tail 3 |
83 |
| - $result.Length | Should Be 3 |
84 |
| - $expected = "Hello2,World2","Hello3,World3","Hello4,World4" |
85 |
| - for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} |
86 |
| - $result=get-content -path $testPath -readcount 1 -tail 3 |
87 |
| - $result.Length | Should Be 3 |
88 |
| - $expected = "Hello2,World2","Hello3,World3","Hello4,World4" |
89 |
| - for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} |
90 |
| - $result=get-content -path $testPath -readcount 99999 -tail 3 |
91 |
| - $result.Length | Should Be 3 |
92 |
| - $expected = "Hello2,World2","Hello3,World3","Hello4,World4" |
93 |
| - for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} |
94 |
| - $result=get-content -path $testPath -readcount 2 -tail 3 |
95 |
| - $result.Length | Should Be 2 |
96 |
| - $expected = "Hello2,World2","Hello3,World3" |
97 |
| - $expected = $expected,"Hello4,World4" |
98 |
| - for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} |
99 |
| - $result=get-content -path $testPath -readcount 2 -tail 2 |
100 |
| - $result.Length | Should Be 2 |
101 |
| - $expected = "Hello3,World3","Hello4,World4" |
102 |
| - for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} |
103 |
| - $result=get-content -path $testPath -delimiter "," -tail 2 |
104 |
| - $result.Length | Should Be 2 |
105 |
| - $expected = "World3${nl}Hello4", "World4${nl}" |
106 |
| - for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} |
107 |
| - $result=get-content -path $testPath -delimiter "o" -tail 3 |
108 |
| - $result.Length | Should Be 3 |
109 |
| - $expected = "rld3${nl}Hell", '4,W', "rld4${nl}" |
110 |
| - for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} |
111 |
| - $result=get-content -path $testPath -encoding:Byte -tail 10 |
112 |
| - $result.Length | Should Be 10 |
113 |
| - if ($IsWindows) { |
114 |
| - $expected = 52, 44, 87, 111, 114, 108, 100, 52, 13, 10 |
115 |
| - } else { |
116 |
| - $expected = 111, 52, 44, 87, 111, 114, 108, 100, 52, 10 |
117 |
| - } |
118 |
| - for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} |
119 |
| - } |
120 |
| - #[BugId(BugDatabase.WindowsOutOfBandReleases, 905829)] |
121 |
| - It "should get-content that matches the input string"{ |
122 |
| - set-content $testPath "Hello,llllWorlld","Hello2,llllWorlld2" |
123 |
| - $result=get-content $testPath -delimiter "ll" |
124 |
| - $result.Length | Should Be 9 |
125 |
| - $expected = 'He', 'o,', '', 'Wor', "d${nl}He", 'o2,', '', 'Wor', "d2${nl}" |
126 |
| - for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} |
127 |
| - } |
| 40 | + } |
| 41 | + It "Should be able to return a specific line from a file" { |
| 42 | + (Get-Content -Path $testPath2)[1] | Should be $secondline |
| 43 | + } |
| 44 | + It "Should be able to specify the number of lines to get the content of using the TotalCount switch" { |
| 45 | + $returnArray = (Get-Content -Path $testPath2 -TotalCount 2) |
| 46 | + $returnArray[0] | Should Be $firstline |
| 47 | + $returnArray[1] | Should Be $secondline |
| 48 | + } |
| 49 | + It "Should be able to specify the number of lines to get the content of using the Head switch" { |
| 50 | + $returnArray = (Get-Content -Path $testPath2 -Head 2) |
| 51 | + $returnArray[0] | Should Be $firstline |
| 52 | + $returnArray[1] | Should Be $secondline |
| 53 | + } |
| 54 | + It "Should be able to specify the number of lines to get the content of using the First switch" { |
| 55 | + $returnArray = (Get-Content -Path $testPath2 -First 2) |
| 56 | + $returnArray[0] | Should Be $firstline |
| 57 | + $returnArray[1] | Should Be $secondline |
| 58 | + } |
| 59 | + It "Should return the last line of a file using the Tail switch" { |
| 60 | + Get-Content -Path $testPath -Tail 1 | Should Be $testString |
| 61 | + } |
| 62 | + It "Should return the last lines of a file using the Last alias" { |
| 63 | + Get-Content -Path $testPath2 -Last 1 | Should Be $fifthline |
| 64 | + } |
| 65 | + It "Should be able to get content within a different drive" { |
| 66 | + Push-Location env: |
| 67 | + $expectedoutput = [Environment]::GetEnvironmentVariable("PATH"); |
| 68 | + { Get-Content PATH } | Should Not Throw |
| 69 | + Get-Content PATH | Should Be $expectedoutput |
| 70 | + Pop-Location |
| 71 | + } |
| 72 | + #[BugId(BugDatabase.WindowsOutOfBandReleases, 906022)] |
| 73 | + It "should throw 'PSNotSupportedException' when you set-content to an unsupported provider" -Skip:($IsLinux -Or $IsOSX) { |
| 74 | + {get-content -path HKLM:\\software\\microsoft -ea stop} | Should Throw "IContentCmdletProvider interface is not implemented" |
| 75 | + } |
| 76 | + It "should Get-Content with a variety of -Tail and -ReadCount values" {#[DRT] |
| 77 | + set-content -path $testPath "Hello,World","Hello2,World2","Hello3,World3","Hello4,World4" |
| 78 | + $result=get-content -path $testPath -readcount:-1 -tail 5 |
| 79 | + $result.Length | Should Be 4 |
| 80 | + $expected = "Hello,World","Hello2,World2","Hello3,World3","Hello4,World4" |
| 81 | + for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} |
| 82 | + $result=get-content -path $testPath -readcount 0 -tail 3 |
| 83 | + $result.Length | Should Be 3 |
| 84 | + $expected = "Hello2,World2","Hello3,World3","Hello4,World4" |
| 85 | + for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} |
| 86 | + $result=get-content -path $testPath -readcount 1 -tail 3 |
| 87 | + $result.Length | Should Be 3 |
| 88 | + $expected = "Hello2,World2","Hello3,World3","Hello4,World4" |
| 89 | + for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} |
| 90 | + $result=get-content -path $testPath -readcount 99999 -tail 3 |
| 91 | + $result.Length | Should Be 3 |
| 92 | + $expected = "Hello2,World2","Hello3,World3","Hello4,World4" |
| 93 | + for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} |
| 94 | + $result=get-content -path $testPath -readcount 2 -tail 3 |
| 95 | + $result.Length | Should Be 2 |
| 96 | + $expected = "Hello2,World2","Hello3,World3" |
| 97 | + $expected = $expected,"Hello4,World4" |
| 98 | + for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} |
| 99 | + $result=get-content -path $testPath -readcount 2 -tail 2 |
| 100 | + $result.Length | Should Be 2 |
| 101 | + $expected = "Hello3,World3","Hello4,World4" |
| 102 | + for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} |
| 103 | + $result=get-content -path $testPath -delimiter "," -tail 2 |
| 104 | + $result.Length | Should Be 2 |
| 105 | + $expected = "World3${nl}Hello4", "World4${nl}" |
| 106 | + for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} |
| 107 | + $result=get-content -path $testPath -delimiter "o" -tail 3 |
| 108 | + $result.Length | Should Be 3 |
| 109 | + $expected = "rld3${nl}Hell", '4,W', "rld4${nl}" |
| 110 | + for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} |
| 111 | + $result=get-content -path $testPath -encoding:Byte -tail 10 |
| 112 | + $result.Length | Should Be 10 |
| 113 | + if ($IsWindows) { |
| 114 | + $expected = 52, 44, 87, 111, 114, 108, 100, 52, 13, 10 |
| 115 | + } else { |
| 116 | + $expected = 111, 52, 44, 87, 111, 114, 108, 100, 52, 10 |
| 117 | + } |
| 118 | + for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} |
| 119 | + } |
| 120 | + #[BugId(BugDatabase.WindowsOutOfBandReleases, 905829)] |
| 121 | + It "should get-content that matches the input string"{ |
| 122 | + set-content $testPath "Hello,llllWorlld","Hello2,llllWorlld2" |
| 123 | + $result=get-content $testPath -delimiter "ll" |
| 124 | + $result.Length | Should Be 9 |
| 125 | + $expected = 'He', 'o,', '', 'Wor', "d${nl}He", 'o2,', '', 'Wor', "d2${nl}" |
| 126 | + for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should BeExactly $expected[$i]} |
| 127 | + } |
128 | 128 |
|
129 |
| - It "Should support NTFS streams using colon syntax" -Skip:(!$IsWindows) { |
130 |
| - Set-Content "${testPath}:Stream" -Value "Foo" |
131 |
| - { Test-Path "${testPath}:Stream" | ShouldBeErrorId "ItemExistsNotSupportedError,Microsoft.PowerShell.Commands,TestPathCommand" } |
132 |
| - Get-Content "${testPath}:Stream" | Should BeExactly "Foo" |
133 |
| - Get-Content $testPath | Should BeExactly $testString |
134 |
| - } |
| 129 | + It "Should support NTFS streams using colon syntax" -Skip:(!$IsWindows) { |
| 130 | + Set-Content "${testPath}:Stream" -Value "Foo" |
| 131 | + { Test-Path "${testPath}:Stream" | ShouldBeErrorId "ItemExistsNotSupportedError,Microsoft.PowerShell.Commands,TestPathCommand" } |
| 132 | + Get-Content "${testPath}:Stream" | Should BeExactly "Foo" |
| 133 | + Get-Content $testPath | Should BeExactly $testString |
| 134 | + } |
135 | 135 |
|
136 |
| - It "Should support NTFS streams using -stream" -Skip:(!$IsWindows) { |
137 |
| - Set-Content -Path $testPath -Stream hello -Value World |
138 |
| - Get-Content -Path $testPath | Should Be $testString |
139 |
| - Get-Content -Path $testPath -Stream hello | Should Be "World" |
140 |
| - $item = Get-Item -Path $testPath -Stream hello |
141 |
| - $item | Should BeOfType System.Management.Automation.Internal.AlternateStreamData |
142 |
| - $item.Stream | Should Be "hello" |
143 |
| - Clear-Content -Path $testPath -Stream hello |
144 |
| - Get-Content -Path $testPath -Stream hello | Should BeNullOrEmpty |
145 |
| - Remove-Item -Path $testPath -Stream hello |
146 |
| - { Get-Content -Path $testPath -Stream hello | ShouldBeErrorId "GetContentReaderFileNotFoundError,Microsoft.PowerShell.Commands.GetContentCommand" } |
147 |
| - } |
| 136 | + It "Should support NTFS streams using -stream" -Skip:(!$IsWindows) { |
| 137 | + Set-Content -Path $testPath -Stream hello -Value World |
| 138 | + Get-Content -Path $testPath | Should Be $testString |
| 139 | + Get-Content -Path $testPath -Stream hello | Should Be "World" |
| 140 | + $item = Get-Item -Path $testPath -Stream hello |
| 141 | + $item | Should BeOfType System.Management.Automation.Internal.AlternateStreamData |
| 142 | + $item.Stream | Should Be "hello" |
| 143 | + Clear-Content -Path $testPath -Stream hello |
| 144 | + Get-Content -Path $testPath -Stream hello | Should BeNullOrEmpty |
| 145 | + Remove-Item -Path $testPath -Stream hello |
| 146 | + { Get-Content -Path $testPath -Stream hello | ShouldBeErrorId "GetContentReaderFileNotFoundError,Microsoft.PowerShell.Commands.GetContentCommand" } |
| 147 | + } |
148 | 148 |
|
149 |
| - It "Should support colons in filename on Linux/Mac" -Skip:($IsWindows) { |
150 |
| - Set-Content "${testPath}:Stream" -Value "Hello" |
151 |
| - "${testPath}:Stream" | Should Exist |
152 |
| - Get-Content "${testPath}:Stream" | Should BeExactly "Hello" |
153 |
| - } |
| 149 | + It "Should support colons in filename on Linux/Mac" -Skip:($IsWindows) { |
| 150 | + Set-Content "${testPath}:Stream" -Value "Hello" |
| 151 | + "${testPath}:Stream" | Should Exist |
| 152 | + Get-Content "${testPath}:Stream" | Should BeExactly "Hello" |
| 153 | + } |
154 | 154 |
|
155 |
| - It "-Stream is not a valid parameter for <cmdlet> on Linux/Mac" -Skip:($IsWindows) -TestCases @( |
156 |
| - @{cmdlet="get-content"}, |
157 |
| - @{cmdlet="set-content"}, |
158 |
| - @{cmdlet="clear-content"}, |
159 |
| - @{cmdlet="add-content"}, |
160 |
| - @{cmdlet="get-item"}, |
161 |
| - @{cmdlet="remove-item"} |
162 |
| - ) { |
163 |
| - param($cmdlet) |
164 |
| - (Get-Command $cmdlet).Parameters["stream"] | Should BeNullOrEmpty |
165 |
| - } |
| 155 | + It "-Stream is not a valid parameter for <cmdlet> on Linux/Mac" -Skip:($IsWindows) -TestCases @( |
| 156 | + @{cmdlet="get-content"}, |
| 157 | + @{cmdlet="set-content"}, |
| 158 | + @{cmdlet="clear-content"}, |
| 159 | + @{cmdlet="add-content"}, |
| 160 | + @{cmdlet="get-item"}, |
| 161 | + @{cmdlet="remove-item"} |
| 162 | + ) { |
| 163 | + param($cmdlet) |
| 164 | + (Get-Command $cmdlet).Parameters["stream"] | Should BeNullOrEmpty |
| 165 | + } |
166 | 166 | }
|
0 commit comments