diff --git a/src/System.Management.Automation/engine/Modules/TestModuleManifestCommand.cs b/src/System.Management.Automation/engine/Modules/TestModuleManifestCommand.cs index 8115c840a31..a38489b51e8 100644 --- a/src/System.Management.Automation/engine/Modules/TestModuleManifestCommand.cs +++ b/src/System.Management.Automation/engine/Modules/TestModuleManifestCommand.cs @@ -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, refresh: true); if (modules.Count == 0) { string errorMsg = StringUtil.Format(Modules.InvalidRequiredModulesinModuleManifest, requiredModule.Name, filePath); @@ -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 }, all: false, refresh: true); if (modules.Count == 0) { string errorMsg = StringUtil.Format(Modules.InvalidModuleListinModuleManifest, moduleListModule.Name, filePath); diff --git a/test/powershell/engine/Module/TestModuleManifest.Tests.ps1 b/test/powershell/engine/Module/TestModuleManifest.Tests.ps1 index 8f885f84c37..744e0d1a3f4 100644 --- a/test/powershell/engine/Module/TestModuleManifest.Tests.ps1 +++ b/test/powershell/engine/Module/TestModuleManifest.Tests.ps1 @@ -217,3 +217,29 @@ 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" { + BeforeAll { + $TestModulesPath = [System.IO.Path]::Combine($PSScriptRoot, 'assets', 'testmodulerunspace') + $UserModulesPath = "$pshome\Modules" + + # Install the Test Module + Copy-Item $TestModulesPath\* $UserModulesPath -Recurse -Force + } + + It "Test-ModuleManifest should not load unnessary modules" { + + $job = start-job -name "job1" -ScriptBlock {test-modulemanifest "$using:UserModulesPath\ModuleWithDependencies2\2.0\ModuleWithDependencies2.psd1" -verbose} | Wait-Job + + $verbose = $job.ChildJobs[0].Verbose.ReadAll() + # 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. + $verbose.Count | Should BeLessThan 15 + } + + AfterAll { + #clean up the test modules + Remove-Item $UserModulesPath\ModuleWithDependencies2 -Recurse -Force -ErrorAction SilentlyContinue + Remove-Item $UserModulesPath\NestedRequiredModule1 -Recurse -Force -ErrorAction SilentlyContinue + } +} + diff --git a/test/powershell/engine/Module/assets/testmodulerunspace/ModuleWithDependencies2/2.0/ModuleWithDependencies2.psd1 b/test/powershell/engine/Module/assets/testmodulerunspace/ModuleWithDependencies2/2.0/ModuleWithDependencies2.psd1 new file mode 100644 index 00000000000..d5a1875ffd1 --- /dev/null +++ b/test/powershell/engine/Module/assets/testmodulerunspace/ModuleWithDependencies2/2.0/ModuleWithDependencies2.psd1 @@ -0,0 +1,65 @@ + +@{ + +# Version number of this module. +ModuleVersion = '2.0' + +# ID used to uniquely identify this module +GUID = '0eae34da-99dd-4608-8d28-c614fe7b0841' + +# Author of this module +Author = 'manikb' + +# Company or vendor of this module +CompanyName = 'Unknown' + +# Copyright statement for this module +Copyright = '(c) 2015 manikb. All rights reserved.' + +# Description of the functionality provided by this module +Description = 'ModuleWithDependencies2 module' + +# Modules that must be imported into the global environment prior to importing this module +RequiredModules = @('NestedRequiredModule1') + +# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess +NestedModules = @('NestedRequiredModule1') + +# Functions to export from this module +FunctionsToExport = @() + +# Cmdlets to export from this module +CmdletsToExport = @() + +# Variables to export from this module +VariablesToExport = @() + +# Aliases to export from this module +AliasesToExport = @() + +# 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. +PrivateData = @{ + + PSData = @{ + + # Tags applied to this module. These help with module discovery in online galleries. + Tags = 'Tag1', 'Tag2', 'Tag-ModuleWithDependencies2-2.0' + + # A URL to the license for this module. + LicenseUri = 'http://modulewithdependencies2.com/license' + + # A URL to the main website for this project. + ProjectUri = 'http://modulewithdependencies2.com/' + + # A URL to an icon representing this module. + IconUri = 'http://modulewithdependencies2.com/icon' + + # ReleaseNotes of this module + ReleaseNotes = 'ModuleWithDependencies2 release notes' + + } # End of PSData hashtable + +} # End of PrivateData hashtable + +} + diff --git a/test/powershell/engine/Module/assets/testmodulerunspace/NestedRequiredModule1/2.5/NestedRequiredModule1.psd1 b/test/powershell/engine/Module/assets/testmodulerunspace/NestedRequiredModule1/2.5/NestedRequiredModule1.psd1 new file mode 100644 index 00000000000..8c274b0e722 --- /dev/null +++ b/test/powershell/engine/Module/assets/testmodulerunspace/NestedRequiredModule1/2.5/NestedRequiredModule1.psd1 @@ -0,0 +1,62 @@ + +@{ + +# Version number of this module. +ModuleVersion = '2.5' + +# ID used to uniquely identify this module +GUID = '369f0ee4-4cda-4ac3-a5c5-08e7bbc06e1a' + +# Author of this module +Author = 'manikb' + +# Company or vendor of this module +CompanyName = 'Unknown' + +# Copyright statement for this module +Copyright = '(c) 2015 manikb. All rights reserved.' + +# Description of the functionality provided by this module +Description = 'NestedRequiredModule1 module' + +# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess +NestedModules = @('NestedRequiredModule1.psm1') + +# Functions to export from this module +FunctionsToExport = @() + +# Cmdlets to export from this module +CmdletsToExport = @() + +# Variables to export from this module +VariablesToExport = @() + +# Aliases to export from this module +AliasesToExport = @() + +# 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. +PrivateData = @{ + + PSData = @{ + + # Tags applied to this module. These help with module discovery in online galleries. + Tags = 'Tag1', 'Tag2', 'Tag-NestedRequiredModule1-2.5' + + # A URL to the license for this module. + LicenseUri = 'http://nestedrequiredmodule1.com/license' + + # A URL to the main website for this project. + ProjectUri = 'http://nestedrequiredmodule1.com/' + + # A URL to an icon representing this module. + IconUri = 'http://nestedrequiredmodule1.com/icon' + + # ReleaseNotes of this module + ReleaseNotes = 'NestedRequiredModule1 release notes' + + } # End of PSData hashtable + +} # End of PrivateData hashtable + +} + diff --git a/test/powershell/engine/Module/assets/testmodulerunspace/NestedRequiredModule1/2.5/NestedRequiredModule1.psm1 b/test/powershell/engine/Module/assets/testmodulerunspace/NestedRequiredModule1/2.5/NestedRequiredModule1.psm1 new file mode 100644 index 00000000000..52544a3cade --- /dev/null +++ b/test/powershell/engine/Module/assets/testmodulerunspace/NestedRequiredModule1/2.5/NestedRequiredModule1.psm1 @@ -0,0 +1 @@ +function Get-NestedRequiredModule1 { Get-Date }