Skip to content

Refactor module version/GUID comparison logic#7125

Merged
adityapatwardhan merged 6 commits into
PowerShell:masterfrom
rjmholt:refactor-module-version-check
Nov 1, 2018
Merged

Refactor module version/GUID comparison logic#7125
adityapatwardhan merged 6 commits into
PowerShell:masterfrom
rjmholt:refactor-module-version-check

Conversation

@rjmholt

@rjmholt rjmholt commented Jun 21, 2018

Copy link
Copy Markdown
Collaborator

PR Summary

TLDR: Factors out module GUID/version checking logic to all use the same code path.

This PR routes all of the module GUID/version checking through ModuleIntrinsics.IsModuleMatchingSpec() (or a sub-method of it), so that all the version checking is consistent and DRY.

Previously the code had a number of 6-line if-conditions and cascading if-elses that all check module versioning in a slightly different way. I've tried to identify the common logic and unify it all.

Breaking Change Summary

Fix #7496
Previous to this PR, having a module loaded with one version, meant that importing a different version of that module would give you the (wrong) loaded version. This change fixes the version check so that the loaded module is only used when it is the correct version. See #7496.

Tests

Tests for this PR were already added in #7499. This PR just removes the -Pending flags from the tests around the breaking change (on the basis that the previous behaviour was a bug).

Fixes #7496.

PR Checklist


This change is Reviewable

@rjmholt rjmholt changed the title Refactor module version/GUID comparison logic WIP: Refactor module version/GUID comparison logic Jun 21, 2018
@rjmholt rjmholt added WG-Cmdlets-Core cmdlets in the Microsoft.PowerShell.Core module Issue-Code Cleanup the issue is for cleaning up the code with no impact on functionality labels Jun 21, 2018
@rjmholt rjmholt changed the title WIP: Refactor module version/GUID comparison logic Refactor module version/GUID comparison logic Jun 21, 2018
@iSazonov

Copy link
Copy Markdown
Collaborator

Also, if our testing coverage on this should be improved, I'm happy to add tests to this PR

To make a quality check of this change we are forced to do a lot of tests locally. So it makes sense to start by reviewing and extending the tests in the repo. I see that we don't actually have the tests for this code. I think we could add the new tests in new separate PR and then continue the PR. The Import-Module.Tests.ps1 file is already large so we could create new Import-Module-Versions.Tests.ps1.

@rjmholt

rjmholt commented Jun 21, 2018

Copy link
Copy Markdown
Collaborator Author

Ah so there is an Import-Module.Tests.ps1? It’s not under /test/powershell/engine/Module like I expected. Where is it?

Yes agreed we should bulk up the tests in another PR to merge before this one. I’ve been thinking about doing that but now I know there’s a test file I will see what we already have...

@iSazonov

Copy link
Copy Markdown
Collaborator

See test\powershell\Modules\Microsoft.PowerShell.Core\ subdirectory.

@daxian-dbw daxian-dbw self-assigned this Jul 2, 2018
@anmenaga

Copy link
Copy Markdown

@rjmholt It would be good to see a green [Feature] test pass result. Can you please run it again?

@anmenaga

Copy link
Copy Markdown

@daxian-dbw @BrucePay We need to get some traction on this; this PR is sitting without a review for more than a month now...

@rjmholt

rjmholt commented Jul 30, 2018

Copy link
Copy Markdown
Collaborator Author

I need to write tests for this I would say. Will mark as WIP.

@rjmholt rjmholt changed the title Refactor module version/GUID comparison logic WIP: Refactor module version/GUID comparison logic Jul 30, 2018
@rjmholt rjmholt added the Breaking-Change breaking change that may affect users label Aug 10, 2018
@stale

stale Bot commented Sep 9, 2018

Copy link
Copy Markdown

This PR has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed if no further activity occurs within 10 days.
Thank you for your contributions.
Community members are welcome to grab these works.

@stale stale Bot added the Stale label Sep 9, 2018
@iSazonov

Copy link
Copy Markdown
Collaborator

@rjmholt Please resolve the merge conflict.

@stale stale Bot removed the Stale label Sep 10, 2018
@rjmholt rjmholt force-pushed the refactor-module-version-check branch from 3467201 to e56b1b4 Compare September 28, 2018 22:28
@rjmholt rjmholt changed the title WIP: Refactor module version/GUID comparison logic Refactor module version/GUID comparison logic Sep 28, 2018
@rjmholt

rjmholt commented Sep 28, 2018

Copy link
Copy Markdown
Collaborator Author

I have now rebased this to include the 774 tests I wrote

@rjmholt rjmholt removed the Breaking-Change breaking change that may affect users label Sep 28, 2018
@rjmholt

rjmholt commented Sep 29, 2018

Copy link
Copy Markdown
Collaborator Author

I've removed the breaking change tag since I think another PR has already made the relevant change. This PR changes no tests and passes all existing.

This is wrong. That test PR skips those tests. This PR should enable them.

EDIT: Tests are now enabled, and pass.

@rjmholt rjmholt added the Breaking-Change breaking change that may affect users label Oct 10, 2018
@iSazonov

Copy link
Copy Markdown
Collaborator

Fix for CI is in #7987

@daxian-dbw daxian-dbw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM except for the comment about the optional parameters.

Comment thread src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs
@iSazonov

Copy link
Copy Markdown
Collaborator

I'd expect that we add new tests because of "breaking change".

@daxian-dbw

Copy link
Copy Markdown
Member

@rjmholt Can you please summarize what the breaking change is in the PR description?

@rjmholt

rjmholt commented Oct 12, 2018

Copy link
Copy Markdown
Collaborator Author

@iSazonov Tests were added in #7499. I added the tests for the new behaviour then and have removed the -Pending flag in this PR.

@daxian-dbw daxian-dbw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for revisit some of the optional parameters.

Comment thread src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs
Comment thread src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs
/// </summary>
/// <param name="alreadyLoadedModule">The already loaded module that matched the name of the module to load.</param>
/// <returns>True if the pre-loaded module matches all GUID and version constraints provided, false otherwise.</returns>
internal bool IsModuleAlreadyLoaded(PSModuleInfo alreadyLoadedModule)

@iSazonov iSazonov Oct 12, 2018

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems the name should be IsAlreadyLoadedModuleMatchingConstraints. The module description should be corrected too.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method has been in PowerShell with this name (and with this behaviour) since at least Win8. I would be happy to change it, so long as it's not going to cause any issues.

Comment thread src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs
@anmenaga

Copy link
Copy Markdown

@rjmholt Please address remaining couple of minor issues that Ilya pointed out and this will be ready.

@anmenaga

Copy link
Copy Markdown

@adityapatwardhan this looks ready.

@iSazonov

Copy link
Copy Markdown
Collaborator

@rjmholt Please address two comments.

@adityapatwardhan

Copy link
Copy Markdown
Member

@rjmholt Please address comments from @iSazonov. After that, is seems ready to merge.

@rjmholt

rjmholt commented Oct 30, 2018

Copy link
Copy Markdown
Collaborator Author

@adityapatwardhan, @iSazonov Feedback addressed -- let me know if any further changes are required.

@adityapatwardhan

adityapatwardhan commented Oct 30, 2018

Copy link
Copy Markdown
Member

@rjmholt please have a look at failing CI.

@adityapatwardhan

Copy link
Copy Markdown
Member

@SteveL-MSFT The newly added test is failing. It is marked as Feature and Feature tests were not run when the PR got merged.

@SteveL-MSFT

Copy link
Copy Markdown
Member

Looking

@SteveL-MSFT

SteveL-MSFT commented Oct 31, 2018

Copy link
Copy Markdown
Member

Test fix #8152

@iSazonov

Copy link
Copy Markdown
Collaborator

@rjmholt Please look CodeFactor issues and fix if they in your code.

@adityapatwardhan adityapatwardhan merged commit 5d06fba into PowerShell:master Nov 1, 2018
@iSazonov

iSazonov commented Nov 2, 2018

Copy link
Copy Markdown
Collaborator

@adityapatwardhan @TravisEz13 Could you please reword the merged commit? [Breaking change] was skipped in description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Breaking-Change breaking change that may affect users Issue-Code Cleanup the issue is for cleaning up the code with no impact on functionality WG-Cmdlets-Core cmdlets in the Microsoft.PowerShell.Core module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Import-Module on path ignores version constraints

7 participants