-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Test-ModuleManifest should not load unnecessary modules #4541
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
Conversation
69f192f
to
5b61238
Compare
/********************************************************************++ | ||
Copyright (c) Microsoft Corporation. All rights reserved. | ||
--********************************************************************/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something is wrong with your change in this file. The diff is messed up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix the change in TestModuleManifestCommand.cs
@daxian-dbw code file format fixed |
@chunqingchen Thanks! Will continue the review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test assert folder testmodulerunspace
should be moved to engine\Module\assets
folder. Create assets
folder if it doesn't exist.
All test assets should follow this pattern, take a look at the test\powershell\engine\Help\assets
folder as an example.
|
||
Describe "Test-ModuleManifest Performance bug followup" -tags "CI" { | ||
|
||
$TestModulesFolder= 'testmodulerunspace' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setup should go in BeforeAll
block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved
|
||
#clen up the test module | ||
Remove-Item $UserModulesPath\ModuleWithDependencies2 -Recurse -Force -ErrorAction Ignore | ||
Remove-Item $UserModulesPath\NestedRequiredModule1 -Recurse -Force -ErrorAction Ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cleanup should go in AfterAll
block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved
|
||
$TestModulesFolder= 'testmodulerunspace' | ||
$TestModulesPath = Join-path $PSScriptRoot $TestModulesFolder | ||
$script:UserModulesPath = Join-path [System.AppDomain]::CurrentDomain.BaseDirectory '\Modules' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not UserModulePath. If you want to use the Module folder that's next to powershell.exe
, then replace ...currentdomain.basedirectory
with $pshome
.
And also, there is no need to use $script:
for this variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved
|
||
|
||
# Install the Test Module | ||
Copy-Item $TestModulesPath\* $UserModulesPath -Recurse -Force |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setup should go in BeforeAll
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved
catch | ||
{ | ||
throw $_ | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this catch doesn't seem necessary -- if it just rethrows the exception, why would you catch it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved
throw $_ | ||
} | ||
|
||
$verbose = $job.ChildJobs[0].Verbose.ReadAll() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't you need to wait for the job to finish to read all verbose message?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved
} | ||
|
||
$verbose = $job.ChildJobs[0].Verbose.ReadAll() | ||
$verbose.Count -le 50 | should be true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does the number 50
mean here? Why do you think -le 50
means it doesn't load unnecessary modules?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prior to the fix test-modulemanifest will load all the modules in the $pshome, creating a far more big verbose list.
50 is a random small number that small enough to make sure the issue doesn't repro.
It can't be fixed as well since the verbose message contains other information that may vary.
I've changed the number to an even smaller number 15.
# Generated by: manikb | ||
# | ||
# Generated on: 10/29/2015 | ||
# |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please remove all comments in this file? They make it hard to see what fields are actually used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved
# | ||
# Generated on: 4/14/2015 | ||
# | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, please remove all comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved
@@ -0,0 +1 @@ | |||
function Get-ModuleWithDependencies2 { Get-Date } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see this file used in ModuleWithDependencies2.psd1
, is it really needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
deleted
@@ -216,7 +216,7 @@ protected override void ProcessRecord() | |||
{ | |||
foreach (ModuleSpecification moduleListModule in moduleListModules) | |||
{ | |||
var modules = GetModule(new[] { moduleListModule.Name }, true, true); | |||
var modules = GetModule(new[] { moduleListModule.Name }, false, true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use named parameters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a sync fix of the bug fix under windows bug 7980238.
so I would prefer to keep the code the way the original fix was
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think they should be made named parameters (as well as the other GetModule
call in the same file). We don't make any changes to the original fix when re-submitting it for the sake of less churn. Now when making changes to the Github code base, we want the changes to follow best practices.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment regarding named parameters is not resolved yet.
@@ -217,3 +217,37 @@ Describe "Tests for circular references in required modules" -tags "CI" { | |||
{ TestImportModule $false $false $true } | ShouldBeErrorId "Modules_InvalidManifest,Microsoft.PowerShell.Commands.ImportModuleCommand" | |||
} | |||
} | |||
|
|||
Describe "Test-ModuleManifest Performance bug followup" -tags "CI" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix indentation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved
|
||
$TestModulesFolder= 'testmodulerunspace' | ||
$TestModulesPath = Join-path $PSScriptRoot $TestModulesFolder | ||
$script:UserModulesPath = Join-path [System.AppDomain]::CurrentDomain.BaseDirectory '\Modules' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not need \Modules, Modules should be sufficient
@@ -0,0 +1 @@ | |||
function Get-NestedRequiredModule1 { Get-Date } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add end of line.
@@ -0,0 +1 @@ | |||
function Get-ModuleWithDependencies2 { Get-Date } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add end of line.
BeforeAll { | ||
$TestModulesFolder= 'testmodulerunspace' | ||
$TestModulesPath = Join-path "$PSScriptRoot\assets" $TestModulesFolder | ||
$UserModulesPath = Join-path "$pshome\Modules" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Join-path "$pshome\Modules"
Does this call to Join-Path even work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved
$job = start-job -name "job1" -ScriptBlock {test-modulemanifest "$using:UserModulesPath\ModuleWithDependencies2\2.0\ModuleWithDependencies2.psd1" -verbose} | Wait-Job | ||
|
||
$verbose = $job.ChildJobs[0].Verbose.ReadAll() | ||
$verbose.Count -le 15 | should be true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should use BeLessThan
here. See https://github.com/pester/Pester/wiki/Should#belessthan
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And please put your rational about using the number '15'
in comments here, so that people look at this code later will understand it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved
# PowerShellHostName = '' | ||
|
||
# Minimum version of the Windows PowerShell host required by this module | ||
# PowerShellHostVersion = '' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are so many comments in this file that you haven't deleted yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i thought you mean the comments added in addition to the auto generated ones. resolved.
# PowerShellHostName = '' | ||
|
||
# Minimum version of the Windows PowerShell host required by this module | ||
# PowerShellHostVersion = '' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved
45d680f
to
ab26a46
Compare
@daxian-dbw @adityapatwardhan your comments are resolved |
@@ -183,7 +183,7 @@ protected override void ProcessRecord() | |||
{ | |||
foreach (ModuleSpecification requiredModule in requiredModules) | |||
{ | |||
var modules = GetModule(new[] { requiredModule.Name }, false, true); | |||
var modules = GetModule(new[] { requiredModule.Name }, all: false, frefresh: true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo frefresh -> refresh
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved, thank you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved
Describe "Test-ModuleManifest Performance bug followup" -tags "CI" { | ||
BeforeAll { | ||
$TestModulesFolder= 'testmodulerunspace' | ||
$TestModulesPath = Join-path "$PSScriptRoot\assets" $TestModulesFolder |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be
$TestModulesPath = Join-path -Path "$PSScriptRoot\assets" -ChildPath $TestModulesFolder -AdditionalChildPath 'testmodulerunspace'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't get it. Any good besides it putting two lines into one? It also throws error once I copied it. Can we just skip this comment?
NestedModules = @('NestedRequiredModule1') | ||
|
||
# Functions to export from this module | ||
FunctionsToExport = '*' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be FunctionsToExport = @()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved, thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved
FunctionsToExport = '*' | ||
|
||
# Cmdlets to export from this module | ||
CmdletsToExport = '*' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be CmdletsToExport = @()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved
VariablesToExport = '*' | ||
|
||
# Aliases to export from this module | ||
AliasesToExport = '*' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be AliasesToExport = @()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved
NestedModules = @('NestedRequiredModule1.psm1') | ||
|
||
# Functions to export from this module | ||
FunctionsToExport = '*' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comments as the other module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved
|
||
AfterAll { | ||
#clean up the test module | ||
Remove-Item $UserModulesPath\ModuleWithDependencies2 -Recurse -Force -ErrorAction Ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be -ErrorAction SilentlyContinue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved, thanks
AfterAll { | ||
#clean up the test module | ||
Remove-Item $UserModulesPath\ModuleWithDependencies2 -Recurse -Force -ErrorAction Ignore | ||
Remove-Item $UserModulesPath\NestedRequiredModule1 -Recurse -Force -ErrorAction Ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be
10000
-ErrorAction SilentlyContinue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolved
@@ -0,0 +1 @@ | |||
function Get-NestedRequiredModule1 { Get-Date } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing new line at end of file.
Fix issue #4533
Summary of the issue:
Test-ModuleManifest is loading unnecessary modules
Fix:
Change the recursive loading flag from 'true' to 'false'