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
Preference variable to suppress Mandatory prompts #2408
Comments
The PowerShell team has generally provided guidance that it's not important for cmdlet unit tests to test this behavior - a cmdlet unit test should simply verify that ((Get-Command Get-Command).Parameters['Name'].Attributes | ? { $_ -is [parameter] }).Mandatory | Should Be $false This way, you leave the responsibility of testing prompting behavior on |
I guess that kind of makes sense. I'll feed this back to the rest of my team. HelpMessage should be testable in the same way, right? I still argue that it would be a good idea to provide this capability, if only to cover situations where you can't control whether the powershell session has been launched with Thanks |
I think it's a good request. Here is a scenario. In a big non-interactive script, we currently hang, when such mistake slips in. Current behavior makes troubleshooting harder. It would be much better to have an option to error-out immediately when script knows that it's not intended for the interactive use. |
HelpMessage could be tested in the same way, but we don't generally test the exact message because we rely on code reviews to ensure the messsage is good plus localization makes the test only work on specific locales. I disagree that this functionality is needed. If PowerShell is not interactive, the parameter binder throws an exception if a mandatory parameter is missing - and this is exactly what you want - report an error, not to ignore it. |
Here is an example of a non-interactive runspace throwing an exception:
|
Indeed, then if somebody wants such check she can write a helper function to start script in non-interactive runspace. |
I'm not sure what that example was showing. It doesn't look like anything I'm trying to do. There's a disconnect here between something which is 'needed' (a poisonous word, IMHO) and something that seems like a good idea. This is definitely in the latter group, given the guidance @lzybkr posted above. In any case, you're the guys in charge so if you think it's not a good suggestion then we can close it. |
I wanted to demonstrate how the PowerShell team would test this behavior (non-interactive) in our tests. A new process works similarly but is much slower:
In my opinion, adding a preference variable to control this behavior is neither necessary nor a good idea. The variable introduces new complexity that needs to be documented and explained. I'm closing the issue as I believe I've suggested reasonable solutions to testing the desired behavior. If there are situations I haven't covered, feel free to reopen. |
Yeah I'm not sure the second example would achieve what we're after. We'll start looking into using the first example since right now we're not using the Mandatory parameter for a parameter which is mandatory. Instead, we're letting other validation handle that situation which is not necessarily ideal. Thanks for looking into it anyway |
For anyone else having this issue in case you use a
|
I'm writing tests for various scripts that have multiple parameter sets. The above checks for the Mandatory attribute on a parameter aren't quite what I need. For example: Param(
[Parameter(ParameterSetName = 'MatchName', Mandatory)][Parameter(ParameterSetName = 'MatchPattern', Mandatory)][SessionObject] $Session,
[Parameter(ParameterSetName = 'MatchName')][String] $Name,
[Parameter(ParameterSetName = 'MatchPattern', Mandatory)][String] $Pattern,
[Parameter(ParameterSetName = 'MatchPattern', Mandatory)][String] $MatchOn
) I want to be able to check that Pattern is passed if MatchOn is passed. If Name is passed, I don't care about that pattern match being mandatory. So I could write some logic that tries to emulate what PowerShell itself is doing, but it would seem more useful to be able to just run the command and see how PowerShell reacts (and without the risk of it sitting waiting for input in the case of a missing param). |
The ability to mark parameters as Mandatory is great, but there is one critical omission - the ability to suppress prompts for Mandatory variables for a session.
The main reason this is important is because of unit testing. We want to set certain parameters as Mandatory=$true and then unit test (using Pester) that the parameters are in fact enforced as mandatory.
Unfortunately, this actually causes Pester to pause waiting for input. Our temporary solution is to not use Mandatory.
What we'd like to do is set something like
$MandatoryPreference = 'Continue'
so that if we miss something, we get an actual error thrown backThe text was updated successfully, but these errors were encountered: