File size: 4,518 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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe "Remote module tests" -Tags 'Feature','RequireAdminOnWindows' {

    BeforeAll {
        $pendingTest = (Test-IsWinWow64)
        $skipTest = !$IsWindows

        $originalDefaultParameterValues = $PSDefaultParameterValues.Clone()

        if ($pendingTest -or $skipTest)
        {
            if ($skipTest)
            {
                $PSDefaultParameterValues["it:skip"] = $true
            }
            elseif ($pendingTest)
            {
                $PSDefaultParameterValues["it:pending"] = $true
            }

            return
        }

        $pssession = New-RemoteSession
        # pending https://github.com/PowerShell/PowerShell/issues/4819
        # $cimsession = New-RemoteSession -CimSession
    }

    AfterAll {

        if ($pendingTest -or $skipTest)
        {
            $global:PSDefaultParameterValues = $originalDefaultParameterValues
            return
        }

        if ($pssession -ne $null) { Remove-PSSession $pssession -ErrorAction SilentlyContinue }
    }

    It "Get-Module fails if not using -ListAvailable with '<parameter>'" -TestCases @(
        @{parameter="PSSession" ; value=$pssession}
        # @{parameter="CIMSession"; value=$cimsession}
    ) {
        param($parameter, $value)
        $parameters = @{$parameter=$value}
        { Get-Module @parameters -ErrorAction Stop } | Should -Throw -ErrorId "RemoteDiscoveryWorksOnlyInListAvailableMode,Microsoft.PowerShell.Commands.GetModuleCommand"
    }

    It "Get-Module succeeds using -ListAvailable with '<parameter>'" -TestCases @(
        @{parameter="PSSession" ; value=$pssession},
        @{parameter="PSSession" ; value=$pssession ; name="Pester"}
        # @{parameter="CIMSession"; value=$cimsession},
        # @{parameter="CIMSession"; value=$cimsession; name="pESTER"}
    ) {
        param($parameter, $value, $name)
        $parameters = @{$parameter=$value; ListAvailable=$true; Refresh=$true}
        if ($name) {
            $parameters += @{name=$name}
        }
        $modules = Get-Module @parameters
        $modules | Should -Not -BeNullOrEmpty
        $modules[0] | Should -BeOfType System.Management.Automation.PSModuleInfo
    }

    It "Get-Module can be called as an API with '<parameter>' = '<value>'" -TestCases @(
        @{parameter = "Name"              ; value = "foo"},
        @{parameter = "FullyQualifiedName"; value = @{ModuleName="foo";ModuleVersion="1.2.3"}},
        @{parameter = "All"               ; value = $true},
        @{parameter = "All"               ; value = $false},
        @{parameter = "ListAvailable"     ; value = $true},
        @{parameter = "ListAvailable"     ; value = $false},
        @{parameter = "PSEdition"         ; value = "foo"},
        @{parameter = "Refresh"           ; value = $true},
        @{parameter = "Refresh"           ; value = $false},
        @{parameter = "PSSession"         ; value = $pssession},
        # @{parameter = "CimSession"        ; value = $cimsession},
        @{parameter = "CimResourceUri"    ; value = "http://foo/"},
        @{parameter = "CimNamespace"      ; value = "foo"},
        @{parameter = "PSEdition"         ; value = "Core"},
        @{parameter = "PSEdition"         ; value = "Desktop"}
        ) {
        param($parameter, $value)

        $getModuleCommand = [Microsoft.PowerShell.Commands.GetModuleCommand]::new()
        $getModuleCommand.$parameter = $value
        if ($parameter -eq "FullyQualifiedName") {
            $getModuleCommand.FullyQualifiedName | Should -BeOfType Microsoft.PowerShell.Commands.ModuleSpecification
            $getModuleCommand.FullyQualifiedName.Name | Should -BeExactly "foo"
            $getModuleCommand.FullyQualifiedName.Version | Should -Be "1.2.3"
        } else {
            $getModuleCommand.$parameter | Should -Be $value
        }
    }

    It "Failure if -Name and -FullyQualifiedName are both specified" {
        { Get-Module -Name foo -FullyQualifiedName @{ModuleName='foo'} -ErrorAction Stop } | Should -Throw -ErrorId "CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetModuleCommand"
    }

    It "Get-Module supports pipeline" {
        $module = Get-Module -Name Microsoft.PowerShell.Utility
        Compare-Object $module ($module | Get-Module) | Should -BeNullOrEmpty
    }

    It "New-CimSession works" -Pending {
        # enable all CimSession tests once https://github.com/PowerShell/PowerShell/issues/4819 is fixed
    }
}