Last active
November 29, 2016 17:09
-
-
Save alx9r/7d3c220163bf55daca6fcc544c07ee27 to your computer and use it in GitHub Desktop.
Pester test demonstrating [Remove-Module;] Import-Module does not always reload modules.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$guid = '2282bfa0-cfdc-4ee5-a7a4-7aca5b33dc9f' | |
$workingFolderPath = "$([System.IO.Path]::GetTempPath())$guid" | |
$moduleContents = [ordered]@{ | |
NoClasses = @{ | |
Root = @' | |
Import-Module "$PSScriptRoot\Sub.psm1" -Args $Args | |
$passedArgs = $Args | |
function Get-PassedArgsRoot1 { $passedArgs } | |
function Get-PassedArgsRoot2 { $passedArgs } | |
'@ | |
Sub = @' | |
$passedArgs = $Args | |
function Get-PassedArgsSub { $passedArgs } | |
'@ | |
} | |
Classes = @{ | |
Root = @' | |
Import-Module "$PSScriptRoot\Sub.psm1" -Args $Args | |
$passedArgs = $Args | |
class Root { $passedArgs = $passedArgs } | |
function Get-PassedArgsRoot1 { $passedArgs } | |
function Get-PassedArgsRoot2 { [Root]::new().passedArgs } | |
'@ | |
Sub = @' | |
$passedArgs = $Args | |
class Sub { $passedArgs = $passedArgs } | |
function Get-PassedArgsSub { [Sub]::new().passedArgs } | |
'@ | |
} | |
} | |
Describe 'Remove Modules' { | |
foreach ( $module in (Get-Module | ? {$_.Path -match $guid}) ) | |
{ | |
Context $module.Name { | |
It 'force remove' { | |
Remove-Module $module.Name -ea Stop | |
} | |
It 'confirm removed' { | |
Get-Module $module.Name -ea Stop | | |
Should beNullOrEmpty | |
} | |
It 'remove folder' { | |
$module.ModuleBase | Remove-Item -Recurse -Force -ea Stop | |
} | |
It 'confirm removed' { | |
Test-Path $module.ModuleBase -ea Stop | | |
Should be $false | |
} | |
} | |
} | |
} | |
foreach | |
( | |
$reloadMethod in @( | |
'ForceImport','RemoveThenImport','ForceRemoveThenImport', | |
'ForceRemoveTouchRootThenImport' | |
'ForceRemoveTouchSubThenImport' | |
'ForceRemoveAppendNewLineRootThenImport' | |
'ForceRemoveAppendNewLineSubThenImport' | |
'ForceRemoveAppendNewLineBothThenImport' | |
) | |
) | |
{ | |
foreach ( $contentsName in $moduleContents.Keys ) | |
{ | |
$moduleName = "DifferentArgs_$contentsName`_$reloadMethod" | |
Describe "$moduleName" { | |
$rootModuleFolderPath = "$workingFolderPath\$moduleName" | |
Context 'set up the module files' { | |
It 'create the root module folder' { | |
New-Item $rootModuleFolderPath -ItemType Container -Force | |
} | |
It 'create the root module file' { | |
$moduleContents.$contentsName.Root | Set-Content "$rootModuleFolderPath\$moduleName.psm1" -Force -ea Stop | |
} | |
It 'create the sub module file' { | |
$moduleContents.$contentsName.Sub | Set-Content "$rootModuleFolderPath\Sub.psm1" | |
} | |
} | |
Context "import the module with argument value 1 and test" { | |
It 'import the module' { | |
Import-Module $rootModuleFolderPath -Args 'value 1' -Force -PassThru -ea Stop | |
} | |
It "the root module returns value 1 (Get-PassedArgsRoot1)" { | |
$r = & "$moduleName\Get-PassedArgsRoot1" | |
$r | Should be 'value 1' | |
} | |
It "the root module returns value 1 (Get-PassedArgsRoot2)" { | |
$r = & "$moduleName\Get-PassedArgsRoot2" | |
$r | Should be 'value 1' | |
} | |
It "the sub module returns value 1" { | |
$r = & "$moduleName\Get-PassedArgsSub" | |
$r | Should be 'value 1' | |
} | |
} | |
Context "reload the module with argument value 2 and test" { | |
It "$reloadMethod the module" @{ | |
'ForceImport' = { | |
Import-Module $rootModuleFolderPath -Args 'value 2' -Force -ea Stop | |
} | |
'RemoveThenImport' = { | |
Remove-Module $moduleName -ea Stop | |
Import-Module $rootModuleFolderPath -Args 'value 2' -ea Stop | |
} | |
'ForceRemoveThenImport' = { | |
Remove-Module $moduleName -Force -ea Stop | |
Import-Module $rootModuleFolderPath -Args 'value 2' -ea Stop | |
} | |
'ForceRemoveTouchRootThenImport' = { | |
Remove-Module $moduleName -Force -ea Stop | |
(Get-Item "$rootModuleFolderPath\$moduleName.psm1" -ea Stop).LastWriteTime = Get-Date | |
Import-Module $rootModuleFolderPath -Args 'value 2' -ea Stop | |
} | |
'ForceRemoveTouchSubThenImport' = { | |
Remove-Module $moduleName -Force -ea Stop | |
(Get-Item "$rootModuleFolderPath\Sub.psm1" -ea Stop).LastWriteTime = Get-Date | |
Import-Module $rootModuleFolderPath -Args 'value 2' -ea Stop | |
} | |
'ForceRemoveAppendNewLineRootThenImport' = { | |
Remove-Module $moduleName -Force -ea Stop | |
$c = Get-Content "$rootModuleFolderPath\$moduleName.psm1" -ea Stop | |
$c+"`n`r" | | |
Set-Content "$rootModuleFolderPath\$moduleName.psm1" -ea Stop | |
Import-Module $rootModuleFolderPath -Args 'value 2' -ea Stop | |
} | |
'ForceRemoveAppendNewLineSubThenImport' = { | |
Remove-Module $moduleName -Force -ea Stop | |
$c = Get-Content "$rootModuleFolderPath\Sub.psm1" -ea Stop | |
$c+"`n`r" | | |
Set-Content "$rootModuleFolderPath\Sub.psm1" -ea Stop | |
Import-Module $rootModuleFolderPath -Args 'value 2' -ea Stop | |
} | |
'ForceRemoveAppendNewLineBothThenImport' = { | |
Remove-Module $moduleName -Force -ea Stop | |
$c = Get-Content "$rootModuleFolderPath\$moduleName.psm1" -ea Stop | |
$c+"`n`r" | | |
Set-Content "$rootModuleFolderPath\$moduleName.psm1" -ea Stop | |
$c = Get-Content "$rootModuleFolderPath\Sub.psm1" -ea Stop | |
$c+"`n`r" | | |
Set-Content "$rootModuleFolderPath\Sub.psm1" -ea Stop | |
Import-Module $rootModuleFolderPath -Args 'value 2' -ea Stop | |
} | |
}.$reloadMethod | |
It "the root module returns value 2 (Get-PassedArgsRoot1)" { | |
$r = & "$moduleName\Get-PassedArgsRoot1" | |
$r | Should be 'value 2' | |
} | |
It "the root module returns value 2 (Get-PassedArgsRoot2)" { | |
$r = & "$moduleName\Get-PassedArgsRoot2" | |
$r | Should be 'value 2' | |
} | |
It "the sub module returns value 2" { | |
$r = & "$moduleName\Get-PassedArgsSub" | |
$r | Should be 'value 2' | |
} | |
} | |
} | |
} | |
} | |
$moduleContents = [ordered]@{ | |
NoClasses = @{ | |
RootContent1 = @' | |
Import-Module "$PSScriptRoot\Sub.psm1" | |
function Get-ValueRoot { 'value 1' } | |
'@ | |
RootContent2 = @' | |
Import-Module "$PSScriptRoot\Sub.psm1" | |
function Get-ValueRoot { 'value 2' } | |
'@ | |
SubContent1 = @' | |
function Get-ValueSub { 'value 1' } | |
'@ | |
SubContent2 = @' | |
function Get-ValueSub { 'value 2' } | |
'@ | |
} | |
Classes = @{ | |
RootContent1 = @' | |
Import-Module "$PSScriptRoot\Sub.psm1" | |
class Root { $p = 'value 1' } | |
function Get-ValueRoot { [Root]::new().p } | |
'@ | |
RootContent2 = @' | |
Import-Module "$PSScriptRoot\Sub.psm1" | |
class Root { $p = 'value 2' } | |
function Get-ValueRoot { [Root]::new().p } | |
'@ | |
SubContent1 = @' | |
class Sub { $p = 'value 1' } | |
function Get-ValueSub { [Sub]::new().p } | |
'@ | |
SubContent2 = @' | |
class Sub { $p = 'value 2' } | |
function Get-ValueSub { [Sub]::new().p } | |
'@ | |
} | |
} | |
foreach ( $reloadMethod in 'ForceImport','RemoveThenImport','ForceRemoveThenImport' ) | |
{ | |
foreach ( $contentsName in $moduleContents.Keys ) | |
{ | |
$tests = @( | |
# | | Expected Values | | |
# | modulePrefix| changeRoot | changeSub | rootBefore | rootAfter | subBefore | subAfter | | |
@( 'ChangeBoth', $true, $true, 'value 1', 'value 2', 'value 1', 'value 2' ), | |
@( 'ChangeSub', $false, $true, 'value 1', 'value 1', 'value 1', 'value 2' ), | |
@( 'ChangeRoot', $true, $false, 'value 1', 'value 2', 'value 1', 'value 1' ) | |
) | |
foreach ( $values in $tests ) | |
{ | |
$modulePrefix,$changeRoot,$changeSub,$rootBefore,$rootAfter,$subBefore,$subAfter = $values | |
$moduleName = "$modulePrefix`_$contentsName`_$reloadMethod" | |
Describe "reloading modules after changes - $moduleName" { | |
$rootModuleFolderPath = "$workingFolderPath\$moduleName" | |
Context 'set up the module files' { | |
It 'create the root module folder' { | |
New-Item $rootModuleFolderPath -ItemType Container -Force | |
} | |
It 'create the root module file' { | |
$moduleContents.$contentsName.RootContent1 | Set-Content "$rootModuleFolderPath\$moduleName.psm1" -Force -ea Stop | |
} | |
It 'create the sub module file' { | |
$moduleContents.$contentsName.SubContent1 | Set-Content "$rootModuleFolderPath\Sub.psm1" | |
} | |
} | |
Context 'import the module and test' { | |
It 'import the module' { | |
Import-Module $rootModuleFolderPath -Force -ea Stop | |
} | |
It "root returns $rootBefore" { | |
$r = Get-ValueRoot | |
$r | Should be $rootBefore | |
} | |
It "sub returns $subBefore" { | |
$r = Get-ValueSub | |
$r | Should be $subBefore | |
} | |
} | |
Context 'change contents' { | |
if ( $changeRoot ) | |
{ | |
It 'overwrite the root module file' { | |
$moduleContents.$contentsName.RootContent2 | | |
Set-Content "$rootModuleFolderPath\$moduleName.psm1" -Force -ea Stop | |
} | |
} | |
if ( $changeSub ) | |
{ | |
It 'overwrite the sub module file' { | |
$moduleContents.$contentsName.SubContent2 | | |
Set-Content "$rootModuleFolderPath\Sub.psm1" -Force -ea Stop | |
} | |
} | |
} | |
Context 're-import the module and test' { | |
It "$reloadMethod the module" @{ | |
'ForceImport' = { | |
Import-Module $rootModuleFolderPath -Force -ea Stop | |
} | |
'RemoveThenImport' = { | |
Remove-Module $moduleName -ea Stop | |
Import-Module $rootModuleFolderPath -ea Stop | |
} | |
'ForceRemoveThenImport' = { | |
Remove-Module $moduleName -Force -ea Stop | |
Import-Module $rootModuleFolderPath -ea Stop | |
} | |
}.$reloadMethod | |
It "root returns $rootAfter" { | |
$r = Get-ValueRoot | |
$r | Should be $rootAfter | |
} | |
It "sub returns $subAfter" { | |
$r = Get-ValueSub | |
$r | Should be $subAfter | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment