-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fix New-ModuleManifest handling of uri's #3631
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix New-ModuleManifest handling of uri's #3631
Conversation
dantraMSFT
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrap the return statement in brackets
| /// <param name="name">The string to quote</param> | ||
| /// <returns>The quoted string</returns> | ||
| private string QuoteName(object name) | ||
| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method comments should be updated to reflect string or URI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will fix
| BuildPrivateDataInModuleManifest(result, streamWriter); | ||
|
|
||
| BuildModuleManifest(result, "HelpInfoURI", Modules.HelpInfoURI, !string.IsNullOrEmpty(_helpInfoUri), () => QuoteName(_helpInfoUri), streamWriter); | ||
| BuildModuleManifest(result, "HelpInfoURI", Modules.HelpInfoURI, !string.IsNullOrEmpty(_helpInfoUri), () => QuoteName((null != _helpInfoUri) ? new Uri(_helpInfoUri) : null), streamWriter); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This statement is getting rather convoluted. Suggest breaking out the QuoteName logic for readability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since QuoteName() takes a null but Uri constructor doesn't accept null, I think we'd end up with a bunch of code that makes it harder to read in this particular case.
| Dbg.Assert(!String.IsNullOrWhiteSpace(parameterName), "parameterName should not be null or whitespace"); | ||
|
|
||
| if (uri != null && !Uri.IsWellFormedUriString(uri.ToString(), UriKind.Absolute)) | ||
| if (uri != null && !Uri.IsWellFormedUriString(uri.AbsoluteUri, UriKind.Absolute)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're using inconsistent comparison to null ordering. (i.e., value != null versus null != value). Suggest picking one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer null on the left, but most of the code in this file has it on the right, I'll change my usage to make it consistent
| { | ||
| if (name == null) | ||
| return "''"; | ||
| else if (name is Uri) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid casting multiple times. consider assigning name as Uri and testing for null.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, name may or may not be a Uri. I'm special casing when it is a Uri, I want to use the AbsoluteUri member value.
| /// <param name="name">The string to quote</param> | ||
| /// <returns>The quoted string</returns> | ||
| private string QuoteName(object name) | ||
| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method comments should be updated to reflect string or URI. Better would be to have two explicit overloads and remove the 'object' overload since I don't think anything other than uri and string are expected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked through the calls to QuoteName() and I believe you are right that it's only a string or Uri. I'll separate it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually Version is another type that needs to be supported
3c75047 to
5adb431
Compare
…by using ToString() which just outputs the original string. If the string was a uri with spaces, ToString() doesn't return the escaped version. The AbsoluteUri property should be used instead which returns an escaped absolute uri (if valid). Also renamed TestModuleManfest.ps1 to TestModuleManifest.Tests.ps1 so that it gets picked up correctly as Pester test. Since HelpInfoUri is just a string, ensure it is a valid absolute uri and escaped correctly whereas before it was just an opaque string that wasn't validated.
5adb431 to
5017f79
Compare
New-ModuleManifest was incorrectly checking if a Uri was well formed by using ToString() which just outputs the original string. If the string was a uri with spaces, ToString() doesn't return the escaped version. The AbsoluteUri property should be used instead which returns an escaped absolute uri (if valid).
Also renamed TestModuleManfest.ps1 to TestModuleManifest.Tests.ps1 so that it gets picked up correctly as Pester test.
Since HelpInfoUri is just a string, ensure it is a valid absolute uri and escaped correctly whereas before it was just an opaque string that wasn't validated.
Addresses #3336