Skip to content

Fix module specification hashtable in ModuleSpecification.Tests.ps1 - #7663

Merged
Aditya Patwardhan (adityapatwardhan) merged 2 commits into
PowerShell:masterfrom
sethvs:moduleSpecification.Tests
Sep 4, 2018
Merged

Fix module specification hashtable in ModuleSpecification.Tests.ps1#7663
Aditya Patwardhan (adityapatwardhan) merged 2 commits into
PowerShell:masterfrom
sethvs:moduleSpecification.Tests

Conversation

@sethvs

Copy link
Copy Markdown
Contributor

PR Summary

Change Name to ModuleName in module specification hashtable.

PR Checklist

@iSazonov

Copy link
Copy Markdown
Collaborator

Sergey Vasin (@sethvs) The broken names was created specially for test "Cannot create from invalid module hashtables".

@sethvs

Copy link
Copy Markdown
Contributor Author

Ilya (@iSazonov)
I believe this is not the case.
Each testcase tests for some specific error in hashtable.

@{TestName = "Version+RequiredVersion"
ModuleSpecification = @{ ModuleName = "BadVersionModule"; ModuleVersion = "3.1"; RequiredVersion = "3.1" }},
@{TestName = "NoName"
ModuleSpecification = @{ ModuleVersion = "0.2" }},
@{TestName = "BadField"
ModuleSpecification = @{ ModuleName = "StrangeFieldModule"; RequiredVersion = "7.4"; Duck = "1.2" }},
@{TestName = "BadType"
ModuleSpecification = @{ ModuleName = "BadTypeModule"; RequiredVersion = "Hello!" }

Now, with Name instead of ModuleName, the code that checks the hashtable throws errors because of the wrong field - Name, instead of the actual error, that should be tested.

For example, the first testcase:

$ModuleSpecification = @{ Name = "BadVersionModule"; ModuleVersion = "3.1"; RequiredVersion = "3.1" }
[Microsoft.PowerShell.Commands.ModuleSpecification]::new($ModuleSpecification)
Exception calling ".ctor" with "1" argument(s): "The hashtable describing a module contains one or more members that are not valid.  The valid members are (ModuleName, ModuleVersion, RequiredVersion, GUID). Remove the members that are not valid ('Name'), then try again."
At line:1 char:1
+ [Microsoft.PowerShell.Commands.ModuleSpecification]::new($ModuleSpeci ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentException

should actually be:

$ModuleSpecification = @{ ModuleName = "BadVersionModule"; ModuleVersion = "3.1"; RequiredVersion = "3.1" }
[Microsoft.PowerShell.Commands.ModuleSpecification]::new($ModuleSpecification)
Exception calling ".ctor" with "1" argument(s): "The parameters ModuleVersion and RequiredVersion cannot be used together. Please specify only one parameter."
At line:1 char:1
+ [Microsoft.PowerShell.Commands.ModuleSpecification]::new($ModuleSpeci ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentException

@{
TestName = "BadType"
ModuleSpecification = @{ Name = "BadTypeModule"; RequiredVersion = "Hello!" }
ModuleSpecification = @{ ModuleName = "BadTypeModule"; RequiredVersion = "Hello!" }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

To make the tests more effective we modify L255 to be something like below for the specific expected failure. This will ensure such failures are not hidden due to faulty test code.

Should -Throw -ErrorId ArgumentException

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Regarding first and third tests:

@{TestName = "Version+RequiredVersion"
ModuleSpecification = @{ ModuleName = "BadVersionModule"; ModuleVersion = "3.1"; RequiredVersion = "3.1" }}
@{TestName = "BadField"
ModuleSpecification = @{ ModuleName = "StrangeFieldModule"; RequiredVersion = "7.4"; Duck = "1.2" }}

They return ArgumentException, just like when we use Name instead of ModuleName in the hastable, but I agree, that specifying expected ErrorId is a good addition.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

@adityapatwardhan

Copy link
Copy Markdown
Member

Sergey Vasin (@sethvs) Thanks for your contribution. I have left a comment to make the test even more effective.

@rjmholt Rob Holt (rjmholt) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good. Thanks for fixing this. Aditya Patwardhan (@adityapatwardhan)'s suggestion is quite right -- a more specific test around the error would have prevented this problem, so it would be good to include here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants