File size: 4,866 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
124
125
126
127
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Describe 'NestedModules' -Tags "CI" {
    BeforeAll {
        function New-TestModule {
            param(
                [string]$Name,
                [string]$Content,
                [string[]]$NestedContents
            )

            New-Item -type directory -Force "TestDrive:\$Name" > $null
            $manifestParams = @{
                Path = "TestDrive:\$Name\$Name.psd1"
            }

            if ($Content) {
                Set-Content -Path "${TestDrive}\$Name\$Name.psm1" -Value $Content
                $manifestParams['RootModule'] = "$Name.psm1"
            }

            if ($NestedContents) {
                $manifestParams['NestedModules'] = 1..$NestedContents.Count | ForEach-Object {
                    $null = New-Item -type directory TestDrive:\$Name\Nested$_
                    $null = Set-Content -Path "${TestDrive}\$Name\Nested$_\Nested$_.psm1" -Value $NestedContents[$_ - 1]
                    "Nested$_"
                }
            }

            New-ModuleManifest @manifestParams

            $resolvedTestDrivePath = Split-Path ((Get-ChildItem TestDrive:\)[0].FullName)
            if (-not ($env:PSModulePath -like "*$resolvedTestDrivePath*")) {
                $env:PSModulePath += "$([System.IO.Path]::PathSeparator)$resolvedTestDrivePath"
            }
        }

        $originalPSModulePath = $env:PSModulePath

        # Create modules in TestDrive:\
        New-TestModule -Name NoRoot -NestedContents @(
            'class A { [string] foo() { return "A1"} }',
            'class A { [string] foo() { return "A2"} }'
        )

        New-TestModule -Name WithRoot -NestedContents @(
            'class A { [string] foo() { return "A1"} }',
            'class A { [string] foo() { return "A2"} }'
        ) -Content 'class A { [string] foo() { return "A0"} }'

        New-TestModule -Name ABC -NestedContents @(
            'class A { [string] foo() { return "A"} }',
            'class B { [string] foo() { return "B"} }'
        ) -Content 'class C { [string] foo() { return "C"} }'
    }

    AfterAll {
        $env:PSModulePath = $originalPSModulePath
        Get-Module @('ABC', 'NoRoot', 'WithRoot') | Remove-Module
    }


    It 'Get-Module is able to find types' {
        $module = Get-Module NoRoot -ListAvailable
        $module.GetExportedTypeDefinitions().Count | Should -Be 1

        $module = Get-Module WithRoot -ListAvailable
        $module.GetExportedTypeDefinitions().Count | Should -Be 1

        $module = Get-Module ABC -ListAvailable
        $module.GetExportedTypeDefinitions().Count | Should -Be 3
    }

    It 'Import-Module pick the right type' {
        $module = Import-Module ABC -PassThru
        $module.GetExportedTypeDefinitions().Count | Should -Be 3
        $module = Import-Module ABC -PassThru -Force
        $module.GetExportedTypeDefinitions().Count | Should -Be 3

        $module = Import-Module NoRoot -PassThru
        $module.GetExportedTypeDefinitions().Count | Should -Be 1
        $module = Import-Module NoRoot -PassThru -Force
        $module.GetExportedTypeDefinitions().Count | Should -Be 1
        [scriptblock]::Create(@'
using module NoRoot
[A]::new().foo()
'@
).Invoke() | Should -Be A2

        $module = Import-Module WithRoot -PassThru
        $module.GetExportedTypeDefinitions().Count | Should -Be 1
        $module = Import-Module WithRoot -PassThru -Force
        $module.GetExportedTypeDefinitions().Count | Should -Be 1
        [scriptblock]::Create(@'
using module WithRoot
[A]::new().foo()
'@
).Invoke() | Should -Be A0
    }

    Context 'execute type creation in the module context' {

        # let's define types to make it more fun
        class A { [string] foo() { return "local"} }
        class B { [string] foo() { return "local"} }
        class C { [string] foo() { return "local"} }

        # We need to think about it: should it work or not.
        # Currently, types are resolved in compile-time to the 'local' versions
        # So at runtime we don't call the module versions.
        It 'Can execute type creation in the module context with new()' -Pending {
            & (Get-Module ABC) { [C]::new().foo() } | Should -Be C
            & (Get-Module NoRoot) { [A]::new().foo() } | Should -Be A2
            & (Get-Module WithRoot) { [A]::new().foo() } | Should -Be A0
            & (Get-Module ABC) { [A]::new().foo() } | Should -Be A
        }

        It 'Can execute type creation in the module context with New-Object' {
            & (Get-Module ABC) { (New-Object C).foo() } | Should -Be C
            & (Get-Module NoRoot) { (New-Object A).foo() } | Should -Be A2
            & (Get-Module WithRoot) { (New-Object A).foo() } | Should -Be A0
            & (Get-Module ABC) { (New-Object A).foo() } | Should -Be A
        }
    }
}