2020-03-24 11:08:37 -07:00
# Copyright (c) Microsoft Corporation.
2018-02-13 09:23:53 -08:00
# Licensed under the MIT License.
2016-07-25 14:01:41 -07:00
Describe "Add-Content cmdlet tests" -Tags "CI" {
2018-10-23 16:27:53 -07:00
BeforeAll {
$file1 = "file1.txt"
Setup -File " $file1 "
2020-12-07 19:31:55 -06:00
$streamContent = "ShouldWork"
2018-10-23 16:27:53 -07:00
}
2016-04-20 21:27:17 -07:00
2016-05-04 17:09:01 -07:00
Context "Add-Content should actually add content" {
2018-04-11 06:23:30 +03:00
It "should Add-Content to TestDrive:\ $file1 " {
$result = Add-Content -Path TestDrive : \ $file1 -Value "ExpectedContent" -PassThru
2018-03-09 15:13:15 -08:00
$result | Should -BeExactly "ExpectedContent"
2016-05-04 17:09:01 -07:00
}
2018-04-11 06:23:30 +03:00
It "should return expected string from TestDrive:\ $file1 " {
$result = Get-Content -Path TestDrive : \ $file1
2018-03-09 15:13:15 -08:00
$result | Should -BeExactly "ExpectedContent"
2016-05-04 17:09:01 -07:00
}
2018-04-11 06:23:30 +03:00
It "should Add-Content to TestDrive:\dynamicfile.txt with dynamic parameters" -Pending: ( $IsLinux -Or $IsMacOS ) { #https://github.com/PowerShell/PowerShell/issues/891
$result = Add-Content -Path TestDrive : \ dynamicfile . txt -Value "ExpectedContent" -PassThru
$result | Should -BeExactly "ExpectedContent"
2016-05-04 17:09:01 -07:00
}
2018-04-11 06:23:30 +03:00
It "should return expected string from TestDrive:\dynamicfile.txt" -Pending: ( $IsLinux -Or $IsMacOS ) { #https://github.com/PowerShell/PowerShell/issues/891
$result = Get-Content -Path TestDrive : \ dynamicfile . txt
$result | Should -BeExactly "ExpectedContent"
}
It "should Add-Content to TestDrive:\ $file1 even when -Value is `$ null" {
$AsItWas = Get-Content -Path TestDrive : \ $file1
{ Add-Content -Path TestDrive : \ $file1 -Value $null -ErrorAction Stop } | Should -Not -Throw
Get-Content -Path TestDrive : \ $file1 | Should -BeExactly $AsItWas
}
2016-05-04 17:09:01 -07:00
It "should throw 'ParameterArgumentValidationErrorNullNotAllowed' when -Path is `$ null" {
2018-03-09 15:13:15 -08:00
{ Add-Content -Path $null -Value "ShouldNotWorkBecausePathIsNull" -ErrorAction Stop } | Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentCommand"
2016-05-04 17:09:01 -07:00
}
2018-04-11 06:23:30 +03:00
2016-05-04 17:09:01 -07:00
#[BugId(BugDatabase.WindowsOutOfBandReleases, 903880)]
It "should throw `" Cannot bind argument to parameter 'Path' `" when -Path is `$ ()" {
2018-03-09 15:13:15 -08:00
{ Add-Content -Path $ () -Value "ShouldNotWorkBecausePathIsInvalid" -ErrorAction Stop } | Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddContentCommand"
2016-05-04 17:09:01 -07:00
}
2018-04-11 06:23:30 +03:00
2018-10-23 16:27:53 -07:00
It "Should throw an error on a directory" {
2018-09-28 21:06:13 +05:30
{ Add-Content -Path . -Value "WriteContainerContentException" -ErrorAction Stop } | Should -Throw -ErrorId "WriteContainerContentException,Microsoft.PowerShell.Commands.AddContentCommand"
}
2020-12-07 19:31:55 -06:00
Context "Add-Content should work with alternate data streams on Windows" {
BeforeAll {
if (! $isWindows ) {
return
}
$ADSTestDir = "addcontentadstest"
$ADSTestFile = "addcontentads.txt"
$streamContent = "This is a test stream."
Setup -Directory " $ADSTestDir "
Setup -File " $ADSTestFile "
}
It "Should add an alternate data stream on a directory" -Skip: (! $IsWindows ) {
Add-Content -Path TestDrive : \ $ADSTestDir -Stream Add-Content -Test-Stream -Value $streamContent -ErrorAction Stop
Get-Content -Path TestDrive : \ $ADSTestDir -Stream Add-Content -Test-Stream | Should -BeExactly $streamContent
}
It "Should add an alternate data stream on a file" -Skip: (! $IsWindows ) {
Add-Content -Path TestDrive : \ $ADSTestFile -Stream Add-Content -Test-Stream -Value $streamContent -ErrorAction Stop
Get-Content -Path TestDrive : \ $ADSTestFile -Stream Add-Content -Test-Stream | Should -BeExactly $streamContent
}
}
2016-05-04 17:09:01 -07:00
#[BugId(BugDatabase.WindowsOutOfBandReleases, 906022)]
2017-09-07 10:34:40 -07:00
It "should throw 'NotSupportedException' when you add-content to an unsupported provider" -Skip: ( $IsLinux -Or $IsMacOS ) {
2018-03-09 15:13:15 -08:00
{ Add-Content -Path HKLM : \\ software \\ microsoft -Value "ShouldNotWorkBecausePathIsUnsupported" -ErrorAction Stop } | Should -Throw -ErrorId "NotSupported,Microsoft.PowerShell.Commands.AddContentCommand"
2016-05-04 17:09:01 -07:00
}
2018-04-11 06:23:30 +03:00
2016-05-04 17:09:01 -07:00
#[BugId(BugDatabase.WindowsOutOfBandReleases, 9058182)]
2017-09-07 10:34:40 -07:00
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
2018-04-11 06:23:30 +03:00
"hello" , "world" | Add-Content -Path TestDrive : \ dynamicfile2 . txt
$result = Get-Content -Path TestDrive : \ dynamicfile2 . txt
2018-03-09 15:13:15 -08:00
$result . length | Should -Be 2
$result [ 0 ] | Should -BeExactly "hello"
$result [ 1 ] | Should -BeExactly "world"
2016-05-04 17:09:01 -07:00
}
2018-10-23 16:27:53 -07:00
It "Should not block reads while writing" {
$logpath = Join-Path $testdrive "test.log"
Set-Content $logpath -Value "hello"
$f = [ System.IO.FileStream ]:: new ( $logpath , [ System.IO.FileMode ]:: Open , [ System.IO.FileAccess ]:: Read , [ System.IO.FileShare ]:: ReadWrite )
Add-Content $logpath -Value "world"
$f . Close ()
$content = Get-Content $logpath
$content | Should -HaveCount 2
$content [ 0 ] | Should -BeExactly "hello"
$content [ 1 ] | Should -BeExactly "world"
}
2016-05-04 17:09:01 -07:00
}
2017-01-16 13:31:14 -08:00
}