MegaBites-AI/Windows-powershell
0308
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Describe 'Switch-Process tests for Unix' -Tags 'CI' {5 BeforeAll {6 $originalDefaultParameterValues = $PSDefaultParameterValues.Clone()7 if ($IsWindows)8 {9 $PSDefaultParameterValues['It:Skip'] = $true10 return11 }12 }13 14 AfterAll {15 $global:PSDefaultParameterValues = $originalDefaultParameterValues16 }17 18 It 'Exec function should map to Switch-Process' {19 $func = Get-Command exec20 $func | Should -BeOfType [System.Management.Automation.CommandInfo]21 $func.Definition | Should -Not -BeNullOrEmpty22 }23 24 It 'Exec by itself does nothing' {25 exec | Should -BeNullOrEmpty26 }27 28 It 'Exec given a cmdlet should fail' {29 { exec Get-Command } | Should -Throw -ErrorId 'CommandNotFound,Microsoft.PowerShell.Commands.SwitchProcessCommand'30 }31 32 It 'Exec given an exe should work' {33 $id, $uname = pwsh -noprofile -noexit -outputformat text -command { $pid; exec uname }34 Get-Process -Id $id -ErrorAction Ignore| Should -BeNullOrEmpty35 $uname | Should -BeExactly (uname)36 }37 38 It 'Exec given an exe and arguments should work' {39 $id, $uname = pwsh -noprofile -noexit -outputformat text -command { $pid; exec uname -a }40 Get-Process -Id $id -ErrorAction Ignore| Should -BeNullOrEmpty41 $uname | Should -BeExactly (uname -a)42 }43 44 It 'Exec will replace the process' {45 $sleep = Get-Command sleep -CommandType Application | Select-Object -First 146 $p = Start-Process pwsh -ArgumentList "-noprofile -command exec $($sleep.Source) 90" -PassThru47 Wait-UntilTrue {48 ($p | Get-Process).Name -eq 'sleep'49 } -timeout 60000 -interval 100 | Should -BeTrue50 }51 52 It 'Error is returned if target command is not found' {53 $invalidCommand = 'doesNotExist'54 $e = { Switch-Process $invalidCommand } | Should -Throw -ErrorId 'CommandNotFound,Microsoft.PowerShell.Commands.SwitchProcessCommand' -PassThru55 $e.Exception.Message | Should -BeLike "*'$invalidCommand'*"56 $e.TargetObject | Should -BeExactly $invalidCommand57 }58 59 It 'The environment will be copied to the child process' {60 $env = pwsh -noprofile -outputformat text -command { $env:TEST_FOO='my test = value'; Switch-Process bash -c 'echo $TEST_FOO' }61 $env | Should -BeExactly 'my test = value'62 }63 64 It 'The command can include a -w parameter' {65 $out = pwsh -noprofile -outputformat text -command { exec /bin/echo 1 -w 2 }66 $out | Should -BeExactly '1 -w 2'67 }68}69 70Describe 'Switch-Process for Windows' -Tag 'CI' {71 BeforeAll {72 $originalDefaultParameterValues = $PSDefaultParameterValues.Clone()73 if (!$IsWindows)74 {75 $PSDefaultParameterValues['It:Skip'] = $true76 return77 }78 }79 80 AfterAll {81 $global:PSDefaultParameterValues = $originalDefaultParameterValues82 }83 84 It 'Switch-Process should not be available' {85 Get-Command -Name Switch-Process -ErrorAction Ignore | Should -BeNullOrEmpty86 }87 88 It 'Exec alias should not be available' {89 Get-Alias -Name exec -ErrorAction Ignore | Should -BeNullOrEmpty90 }91}92 