From e7052f6fdf58b5aa28663530682ef245444d8ffd Mon Sep 17 00:00:00 2001 From: Dave Wyatt Date: Wed, 4 Feb 2015 21:08:06 -0500 Subject: [PATCH] Proposed fix for #275 Both Mock -ModuleName, and InModuleScope, now share the same behavior with regard to module resolution: - The commands look specifically for Script modules. In order to work, there should be exactly one Script module with the given name loaded. If there is exactly one script module with the given name, any other non-script modules are ignored. - Under all other conditions, an appropriate error message is generated. --- Functions/InModuleScope.ps1 | 56 +++++++++++++++++++++++++++---------- Functions/Mock.ps1 | 8 ++---- Pester.Tests.ps1 | 2 +- 3 files changed, 45 insertions(+), 21 deletions(-) diff --git a/Functions/InModuleScope.ps1 b/Functions/InModuleScope.ps1 index 8c664dbd7..9c44e69ac 100644 --- a/Functions/InModuleScope.ps1 +++ b/Functions/InModuleScope.ps1 @@ -68,21 +68,7 @@ function InModuleScope $script:mockTable = @{} } - try - { - $modules = @(Get-Module -Name $ModuleName -All -ErrorAction Stop) - } - catch - { - throw "No module named '$ModuleName' is currently loaded." - } - - if ($modules.Count -gt 1) - { - throw "Multiple modules named '$ModuleName' are currently loaded. Make sure to remove any extra copies of the module from your session before testing." - } - - $module = $modules[0] + $module = Get-ScriptModule -ModuleName $ModuleName -ErrorAction Stop $originalState = $Pester.SessionState $originalScriptBlockScope = Get-ScriptBlockScope -ScriptBlock $ScriptBlock @@ -101,3 +87,43 @@ function InModuleScope Set-ScriptBlockScope -ScriptBlock $ScriptBlock -SessionStateInternal $originalScriptBlockScope } } + +function Get-ScriptModule +{ + [CmdletBinding()] + param ( + [Parameter(Mandatory = $true)] + [string] $ModuleName + ) + + try + { + $modules = @(Get-Module -Name $ModuleName -All -ErrorAction Stop) + } + catch + { + throw "No module named '$ModuleName' is currently loaded." + } + + $scriptModules = @($modules | Where-Object { $_.ModuleType -eq 'Script' }) + + if ($scriptModules.Count -gt 1) + { + throw "Multiple Script modules named '$ModuleName' are currently loaded. Make sure to remove any extra copies of the module from your session before testing." + } + + if ($scriptModules.Count -eq 0) + { + $actualTypes = @( + $modules | + Where-Object { $_.ModuleType -ne 'Script' } | + Select-Object -ExpandProperty ModuleType -Unique + ) + + $actualTypes = $actualTypes -join ', ' + + throw "Module '$ModuleName' is not a Script module. Detected modules of the following types: '$actualTypes'" + } + + return $scriptModules[0] +} diff --git a/Functions/Mock.ps1 b/Functions/Mock.ps1 index 2d3ee7723..1aad08afb 100644 --- a/Functions/Mock.ps1 +++ b/Functions/Mock.ps1 @@ -586,10 +586,8 @@ function Validate-Command([string]$CommandName, [string]$ModuleName) { } if ($ModuleName) { - $module = Microsoft.PowerShell.Core\Get-Module $ModuleName -All | - Sort ModuleType | - Where { ($origCommand = & $_ $scriptBlock $commandName) } | - Select -First 1 + $module = Get-ScriptModule -ModuleName $ModuleName -ErrorAction Stop + $origCommand = & $module $scriptBlock $CommandName } $session = $pester.SessionState @@ -604,7 +602,7 @@ function Validate-Command([string]$CommandName, [string]$ModuleName) { } if ($module) { - $session = & @($module)[0] { $ExecutionContext.SessionState } + $session = & $module { $ExecutionContext.SessionState } } @{Command = $origCommand; Session = $session} diff --git a/Pester.Tests.ps1 b/Pester.Tests.ps1 index 8befcf428..8395f302a 100644 --- a/Pester.Tests.ps1 +++ b/Pester.Tests.ps1 @@ -251,4 +251,4 @@ InModuleScope Pester { } } } -} \ No newline at end of file +}