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

Conversation

fred-gagnon
Copy link

@fred-gagnon fred-gagnon commented Dec 11, 2023

PR Summary

Updated function Get-CompatibleModule to allow specifying a module name as RootModule/NestedModule. This change solves an issue where multiple modules with a similarly named nested module cause an exception Multiple script or manifest modules named... to be thrown when attempting to create a mock in a nested module.

PR Checklist

  • PR has meaningful title
  • Summary describes changes
  • PR is ready to be merged
    • If not, use the arrow next to Create Pull Request to mark it as a draft. PR can be marked Ready for review when it's ready.
  • Tests are added/update (if required)
  • Documentation is updated/added (if required)

@fflaten
Copy link
Collaborator

fflaten commented Jan 2, 2024

Thanks for the PR. Will review this soon.
Related #2235 (comment)

@fflaten fflaten added the Feature label Jan 2, 2024
@fred-gagnon
Copy link
Author

Thanks for the PR. Will review this soon. Related #2235 (comment)

Any idea when it will be reviewed ?

@fflaten
Copy link
Collaborator

fflaten commented Mar 8, 2024

Sorry for the delay. I've been fully occupied at work for a while but will hopefully get some time for OSS this month.

I'm also waiting on @nohwnd to return (find time). He'll need to eventually merge and release the pending PRs once approved.

@nohwnd
Copy link
Member

nohwnd commented Apr 10, 2024

Can you please add a test that uses InModuleScope with this syntax and proves that we ended up in the correct context? E.g. by checking $ExecutionContext.SessionState.Module.Name or a value of a variable.

This would also serve as an example usage.

@fred-gagnon
Copy link
Author

@nohwnd The tests you requested have been added

@fflaten
Copy link
Collaborator

fflaten commented Apr 11, 2024

@nohwnd Won't this also allow mocking, while cleanup will fail?

Haven't had the time to test it yet, but that's a concern I had.

@nohwnd
Copy link
Member

nohwnd commented Apr 12, 2024

Looks like it would impact Mock, because the function is common. @fred-gagnon can you please allow this only from InModuleScope (e.g. by bool parameter that needs to be provided)? I am not sure what the impact is on mocking, and I won't be able to deeply investigate this any time soon.

@@ -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.`.
@@ -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.`.
@fred-gagnon
Copy link
Author

@nohwnd The whole point of this PR is for use-cases I have where I need to inject a Mock in a sub-module and there is more than one sub-modules currently loaded which have the same name, for example when 2 REST clients are simultaneously loaded and each one has a Repository sub-module. If I am to limit the use of this change only for InModuleScope, then it is pointless to keep it open. Unless I can use Mock from within InModuleScope? I’ve never tried that and won’t be able to for a few days.

@nohwnd
Copy link
Member

nohwnd commented Apr 15, 2024

I don't know why I was under the impression that this is for InModuleScope, sorry.

To allow this change for Mocking you need to add ton of tests mocks. I am not trying to be difficult, but we rely on simple name to figure out location of mocks. We also don't inspect child modules to clean them etc. So basically no-one knows what the change would cause (see comment: #2235 (comment) ), and your scenario is so rare that it does not seem worth it to break the usage for everyone else.

If you want to take it all the way through then I am okay with it, but it won't be easy. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants