Version checki 8000 ng tests for Import-Module#7499
Conversation
|
Have marked as WIP while I add GUID tests |
|
Tests marked |
There was a problem hiding this comment.
I'll wait for some refactoring before reviewing in detail
| } | ||
| if ($Guid) | ||
| { | ||
| $mod.Guid | Should -Be $actualGuid |
There was a problem hiding this comment.
This assert is already handled by line 189 so this doesn't need to be here
| } | ||
| if ($Guid) | ||
| { | ||
| $mod.Guid | Should -Be $actualGuid |
There was a problem hiding this comment.
This is already handled by 217
| } | ||
| if ($Guid) | ||
| { | ||
| $mod.Guid | Should -Be $actualGuid |
| } | ||
| if ($Guid) | ||
| { | ||
| $mod.Guid | Should -Be $actualGuid |
| } | ||
| if ($Guid) | ||
| { | ||
| $mod.Guid | Should -Be $actualGuid |
| } | ||
| if ($Guid) | ||
| { | ||
| $mod.Guid | Should -Be $actualGuid |
| } | ||
| if ($Guid) | ||
| { | ||
| $mod.Guid | Should -Be $actualGuid |
| } | ||
| if ($Guid) | ||
| { | ||
| $mod.Guid | Should -Be $actualGuid |
|
|
||
| $mod = Import-Module -FullyQualifiedName $modSpec -PassThru | ||
|
|
||
| $mod.Name | Should -Be $moduleName |
There was a problem hiding this comment.
This validation code is very similar throughout this script. It seems you should have a Validate type function instead of the same code repeated
| if ($ModuleVersion -and $MaximumVersion -and ($ModuleVersion -ge $MaximumVersion)) | ||
| { | ||
| $sb | Should -Throw -ErrorId 'ArgumentOutOfRange,Microsoft.PowerShell.Commands.ImportModuleCommand' | ||
| return |
There was a problem hiding this comment.
why return instead of else?
There was a problem hiding this comment.
Given the choice I prefer if-return, because:
- The reader knows that they can stop reading when they see
return - Less indentation and less horizontal programming
- Closer to pattern match/guard style, which I find helps the "exhaust all possibilities" mentality
- The
returnworks as a forcing function to not juggle state
I see many instances in the code base that I imagine initially started as innocent if-elses that could have been if-returns, but whose complexity has since increased due to that choice:
PowerShell/src/System.Management.Automation/engine/Modules/ImportModuleCommand.cs
Lines 1698 to 1783 in 01634e4
PowerShell/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs
Lines 6351 to 6506 in 01634e4
PowerShell/src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs
Lines 3646 to 3715 in 01634e4
To my mind, using else just before the end of a function is a useless use of else.
| { | ||
| param( | ||
| $Module, | ||
| [string]$Name, |
There was a problem hiding this comment.
Does it make sense to default this to $moduleName, $guid to $actualGuid, and $version to $actualVersion since that is used by most (all?) of the tests?
There was a problem hiding this comment.
Yes I think that makes sense 👍
There was a problem hiding this comment.
Worth noting that the $moduleName one relies on dynamic scope. I kept the other parameters, since I think it's less confusing. But it's not quite consistent with $moduleName.
|
|
||
| if ($ModuleVersion -and $MaximumVersion -and ($ModuleVersion -ge $MaximumVersion)) | ||
| { | ||
| $sb | Should -Throw -ErrorId 'ArgumentOutOfRange,Microsoft.PowerShell.Commands.ImportModuleCommand' |
There was a problem hiding this comment.
If all the negative test cases result in a terminating error, it may be simpler to have the FQErrorId as part of the testcase variation so this would simply be
$sb | Should -Throw -ErrorId $ErrodId
There was a problem hiding this comment.
Just realised this second suggestion doesn't work just yet; Import-Module -MaximumVersion -MinimumVersion throws a different error to Import-Module @{ ModuleVersion = ; MaximumVersion = }.
That's what #7347 changes.
|
@SteveL-MSFT was the feedback addressed? |
|
Reopen the PR to restart stuck CIs |
|
@daxian-dbw this can be merged now. |
PR Summary
Adds a number of tests to check the version of the module to import against constraints given to the cmdlet.
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:to the beginning of the title and remove the prefix when the PR is ready.[feature]if the change is significant or affects feature testsThis adds testing for #7125