8000 Make 'Test-ModuleManifest' not load unnecessary modules (#4541) · PowerShell/PowerShell@fe51fb7 · GitHub
[go: up one dir, main page]

Skip to content

Commit fe51fb7

Browse files
chunqingchendaxian-dbw
authored andcommitted
Make 'Test-ModuleManifest' not load unnecessary modules (#4541)
1 parent 12002e7 commit fe51fb7

File tree

5 files changed

+156
-2
lines changed

5 files changed

+156
-2
lines changed

src/System.Management.Automation/engine/Modules/TestModuleManifestCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ protected override void ProcessRecord()
183183
{
184184
foreach (ModuleSpecification requiredModule in requiredModules)
185185
{
186-
var modules = GetModule(new[] { requiredModule.Name }, false, true);
186+
var modules = GetModule(new[] { requiredModule.Name }, all: false, refresh: true);
187187
if (modules.Count == 0)
188188
{
189189
string errorMsg = StringUtil.Format(Modules.InvalidRequiredModulesinModuleManifest, requiredModule.Name, filePath);
@@ -216,7 +216,7 @@ protected override void ProcessRecord()
216216
{
217217
foreach (ModuleSpecification moduleListModule in moduleListModules)
218218
{
219-
var modules = GetModule(new[] { moduleListModule.Name }, true, true);
219+
var modules = GetModule(new[] { moduleListModule.Name }, all: false, refresh: true);
220220
if (modules.Count == 0)
221221
{
222222
string errorMsg = StringUtil.Format(Modules.InvalidModuleListinModuleManifest, moduleListModule.Name, filePath);

test/powershell/engine/Module/TestModuleManifest.Tests.ps1

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,29 @@ Describe "Tests for circular references in required modules" -tags "CI" {
217217
{ TestImportModule $false $false $true } | ShouldBeErrorId "Modules_InvalidManifest,Microsoft.PowerShell.Commands.ImportModuleCommand"
218218
}
219219
}
220+
221+
Describe "Test-ModuleManifest Performance bug followup" -tags "CI" {
222+
BeforeAll {
223+
$TestModulesPath = [System.IO.Path]::Combine($PSScriptRoot, 'assets', 'testmodulerunspace')
224+
$UserModulesPath = "$pshome\Modules"
225+
226+
# Install the Test Module
227+
Copy-Item $TestModulesPath\* $UserModulesPath -Recurse -Force
228+
}
229+
230+
It "Test-ModuleManifest should not load unnessary modules" {
231+
232+
$job = start-job -name "job1" -ScriptBlock {test-modulemanifest "$using:UserModulesPath\ModuleWithDependencies2\2.0\ModuleWithDependencies2.psd1" -verbose} | Wait-Job
233+
234+
$verbose = $job.ChildJobs[0].Verbose.ReadAll()
235+
# Before the fix, all modules under $pshome will be imported and will be far more than 15 verbose messages. However, we cannot fix the number in case verbose message may vary.
236+
$verbose.Count | Should BeLessThan 15
237+
}
238+
239+
AfterAll {
240+
#clean up the test modules
241+
Remove-Item $UserModulesPath\ModuleWithDependencies2 -Recurse -Force -ErrorAction SilentlyContinue
242+
Remove-Item $UserModulesPath\NestedRequiredModule1 -Recurse -Force -ErrorAction SilentlyContinue
243+
}
244+
}
245+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
@{
3+
4+
# Version number of this module.
5+
ModuleVersion = '2.0'
6+
7+
# ID used to uniquely identify this module
8+
GUID = '0eae34da-99dd-4608-8d28-c614fe7b0841'
9+
10+
# Author of this module
11+
Author = 'manikb'
12+
13+
# Company or vendor of this module
14+
CompanyName = 'Unknown'
15+
16+
# Copyright statement for this module
17+
Copyright = '(c) 2015 manikb. All rights reserved.'
18+
19+
# Description of the functionality provided by this module
20+
Description = 'ModuleWithDependencies2 module'
21+
22+
# Modules that must be imported into the global environment prior to importing this module
23+
RequiredModules = @('NestedRequiredModule1')
24+
25+
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
26+
NestedModules = @('NestedRequiredModule1')
27+
28+
# Functions to export from this module
29+
FunctionsToExport = @()
30+
31+
# Cmdlets to export from this module
32+
CmdletsToExport = @()
33+
34+
# Variables to export from this module
35+
VariablesToExport = @()
36+
37+
# Aliases to export from this module
38+
AliasesToExport = @()
39+
40+
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
41+
PrivateData = @{
42+
43+
PSData = @{
44+
45+
# Tags applied to this module. These help with module discovery in online galleries.
46+
Tags = 'Tag1', 'Tag2', 'Tag-ModuleWithDependencies2-2.0'
47+
48+
# A URL to the license for this module.
49+
LicenseUri = 'http://modulewithdependencies2.com/license'
50+
51+
# A URL to the main website for this project.
52+
ProjectUri = 'http://modulewithdependencies2.com/'
53+
54+
# A URL to an icon representing this module.
55+
IconUri = 'http://modulewithdependencies2.com/icon'
56+
57+
# ReleaseNotes of this module
58+
ReleaseNotes = 'ModuleWithDependencies2 release notes'
59+
60+
} # End of PSData hashtable
61+
62+
} # End of PrivateData hashtable
63+
64+
}
65+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
@{
3+
4+
# Version number of this module.
5+
ModuleVersion = '2.5'
6+
7+
# ID used to uniquely identify this module
8+
GUID = '369f0ee4-4cda-4ac3-a5c5-08e7bbc06e1a'
9+
10+
# Author of this module
11+
Author = 'manikb'
12+
13+
# Company or vendor of this module
14+
CompanyName = 'Unknown'
15+
16+
# Copyright statement for this module
17+
Copyright = '(c) 2015 manikb. All rights reserved.'
18+
19+
# Description of the functionality provided by this module
20+
Description = 'NestedRequiredModule1 module'
21+
22+
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
23+
NestedModules = @('NestedRequiredModule1.psm1')
24+
25+
# Functions to export from this module
26+
FunctionsToExport = @()
27+
28+
# Cmdlets to export from this module
29+
CmdletsToExport = @()
30+
31+
# Variables to export from this module
32+
VariablesToExport = @()
33+
34+
# Aliases to export from this module
35+
AliasesToExport = @()
36+
37+
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
38+
PrivateData = @{
39+
40+
PSData = @{
41+
42+
# Tags applied to this module. These help with module discovery in online galleries.
43+
Tags = 'Tag1', 'Tag2', 'Tag-NestedRequiredModule1-2.5'
44+
45+
# A URL to the license for this module.
46+
LicenseUri = 'http://nestedrequiredmodule1.com/license'
47+
48+
# A URL to the main website for this project.
49+
ProjectUri = 'http://nestedrequiredmodule1.com/'
50+
51+
# A URL to an icon representing this module.
52+
IconUri = 'http://nestedrequiredmodule1.com/icon'
53+
54+
# ReleaseNotes of this module
55+
ReleaseNotes = 'NestedRequiredModule1 release notes'
56+
57+
} # End of PSData hashtable
58+
59+
} # End of PrivateData hashtable
60+
61+
}
62+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
function Get-NestedRequiredModule1 { Get-Date }

0 commit comments

Comments
 (0)
0