MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Out-File DRT Unit Tests" -Tags "CI" {4 It "Should be able to write the contents into a file with -pspath" {5 $tempFile = Join-Path -Path $TestDrive -ChildPath "ExposeBug928965"6 { 1 | Out-File -PSPath $tempFile } | Should -Not -Throw7 $fileContents = Get-Content $tempFile8 $fileContents | Should -Be 19 Remove-Item $tempFile -Force10 }11 12 It "Should be able to write the contents into a file with -pspath" {13 $tempFile = Join-Path -Path $TestDrive -ChildPath "outfileAppendTest.txt"14 { 'This is first line.' | Out-File $tempFile } | Should -Not -Throw15 { 'This is second line.' | Out-File -Append $tempFile } | Should -Not -Throw16 $tempFile | Should -FileContentMatch "first"17 $tempFile | Should -FileContentMatch "second"18 Remove-Item $tempFile -Force19 }20}21 22Describe "Out-File" -Tags "CI" {23 BeforeAll {24 if ($null -ne $PSStyle) {25 $outputRendering = $PSStyle.OutputRendering26 $PSStyle.OutputRendering = 'plaintext'27 }28 29 $expectedContent = "some test text"30 $inObject = New-Object psobject -Property @{text=$expectedContent}31 $testfile = Join-Path -Path $TestDrive -ChildPath outfileTest.txt32 }33 34 AfterAll {35 if ($null -ne $PSStyle) {36 $PSStyle.OutputRendering = $outputRendering37 }38 }39 40 AfterEach {41 Remove-Item -Path $testfile -Force42 }43 44 It "Should be able to be called without error" {45 { Out-File -FilePath $testfile } | Should -Not -Throw46 }47 48 It "Should be able to accept string input via piping" {49 { $expectedContent | Out-File -FilePath $testfile } | Should -Not -Throw50 51 $actual = Get-Content $testfile52 53 $actual | Should -Be $expectedContent54 }55 56 It "Should be able to accept string input via the InputObject switch" {57 { Out-File -FilePath $testfile -InputObject $expectedContent } | Should -Not -Throw58 59 $actual = Get-Content $testfile60 61 $actual | Should -Be $expectedContent62 }63 64 It "Should be able to accept object input" {65 { $inObject | Out-File -FilePath $testfile } | Should -Not -Throw66 67 { Out-File -FilePath $testfile -InputObject $inObject } | Should -Not -Throw68 }69 70 It "Should not overwrite when the noclobber switch is used" {71 72 Out-File -FilePath $testfile -InputObject $inObject73 74 { Out-File -FilePath $testfile -InputObject $inObject -NoClobber -ErrorAction SilentlyContinue } | Should -Throw "already exists."75 { Out-File -FilePath $testfile -InputObject $inObject -NoOverWrite -ErrorAction SilentlyContinue } | Should -Throw "already exists."76 77 $actual = Get-Content $testfile78 79 $actual[0] | Should -BeNullOrEmpty80 $actual[1] | Should -Match "text"81 $actual[2] | Should -Match "----"82 $actual[3] | Should -Match "some test text"83 }84 85 It "Should Append a new line when the append switch is used" {86 { Out-File -FilePath $testfile -InputObject $inObject } | Should -Not -Throw87 { Out-File -FilePath $testfile -InputObject $inObject -Append } | Should -Not -Throw88 89 $actual = Get-Content $testfile90 91 $actual[0] | Should -BeNullOrEmpty92 $actual[1] | Should -Match "text"93 $actual[2] | Should -Match "----"94 $actual[3] | Should -Match "some test text"95 $actual[4] | Should -BeNullOrEmpty96 $actual[5] | Should -BeNullOrEmpty97 $actual[6] | Should -Match "text"98 $actual[7] | Should -Match "----"99 $actual[8] | Should -Match "some test text"100 $actual[9] | Should -BeNullOrEmpty101 $actual[10] | Should -BeNullOrEmpty102 }103 104 It "Should limit each line to the specified number of characters when the width switch is used on objects" {105 106 Out-File -FilePath $testfile -Width 10 -InputObject $inObject107 108 $actual = Get-Content $testfile109 110 $actual[0] | Should -BeNullOrEmpty111 $actual[1] | Should -BeExactly "text"112 $actual[2] | Should -BeExactly "----"113 $actual[3] | Should -BeExactly "some test`u{2026}" # ellipsis114 }115 116 It "Should allow the cmdlet to overwrite an existing read-only file" {117 # create a read-only text file118 { Out-File -FilePath $testfile -InputObject $inObject } | Should -Not -Throw119 Set-ItemProperty -Path $testfile -Name IsReadOnly -Value $true120 121 # write information to the RO file122 { Out-File -FilePath $testfile -InputObject $inObject -Append -Force } | Should -Not -Throw123 124 $actual = Get-Content $testfile125 126 $actual[0] | Should -BeNullOrEmpty127 $actual[1] | Should -Match "text"128 $actual[2] | Should -Match "----"129 $actual[3] | Should -Match "some test text"130 $actual[4] | Should -BeNullOrEmpty131 $actual[5] | Should -BeNullOrEmpty132 $actual[6] | Should -Match "text"133 $actual[7] | Should -Match "----"134 $actual[8] | Should -Match "some test text"135 $actual[9] | Should -BeNullOrEmpty136 $actual[10] | Should -BeNullOrEmpty137 138 # reset to not read only so it can be deleted139 Set-ItemProperty -Path $testfile -Name IsReadOnly -Value $false140 }141 142 It "Should be able to use the 'Path' alias for the 'FilePath' parameter" {143 { Out-File -Path $testfile } | Should -Not -Throw144 }145}146 147 