File size: 4,337 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe "InitialSessionState capacity" -Tags CI {
    BeforeAll {
        $iss = [initialsessionstate]::CreateDefault()

        for ($i = 0; $i -lt 5000; $i++)
        {
            $ssfe = [System.Management.Automation.Runspaces.SessionStateFunctionEntry]::new("f$i", "'fn f$i'")
            $iss.Commands.Add($ssfe)

            $ssve = [System.Management.Automation.Runspaces.SessionStateVariableEntry]::new("v$i", "var v$i", $null)
            $iss.Variables.Add($ssve)

            $ssae = [System.Management.Automation.Runspaces.SessionStateAliasEntry]::new("a$i", "f$i")
            $iss.Commands.Add($ssae)
        }

        $ps = [PowerShell]::Create($iss)
    }

    AfterAll {
        $ps.Dispose()
    }

    BeforeEach {
        $ps.Commands.Clear()
    }

    It "function capacity in initial session state should not be limited" {
        $ps.AddCommand('f4999').Invoke() | Should -Be "fn f4999"
        $ps.Streams.Error | Should -BeNullOrEmpty
    }

    It "alias capacity in initial session state should not be limited" {
        $ps.AddCommand('a4999').Invoke() | Should -Be "fn f4999"
        $ps.Streams.Error | Should -BeNullOrEmpty
    }

    It "variable capacity in initial session state should not be limited" {
        $ps.AddScript('$v4999').Invoke() | Should -Be "var v4999"
        $ps.Streams.Error | Should -BeNullOrEmpty
    }

    It "function capacity should not be limited after runspace is opened" {
        $ps.AddScript('function f5000 { "in f5000" } f5000').Invoke() | Should -Be "in f5000"
        $ps.Streams.Error | Should -BeNullOrEmpty
    }

    It "variable capacity should not be limited after runspace is opened" {
        $ps.AddScript('$v5000 = "var v5000"; $v5000').Invoke() | Should -Be "var v5000"
        $ps.Streams.Error | Should -BeNullOrEmpty
    }

    It "alias capacity should not be limited after runspace is opened" {
        $ps.AddScript('New-Alias -Name a5000 -Value f1; a5000').Invoke() | Should -Be "fn f1"
        $ps.Streams.Error | Should -BeNullOrEmpty
    }
}

##
## A reused InitialSessionState created from a TypeTable should not have duplicate types.
##
Describe "TypeTable duplicate types in reused runspace InitialSessionState TypeTable" -Tags 'Feature' {

    Context "No duplicate types test" {

        BeforeAll {

            $typeTable = [System.Management.Automation.Runspaces.TypeTable]::new([string[]](Join-Path $PSScriptRoot "assets/TestTypeFile.ps1xml"))
            [initialsessionstate] $iss = [initialsessionstate]::Create()
            $iss.Types.Add($typeTable)
            [runspace] $rs1 = [runspacefactory]::CreateRunspace($iss)

            # Process TypeTable types from ISS
            $rs1.Open()

            # Get processed ISS from runspace.
            $issReused = $rs1.InitialSessionState.Clone()
            $issReused.ThrowOnRunspaceOpenError = $true

            # Create new runspace with reused ISS.
            $rs2 = [runspacefactory]::CreateRunspace($issReused)
        }

        AfterAll {

            if ($null -ne $rs1) { $rs1.Dispose() }
            if ($null -ne $rs2) { $rs2.Dispose() }
        }

        It "Verifies that a reused InitialSessionState object created from a TypeTable object does not have duplicate types" {

            { $rs2.Open() } | Should -Not -Throw
        }
    }

    Context "Cannot use shared TypeTable in ISS test" {

        BeforeAll {

            # Create default ISS and add shared TypeTable.
            $typeTable = [System.Management.Automation.Runspaces.TypeTable]::new([string[]](Join-Path $PSScriptRoot "assets/TestTypeFile.ps1xml"))
            [initialsessionstate] $iss = [initialsessionstate]::CreateDefault2()
            $iss.Types.Add($typeTable)
            $iss.ThrowOnRunspaceOpenError = $true
            [runspace] $rs = [runspacefactory]::CreateRunspace($iss)
        }

        AfterAll {

            if ($null -ne $rs) { $rs.Dispose() }
        }

        It "Verifies that shared TypeTable is not allowed in ISS" {

            # Process TypeTable types from ISS.
            $e = { $rs.Open() } | Should -Throw -ErrorId "RuntimeException" -PassThru
	    $e.Exception.InnerException.ErrorRecord.FullyQualifiedErrorId | Should -BeExactly "ErrorsUpdatingTypes"
        }
    }
}