Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes308downloads
Write-Stream.Tests.ps1126 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Stream writer tests" -Tags "CI" {4    $targetfile = Join-Path -Path $TestDrive -ChildPath "writeoutput.txt"5 6    # A custom function is defined here do handle the debug stream dealing with the confirm prompt7    # that would normally8    function Write-Messages9    {10        [CmdletBinding()]11 12        param()13 14        Write-Verbose "Verbose message"15 16        Write-Debug "Debug message"17 18    }19 20    Context "Redirect Stream Tests" {21        # These tests validate that a stream is actually being written to by redirecting the output of that stream22 23        AfterEach { Remove-Item $targetfile }24        It "Should write warnings to the warning stream" {25            Write-Warning "Test Warning" 3>&1 > $targetfile26 27            Get-Content $targetfile | Should -BeExactly "Test Warning"28        }29 30        It "Should write error messages to the error stream" {31            Write-Error "Testing Error" 2>&1 > $targetfile32            # The contents of the error stream should contain the expected text33            $targetfile | Should -FileContentMatch "Testing Error"34        }35 36        It "Should write debug messages to the debug stream" {37            Write-Messages -Debug -ErrorAction SilentlyContinue 5>&1 > $targetfile38            # The contents of the debug stream should contain the expected text39            $targetfile | Should -FileContentMatch "Debug Message"40        }41 42        It "Should write messages to the verbose stream" {43            Write-Messages -Verbose 4>&1 > $targetfile44            # The contents of the debug stream should contain the expected text45            $targetfile | Should -FileContentMatch "Verbose Message"46        }47    }48 49    Context "Error automatic variable" {50        It "Should write error messages to the `$error automatic variable" {51            Write-Error "Test Error Message" -ErrorAction SilentlyContinue52 53            $error[0] | Should -Match "Test Error Message"54        }55    }56 57    Context "Write-Information cmdlet" {58        BeforeAll {59            $ps = [powershell]::Create()60 61            $testInfoData = @(62                @{ Name = 'defaults'; Command = "Write-Information TestMessage";              returnCount = 1; returnValue = 'TestMessage' }63                @{ Name = '-Object';  Command = "Write-Information -MessageData TestMessage"; returnCount = 1; returnValue = 'TestMessage' }64                @{ Name = '-Message'; Command = "Write-Information -Message TestMessage";     returnCount = 1; returnValue = 'TestMessage' }65                @{ Name = '-Msg';     Command = "Write-Information -Msg TestMessage";         returnCount = 1; returnValue = 'TestMessage' }66                @{ Name = '-Tag';     Command = "Write-Information TestMessage -Tag Test";    returnCount = 1; returnValue = 'TestMessage' }67            )68        }69 70        BeforeEach {71            $ps.Commands.Clear()72            $ps.Streams.ClearStreams()73        }74 75        AfterAll {76            $ps.Dispose()77        }78 79       It "Write-Information outputs an information object" {80            # redirect the streams is sufficient81            $result = Write-Information "Test Message" *>&182            $result.NativeThreadId | Should -Not -Be 083            $result.ProcessId | Should -Be $PID84            $result | Should -BeOfType System.Management.Automation.InformationRecord85 86            # Use Match instead of Be so we can avoid dealing with a potential domain name87            $result.Computer | Should -Match "^($([environment]::MachineName)){1}(\.[a-zA-Z0-9]+)*$|^localhost$"88            if ($IsWindows)89            {90                $result.User | Should -Match ".*${env:USERNAME}"91            }92            else93            {94                $result.User | Should -Be $(whoami)95            }96 97            "$result" | Should -BeExactly "Test Message"98       }99 100       It "Write-Information accept objects from pipe" {101            $ps.AddScript("'teststring',12345 | Write-Information -InformationAction Continue").Invoke()102            $result = $ps.Streams.Information103            $result.Count | Should -Be 2104            $result[0].MessageData | Should -BeExactly "teststring"105            $result[1].MessageData | Should -BeExactly "12345"106       }107 108       It "Write-Information works with <Name>" -TestCases:$testInfoData {109            param($Command, $returnCount, $returnValue)110            $ps.AddScript($Command).Invoke()111 112            $result = $ps.Streams.Information113 114            $result.Count | Should -Be $returnCount115            (Compare-Object $result $returnValue -SyncWindow 0).length | Should -Be 0116        }117 118        It "Write-Information accepts `$null" {119            $streamPath = Join-Path $testdrive information.txt120            $null | Write-Information -Tags myTag -ErrorAction Stop -InformationAction SilentlyContinue -InformationVariable i121            $i.Tags | Should -BeExactly "myTag"122            $i.MessageData | Should -Be $null123        }124    }125}126