|
| 1 | +Describe "Remote import-module tests" -Tags 'Feature','RequireAdminOnWindows' { |
| 2 | + |
| 3 | + BeforeAll { |
| 4 | + $originalDefaultParameterValues = $PSDefaultParameterValues.Clone() |
| 5 | + $modulePath = "$testdrive\Modules\TestImport" |
| 6 | + if (!$IsWindows) { |
| 7 | + $PSDefaultParameterValues["it:skip"] = $true |
| 8 | + } else { |
| 9 | + $pssession = New-RemoteSession |
| 10 | + Invoke-Command -Session $pssession -ScriptBlock { $env:PSModulePath += ";${using:testdrive}" } |
| 11 | + # pending https://github.com/PowerShell/PowerShell/issues/4819 |
| 12 | + # $cimsession = New-RemoteSession -CimSession |
| 13 | + $null = New-Item -ItemType Directory -Path $modulePath |
| 14 | + Set-Content -Path $modulePath\testimport.psm1 -Value "function test-hello { 'world' }" |
| 15 | + New-ModuleManifest -Path $modulePath\testimport.psd1 -ModuleVersion 1.2.3 -RootModule testimport.psm1 -FunctionsToExport "test-hello" ` |
| 16 | + -HelpInfoUri "https://help" -Guid (New-Guid) |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + AfterAll { |
| 21 | + $global:PSDefaultParameterValues = $originalDefaultParameterValues |
| 22 | + if ($IsWindows) { |
| 23 | + $pssession | Remove-PSSession -ErrorAction SilentlyContinue |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + BeforeEach { |
| 28 | + Remove-Module TestImport -Force -ErrorAction SilentlyContinue |
| 29 | + } |
| 30 | + |
| 31 | + It "Import-Module can be called as an API with '<parameter>' = '<value>'" -TestCases @( |
| 32 | + @{parameter = "Global" ; value = $true}, |
| 33 | + @{parameter = "Global" ; value = $false}, |
| 34 | + @{parameter = "Prefix" ; value = "Hello"}, |
| 35 | + @{parameter = "Name" ; value = "foo","bar"}, |
| 36 | + @{parameter = "FullyQualifiedName" ; value = @{ModuleName='foo';RequiredVersion='0.0'},@{ModuleName='bar';RequiredVersion='1.1'}}, |
| 37 | + @{parameter = "Assembly" ; script = { [System.AppDomain]::CurrentDomain.GetAssemblies() | Select-Object -First 2 }} |
| 38 | + @{parameter = "Function" ; value = "foo","bar"}, |
| 39 | + @{parameter = "Cmdlet" ; value = "foo","bar"}, |
| 40 | + @{parameter = "Variable" ; value = "foo","bar"}, |
| 41 | + @{parameter = "Alias" ; value = "foo","bar"}, |
| 42 | + @{parameter = "Force" ; value = $true}, |
| 43 | + @{parameter = "Force" ; value = $false}, |
| 44 | + @{parameter = "PassThru" ; value = $true}, |
| 45 | + @{parameter = "PassThru" ; value = $false}, |
| 46 | + @{parameter = "AsCustomObject" ; value = $true}, |
| 47 | + @{parameter = "AsCustomObject" ; value = $false}, |
| 48 | + @{parameter = "MinimumVersion" ; value = "1.2.3"}, |
| 49 | + @{parameter = "MaximumVersion" ; value = "3.2.1"}, |
| 50 | + @{parameter = "RequiredVersion" ; value = "1.1.1"}, |
| 51 | + @{parameter = "ArgumentList" ; value = "hello","world"}, |
| 52 | + @{parameter = "DisableNameChecking"; value = $true}, |
| 53 | + @{parameter = "DisableNameChecking"; value = $false}, |
| 54 | + @{parameter = "NoClobber" ; value = $true}, |
| 55 | + @{parameter = "NoClobber" ; value = $false}, |
| 56 | + @{parameter = "Scope" ; value = "Local"}, |
| 57 | + @{parameter = "Scope" ; value = "Global"}, |
| 58 | + @{parameter = "PSSession" ; value = $pssession}, |
| 59 | + # @{parameter = "CimSession" ; value = $cimsession}, |
| 60 | + @{parameter = "CimResourceUri" ; value = "http://foo/"}, |
| 61 | + @{parameter = "CimNamespace" ; value = "foo"} |
| 62 | + ) { |
| 63 | + param($parameter, $value, $script) |
| 64 | + |
| 65 | + $importModuleCommand = [Microsoft.PowerShell.Commands.ImportModuleCommand]::new() |
| 66 | + if ($script -ne $null) { |
| 67 | + $value = & $script |
| 68 | + } |
| 69 | + $importModuleCommand.$parameter = $value |
| 70 | + if ($parameter -eq "FullyQualifiedName") { |
| 71 | + $importModuleCommand.FullyQualifiedName.Count | Should BeExactly 2 |
| 72 | + $importModuleCommand.FullyQualifiedName | Should BeOfType "Microsoft.PowerShell.Commands.ModuleSpecification" |
| 73 | + $importModuleCommand.FullyQualifiedName[0].Name | Should Be "foo" |
| 74 | + $importModuleCommand.FullyQualifiedName[0].RequiredVersion | Should Be "0.0" |
| 75 | + $importModuleCommand.FullyQualifiedName[1].Name | Should Be "bar" |
| 76 | + $importModuleCommand.FullyQualifiedName[1].RequiredVersion | Should Be "1.1" |
| 77 | + } else { |
| 78 | + $importModuleCommand.$parameter | Should BeExactly $value |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + It "Import-Module can import over remote session: <test>" -TestCases @( |
| 83 | + @{ test = "pssession" ; parameters = @{Name="TestImport";PSSession=$pssession}}, |
| 84 | +# @{ test = "cimsession" ; parameters = @{Name="TestImport";CimSession=$cimsession}}, |
| 85 | + @{ test = "minimumversion" ; parameters = @{Name="TestImport";PSSession=$pssession;MinimumVersion="1.0";Force=$true}}, |
| 86 | + @{ test = "requiredversion" ; parameters = @{Name="TestImport";PSSession=$pssession;RequiredVersion="1.2.3"}}, |
| 87 | + @{ test = "maxiumversion" ; parameters = @{Name="TestImport";PSSession=$pssession;MaximumVersion="2.0"}}, |
| 88 | + @{ test = "invalid miniumversion" ; parameters = @{Name="TestImport";PSSession=$pssession;MinimumVersion="2.0"}; |
| 89 | + errorid = "Modules_ModuleWithVersionNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand,Microsoft.PowerShell.Commands.ImportModuleCommand"}, |
| 90 | + @{ test = "invalid maximumversion" ; parameters = @{Name="TestImport";PSSession=$pssession;MaximumVersion="1.0"}; |
| 91 | + errorid = "Modules_ModuleWithVersionNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand,Microsoft.PowerShell.Commands.ImportModuleCommand"}, |
| 92 | + @{ test = "fullyqualifiedname" ; parameters = @{FullyQualifiedName=@{Modulename="TestImport"; RequiredVersion="1.2.3"};PSSession=$pssession}} |
| 93 | + ) { |
| 94 | + param ($test, $parameters, $errorid) |
| 95 | + |
| 96 | + Invoke-Command -Session $pssession -ScriptBlock { $env:PSModulePath += ";$(Split-Path $using:modulePath)"} |
| 97 | + Get-Module TestImport | Should BeNullOrEmpty |
| 98 | + if ($errorid) { |
| 99 | + { Import-Module @parameters -ErrorAction Stop } | ShouldBeErrorId $errorid |
| 100 | + } else { |
| 101 | + Import-Module @parameters |
| 102 | + $module = Get-Module TestImport |
| 103 | + $module.Name | Should BeExactly "TestImport" |
| 104 | + |
| 105 | + # generated proxy module always uses 1.0 |
| 106 | + $module.Version | Should BeExactly "1.0" |
| 107 | + |
| 108 | + test-hello | Should BeExactly "world" |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments