|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +Describe "PSCommand API tests" -Tag "CI" { |
| 5 | + BeforeAll { |
| 6 | + $shell = [PowerShell]::Create() |
| 7 | + } |
| 8 | + |
| 9 | + AfterAll { |
| 10 | + $shell.Dispose() |
| 11 | + } |
| 12 | + |
| 13 | + BeforeEach { |
| 14 | + $psCommand = [System.Management.Automation.PSCommand]::new() |
| 15 | + } |
| 16 | + |
| 17 | + AfterEach { |
| 18 | + $shell.Commands.Clear() |
| 19 | + } |
| 20 | + |
| 21 | + Context "AddCommand method on PSCommand" { |
| 22 | + It "Can add a command by name and parameter to a PSCommand" { |
| 23 | + $null = $psCommand.AddCommand("Write-Output").AddParameter("InputObject", 5) |
| 24 | + $shell.Commands = $psCommand |
| 25 | + $result = $shell.Invoke() |
| 26 | + $result | Should -Be 5 |
| 27 | + } |
| 28 | + |
| 29 | + It "Can add a command by Command object and parameter to a PSCommand" { |
| 30 | + $command = [System.Management.Automation.Runspaces.Command]::new("Get-Date") |
| 31 | + $null = $psCommand.AddCommand($command) |
| 32 | + $shell.Commands = $psCommand |
| 33 | + $result = $shell.Invoke() |
| 34 | + $result | Should -BeOfType System.DateTime |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + Context "Cloning PSCommands" { |
| 39 | + It "The clone method successfully copies commands" { |
| 40 | + $null = $psCommand.AddCommand("Write-Output").AddParameter("InputObject", 5) |
| 41 | + |
| 42 | + $newCommand = $psCommand.Clone() |
| 43 | + |
| 44 | + $newCommand.Commands | Should -Not -BeNullOrEmpty |
| 45 | + $newCommand.Commands[0].CommandText | Should -Be "Write-Output" |
| 46 | + $newCommand.Commands[0].Parameters | Should -Not -BeNullOrEmpty |
| 47 | + $newCommand.Commands[0].Parameters[0].Name | Should -Be "InputObject" |
| 48 | + $newCommand.Commands[0].Parameters[0].Value | Should -Be 5 |
| 49 | + } |
| 50 | + |
| 51 | + It "clones properly when using the setter on the PowerShell type" { |
| 52 | + try { |
| 53 | + $otherShell = [powershell]::Create('CurrentRunspace') |
| 54 | + |
| 55 | + # We manually create a CmdletInfo here (with an unresolable command) to verify that CmdletInfo's are cloned. |
| 56 | + $cmdlet = [System.Management.Automation.CmdletInfo]::new('un-resolvable', [Microsoft.PowerShell.Commands.OutStringCommand]) |
| 57 | + $null = $otherShell.AddCommand($cmdlet).AddParameter("InputObject", 'test') |
| 58 | + |
| 59 | + # Setter for "Commands" calls PSCommand.Clone() |
| 60 | + $shell.Commands = $otherShell.Commands |
| 61 | + |
| 62 | + $result = $shell.Invoke() |
| 63 | + $result | Should -Be "test |
| 64 | +" |
| 65 | + } |
| 66 | + finally { |
| 67 | + $otherShell.Dispose() |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments