From 69a630b6ebc96b3364a8a31bae9795f5249b6fee Mon Sep 17 00:00:00 2001 From: Chunqing Chen Date: Thu, 10 Aug 2017 12:15:20 -0700 Subject: [PATCH 1/3] Test-ModuleManifest should not load unnessary modules --- .../Modules/TestModuleManifestCommand.cs | 4 +- .../Module/TestModuleManifest.Tests.ps1 | 28 ++++++++ .../2.0/ModuleWithDependencies2.psd1 | 65 +++++++++++++++++++ .../2.5/NestedRequiredModule1.psd1 | 62 ++++++++++++++++++ .../2.5/NestedRequiredModule1.psm1 | 1 + 5 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 test/powershell/engine/Module/assets/testmodulerunspace/ModuleWithDependencies2/2.0/ModuleWithDependencies2.psd1 create mode 100644 test/powershell/engine/Module/assets/testmodulerunspace/NestedRequiredModule1/2.5/NestedRequiredModule1.psd1 create mode 100644 test/powershell/engine/Module/assets/testmodulerunspace/NestedRequiredModule1/2.5/NestedRequiredModule1.psm1 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..7799a8556b0 100644 --- a/test/powershell/engine/Module/TestModuleManifest.Tests.ps1 +++ b/test/powershell/engine/Module/TestModuleManifest.Tests.ps1 @@ -217,3 +217,31 @@ 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 { + $TestModulesFolder= 'testmodulerunspace' + $TestModulesPath = Join-path "$PSScriptRoot\assets" $TestModulesFolder + $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 module + Remove-Item $UserModulesPath\ModuleWithDependencies2 -Recurse -Force -ErrorAction Ignore + Remove-Item $UserModulesPath\NestedRequiredModule1 -Recurse -Force -ErrorAction Ignore + } + +} + 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..4a6f4ba32f8 --- /dev/null +++ b/test/powershell/engine/Module/assets/testmodulerunspace/NestedRequiredModule1/2.5/NestedRequiredModule1.psm1 @@ -0,0 +1 @@ +function Get-NestedRequiredModule1 { Get-Date } \ No newline at end of file From 0eda0791ce4be8e0302533113b9a9b80525e0c7f Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 28 Aug 2017 14:24:34 -0700 Subject: [PATCH 2/3] Address some remaining comments --- .../engine/Module/TestModuleManifest.Tests.ps1 | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/test/powershell/engine/Module/TestModuleManifest.Tests.ps1 b/test/powershell/engine/Module/TestModuleManifest.Tests.ps1 index 7799a8556b0..744e0d1a3f4 100644 --- a/test/powershell/engine/Module/TestModuleManifest.Tests.ps1 +++ b/test/powershell/engine/Module/TestModuleManifest.Tests.ps1 @@ -220,8 +220,7 @@ Describe "Tests for circular references in required modules" -tags "CI" { Describe "Test-ModuleManifest Performance bug followup" -tags "CI" { BeforeAll { - $TestModulesFolder= 'testmodulerunspace' - $TestModulesPath = Join-path "$PSScriptRoot\assets" $TestModulesFolder + $TestModulesPath = [System.IO.Path]::Combine($PSScriptRoot, 'assets', 'testmodulerunspace') $UserModulesPath = "$pshome\Modules" # Install the Test Module @@ -238,10 +237,9 @@ Describe "Test-ModuleManifest Performance bug followup" -tags "CI" { } AfterAll { - #clean up the test module - Remove-Item $UserModulesPath\ModuleWithDependencies2 -Recurse -Force -ErrorAction Ignore - Remove-Item $UserModulesPath\NestedRequiredModule1 -Recurse -Force -ErrorAction Ignore + #clean up the test modules + Remove-Item $UserModulesPath\ModuleWithDependencies2 -Recurse -Force -ErrorAction SilentlyContinue + Remove-Item $UserModulesPath\NestedRequiredModule1 -Recurse -Force -ErrorAction SilentlyContinue } - } From b523ce8a7d0d28c5f1ac99dcb56ffa92801f021b Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 28 Aug 2017 14:25:33 -0700 Subject: [PATCH 3/3] Add a new line at end --- .../NestedRequiredModule1/2.5/NestedRequiredModule1.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 4a6f4ba32f8..52544a3cade 100644 --- a/test/powershell/engine/Module/assets/testmodulerunspace/NestedRequiredModule1/2.5/NestedRequiredModule1.psm1 +++ b/test/powershell/engine/Module/assets/testmodulerunspace/NestedRequiredModule1/2.5/NestedRequiredModule1.psm1 @@ -1 +1 @@ -function Get-NestedRequiredModule1 { Get-Date } \ No newline at end of file +function Get-NestedRequiredModule1 { Get-Date }