Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes308downloads
Read-Host.Tests.ps174 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Read-Host Test" -Tag "CI" {4    BeforeAll {5        $th = New-TestHost6        $rs = [runspacefactory]::Createrunspace($th)7        $rs.open()8        $ps = [powershell]::Create()9        $ps.Runspace = $rs10        $ps.Commands.Clear()11    }12 13    AfterEach {14        $ps.Commands.Clear()15        $th.UI.Streams.Clear()16    }17 18    AfterAll {19        $rs.Close()20        $rs.Dispose()21        $ps.Dispose()22    }23 24    It "Read-Host returns expected string" {25        $result = $ps.AddCommand("Read-Host").Invoke()26        $result | Should -Be $th.UI.ReadLineData27    }28 29    It "Read-Host sets the prompt correctly" {30        $result = $ps.AddScript("Read-Host -prompt myprompt").Invoke()31        $prompt = $th.ui.streams.prompt[0]32        $prompt | Should -Not -BeNullOrEmpty33        $prompt.split(":")[-1] | Should -Be myprompt34        $result | Should -BeExactly 'this is a prompt response'35    }36 37    It "Read-Host returns a secure string when using -AsSecureString parameter" {38        $result = $ps.AddScript("Read-Host -AsSecureString").Invoke() | Select-Object -First 139        $result | Should -BeOfType SecureString40        $result | ConvertFrom-SecureString -AsPlainText | Should -BeExactly 'TEST'41    }42 43    It "Read-Host returns a string when using -MaskInput parameter" {44        $result = $ps.AddScript("Read-Host -MaskInput").Invoke()45        $result | Should -BeExactly 'TEST'46    }47 48    It "Read-Host returns a string when using -MaskInput parameter used with -Prompt" {49        $result = $ps.AddScript("Read-Host -MaskInput -Prompt Test").Invoke()50        $prompt = $th.ui.streams.prompt[0]51        $prompt | Should -Not -BeNullOrEmpty52        $prompt.split(":")[-1] | Should -BeExactly 'Test'53        $result | Should -BeExactly 'this is a prompt response'54    }55 56    It "Read-Host throws an error when both -AsSecureString parameter and -MaskInput parameter are used" {57        # Contrary to the rest of the tests this does not need to be invoked through a runspace since it is going to throw an error.58        $errorId = "AmbiguousParameterSet,Microsoft.PowerShell.Commands.ReadHostCommand"59        {Read-Host -MaskInput -AsSecureString} | Should -Throw -ErrorId $errorId60    }61 62    It "Read-Host doesn't enter command prompt mode" {63        $result = "!1" | & "$PSHOME/pwsh" -NoProfile -c "Read-host -Prompt 'foo'"64        if ($IsWindows) {65            # Windows write to console directly so can't capture prompt in stdout66            $expected = @('!1','!1')67        }68        else {69            $expected = @('foo: !1','!1')70        }71        $result | Should -BeExactly $expected72    }73}74