-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Fix importing module remote using version filters and added tests #4900
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
Describe "Remote import-module tests" -Tags 'Feature','RequireAdminOnWindows' { | ||
|
||
BeforeAll { | ||
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone() | ||
$modulePath = "$testdrive\Modules\TestImport" | ||
if (!$IsWindows) { | ||
$PSDefaultParameterValues["it:skip"] = $true | ||
} else { | ||
$pssession = New-RemoteSession | ||
Invoke-Command -Session $pssession -ScriptBlock { $env:PSModulePath += ";${using:testdrive}" } | ||
# pending https://github.com/PowerShell/PowerShell/issues/4819 | ||
# $cimsession = New-RemoteSession -CimSession | ||
$null = New-Item -ItemType Directory -Path $modulePath | ||
Set-Content -Path $modulePath\testimport.psm1 -Value "function test-hello { 'world' }" | ||
New-ModuleManifest -Path $modulePath\testimport.psd1 -ModuleVersion 1.2.3 -RootModule testimport.psm1 -FunctionsToExport "test-hello" ` | ||
-HelpInfoUri "https://help" -Guid (New-Guid) | ||
} | ||
} | ||
|
||
AfterAll { | ||
$global:PSDefaultParameterValues = $originalDefaultParameterValues | ||
if ($IsWindows) { | ||
$pssession | Remove-PSSession -ErrorAction SilentlyContinue | ||
} | ||
} | ||
|
||
BeforeEach { | ||
Remove-Module TestImport -Force -ErrorAction SilentlyContinue | ||
} | ||
|
||
It "Import-Module can be called as an API with '<parameter>' = '<value>'" -TestCases @( | ||
@{parameter = "Global" ; value = $true}, | ||
@{parameter = "Global" ; value = $false}, | ||
@{parameter = "Prefix" ; value = "Hello"}, | ||
@{parameter = "Name" ; value = "foo","bar"}, | ||
@{parameter = "FullyQualifiedName" ; value = @{ModuleName='foo';RequiredVersion='0.0'},@{ModuleName='bar';RequiredVersion='1.1'}}, | ||
@{parameter = "Assembly" ; script = { [System.AppDomain]::CurrentDomain.GetAssemblies() | Select-Object -First 2 }} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't the Assembly array be pre-computed and passed in as a "value" parameter? Then you wouldn't need the special case for "script" parameter. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My thinking is that if we add new test cases here, we can reuse |
||
@{parameter = "Function" ; value = "foo","bar"}, | ||
@{parameter = "Cmdlet" ; value = "foo","bar"}, | ||
@{parameter = "Variable" ; value = "foo","bar"}, | ||
@{parameter = "Alias" ; value = "foo","bar"}, | ||
@{parameter = "Force" ; value = $true}, | ||
@{parameter = "Force" ; value = $false}, | ||
@{parameter = "PassThru" ; value = $true}, | ||
@{parameter = "PassThru" ; value = $false}, | ||
@{parameter = "AsCustomObject" ; value = $true}, | ||
@{parameter = "AsCustomObject" ; value = $false}, | ||
@{parameter = "MinimumVersion" ; value = "1.2.3"}, | ||
@{parameter = "MaximumVersion" ; value = "3.2.1"}, | ||
@{parameter = "RequiredVersion" ; value = "1.1.1"}, | ||
@{parameter = "ArgumentList" ; value = "hello","world"}, | ||
@{parameter = "DisableNameChecking"; value = $true}, | ||
@{parameter = "DisableNameChecking"; value = $false}, | ||
@{parameter = "NoClobber" ; value = $true}, | ||
@{parameter = "NoClobber" ; value = $false}, | ||
@{parameter = "Scope" ; value = "Local"}, | ||
@{parameter = "Scope" ; value = "Global"}, | ||
@{parameter = "PSSession" ; value = $pssession}, | ||
# @{parameter = "CimSession" ; value = $cimsession}, | ||
@{parameter = "CimResourceUri" ; value = "http://foo/"}, | ||
@{parameter = "CimNamespace" ; value = "foo"} | ||
) { | ||
param($parameter, $value, $script) | ||
|
||
$importModuleCommand = [Microsoft.PowerShell.Commands.ImportModuleCommand]::new() | ||
if ($script -ne $null) { | ||
$val 9E91 ue = & $script | ||
} | ||
$importModuleCommand.$parameter = $value | ||
if ($parameter -eq "FullyQualifiedName") { | ||
$importModuleCommand.FullyQualifiedName.Count | Should BeExactly 2 | ||
$importModuleCommand.FullyQualifiedName | Should BeOfType "Microsoft.PowerShell.Commands.ModuleSpecification" | ||
$importModuleCommand.FullyQualifiedName[0].Name | Should Be "foo" | ||
$importModuleCommand.FullyQualifiedName[0].RequiredVersion | Should Be "0.0" | ||
$importModuleCommand.FullyQualifiedName[1].Name | Should Be "bar" | ||
$importModuleCommand.FullyQualifiedName[1].RequiredVersion | Should Be "1.1" | ||
} else { | ||
$importModuleCommand.$parameter | Should BeExactly $value | ||
} | ||
} | ||
|
||
It "Import-Module can import over remote session: <test>" -TestCases @( | ||
@{ test = "pssession" ; parameters = @{Name="TestImport";PSSession=$pssession}}, | ||
# @{ test = "cimsession" ; parameters = @{Name="TestImport";CimSession=$cimsession}}, | ||
@{ test = "minimumversion" ; parameters = @{Name="TestImport";PSSession=$pssession;MinimumVersion="1.0";Force=$true}}, | ||
@{ test = "requiredversion" ; parameters = @{Name="TestImport";PSSession=$pssession;RequiredVersion="1.2.3"}}, | ||
@{ test = "maxiumversion" ; parameters = @{Name="TestImport";PSSession=$pssession;MaximumVersion="2.0"}}, | ||
@{ test = "invalid miniumversion" ; parameters = @{Name="TestImport";PSSession=$pssession;MinimumVersion="2.0"}; | ||
errorid = "Modules_ModuleWithVersionNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand,Microsoft.PowerShell.Commands.ImportModuleCommand"}, | ||
@{ test = "invalid maximumversion" ; parameters = @{Name="TestImport";PSSession=$pssession;MaximumVersion="1.0"}; | ||
errorid = "Modules_ModuleWithVersionNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand,Microsoft.PowerShell.Commands.ImportModuleCommand"}, | ||
@{ test = "fullyqualifiedname" ; parameters = @{FullyQualifiedName=@{Modulename="TestImport"; RequiredVersion="1.2.3"};PSSession=$pssession}} | ||
) { | ||
param ($test, $parameters, $errorid) | ||
|
||
Invoke-Command -Session $pssession -ScriptBlock { $env:PSModulePath += ";$(Split-Path $using:modulePath)"} | ||
Get-Module TestImport | Should BeNullOrEmpty | ||
if ($errorid) { | ||
{ Import-Module @parameters -ErrorAction Stop } | ShouldBeErrorId $errorid | ||
} else { | ||
Import-Module @parameters | ||
$module = Get-Module TestImport | ||
$module.Name | Should BeExactly "TestImport" | ||
|
||
# generated proxy module always uses 1.0 | ||
$module.Version | Should BeExactly "1.0" | ||
|
||
test-hello | Should BeExactly "world" | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more. 8000
I don't understand why the PSSession is added/reverted from the ArgumentList here. The remote session has already been imported (via the Export-PSSession Cmdlet) and is no longer needed. It looks like this can be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually this does need to be here. It is used to fill the PSSession property on the module info.