2016-03-04 23:48:30 +01:00
|
|
|
Describe "Out-File" {
|
2015-09-15 00:07:31 +02:00
|
|
|
$expectedContent = "some test text"
|
2015-11-05 00:13:21 +01:00
|
|
|
$inObject = New-Object psobject -Property @{text=$expectedContent}
|
2016-02-05 17:53:44 +01:00
|
|
|
$testfile = Join-Path -Path $TestDrive -ChildPath outfileTest.txt
|
2015-08-19 23:13:15 +02:00
|
|
|
|
|
|
|
AfterEach {
|
2016-03-04 23:52:27 +01:00
|
|
|
Remove-Item -Path $testfile -Force
|
2015-08-19 23:13:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to be called without error" {
|
2016-03-04 23:52:27 +01:00
|
|
|
{ Out-File -FilePath $testfile } | Should Not Throw
|
2015-08-19 23:13:15 +02:00
|
|
|
}
|
|
|
|
|
2015-09-04 01:15:50 +02:00
|
|
|
It "Should be able to accept string input via piping" {
|
2016-03-04 23:52:27 +01:00
|
|
|
{ $expectedContent | Out-File -FilePath $testfile } | Should Not Throw
|
2015-08-19 23:13:15 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$actual = Get-Content $testfile
|
2015-09-04 01:15:50 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$actual | Should Be $expectedContent
|
2015-09-04 01:15:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to accept string input via the InputObject swictch" {
|
2016-03-04 23:52:27 +01:00
|
|
|
{ Out-File -FilePath $testfile -InputObject $expectedContent } | Should Not Throw
|
2015-09-04 01:15:50 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$actual = Get-Content $testfile
|
2015-09-04 01:15:50 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$actual | Should Be $expectedContent
|
2015-08-19 23:13:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should be able to accept object input" {
|
2016-03-04 23:52:27 +01:00
|
|
|
{ $inObject | Out-File -FilePath $testfile } | Should Not Throw
|
2015-08-19 23:13:15 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
{ Out-File -FilePath $testfile -InputObject $inObject } | Should Not Throw
|
2015-08-19 23:13:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should not overwrite when the noclobber switch is used" {
|
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Out-File -FilePath $testfile -InputObject $inObject
|
2015-08-19 23:13:15 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
{ Out-File -FilePath $testfile -InputObject $inObject -NoClobber -ErrorAction SilentlyContinue } | Should Throw "already exists."
|
|
|
|
{ Out-File -FilePath $testfile -InputObject $inObject -NoOverWrite -ErrorAction SilentlyContinue } | Should Throw "already exists."
|
2015-08-19 23:13:15 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$actual = Get-Content $testfile
|
2015-08-19 23:13:15 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$actual[0] | Should Be ""
|
|
|
|
$actual[1] | Should Match "text"
|
|
|
|
$actual[2] | Should Match "----"
|
|
|
|
$actual[3] | Should Match "some test text"
|
2015-08-19 23:13:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
It "Should Append a new line when the append switch is used" {
|
2016-03-04 23:52:27 +01:00
|
|
|
{ Out-File -FilePath $testfile -InputObject $inObject } | Should Not Throw
|
|
|
|
{ Out-File -FilePath $testfile -InputObject $inObject -Append } | Should Not Throw
|
|
|
|
|
|
|
|
$actual = Get-Content $testfile
|
|
|
|
|
|
|
|
$actual[0] | Should Be ""
|
|
|
|
$actual[1] | Should Match "text"
|
|
|
|
$actual[2] | Should Match "----"
|
|
|
|
$actual[3] | Should Match "some test text"
|
|
|
|
$actual[4] | Should Be ""
|
|
|
|
$actual[5] | Should Be ""
|
|
|
|
$actual[6] | Should Be ""
|
|
|
|
$actual[7] | Should Match "text"
|
|
|
|
$actual[8] | Should Match "----"
|
|
|
|
$actual[9] | Should Match "some test text"
|
|
|
|
$actual[10] | Should Be ""
|
|
|
|
$actual[11] | Should Be ""
|
2015-08-19 23:13:15 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
It "Should limit each line to the specified number of characters when the width switch is used on objects" {
|
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
Out-File -FilePath $testfile -Width 10 -InputObject $inObject
|
2015-08-19 23:13:15 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$actual = Get-Content $testfile
|
2015-08-19 23:13:15 +02:00
|
|
|
|
2016-03-04 23:52:27 +01:00
|
|
|
$actual[0] | Should Be ""
|
|
|
|
$actual[1] | Should Be "text "
|
|
|
|
$actual[2] | Should Be "---- "
|
|
|
|
$actual[3] | Should Be "some te..."
|
2015-08-19 23:13:15 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
It "Should allow the cmdlet to overwrite an existing read-only file" {
|
2016-03-04 23:52:27 +01:00
|
|
|
# create a read-only text file
|
|
|
|
{ Out-File -FilePath $testfile -InputObject $inObject } | Should Not Throw
|
|
|
|
Set-ItemProperty -Path $testfile -Name IsReadOnly -Value $true
|
|
|
|
|
|
|
|
# write information to the RO file
|
|
|
|
{ Out-File -FilePath $testfile -InputObject $inObject -Append -Force } | Should Not Throw
|
|
|
|
|
|
|
|
$actual = Get-Content $testfile
|
|
|
|
|
|
|
|
$actual[0] | Should Be ""
|
|
|
|
$actual[1] | Should Match "text"
|
|
|
|
$actual[2] | Should Match "----"
|
|
|
|
$actual[3] | Should Match "some test text"
|
|
|
|
$actual[4] | Should Be ""
|
|
|
|
$actual[5] | Should Be ""
|
|
|
|
$actual[6] | Should Be ""
|
|
|
|
$actual[7] | Should Match "text"
|
|
|
|
$actual[8] | Should Match "----"
|
|
|
|
$actual[9] | Should Match "some test text"
|
|
|
|
$actual[10] | Should Be ""
|
|
|
|
$actual[11] | Should Be ""
|
|
|
|
|
|
|
|
# reset to not read only so it can be deleted
|
|
|
|
Set-ItemProperty -Path $testfile -Name IsReadOnly -Value $false
|
2015-08-19 23:13:15 +02:00
|
|
|
}
|
2015-08-24 19:06:48 +02:00
|
|
|
}
|