Skip to content

Commit

Permalink
Allow using 'RootModule/NestedModule' as ModuleName
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederic Gagnon committed Dec 11, 2023
1 parent acc66a9 commit 8623d0a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/functions/InModuleScope.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,12 @@ function Get-CompatibleModule {
if ($PesterPreference.Debug.WriteDebugMessages.Value) {
Write-PesterDebugMessage -Scope Runtime "Searching for a module $ModuleName."
}
$modules = @(& $SafeCommands['Get-Module'] -Name $ModuleName -All -ErrorAction Stop)
if ($ModuleName -match '(?<RootModule>\w+)/(?<NestedModule>\w+)') {
$modules = @(& $SafeCommands['Get-Module'] -Name $Matches['RootModule'] -All -ErrorAction Stop | & $SafeCommands['Select-Object'] -ExpandProperty NestedModules | & $SafeCommands['Where-Object'] { $_.Name -eq $Matches['NestedModule'] })
}
else {
$modules = @(& $SafeCommands['Get-Module'] -Name $ModuleName -All -ErrorAction Stop)
}
}
catch {
throw "No modules named '$ModuleName' are currently loaded."
Expand Down
25 changes: 24 additions & 1 deletion tst/functions/InModuleScope.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,29 @@ Describe 'Get-CompatibleModule' {
}
}

Context 'when module name matches a root and a nested module' {
BeforeAll {
$nestedModuleName = 'NestedModule'
$moduleName = 'RootWithNestedModule'
$moduleManifestPath = "TestDrive:/$moduleName.psd1"
New-Item -Path "TestDrive:/$nestedModuleName.psm1" -ItemType File -Force -ErrorAction Stop | Out-Null
New-ModuleManifest -Path $moduleManifestPath -NestedModules ".\$nestedModuleName.psm1"
Import-Module $moduleManifestPath -Force
}

AfterAll {
Get-Module $moduleName -ErrorAction SilentlyContinue | Remove-Module -Force
}

It 'should return a single ModuleInfo object' {
$moduleInfo = InPesterModuleScope { Get-CompatibleModule -ModuleName 'RootWithNestedModule/NestedModule' }
$moduleInfo | Should -Not -BeNullOrEmpty
@($moduleInfo).Count | Should -Be 1
$moduleInfo.Name | Should -Be 'NestedModule'
$moduleInfo.ModuleType | Should -Be 'Script'
}
}

}

Describe 'InModuleScope arguments and parameter binding' {
Expand Down Expand Up @@ -340,4 +363,4 @@ Describe 'Working with manifest modules' {
$res = InModuleScope -ModuleName $moduleName -ScriptBlock { myPrivateFunction }
$res | Should -Be 'real'
}
}
}

0 comments on commit 8623d0a

Please sign in to comment.