Skip to content

Commit

Permalink
Merge pull request #278 from dlwyatt/MultiModuleFix
Browse files Browse the repository at this point in the history
Proposed fix for #275
  • Loading branch information
dlwyatt committed Feb 5, 2015
2 parents 3aaad55 + e7052f6 commit 05b4594
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 21 deletions.
56 changes: 41 additions & 15 deletions Functions/InModuleScope.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
}
8 changes: 3 additions & 5 deletions Functions/Mock.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion Pester.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,4 @@ InModuleScope Pester {
}
}
}
}
}

0 comments on commit 05b4594

Please sign in to comment.