Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make PowerShell class not affiliate with Runspace when declaring the NoRunspaceAffinity attribute #18138

Merged
merged 4 commits into from
Sep 22, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add test for custom constructor
  • Loading branch information
daxian-dbw committed Sep 21, 2022
commit c7b846e7c67cd44b938d17f35ab5f4bd4a2f5090
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Describe "Class can be defined without Runspace affinity" -Tags "CI" {
[NoRunspaceAffinity()]
class NoAffinity {
[string] $Name;
[int] $RunspaceId;

NoAffinity() {
$this.RunspaceId = [runspace]::DefaultRunspace.Id
}

static [int] Echo() {
return [runspace]::DefaultRunspace.Id
Expand All @@ -23,25 +28,22 @@ Describe "Class can be defined without Runspace affinity" -Tags "CI" {

## Running directly should use the current Runspace/SessionState.
$t::Echo() | Should -Be $Host.Runspace.Id
$o.RunspaceId | Should -Be $Host.Runspace.Id
$o.SetAndEcho('Blue') | Should -Be $Host.Runspace.Id
$o.Name | Should -Be 'Blue'

## Running in a new Runspace should use that Runspace and its current SessionState.
try {
$ps = [powershell]::Create()
$ps.AddScript('function CallEcho($type) { $type::Echo() }').Invoke() > $null
$ps.Commands.Clear()
$ps.AddScript('function CallSetAndEcho($obj) { $obj.SetAndEcho(''Hello world'') }').Invoke() > $null
$ps.Commands.Clear()
$ps.AddScript('function GetName($obj) { $obj.Name }').Invoke() > $null
$ps.Commands.Clear()

$ps.AddCommand('CallEcho').AddArgument($t).Invoke() | Should -Be $ps.Runspace.Id
$ps.Commands.Clear()
$ps.AddCommand('CallSetAndEcho').AddArgument($o).Invoke() | Should -Be $ps.Runspace.Id
$ps.Commands.Clear()
$ps.AddCommand('GetName').AddArgument($o).Invoke() | Should -Be 'Hello world'
$ps.Commands.Clear()
$ps.AddScript('function CallEcho($type) { $type::Echo() }').Invoke() > $null; $ps.Commands.Clear()
$ps.AddScript('function CallSetAndEcho($obj) { $obj.SetAndEcho(''Hello world'') }').Invoke() > $null; $ps.Commands.Clear()
$ps.AddScript('function GetName($obj) { $obj.Name }').Invoke() > $null; $ps.Commands.Clear()
$ps.AddScript('function NewObj($type) { $type::new().RunspaceId }').Invoke() > $null; $ps.Commands.Clear()

$ps.AddCommand('CallEcho').AddArgument($t).Invoke() | Should -Be $ps.Runspace.Id; $ps.Commands.Clear()
$ps.AddCommand('CallSetAndEcho').AddArgument($o).Invoke() | Should -Be $ps.Runspace.Id; $ps.Commands.Clear()
$ps.AddCommand('GetName').AddArgument($o).Invoke() | Should -Be 'Hello world'; $ps.Commands.Clear()
$ps.AddCommand('NewObj').AddArgument($t).Invoke() | Should -Be $ps.Runspace.Id; $ps.Commands.Clear()
}
finally {
$ps.Dispose()
Expand Down