From 4d0da14cc1e66e86f5b0cd8c0dd5870df4cd7cce Mon Sep 17 00:00:00 2001 From: sethvs Date: Tue, 24 Jul 2018 14:27:59 +0300 Subject: [PATCH 1/6] Throw error message when MaxumimVersion is less than Version when using Import-Module cmdlet with -FullyQualifiedName parameter. --- .../engine/Modules/ModuleSpecification.cs | 8 +++++++- src/System.Management.Automation/resources/Modules.resx | 3 +++ .../Microsoft.PowerShell.Core/Import-Module.Tests.ps1 | 4 ++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/Modules/ModuleSpecification.cs b/src/System.Management.Automation/engine/Modules/ModuleSpecification.cs index 775b14a555e..cc9f5656915 100644 --- a/src/System.Management.Automation/engine/Modules/ModuleSpecification.cs +++ b/src/System.Management.Automation/engine/Modules/ModuleSpecification.cs @@ -136,7 +136,7 @@ internal static Exception ModuleSpecificationInitHelper(ModuleSpecification modu string message; if (badKeys.Length != 0) { - message = StringUtil.Format(Modules.InvalidModuleSpecificationMember, "ModuleName, ModuleVersion, RequiredVersion, GUID", badKeys); + message = StringUtil.Format(Modules.InvalidModuleSpecificationMember, "ModuleName, ModuleVersion, MaximumVersion, RequiredVersion, GUID", badKeys); return new ArgumentException(message); } @@ -163,6 +163,12 @@ internal static Exception ModuleSpecificationInitHelper(ModuleSpecification modu message = StringUtil.Format(SessionStateStrings.GetContent_TailAndHeadCannotCoexist, "MaximumVersion", "RequiredVersion"); return new ArgumentException(message); } + + if (moduleSpecification.Version > ModuleCmdletBase.GetMaximumVersion(moduleSpecification.MaximumVersion)) + { + message = StringUtil.Format(Modules.ModuleSpecificationMemberIsLessThanOther, "MaximumVersion", "Version"); + return new ArgumentException(message); + } return null; } diff --git a/src/System.Management.Automation/resources/Modules.resx b/src/System.Management.Automation/resources/Modules.resx index f88928b3592..857e58bb6f7 100644 --- a/src/System.Management.Automation/resources/Modules.resx +++ b/src/System.Management.Automation/resources/Modules.resx @@ -165,6 +165,9 @@ The hashtable describing a module contains one or more members that are not valid. The valid members are ({0}). Remove the members that are not valid ({1}), then try again. + + The member '{0}' cannot be less than '{1}'. + Cannot load the module '{0}' because the module nesting limit has been exceeded. Modules can only be nested to {1} levels. Evaluate and change the order in which you are loading modules to prevent exceeding the nesting limit, and then try running your script again. diff --git a/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 index 263f5eacf4d..c4f519593c0 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 @@ -56,6 +56,10 @@ Describe "Import-Module" -Tags "CI" { Import-Module TestModule -RequiredVersion 1.1 (Get-Module TestModule).Version | Should -BeIn "1.1" } + + It "should throw if 'MaximumVersion' is less than 'Version' when using -FullyQualifiedName parameter" { + { Import-Module -FullyQualifiedName @{ModuleName = $moduleName; Version = '2.0'; MaximumVersion = '1.0'} } | Should -Throw -ExceptionType 'System.Management.Automation.ParameterBindingException' + } } Describe "Import-Module with ScriptsToProcess" -Tags "CI" { From 321537c6e52b24e9187afd267bb0b18b242bbed3 Mon Sep 17 00:00:00 2001 From: sethvs Date: Tue, 24 Jul 2018 14:54:43 +0300 Subject: [PATCH 2/6] Fix logic and typos. --- .../engine/Modules/ModuleSpecification.cs | 5 +++-- .../Microsoft.PowerShell.Core/Import-Module.Tests.ps1 | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/engine/Modules/ModuleSpecification.cs b/src/System.Management.Automation/engine/Modules/ModuleSpecification.cs index cc9f5656915..3e9bf6d17fb 100644 --- a/src/System.Management.Automation/engine/Modules/ModuleSpecification.cs +++ b/src/System.Management.Automation/engine/Modules/ModuleSpecification.cs @@ -164,9 +164,10 @@ internal static Exception ModuleSpecificationInitHelper(ModuleSpecification modu return new ArgumentException(message); } - if (moduleSpecification.Version > ModuleCmdletBase.GetMaximumVersion(moduleSpecification.MaximumVersion)) + if (moduleSpecification.Version != null && moduleSpecification.MaximumVersion != null && + moduleSpecification.Version > ModuleCmdletBase.GetMaximumVersion(moduleSpecification.MaximumVersion)) { - message = StringUtil.Format(Modules.ModuleSpecificationMemberIsLessThanOther, "MaximumVersion", "Version"); + message = StringUtil.Format(Modules.ModuleSpecificationMemberIsLessThanOther, "MaximumVersion", "ModuleVersion"); return new ArgumentException(message); } return null; diff --git a/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 index c4f519593c0..f49fd472639 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 @@ -58,7 +58,7 @@ Describe "Import-Module" -Tags "CI" { } It "should throw if 'MaximumVersion' is less than 'Version' when using -FullyQualifiedName parameter" { - { Import-Module -FullyQualifiedName @{ModuleName = $moduleName; Version = '2.0'; MaximumVersion = '1.0'} } | Should -Throw -ExceptionType 'System.Management.Automation.ParameterBindingException' + { Import-Module -FullyQualifiedName @{ModuleName = $moduleName; ModuleVersion = '2.0'; MaximumVersion = '1.0'} } | Should -Throw -ExceptionType 'System.Management.Automation.ParameterBindingException' } } From 9b27ae81e0216c7c4cd5bf4a59b0ba164d27e955 Mon Sep 17 00:00:00 2001 From: sethvs Date: Wed, 15 Aug 2018 14:01:51 +0300 Subject: [PATCH 3/6] Fix error message. --- .../engine/Modules/ModuleSpecification.cs | 2 +- src/System.Management.Automation/resources/Modules.resx | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/engine/Modules/ModuleSpecification.cs b/src/System.Management.Automation/engine/Modules/ModuleSpecification.cs index 3e9bf6d17fb..d7e1255cafd 100644 --- a/src/System.Management.Automation/engine/Modules/ModuleSpecification.cs +++ b/src/System.Management.Automation/engine/Modules/ModuleSpecification.cs @@ -167,7 +167,7 @@ internal static Exception ModuleSpecificationInitHelper(ModuleSpecification modu if (moduleSpecification.Version != null && moduleSpecification.MaximumVersion != null && moduleSpecification.Version > ModuleCmdletBase.GetMaximumVersion(moduleSpecification.MaximumVersion)) { - message = StringUtil.Format(Modules.ModuleSpecificationMemberIsLessThanOther, "MaximumVersion", "ModuleVersion"); + message = StringUtil.Format(Modules.ModuleSpecificationMemberIsLessThanOther, moduleSpecification.Version, moduleSpecification.MaximumVersion); return new ArgumentException(message); } return null; diff --git a/src/System.Management.Automation/resources/Modules.resx b/src/System.Management.Automation/resources/Modules.resx index 857e58bb6f7..40e73251978 100644 --- a/src/System.Management.Automation/resources/Modules.resx +++ b/src/System.Management.Automation/resources/Modules.resx @@ -163,10 +163,10 @@ The '{0}' module cannot be imported because its manifest contains one or more members that are not valid. The valid manifest members are ({1}). Remove the members that are not valid ({2}), then try to import the module again. - The hashtable describing a module contains one or more members that are not valid. The valid members are ({0}). Remove the members that are not valid ({1}), then try again. + The hashtable describing a module contains one or more members that are not valid. The valid members are ({0}). Remove the members that are not valid ({1}), then try again. - The member '{0}' cannot be less than '{1}'. + The hashtable describing a module cannot have its ModuleVersion ({0}) greater than its MaximumVersion ({1}). Cannot load the module '{0}' because the module nesting limit has been exceeded. Modules can only be nested to {1} levels. Evaluate and change the order in which you are loading modules to prevent exceeding the nesting limit, and then try running your script again. From 77241e11cdaf26d726efb4f100135a3a570b2134 Mon Sep 17 00:00:00 2001 From: sethvs Date: Wed, 15 Aug 2018 14:32:52 +0300 Subject: [PATCH 4/6] Add test. --- .../Microsoft.PowerShell.Core/Import-Module.Tests.ps1 | 2 +- .../engine/Module/ModuleSpecification.Tests.ps1 | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 index f49fd472639..fad525e1ff1 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Core/Import-Module.Tests.ps1 @@ -57,7 +57,7 @@ Describe "Import-Module" -Tags "CI" { (Get-Module TestModule).Version | Should -BeIn "1.1" } - It "should throw if 'MaximumVersion' is less than 'Version' when using -FullyQualifiedName parameter" { + It "should throw if 'MaximumVersion' is less than 'ModuleVersion' when using -FullyQualifiedName parameter" { { Import-Module -FullyQualifiedName @{ModuleName = $moduleName; ModuleVersion = '2.0'; MaximumVersion = '1.0'} } | Should -Throw -ExceptionType 'System.Management.Automation.ParameterBindingException' } } diff --git a/test/powershell/engine/Module/ModuleSpecification.Tests.ps1 b/test/powershell/engine/Module/ModuleSpecification.Tests.ps1 index f41f3a262f3..1da2e51b872 100644 --- a/test/powershell/engine/Module/ModuleSpecification.Tests.ps1 +++ b/test/powershell/engine/Module/ModuleSpecification.Tests.ps1 @@ -228,7 +228,7 @@ Describe "ModuleSpecification objects and logic" -Tag "CI" { $testCases = @( @{ TestName = "Version+RequiredVersion" - ModuleSpecification = @{ Name = "BadVersionModule"; ModuleVersion = "3.1"; RequiredVersion = "3.1" } + ModuleSpecification = @{ ModuleName = "BadVersionModule"; ModuleVersion = "3.1"; RequiredVersion = "3.1" } }, @{ TestName = "NoName" @@ -236,11 +236,15 @@ Describe "ModuleSpecification objects and logic" -Tag "CI" { }, @{ TestName = "BadField" - ModuleSpecification = @{ Name = "StrangeFieldModule"; RequiredVersion = "7.4"; Duck = "1.2" } + ModuleSpecification = @{ ModuleName = "StrangeFieldModule"; RequiredVersion = "7.4"; Duck = "1.2" } }, @{ TestName = "BadType" - ModuleSpecification = @{ Name = "BadTypeModule"; RequiredVersion = "Hello!" } + ModuleSpecification = @{ ModuleName = "BadTypeModule"; RequiredVersion = "Hello!" } + }, + @{ + TestName = "ModuleVersion is greater than MaximumVersion" + ModuleSpecification = @{ ModuleName = "ModuleVersion>MaximumVersion"; ModuleVersion = "3.0"; MaximumVersion = '2.0' } } ) } From fdfe12ee2a2fe0bab0ec09eec285c55329cd1581 Mon Sep 17 00:00:00 2001 From: sethvs Date: Thu, 30 Aug 2018 10:55:15 +0300 Subject: [PATCH 5/6] Split nonrelated fixes into separate PRs. --- src/System.Management.Automation/resources/Modules.resx | 2 +- test/powershell/engine/Module/ModuleSpecification.Tests.ps1 | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/System.Management.Automation/resources/Modules.resx b/src/System.Management.Automation/resources/Modules.resx index 40e73251978..e3df749e422 100644 --- a/src/System.Management.Automation/resources/Modules.resx +++ b/src/System.Management.Automation/resources/Modules.resx @@ -163,7 +163,7 @@ The '{0}' module cannot be imported because its manifest contains one or more members that are not valid. The valid manifest members are ({1}). Remove the members that are not valid ({2}), then try to import the module again. - The hashtable describing a module contains one or more members that are not valid. The valid members are ({0}). Remove the members that are not valid ({1}), then try again. + The hashtable describing a module contains one or more members that are not valid. The valid members are ({0}). Remove the members that are not valid ({1}), then try again. The hashtable describing a module cannot have its ModuleVersion ({0}) greater than its MaximumVersion ({1}). diff --git a/test/powershell/engine/Module/ModuleSpecification.Tests.ps1 b/test/powershell/engine/Module/ModuleSpecification.Tests.ps1 index 1da2e51b872..834c8d57f31 100644 --- a/test/powershell/engine/Module/ModuleSpecification.Tests.ps1 +++ b/test/powershell/engine/Module/ModuleSpecification.Tests.ps1 @@ -228,7 +228,7 @@ Describe "ModuleSpecification objects and logic" -Tag "CI" { $testCases = @( @{ TestName = "Version+RequiredVersion" - ModuleSpecification = @{ ModuleName = "BadVersionModule"; ModuleVersion = "3.1"; RequiredVersion = "3.1" } + ModuleSpecification = @{ Name = "BadVersionModule"; ModuleVersion = "3.1"; RequiredVersion = "3.1" } }, @{ TestName = "NoName" @@ -236,11 +236,11 @@ Describe "ModuleSpecification objects and logic" -Tag "CI" { }, @{ TestName = "BadField" - ModuleSpecification = @{ ModuleName = "StrangeFieldModule"; RequiredVersion = "7.4"; Duck = "1.2" } + ModuleSpecification = @{ Name = "StrangeFieldModule"; RequiredVersion = "7.4"; Duck = "1.2" } }, @{ TestName = "BadType" - ModuleSpecification = @{ ModuleName = "BadTypeModule"; RequiredVersion = "Hello!" } + ModuleSpecification = @{ Name = "BadTypeModule"; RequiredVersion = "Hello!" } }, @{ TestName = "ModuleVersion is greater than MaximumVersion" From 1a784a45a2f75a7893cc18c281ef8aaeb2a1b427 Mon Sep 17 00:00:00 2001 From: sethvs Date: Wed, 5 Sep 2018 16:34:21 +0300 Subject: [PATCH 6/6] Fix error introduced while resolving conflict. --- test/powershell/engine/Module/ModuleSpecification.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/engine/Module/ModuleSpecification.Tests.ps1 b/test/powershell/engine/Module/ModuleSpecification.Tests.ps1 index b213657df15..588f1e83061 100644 --- a/test/powershell/engine/Module/ModuleSpecification.Tests.ps1 +++ b/test/powershell/engine/Module/ModuleSpecification.Tests.ps1 @@ -243,7 +243,7 @@ Describe "ModuleSpecification objects and logic" -Tag "CI" { }, @{ TestName = "BadType" - ModuleSpecification = @{ Name = "BadTypeModule"; RequiredVersion = "Hello!" } + ModuleSpecification = @{ ModuleName = "BadTypeModule"; RequiredVersion = "Hello!" } ErrorId = 'PSInvalidCastException' }, @{