Skip to content
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

Proposed fix for #275 #278

Merged
merged 1 commit into from
Feb 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
}
}
}
}
}