MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Get-Content" -Tags "CI" {4 $testString = "This is a test content for a file"5 $nl = [Environment]::NewLine6 $firstline = "Here's a first line "7 $secondline = " here's a second line"8 $thirdline = "more text"9 $fourthline = "just to make sure"10 $fifthline = "there's plenty to work with"11 $testString2 = $firstline + $nl + $secondline + $nl + $thirdline + $nl + $fourthline + $nl + $fifthline12 $testPath = Join-Path -Path $TestDrive -ChildPath testfile113 $testPath2 = Join-Path -Path $TestDrive -ChildPath testfile214 $testContent = "AA","BB","CC","DD"15 $testDelimiterContent = "Hello1,World1","Hello2,World2","Hello3,World3","Hello4,World4"16 17 BeforeEach {18 New-Item -Path $testPath -ItemType file -Force -Value $testString19 New-Item -Path $testPath2 -ItemType file -Force -Value $testString220 }21 22 AfterEach {23 Remove-Item -Path $testPath -Force24 Remove-Item -Path $testPath2 -Force25 }26 27 It "Should throw an error on a directory" {28 { Get-Content . -ErrorAction Stop } |29 Should -Throw -ErrorId "GetContainerContentException,Microsoft.PowerShell.Commands.GetContentCommand"30 }31 32 It "Should return an Object when listing only a single line and the correct information from a file" {33 $content = (Get-Content -Path $testPath)34 $content | Should -BeExactly $testString35 $content.Count | Should -Be 136 $content | Should -BeOfType System.String37 }38 39 It "Should deliver an array object when listing a file with multiple lines and the correct information from a file" {40 $content = (Get-Content -Path $testPath2)41 @(Compare-Object $content $testString2.Split($nl) -SyncWindow 0).Length | Should -Be 042 ,$content | Should -BeOfType System.Array43 }44 45 It "Should be able to return a specific line from a file" {46 (Get-Content -Path $testPath2)[1] | Should -BeExactly $secondline47 }48 49 It "Should be able to specify the number of lines to get the content of using the TotalCount switch" {50 $returnArray = (Get-Content -Path $testPath2 -TotalCount 2)51 $returnArray[0] | Should -BeExactly $firstline52 $returnArray[1] | Should -BeExactly $secondline53 }54 55 It "Should be able to specify the number of lines to get the content of using the Head switch" {56 $returnArray = (Get-Content -Path $testPath2 -Head 2)57 $returnArray[0] | Should -BeExactly $firstline58 $returnArray[1] | Should -BeExactly $secondline59 }60 61 It "Should be able to specify the number of lines to get the content of using the First switch" {62 $returnArray = (Get-Content -Path $testPath2 -First 2)63 $returnArray[0] | Should -BeExactly $firstline64 $returnArray[1] | Should -BeExactly $secondline65 }66 67 It "Should return the last line of a file using the Tail switch" {68 Get-Content -Path $testPath -Tail 1 | Should -BeExactly $testString69 }70 71 It "Should return the last lines of a file using the Last alias" {72 Get-Content -Path $testPath2 -Last 1 | Should -BeExactly $fifthline73 }74 75 It 'Verifies -TotalCount reports a ParameterArgumentValidationError error for negative values' {76 {Get-Content -Path $testPath2 -TotalCount -2} | Should -Throw -ErrorId 'ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetContentCommand'77 }78 79 It 'Verifies -Tail reports a ParameterArgumentValidationError error for negative values' {80 {Get-Content -Path $testPath2 -Tail -2} | Should -Throw -ErrorId 'ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetContentCommand'81 }82 83 It "Should be able to get content within a different drive" {84 Push-Location env:85 $expectedoutput = [Environment]::GetEnvironmentVariable("PATH");86 { Get-Content PATH } | Should -Not -Throw87 Get-Content PATH | Should -BeExactly $expectedoutput88 Pop-Location89 }90 91 #[BugId(BugDatabase.WindowsOutOfBandReleases, 906022)]92 It "should throw 'PSNotSupportedException' when you Set-Content to an unsupported provider" -Skip:($IsLinux -Or $IsMacOS) {93 {Get-Content -Path HKLM:\\software\\microsoft -ErrorAction Stop} | Should -Throw "IContentCmdletProvider interface is not implemented"94 }95 96 It 'Verifies -Tail reports a TailNotSupported error for unsupported providers' {97 {Get-Content -Path Variable:\PSHOME -Tail 1 -ErrorAction Stop} | Should -Throw -ErrorId 'TailNotSupported,Microsoft.PowerShell.Commands.GetContentCommand'98 }99 100 It 'Verifies using -Tail and -TotalCount together reports a TailAndHeadCannotCoexist error' {101 { Get-Content -Path Variable:\PSHOME -Tail 1 -TotalCount 5 -ErrorAction Stop} | Should -Throw -ErrorId 'TailAndHeadCannotCoexist,Microsoft.PowerShell.Commands.GetContentCommand'102 }103 104 It 'Verifies -Tail with content that uses an explicit/implicit encoding' -TestCases @(105 @{EncodingName = 'String'},106 @{EncodingName = 'OEM'},107 @{EncodingName = 'Unicode'},108 @{EncodingName = 'BigEndianUnicode'},109 @{EncodingName = 'BigEndianUTF32'},110 @{EncodingName = 'UTF8'},111 @{EncodingName = 'UTF8BOM'},112 @{EncodingName = 'UTF8NoBOM'},113 @{EncodingName = 'UTF7'},114 @{EncodingName = 'UTF32'},115 @{EncodingName = 'Ascii'},116 @{EncodingName = 'ANSI'}117 ){118 param($EncodingName)119 120 $contentSets =121 @(@('a1','aa2','aaa3','aaaa4','aaaaa5'), # utf-8122 @('€1','€€2','€€€3','€€€€4','€€€€€5'), # utf-16123 @('𐍈1','𐍈𐍈2','𐍈𐍈𐍈3','𐍈𐍈𐍈𐍈4','𐍈𐍈𐍈𐍈𐍈5')) # utf-32124 ForEach ($content in $contentSets)125 {126 $tailCount = 4127 $testPath = Join-Path -Path $TestDrive -ChildPath 'TailWithEncoding.txt'128 $content | Set-Content -Path $testPath -Encoding $EncodingName129 130 # read and verify using explicit encoding131 $expected = (Get-Content -Path $testPath -Encoding $EncodingName)[-$tailCount]132 $actual = Get-Content -Path $testPath -Tail $tailCount -Encoding $EncodingName133 $actual | Should -BeOfType string134 $actual.Length | Should -Be $tailCount135 $actual[0] | Should -BeExactly $expected136 137 # read and verify using implicit encoding138 $expected = (Get-Content -Path $testPath)[-$tailCount]139 $actual = Get-Content -Path $testPath -Tail $tailCount140 $actual | Should -BeOfType string141 $actual.Length | Should -Be $tailCount142 $actual[0] | Should -BeExactly $expected143 }144 }145 146 It "should Get-Content with a variety of -Tail and -ReadCount: <test>" -TestCases @(147 @{ test = "negative readcount"148 GetContentParams = @{Path = $testPath; Readcount = -1; Tail = 5}149 expectedLength = 4150 expectedContent = "AA","BB","CC","DD"151 }152 @{ test = "readcount=0"153 GetContentParams = @{Path = $testPath; Readcount = 0; Tail = 3}154 expectedLength = 3155 expectedContent = "BB","CC","DD"156 }157 @{ test = "readcount=1"158 GetContentParams = @{Path = $testPath; Readcount = 1; Tail = 3}159 expectedLength = 3160 expectedContent = "BB","CC","DD"161 }162 @{ test = "high readcount"163 GetContentParams = @{Path = $testPath; Readcount = 99999; Tail = 3}164 expectedLength = 3165 expectedContent = "BB","CC","DD"166 }167 @{ test = "readcount=2 tail=3"168 GetContentParams = @{Path = $testPath; Readcount = 2; Tail = 3}169 expectedLength = 2170 expectedContent = ("BB","CC"), "DD"171 }172 @{ test = "readcount=2 tail=2"173 GetContentParams = @{Path = $testPath; Readcount = 2; Tail = 2}174 expectedLength = 2175 expectedContent = "CC","DD"176 }177 ) {178 param($GetContentParams, $expectedLength, $expectedContent)179 Set-Content -Path $testPath $testContent180 $result = Get-Content @GetContentParams181 $result.Length | Should -Be $expectedLength182 $result | Should -BeExactly $expectedContent183 }184 185 It "should Get-Content with a variety of -Delimiter and -Tail: <test>" -TestCases @(186 @{ test = ", as delimiter"187 GetContentParams = @{Path = $testPath; Delimiter = ","; Tail = 2}188 expectedLength = 2189 expectedContent = "World3${nl}Hello4", "World4${nl}"190 }191 @{ test = "o as delimiter"192 GetContentParams = @{Path = $testPath; Delimiter = "o"; Tail = 3}193 expectedLength = 3194 expectedContent = "rld3${nl}Hell", '4,W', "rld4${nl}"195 }196 ) {197 param($GetContentParams, $expectedLength, $expectedContent)198 Set-Content -Path $testPath $testDelimiterContent199 $result = Get-Content @GetContentParams200 $result.Length | Should -Be $expectedLength201 $result | Should -BeExactly $expectedContent202 }203 204 It "should Get-Content with a variety of -Tail values and -AsByteStream parameter" -TestCases @(205 @{206 GetContentParams = @{Path = $testPath; Tail = 10; AsByteStream = $true}207 expectedLength = 10208 # Byte encoding of \r\nCC\r\nDD\r\n209 expectedWindowsContent = 13, 10, 67, 67, 13, 10, 68, 68, 13, 10210 # Byte encoding of \nBB\nCC\nDD\n211 expectedNotWindowsContent = 10, 66, 66, 10, 67, 67, 10, 68, 68, 10212 }213 ) {214 param($GetContentParams, $expectedLength, $expectedWindowsContent, $expectedNotWindowsContent)215 Set-Content -Path $testPath $testContent216 $result = Get-Content @GetContentParams217 $result.Length | Should -Be $expectedLength218 if ($IsWindows) {219 $result | Should -BeExactly $expectedWindowsContent220 } else {221 $result | Should -BeExactly $expectedNotWindowsContent222 }223 }224 225 #[BugId(BugDatabase.WindowsOutOfBandReleases, 905829)]226 It "should Get-Content that matches the input string"{227 Set-Content $testPath "Hello,llllWorlld","Hello2,llllWorlld2"228 $result=Get-Content $testPath -Delimiter "ll"229 $result.Length | Should -Be 9230 $expected = 'He', 'o,', '', 'Wor', "d${nl}He", 'o2,', '', 'Wor', "d2${nl}"231 for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i] | Should -BeExactly $expected[$i]}232 }233 234 Context "Alternate Data Stream support on Windows" {235 It "Should support NTFS streams using colon syntax" -Skip:(!$IsWindows) {236 Set-Content "${testPath}:Stream" -Value "Foo"237 { Test-Path "${testPath}:Stream" | Should -Throw -ErrorId "ItemExistsNotSupportedError,Microsoft.PowerShell.Commands,TestPathCommand" }238 Get-Content "${testPath}:Stream" | Should -BeExactly "Foo"239 Get-Content $testPath | Should -BeExactly $testString240 }241 242 It "Should support NTFS streams using -Stream" -Skip:(!$IsWindows) {243 Set-Content -Path $testPath -Stream hello -Value World244 Get-Content -Path $testPath | Should -BeExactly $testString245 Get-Content -Path $testPath -Stream hello | Should -BeExactly "World"246 $item = Get-Item -Path $testPath -Stream hello247 $item | Should -BeOfType System.Management.Automation.Internal.AlternateStreamData248 $item.Stream | Should -BeExactly "hello"249 Clear-Content -Path $testPath -Stream hello250 Get-Content -Path $testPath -Stream hello | Should -BeNullOrEmpty251 Remove-Item -Path $testPath -Stream hello252 { Get-Content -Path $testPath -Stream hello -ErrorAction stop} | Should -Throw -ErrorId "GetContentReaderFileNotFoundError,Microsoft.PowerShell.Commands.GetContentCommand"253 }254 }255 256 It "Should support colons in filename on Linux/Mac" -Skip:($IsWindows) {257 Set-Content "${testPath}:Stream" -Value "Hello"258 "${testPath}:Stream" | Should -Exist259 Get-Content "${testPath}:Stream" | Should -BeExactly "Hello"260 }261 262 It "-Stream is not a valid parameter for <cmdlet> on Linux/Mac" -Skip:($IsWindows) -TestCases @(263 @{cmdlet="Get-Content"},264 @{cmdlet="Set-Content"},265 @{cmdlet="Clear-Content"},266 @{cmdlet="Add-Content"},267 @{cmdlet="Get-Item"},268 @{cmdlet="Remove-Item"}269 ) {270 param($cmdlet)271 (Get-Command $cmdlet).Parameters["stream"] | Should -BeNullOrEmpty272 }273 274 It "Should return no content when an empty path is used with -Raw switch" {275 Get-ChildItem $TestDrive -Filter "*.raw" | Get-Content -Raw | Should -BeNullOrEmpty276 }277 278 It "Should return no content when -TotalCount value is 0" {279 Get-Content -Path $testPath -TotalCount 0 | Should -BeNullOrEmpty280 }281 282 It "Should return no content when -Tail value is 0" {283 Get-Content -Path $testPath -Tail 0 | Should -BeNullOrEmpty284 }285 286 It "Should wait for content when using -Tail 0 and -Wait" {287 $testValues = @(1,2,3)288 $Job = Start-Job -ScriptBlock {Get-Content -Path $using:testPath -Tail 0 -Wait}289 Start-Sleep -Seconds 3290 Add-Content -Value $testValues -Path $testPath291 Start-Sleep -Seconds 3292 Compare-Object -ReferenceObject $testValues -DifferenceObject ($Job | Receive-Job) | Should -BeNullOrEmpty293 $Job | Remove-Job -Force294 }295 296 It "Should throw TailAndHeadCannotCoexist when both -Tail and -TotalCount are used" {297 {298 Get-Content -Path $testPath -Tail 1 -TotalCount 1 -ErrorAction Stop299 } | Should -Throw -ErrorId "TailAndHeadCannotCoexist,Microsoft.PowerShell.Commands.GetContentCommand"300 }301 302 It "Should throw TailNotSupported when -Tail used with an unsupported provider" {303 Push-Location env:304 {305 Get-Content PATH -Tail 1 -ErrorAction Stop306 } | Should -Throw -ErrorId "TailNotSupported,Microsoft.PowerShell.Commands.GetContentCommand"307 Pop-Location308 }309 310 It "Should throw InvalidOperation when -Tail and -Raw are used" {311 {312 Get-Content -Path $testPath -Tail 1 -ErrorAction Stop -Raw313 } | Should -Throw -ErrorId "InvalidOperation,Microsoft.PowerShell.Commands.GetContentCommand"314 }315 316 It "Should throw ItemNotFound when path matches no files with <variation>" -TestCases @(317 @{ variation = "no additional parameters"; params = @{} },318 @{ variation = "dynamic parameter" ; params = @{ Raw = $true }}319 ) {320 param($params)321 322 { Get-Content -Path "/DoesNotExist*.txt" @params -ErrorAction Stop } | Should -Throw -ErrorId "ItemNotFound,Microsoft.PowerShell.Commands.GetContentCommand"323 }324 325 Context "Check Get-Content containing multi-byte chars" {326 BeforeAll {327 $firstLine = "Hello,World"328 $secondLine = "Hello2,World2"329 $thirdLine = "Hello3,World3"330 $fourthLine = "Hello4,World4"331 $fileContent = $firstLine,$secondLine,$thirdLine,$fourthLine332 }333 BeforeEach{334 Set-Content -Path $testPath $fileContent335 }336 It "Should return all lines when -Tail value is more than number of lines in the file"{337 $result = Get-Content -Path $testPath -ReadCount -1 -Tail 5 -Encoding UTF7338 $result.Length | Should -Be 4339 $expected = $fileContent340 Compare-Object -ReferenceObject $expected -DifferenceObject $result | Should -BeNullOrEmpty341 }342 It "Should return last three lines at one time for -ReadCount 0 and -Tail 3"{343 $result = Get-Content -Path $testPath -ReadCount 0 -Tail 3 -Encoding UTF7344 $result.Length | Should -Be 3345 $expected = $secondLine,$thirdLine,$fourthLine346 Compare-Object -ReferenceObject $expected -DifferenceObject $result | Should -BeNullOrEmpty347 }348 It "Should return last three lines reading one at a time for -ReadCount 1 and -Tail 3"{349 $result = Get-Content -Path $testPath -ReadCount 1 -Tail 3 -Encoding UTF7350 $result.Length | Should -Be 3351 $expected = $secondLine,$thirdLine,$fourthLine352 Compare-Object -ReferenceObject $expected -DifferenceObject $result | Should -BeNullOrEmpty353 }354 It "Should return last three lines at one time for -ReadCount 99999 and -Tail 3"{355 $result = Get-Content -Path $testPath -ReadCount 99999 -Tail 3 -Encoding UTF7356 $result.Length | Should -Be 3357 $expected = $secondLine,$thirdLine,$fourthLine358 Compare-Object -ReferenceObject $expected -DifferenceObject $result | Should -BeNullOrEmpty359 }360 It "Should return last three lines two lines at a time for -ReadCount 2 and -Tail 3"{361 $result = Get-Content -Path $testPath -ReadCount 2 -Tail 3 -Encoding UTF7362 $result.Length | Should -Be 2363 $expected = New-Object System.Array[] 2364 $expected[0] = ($secondLine,$thirdLine)365 $expected[1] = $fourthLine366 Compare-Object -ReferenceObject $expected -DifferenceObject $result | Should -BeNullOrEmpty367 }368 It "Should not return any content when -TotalCount 0"{369 $result = Get-Content -Path $testPath -TotalCount 0 -ReadCount 1 -Encoding UTF7370 $result.Length | Should -Be 0371 }372 It "Should return first three lines two lines at a time for -TotalCount 3 and -ReadCount 2"{373 $result = Get-Content -Path $testPath -TotalCount 3 -ReadCount 2 -Encoding UTF7374 $result.Length | Should -Be 2375 $expected = New-Object System.Array[] 2376 $expected[0] = ($firstLine,$secondLine)377 $expected[1] = $thirdLine378 Compare-Object -ReferenceObject $expected -DifferenceObject $result | Should -BeNullOrEmpty379 }380 It "Should return the same number of first lines as set in -TotalCount at one time for -ReadCount 0" {381 $result = Get-Content -Path $testPath -TotalCount 3 -ReadCount 0382 $result.Length | Should -Be 3383 $expected = $firstLine,$secondLine,$thirdLine384 Compare-Object -ReferenceObject $expected -DifferenceObject $result | Should -BeNullOrEmpty385 }386 It "A warning should be emitted if both -AsByteStream and -Encoding are used together" {387 [byte[]][char[]]"test" | Set-Content -Encoding Unicode -AsByteStream "${TESTDRIVE}\bfile.txt" -WarningVariable contentWarning *> $null388 $contentWarning.Message | Should -Match "-AsByteStream"389 }390 }391}392 393Describe "Get-Content -Raw test" -Tags "CI" {394 395 It "Reads - <testname> in full" -TestCases @(396 @{character = "a`nb`n"; testname = "LF-terminated files"; filename = "lf.txt"}397 @{character = "a`r`nb`r`n"; testname = "CRLF-terminated files"; filename = "crlf.txt"}398 @{character = "a`nb"; testname = "LF-separated files without trailing newline"; filename = "lf-nt.txt"}399 @{character = "a`r`nb"; testname = "CRLF-separated files without trailing newline"; filename = "crlf-nt.txt"}400 ) {401 param ($character, $filename)402 Set-Content -Encoding Ascii -NoNewline "$TestDrive\$filename" -Value $character403 Get-Content -Raw "$TestDrive\$filename" | Should -BeExactly $character404 }405}406 