Pester icon indicating copy to clipboard operation
Pester copied to clipboard

Allow using 'RootModule/NestedModule' as ModuleName

Open fred-gagnon opened this issue 1 year ago • 9 comments

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

  • [X] PR has meaningful title
  • [X] Summary describes changes
  • [X] 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.
  • [X] Tests are added/update (if required)
  • [ ] Documentation is updated/added (if required)

fred-gagnon avatar Dec 11 '23 20:12 fred-gagnon

Thanks for the PR. Will review this soon. Related https://github.com/pester/Pester/issues/2235#issuecomment-1264617847

fflaten avatar Jan 02 '24 23:01 fflaten

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

Any idea when it will be reviewed ?

fred-gagnon avatar Mar 08 '24 13:03 fred-gagnon

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.

fflaten avatar Mar 08 '24 17:03 fflaten

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.

nohwnd avatar Apr 10 '24 21:04 nohwnd

@nohwnd The tests you requested have been added

fred-gagnon avatar Apr 11 '24 12:04 fred-gagnon

@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.

fflaten avatar Apr 11 '24 15:04 fflaten

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.

nohwnd avatar Apr 12 '24 20:04 nohwnd

@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.

fred-gagnon avatar Apr 12 '24 23:04 fred-gagnon

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: https://github.com/pester/Pester/issues/2235#issuecomment-1264617847 ), 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. 🙂

nohwnd avatar Apr 15 '24 07:04 nohwnd

Closing due to no activity on this PR, taking it to completion would require the implementation described above, if you want to commit to that please let me know.

nohwnd avatar Jul 02 '24 09:07 nohwnd