Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add to PR Summary:

Also fixing following error message to include 'MaximumVersion' in the list of 'valid members':
  <data name="InvalidModuleSpecificationMember" xml:space="preserve">
    <value>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.</value>
  </data>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this should use $"{nameof(...)}" for the bad members

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nameof(badKeys) returns 'badKeys', but we need its actual value.
Or am I missing something?

return new ArgumentException(message);
}

Expand All @@ -163,6 +163,13 @@ internal static Exception ModuleSpecificationInitHelper(ModuleSpecification modu
message = StringUtil.Format(SessionStateStrings.GetContent_TailAndHeadCannotCoexist, "MaximumVersion", "RequiredVersion");
return new ArgumentException(message);
}

if (moduleSpecification.Version != null && moduleSpecification.MaximumVersion != null &&
moduleSpecification.Version > ModuleCmdletBase.GetMaximumVersion(moduleSpecification.MaximumVersion))
{
message = StringUtil.Format(Modules.ModuleSpecificationMemberIsLessThanOther, moduleSpecification.Version, moduleSpecification.MaximumVersion);
return new ArgumentException(message);
}
return null;
}

Expand Down
3 changes: 3 additions & 0 deletions src/System.Management.Automation/resources/Modules.resx
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@
<data name="InvalidModuleSpecificationMember" xml:space="preserve">
<value>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.</value>
</data>
<data name="ModuleSpecificationMemberIsLessThanOther" xml:space="preserve">
<value>The hashtable describing a module cannot have its ModuleVersion ({0}) greater than its MaximumVersion ({1}).</value>
</data>
<data name="ModuleTooDeeplyNested" xml:space="preserve">
<value>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.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 'ModuleVersion' when using -FullyQualifiedName parameter" {
{ Import-Module -FullyQualifiedName @{ModuleName = $moduleName; ModuleVersion = '2.0'; MaximumVersion = '1.0'} } | Should -Throw -ExceptionType 'System.Management.Automation.ParameterBindingException'
}
}

Describe "Import-Module with ScriptsToProcess" -Tags "CI" {
Expand Down
5 changes: 5 additions & 0 deletions test/powershell/engine/Module/ModuleSpecification.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ Describe "ModuleSpecification objects and logic" -Tag "CI" {
TestName = "BadType"
ModuleSpecification = @{ ModuleName = "BadTypeModule"; RequiredVersion = "Hello!" }
ErrorId = 'PSInvalidCastException'
},
@{
TestName = "ModuleVersion is greater than MaximumVersion"
ModuleSpecification = @{ ModuleName = "ModuleVersion>MaximumVersion"; ModuleVersion = "3.0"; MaximumVersion = '2.0' }
ErrorId = 'ArgumentException'
}
)
}
Expand Down