MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "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" -PassThru14 $result | Should -BeExactly "ExpectedContent"15 }16 17 It "should return expected string from TestDrive:\$file1" {18 $result = Get-Content -Path TestDrive:\$file119 $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/89123 $result = Add-Content -Path TestDrive:\dynamicfile.txt -Value "ExpectedContent" -PassThru24 $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/89128 $result = Get-Content -Path TestDrive:\dynamicfile.txt29 $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:\$file134 { Add-Content -Path TestDrive:\$file1 -Value $null -ErrorAction Stop } | Should -Not -Throw35 Get-Content -Path TestDrive:\$file1 | Should -BeExactly $AsItWas36 }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 return55 }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 $streamContent66 }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 $streamContent71 }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/89181 "hello","world" | Add-Content -Path TestDrive:\dynamicfile2.txt82 $result = Get-Content -Path TestDrive:\dynamicfile2.txt83 $result.length | Should -Be 284 $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 $logpath100 $content | Should -HaveCount 2101 $content[0] | Should -BeExactly "hello"102 $content[1] | Should -BeExactly "world"103 }104 }105}106 