mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Copy the CommandInfo property in Command.Clone() (#12301)
This commit is contained in:
@@ -109,6 +109,7 @@ namespace System.Management.Automation.Runspaces
|
||||
MergeToResult = command.MergeToResult;
|
||||
_mergeUnclaimedPreviousCommandResults = command._mergeUnclaimedPreviousCommandResults;
|
||||
IsEndOfStatement = command.IsEndOfStatement;
|
||||
CommandInfo = command.CommandInfo;
|
||||
|
||||
foreach (CommandParameter param in command.Parameters)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
# Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
# Licensed under the MIT License.
|
||||
|
||||
Describe "PSCommand API tests" -Tag "CI" {
|
||||
BeforeAll {
|
||||
$shell = [PowerShell]::Create()
|
||||
}
|
||||
|
||||
AfterAll {
|
||||
$shell.Dispose()
|
||||
}
|
||||
|
||||
BeforeEach {
|
||||
$psCommand = [System.Management.Automation.PSCommand]::new()
|
||||
}
|
||||
|
||||
AfterEach {
|
||||
$shell.Commands.Clear()
|
||||
}
|
||||
|
||||
Context "AddCommand method on PSCommand" {
|
||||
It "Can add a command by name and parameter to a PSCommand" {
|
||||
$null = $psCommand.AddCommand("Write-Output").AddParameter("InputObject", 5)
|
||||
$shell.Commands = $psCommand
|
||||
$result = $shell.Invoke()
|
||||
$result | Should -Be 5
|
||||
}
|
||||
|
||||
It "Can add a command by Command object and parameter to a PSCommand" {
|
||||
$command = [System.Management.Automation.Runspaces.Command]::new("Get-Date")
|
||||
$null = $psCommand.AddCommand($command)
|
||||
$shell.Commands = $psCommand
|
||||
$result = $shell.Invoke()
|
||||
$result | Should -BeOfType System.DateTime
|
||||
}
|
||||
}
|
||||
|
||||
Context "Cloning PSCommands" {
|
||||
It "The clone method successfully copies commands" {
|
||||
$null = $psCommand.AddCommand("Write-Output").AddParameter("InputObject", 5)
|
||||
|
||||
$newCommand = $psCommand.Clone()
|
||||
|
||||
$newCommand.Commands | Should -Not -BeNullOrEmpty
|
||||
$newCommand.Commands[0].CommandText | Should -Be "Write-Output"
|
||||
$newCommand.Commands[0].Parameters | Should -Not -BeNullOrEmpty
|
||||
$newCommand.Commands[0].Parameters[0].Name | Should -Be "InputObject"
|
||||
$newCommand.Commands[0].Parameters[0].Value | Should -Be 5
|
||||
}
|
||||
|
||||
It "clones properly when using the setter on the PowerShell type" {
|
||||
try {
|
||||
$otherShell = [powershell]::Create('CurrentRunspace')
|
||||
|
||||
# We manually create a CmdletInfo here (with an unresolable command) to verify that CmdletInfo's are cloned.
|
||||
$cmdlet = [System.Management.Automation.CmdletInfo]::new('un-resolvable', [Microsoft.PowerShell.Commands.OutStringCommand])
|
||||
$null = $otherShell.AddCommand($cmdlet).AddParameter("InputObject", 'test')
|
||||
|
||||
# Setter for "Commands" calls PSCommand.Clone()
|
||||
$shell.Commands = $otherShell.Commands
|
||||
|
||||
$result = $shell.Invoke()
|
||||
$result | Should -Be "test
|
||||
"
|
||||
}
|
||||
finally {
|
||||
$otherShell.Dispose()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user