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

Add missing container-parameters to Data #1986

Merged
merged 13 commits into from Jun 23, 2021

Conversation

fflaten
Copy link
Collaborator

@fflaten fflaten commented Jun 11, 2021

PR Summary

Pester containers may use parameters to support data-driven test generation.

This scenario currently has the following limitations:

  • Default values for parameters are available as variables in Discovery, but not in Run (test execution) unless passed using tricks like Describe 'd1' -ForEach { myparam = $myparam}. This makes debugging harder since Debug Tests in VSCode doesn't allow the user to provide -Data ... for the container (file).
  • Default values are also not available in *All/*Each setup-blocks placed outside any Describe/Context-blocks. The Describe -ForEach ..-workaround above does not work for these root-scoped setup-blocks.
  • Discovery and Run are inconsistent when using aliases for script parameters. Given a container with parameter "MyParam" which has a alias of "MyAlias". When invoked with New-PesterContainer ... -Data @{ MyAlias = 123 }:
    • In Discovery: $MyParam would be 123 and $MyAlias would not exist. This is default PowerShell-behavior.
    • In Run: $MyParam would not exist and $MyAlias would be 123. This is because Pester adds uses the provided -Data directly during Run.

This PR adds logic to detect container-parameters not defined by the user at runtime and automatically adds them to Data for the Root-block of the container. New behavior:

  • Script-parameters are always available as variables in Discovery and Run. Has default value (null or value set in Param-block) if not provided by the user.
  • Default values are available in root-scoped setup blocks.
  • $MyParam (see example above) is also defined in Run like it is in Discovery to be more consistent and similar to normal PowerShell-function/script behavior.

Fix #1980

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)

src/Pester.Runtime.ps1 Outdated Show resolved Hide resolved
@fflaten fflaten changed the title Use default value for undefined container parameters Add missing container-parameters to Data Jun 14, 2021
@fflaten fflaten marked this pull request as ready for review June 14, 2021 20:39
@fflaten fflaten requested a review from nohwnd June 14, 2021 20:40
src/Pester.Runtime.ps1 Outdated Show resolved Hide resolved
@fflaten
Copy link
Collaborator Author

fflaten commented Jun 15, 2021

I think this is ready for review. Wondered if it should have some tests when using InModuleScope .. -Parameters ... outside the blocks etc, but I'm not really sure how that's supposed to work even before this. Couldn't really pass any Data to run-phase, but that's probably user-error. I try to stay away from InModuleScope outside *All/*Each/It.

@nohwnd
Copy link
Member

nohwnd commented Jun 23, 2021

I am not sure how that should work either. 😬

@nohwnd nohwnd merged commit 570d3b6 into pester:main Jun 23, 2021
@fflaten fflaten deleted the default-parametervalues branch June 24, 2021 10:13
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.

Default parameter values are not applied in the test script
3 participants