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

Fixed ScriptsToProcess when -Version param is used in Import-Module #3897

Merged
merged 3 commits into from Jun 5, 2017

Conversation

anmenaga
Copy link
Contributor

This is fix for #3738 "ScriptsToProcess not honored when Import-Module is passed -Version param".

The bug was cased by existing check in LoadModule functions:

If MinimumVersion/RequiredVersion/MaximumVersion has been specified, then only try to process manifest modules...
If the -Version flag was specified, don't look for non-manifest modules.

When loading usual module files, this check is valid;
However, same LoadModule function is used to load ScriptsToProcess - and this when this check was
triggered and ScriptsToProcess were not executed.

The fix is to work around that condition for ScriptsToProcess in the same way it is currently done for NestedModules - temporary reset the version info for the duration of processing ScriptsToProcess / NestedModules.

Test results before the fix:
befoerfix

Test results after the fix:
afterfix

manifestInfo.AddDetectedCmdletExport(detectedCmdlet);
}
bool found = false;
PSModuleInfo module = LoadModule(scriptFile,
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is actually existing code; I didn't change it; looks like Github's diff'er has room for improvement :) - see picture below.
Anyway, there are multiple LoadModule calls like this one throughout the file, so if refactoring is to be done - it has to be done to all of them - and that is out of scope of this bugfix.

diff

}
foreach (string detectedCmdlet in module.ExportedCmdlets.Keys)
{
manifestInfo.AddDetectedCmdletExport(detectedCmdlet);
Copy link
Member

Choose a reason for hiding this comment

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

AddDetected* methods from PSModuleInfo in most cases seem to be called multiple times using a for loop. Will it make sense to add an overload which accepts IEnumerable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe. But as previous comment - this would be refactoring of unrelated code - out of scope of this bug fix.

Import-Module .\module2.psd1 -Version 1.0
Get-Content out.txt | Should Be '21'
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Add new line at the end.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.


AfterAll {
Pop-Location
#Remove-Item $moduleRootPath -Recurse -Force -ErrorAction SilentlyContinue
Copy link
Member

Choose a reason for hiding this comment

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

Remove commented line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

Remove-Item out.txt -Force -ErrorAction SilentlyContinue
}

It "Verify ScriptsToProcess are executed for top-level module" {
Copy link
Member

Choose a reason for hiding this comment

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

You may want to take a look at TestCases parameter of Pester It.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for suggestion; I did give it a try. I believe that semantic difference between these tests is larger than what TestCases parameter is aimed for; plus I don't like that TestCases parameter has worse logging (same test name repeated several times vs individual, descriptive test names in case of individual 'It's).

Copy link
Member

Choose a reason for hiding this comment

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

You can have custom test case names for individual test cases. Have look at: https://github.com/PowerShell/PowerShell/blob/02b5f357a20e6dee9f8e60e3adb9025be3c94490/test/powershell/engine/ParameterBinding/NullableBooleanDCR.Tests.ps1

Here the parameters will be parameter splat for Import-Module, and expected result, test name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is actually cool; thank you!
I've updated tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea; I've updated tests.

BeforeAll {
$moduleRootPath = Join-Path $TestDrive 'TestModules'
New-Item $moduleRootPath -ItemType Directory -Force | Out-Null
Push-Location $moduleRootPath
Copy link
Member

Choose a reason for hiding this comment

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

Why do you need to push location? Can you use full paths for all cmdlets like New-Item, New-ModuleManifest etc?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I chose push location specifically because I didn't want to deal with full paths. :)

Remove-Item out.txt -Force -ErrorAction SilentlyContinue
}

It "Verify ScriptsToProcess are executed for top-level module" {
Copy link
Member

Choose a reason for hiding this comment

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

You can have custom test case names for individual test cases. Have look at: https://github.com/PowerShell/PowerShell/blob/02b5f357a20e6dee9f8e60e3adb9025be3c94490/test/powershell/engine/ParameterBinding/NullableBooleanDCR.Tests.ps1

Here the parameters will be parameter splat for Import-Module, and expected result, test name.

@daxian-dbw daxian-dbw merged commit 28ec9a3 into PowerShell:master Jun 5, 2017
@anmenaga anmenaga deleted the ScriptsToProcessVersionFix branch October 31, 2018 21:20
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.

None yet

4 participants