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

Allow using 'RootModule/NestedModule' as ModuleName #2412

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@
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'] })

Check notice

Code scanning / PSScriptAnalyzer

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`. Note

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`.

Check notice

Code scanning / PSScriptAnalyzer

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`. Note

The built-in *-Object-cmdlets are slow compared to alternatives in .NET. To fix a violation of this rule, consider using an alternative like foreach/for-keyword etc.`.
}
else {
$modules = @(& $SafeCommands['Get-Module'] -Name $ModuleName -All -ErrorAction Stop)
}
}
catch {
throw "No modules named '$ModuleName' are currently loaded."
Expand Down
45 changes: 44 additions & 1 deletion tst/functions/InModuleScope.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,49 @@ 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
'[string]$Script:ModuleVar = "Foo"' | Out-File -FilePath "TestDrive:/$nestedModuleName.psm1" -Encoding ascii
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 when using a slash as delimiter' {
$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'
}

It 'should return a single ModuleInfo object when using a backslash as delimiter' {
$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'
}

It 'should return the module session state name' {
$name = InModuleScope -ModuleName 'RootWithNestedModule\NestedModule' -ScriptBlock { $ExecutionContext.SessionState.Module.Name }
$name | Should -Be 'NestedModule'
}

It 'should return a nested module variable value' {
InModuleScope -ModuleName 'RootWithNestedModule\NestedModule' -ScriptBlock {
$Script:ModuleVar | Should -Be 'Foo'
}
}
}

}

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