"Fossies" - the Fresh Open Source Software Archive 
Member "PowerShell-7.2.6/test/powershell/Modules/Microsoft.PowerShell.Management/Add-Content.Tests.ps1" (11 Aug 2022, 5010 Bytes) of package /linux/misc/PowerShell-7.2.6.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Microsoft PowerShell source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 # Copyright (c) Microsoft Corporation.
2 # Licensed under the MIT License.
3 Describe "Add-Content cmdlet tests" -Tags "CI" {
4
5 BeforeAll {
6 $file1 = "file1.txt"
7 Setup -File "$file1"
8 $streamContent = "ShouldWork"
9 }
10
11 Context "Add-Content should actually add content" {
12 It "should Add-Content to TestDrive:\$file1" {
13 $result = Add-Content -Path TestDrive:\$file1 -Value "ExpectedContent" -PassThru
14 $result | Should -BeExactly "ExpectedContent"
15 }
16
17 It "should return expected string from TestDrive:\$file1" {
18 $result = Get-Content -Path TestDrive:\$file1
19 $result | Should -BeExactly "ExpectedContent"
20 }
21
22 It "should Add-Content to TestDrive:\dynamicfile.txt with dynamic parameters" -Pending:($IsLinux -Or $IsMacOS) {#https://github.com/PowerShell/PowerShell/issues/891
23 $result = Add-Content -Path TestDrive:\dynamicfile.txt -Value "ExpectedContent" -PassThru
24 $result | Should -BeExactly "ExpectedContent"
25 }
26
27 It "should return expected string from TestDrive:\dynamicfile.txt" -Pending:($IsLinux -Or $IsMacOS) {#https://github.com/PowerShell/PowerShell/issues/891
28 $result = Get-Content -Path TestDrive:\dynamicfile.txt
29 $result | Should -BeExactly "ExpectedContent"
30 }
31
32 It "should Add-Content to TestDrive:\$file1 even when -Value is `$null" {
33 $AsItWas = Get-Content -Path TestDrive:\$file1
34 { Add-Content -Path TestDrive:\$file1 -Value $null -ErrorAction Stop } | Should -Not -Throw
35 Get-Content -Path TestDrive:\$file1 | Should -BeExactly $AsItWas
36 }
37
38 It "should throw 'ParameterArgumentValidationErrorNullNotAllowed' when -Path is `$null" {
39 { Add-Content -Path $null -Value "ShouldNotWorkBecausePathIsNull" -ErrorAction Stop } | Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentCommand"
40 }
41
42 #[BugId(BugDatabase.WindowsOutOfBandReleases, 903880)]
43 It "should throw `"Cannot bind argument to parameter 'Path'`" when -Path is `$()" {
44 { Add-Content -Path $() -Value "ShouldNotWorkBecausePathIsInvalid" -ErrorAction Stop } | Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentCommand"
45 }
46
47 It "Should throw an error on a directory" {
48 { Add-Content -Path . -Value "WriteContainerContentException" -ErrorAction Stop } | Should -Throw -ErrorId "WriteContainerContentException,Microsoft.PowerShell.Commands.AddContentCommand"
49 }
50
51 Context "Add-Content should work with alternate data streams on Windows" {
52 BeforeAll {
53 if (!$isWindows) {
54 return
55 }
56 $ADSTestDir = "addcontentadstest"
57 $ADSTestFile = "addcontentads.txt"
58 $streamContent = "This is a test stream."
59 Setup -Directory "$ADSTestDir"
60 Setup -File "$ADSTestFile"
61 }
62
63 It "Should add an alternate data stream on a directory" -Skip:(!$IsWindows) {
64 Add-Content -Path TestDrive:\$ADSTestDir -Stream Add-Content-Test-Stream -Value $streamContent -ErrorAction Stop
65 Get-Content -Path TestDrive:\$ADSTestDir -Stream Add-Content-Test-Stream | Should -BeExactly $streamContent
66 }
67
68 It "Should add an alternate data stream on a file" -Skip:(!$IsWindows) {
69 Add-Content -Path TestDrive:\$ADSTestFile -Stream Add-Content-Test-Stream -Value $streamContent -ErrorAction Stop
70 Get-Content -Path TestDrive:\$ADSTestFile -Stream Add-Content-Test-Stream | Should -BeExactly $streamContent
71 }
72 }
73
74 #[BugId(BugDatabase.WindowsOutOfBandReleases, 906022)]
75 It "should throw 'NotSupportedException' when you add-content to an unsupported provider" -Skip:($IsLinux -Or $IsMacOS) {
76 { Add-Content -Path HKLM:\\software\\microsoft -Value "ShouldNotWorkBecausePathIsUnsupported" -ErrorAction Stop } | Should -Throw -ErrorId "NotSupported,Microsoft.PowerShell.Commands.AddContentCommand"
77 }
78
79 #[BugId(BugDatabase.WindowsOutOfBandReleases, 9058182)]
80 It "should be able to pass multiple [string]`$objects to Add-Content through the pipeline to output a dynamic Path file" -Pending:($IsLinux -Or $IsMacOS) {#https://github.com/PowerShell/PowerShell/issues/891
81 "hello","world" | Add-Content -Path TestDrive:\dynamicfile2.txt
82 $result = Get-Content -Path TestDrive:\dynamicfile2.txt
83 $result.length | Should -Be 2
84 $result[0] | Should -BeExactly "hello"
85 $result[1] | Should -BeExactly "world"
86 }
87
88 It "Should not block reads while writing" {
89 $logpath = Join-Path $testdrive "test.log"
90
91 Set-Content $logpath -Value "hello"
92
93 $f = [System.IO.FileStream]::new($logpath, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::ReadWrite)
94
95 Add-Content $logpath -Value "world"
96
97 $f.Close()
98
99 $content = Get-Content $logpath
100 $content | Should -HaveCount 2
101 $content[0] | Should -BeExactly "hello"
102 $content[1] | Should -BeExactly "world"
103 }
104 }
105 }