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

Make InModuleScope-parameters available as variables in scriptblock #1957

Merged
merged 8 commits into from May 24, 2021

Conversation

fflaten
Copy link
Collaborator

@fflaten fflaten commented May 23, 2021

InModuleScope currently requires param-block to make parameters available by name (if not, only available as arguments). This PR add parameters as dynamic variables inside the scriptblock similar to how it works with mocks.

Fix #1603

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 fflaten marked this pull request as ready for review May 23, 2021 23:39
@fflaten fflaten requested a review from nohwnd May 23, 2021 23:39
@fflaten
Copy link
Collaborator Author

fflaten commented May 23, 2021

@nohwnd Am I on the right track? I've yet to understand when to set sessionstate vs scriptblock hints, so I know they're wrong. Haven't used the debug-messages before, so not sure what I'd expect or what would be useful in this scenario.

@nohwnd
Copy link
Member

nohwnd commented May 24, 2021

LGTM, merge if you are done, or re-request review later :)

@fflaten
Copy link
Collaborator Author

fflaten commented May 24, 2021

Added another test and updated help with example. Good to go. I don't have permissions to merge with protected branch, so your turn 👍

@nohwnd nohwnd merged commit db92c5d into pester:v5.0 May 24, 2021
@fflaten fflaten deleted the inmodulescope-parameters branch May 24, 2021 14:28
@plastikfan
Copy link
Contributor

I've just tested this functionality on a test suite and am not getting the expected result. I am not sure though if I'm getting this right so it may be something I'm doing wrong. Should I raise a new issue? I've have a sample module that you can test this with.

Effectively, the old test code defines the InModuleScope statement as:

    ) {
      InModuleScope ims -Parameters @{ Scenario = $Scenario; Result = $Result; ExpectedSuccess = $ExpectedSuccess; } {
        param(
          [string]$Scenario,
          [object]$Result,
          [boolean]$ExpectedSuccess
        )

Where the template parameters are Scenario, Result and ExpectedSuccess.

I've changed this to become:

    ) {
      InModuleScope ims {

Please note that the binding error I'm currently seeing is with $Result:

[-] convert-ActionResult.given:  .should: return result with Success = True 22ms (21ms|1ms)
 RuntimeException: The variable '$Result' cannot be retrieved because it has not been set.
 at <ScriptBlock>, C:\Users\Plastikfan\dev\PoSh\ims\convert-ActionResult.tests.ps1:75

... and $Result can be a string or a PSCustomObject.

The whole module and test suite are available at ims

If you really need to raise a new issue, then let me know. I suspect that I'm not doing this the way as intended, so I'd like this to be checked first before I raise an issue.

Cheers,

@fflaten
Copy link
Collaborator Author

fflaten commented Aug 2, 2021

I've changed this to become: ) { InModuleScope ims {

For future readers; as mentioned in the discussion, this PR removes the requirement for the param-block only. You still need to specify -Parameters <hash table> to pass the values into the module state where the scriptblock is executed. Each key will be available as named variables inside the scriptblock.

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

Successfully merging this pull request may close these issues.

v5: InModuleScope fails to pass values from the current scope without explicit -Parameters
3 participants