File size: 19,441 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe "Get-Content" -Tags "CI" {
    $testString = "This is a test content for a file"
    $nl         = [Environment]::NewLine
    $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
    $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" {
        { Get-Content . -ErrorAction Stop } |
            Should -Throw -ErrorId "GetContainerContentException,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 -BeExactly $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 -BeExactly $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 -BeExactly $firstline
        $returnArray[1] | Should -BeExactly $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 -BeExactly $firstline
        $returnArray[1] | Should -BeExactly $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 -BeExactly $firstline
        $returnArray[1] | Should -BeExactly $secondline
    }

    It "Should return the last line of a file using the Tail switch" {
        Get-Content -Path $testPath -Tail 1 | Should -BeExactly $testString
    }

    It "Should return the last lines of a file using the Last alias" {
        Get-Content -Path $testPath2 -Last 1 | Should -BeExactly $fifthline
    }

    It 'Verifies -TotalCount reports a ParameterArgumentValidationError error for negative values' {
        {Get-Content -Path $testPath2 -TotalCount -2} | Should -Throw -ErrorId 'ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetContentCommand'
    }

    It 'Verifies -Tail reports a ParameterArgumentValidationError error for negative values' {
        {Get-Content -Path $testPath2 -Tail -2} | Should -Throw -ErrorId 'ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetContentCommand'
    }

    It "Should be able to get content within a different drive" {
        Push-Location env:
        $expectedoutput = [Environment]::GetEnvironmentVariable("PATH");
        { Get-Content PATH } | Should -Not -Throw
        Get-Content PATH     | Should -BeExactly $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 -ErrorAction 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} | Should -Throw -ErrorId '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} | Should -Throw -ErrorId 'TailAndHeadCannotCoexist,Microsoft.PowerShell.Commands.GetContentCommand'
    }

    It 'Verifies -Tail with content that uses an explicit/implicit encoding' -TestCases @(
        @{EncodingName = 'String'},
        @{EncodingName = 'OEM'},
        @{EncodingName = 'Unicode'},
        @{EncodingName = 'BigEndianUnicode'},
        @{EncodingName = 'BigEndianUTF32'},
        @{EncodingName = 'UTF8'},
        @{EncodingName = 'UTF8BOM'},
        @{EncodingName = 'UTF8NoBOM'},
        @{EncodingName = 'UTF7'},
        @{EncodingName = 'UTF32'},
        @{EncodingName = 'Ascii'},
        @{EncodingName = 'ANSI'}
        ){
        param($EncodingName)

        $contentSets =
            @(@('a1','aa2','aaa3','aaaa4','aaaaa5'), # utf-8
              @('€1','€€2','€€€3','€€€€4','€€€€€5'), # utf-16
              @('𐍈1','𐍈𐍈2','𐍈𐍈𐍈3','𐍈𐍈𐍈𐍈4','𐍈𐍈𐍈𐍈𐍈5')) # utf-32
        ForEach ($content in $contentSets)
        {
            $tailCount = 4
            $testPath = Join-Path -Path $TestDrive -ChildPath 'TailWithEncoding.txt'
            $content | Set-Content -Path $testPath -Encoding $EncodingName

            # read and verify using explicit encoding
            $expected = (Get-Content -Path $testPath -Encoding $EncodingName)[-$tailCount]
            $actual = Get-Content -Path $testPath -Tail $tailCount -Encoding $EncodingName
            $actual | Should -BeOfType string
            $actual.Length | Should -Be $tailCount
            $actual[0] | Should -BeExactly $expected

            # read and verify using implicit encoding
            $expected = (Get-Content -Path $testPath)[-$tailCount]
            $actual = Get-Content -Path $testPath -Tail $tailCount
            $actual | Should -BeOfType string
            $actual.Length | Should -Be $tailCount
            $actual[0] | Should -BeExactly $expected
        }
    }

    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"
        }
        @{ 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 | Should -BeExactly $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"
        $result=Get-Content $testPath -Delimiter "ll"
        $result.Length    | Should -Be 9
        $expected = 'He', 'o,', '', 'Wor', "d${nl}He", 'o2,', '', 'Wor', "d2${nl}"
        for ($i = 0; $i -lt $result.Length ; $i++) { $result[$i]    | Should -BeExactly $expected[$i]}
    }

    Context "Alternate Data Stream support on Windows" {
        It "Should support NTFS streams using colon syntax" -Skip:(!$IsWindows) {
            Set-Content "${testPath}:Stream" -Value "Foo"
            { Test-Path "${testPath}:Stream" | Should -Throw -ErrorId "ItemExistsNotSupportedError,Microsoft.PowerShell.Commands,TestPathCommand" }
            Get-Content "${testPath}:Stream" | Should -BeExactly "Foo"
            Get-Content $testPath | Should -BeExactly $testString
        }

        It "Should support NTFS streams using -Stream" -Skip:(!$IsWindows) {
            Set-Content -Path $testPath -Stream hello -Value World
            Get-Content -Path $testPath | Should -BeExactly $testString
            Get-Content -Path $testPath -Stream hello | Should -BeExactly "World"
            $item = Get-Item -Path $testPath -Stream hello
            $item | Should -BeOfType System.Management.Automation.Internal.AlternateStreamData
            $item.Stream | Should -BeExactly "hello"
            Clear-Content -Path $testPath -Stream hello
            Get-Content -Path $testPath -Stream hello | Should -BeNullOrEmpty
            Remove-Item -Path $testPath -Stream hello
            { Get-Content -Path $testPath -Stream hello -ErrorAction stop} | Should -Throw -ErrorId "GetContentReaderFileNotFoundError,Microsoft.PowerShell.Commands.GetContentCommand"
        }
    }

    It "Should support colons in filename on Linux/Mac" -Skip:($IsWindows) {
        Set-Content "${testPath}:Stream" -Value "Hello"
        "${testPath}:Stream" | Should -Exist
        Get-Content "${testPath}:Stream" | Should -BeExactly "Hello"
    }

    It "-Stream is not a valid parameter for <cmdlet> on Linux/Mac" -Skip:($IsWindows) -TestCases @(
        @{cmdlet="Get-Content"},
        @{cmdlet="Set-Content"},
        @{cmdlet="Clear-Content"},
        @{cmdlet="Add-Content"},
        @{cmdlet="Get-Item"},
        @{cmdlet="Remove-Item"}
    ) {
        param($cmdlet)
        (Get-Command $cmdlet).Parameters["stream"] | Should -BeNullOrEmpty
    }

    It "Should return no content when an empty path is used with -Raw switch" {
        Get-ChildItem $TestDrive -Filter "*.raw" | Get-Content -Raw | Should -BeNullOrEmpty
    }

    It "Should return no content when -TotalCount value is 0" {
        Get-Content -Path $testPath -TotalCount 0 | Should -BeNullOrEmpty
    }

    It "Should return no content when -Tail value is 0" {
        Get-Content -Path $testPath -Tail 0 | Should -BeNullOrEmpty
    }

    It "Should wait for content when using -Tail 0 and -Wait" {
        $testValues = @(1,2,3)
        $Job = Start-Job -ScriptBlock {Get-Content -Path $using:testPath -Tail 0 -Wait}
        Start-Sleep -Seconds 3
        Add-Content -Value $testValues -Path $testPath
        Start-Sleep -Seconds 3
        Compare-Object -ReferenceObject $testValues -DifferenceObject ($Job | Receive-Job) | Should -BeNullOrEmpty
        $Job | Remove-Job -Force
    }

    It "Should throw TailAndHeadCannotCoexist when both -Tail and -TotalCount are used" {
        {
        Get-Content -Path $testPath -Tail 1 -TotalCount 1 -ErrorAction Stop
        } | Should -Throw -ErrorId "TailAndHeadCannotCoexist,Microsoft.PowerShell.Commands.GetContentCommand"
    }

    It "Should throw TailNotSupported when -Tail used with an unsupported provider" {
        Push-Location env:
        {
        Get-Content PATH -Tail 1 -ErrorAction Stop
        } | Should -Throw -ErrorId "TailNotSupported,Microsoft.PowerShell.Commands.GetContentCommand"
        Pop-Location
    }

    It "Should throw InvalidOperation when -Tail and -Raw are used" {
        {
        Get-Content -Path $testPath -Tail 1 -ErrorAction Stop -Raw
        } | Should -Throw -ErrorId "InvalidOperation,Microsoft.PowerShell.Commands.GetContentCommand"
    }

    It "Should throw ItemNotFound when path matches no files with <variation>" -TestCases @(
        @{ variation = "no additional parameters"; params = @{} },
        @{ variation = "dynamic parameter"       ; params = @{ Raw = $true }}
    ) {
        param($params)

        { Get-Content -Path "/DoesNotExist*.txt" @params -ErrorAction Stop } | Should -Throw -ErrorId "ItemNotFound,Microsoft.PowerShell.Commands.GetContentCommand"
    }

    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
        }
        BeforeEach{
            Set-Content -Path $testPath $fileContent
        }
        It "Should return all lines when -Tail value is more than number of lines in the file"{
            $result = Get-Content -Path $testPath -ReadCount -1 -Tail 5 -Encoding UTF7
            $result.Length | Should -Be 4
            $expected = $fileContent
            Compare-Object -ReferenceObject $expected -DifferenceObject $result | Should -BeNullOrEmpty
        }
        It "Should return last three lines at one time for -ReadCount 0 and -Tail 3"{
            $result = Get-Content -Path $testPath -ReadCount 0 -Tail 3 -Encoding UTF7
            $result.Length | Should -Be 3
            $expected = $secondLine,$thirdLine,$fourthLine
            Compare-Object -ReferenceObject $expected -DifferenceObject $result | Should -BeNullOrEmpty
        }
        It "Should return last three lines reading one at a time for -ReadCount 1 and -Tail 3"{
            $result = Get-Content -Path $testPath -ReadCount 1 -Tail 3 -Encoding UTF7
            $result.Length | Should -Be 3
            $expected = $secondLine,$thirdLine,$fourthLine
            Compare-Object -ReferenceObject $expected -DifferenceObject $result | Should -BeNullOrEmpty
        }
        It "Should return last three lines at one time for -ReadCount 99999 and -Tail 3"{
            $result = Get-Content -Path $testPath -ReadCount 99999 -Tail 3 -Encoding UTF7
            $result.Length | Should -Be 3
            $expected = $secondLine,$thirdLine,$fourthLine
            Compare-Object -ReferenceObject $expected -DifferenceObject $result | Should -BeNullOrEmpty
        }
        It "Should return last three lines two lines at a time for -ReadCount 2 and -Tail 3"{
            $result = Get-Content -Path $testPath -ReadCount 2 -Tail 3 -Encoding UTF7
            $result.Length | Should -Be 2
            $expected = New-Object System.Array[] 2
            $expected[0] = ($secondLine,$thirdLine)
            $expected[1] = $fourthLine
            Compare-Object -ReferenceObject $expected -DifferenceObject $result | Should -BeNullOrEmpty
        }
        It "Should not return any content when -TotalCount 0"{
            $result = Get-Content -Path $testPath -TotalCount 0 -ReadCount 1 -Encoding UTF7
            $result.Length | Should -Be 0
        }
        It "Should return first three lines two lines at a time for -TotalCount 3 and -ReadCount 2"{
            $result = Get-Content -Path $testPath -TotalCount 3 -ReadCount 2 -Encoding UTF7
            $result.Length | Should -Be 2
            $expected = New-Object System.Array[] 2
            $expected[0] = ($firstLine,$secondLine)
            $expected[1] = $thirdLine
            Compare-Object -ReferenceObject $expected -DifferenceObject $result | Should -BeNullOrEmpty
        }
        It "Should return the same number of first lines as set in -TotalCount at one time for -ReadCount 0" {
            $result = Get-Content -Path $testPath -TotalCount 3 -ReadCount 0
            $result.Length | Should -Be 3
            $expected = $firstLine,$secondLine,$thirdLine
            Compare-Object -ReferenceObject $expected -DifferenceObject $result | Should -BeNullOrEmpty
        }
        It "A warning should be emitted if both -AsByteStream and -Encoding are used together" {
            [byte[]][char[]]"test" | Set-Content -Encoding Unicode -AsByteStream "${TESTDRIVE}\bfile.txt" -WarningVariable contentWarning *> $null
            $contentWarning.Message | Should -Match "-AsByteStream"
        }
    }
}

Describe "Get-Content -Raw test" -Tags "CI" {

    It "Reads - <testname> in full" -TestCases @(
      @{character = "a`nb`n"; testname = "LF-terminated files"; filename = "lf.txt"}
      @{character = "a`r`nb`r`n"; testname = "CRLF-terminated files"; filename = "crlf.txt"}
      @{character = "a`nb"; testname = "LF-separated files without trailing newline"; filename = "lf-nt.txt"}
      @{character = "a`r`nb"; testname = "CRLF-separated files without trailing newline"; filename = "crlf-nt.txt"}
    ) {
        param ($character, $filename)
        Set-Content -Encoding Ascii -NoNewline "$TestDrive\$filename" -Value $character
        Get-Content -Raw "$TestDrive\$filename" | Should -BeExactly $character
    }
}