Repository navigation
Use InvariantCulture for implicit argument conversion - #5106
Dongbo Wang (daxian-dbw) wants to merge 3 commits into
Conversation
| [CmdletBinding()] | ||
| param([DateTime] $date) | ||
| $date | ||
| } |
There was a problem hiding this comment.
Should we add C# cmdlet?
There was a problem hiding this comment.
We can use Get-Date -Date instead, so no need to add a C# cmdlet.
PS:162> gcm Get-Date -Syntax
Get-Date [[-Date] <datetime>] ...
| } | ||
|
|
||
| # Explict type cast use 'InvariantCulture' by default | ||
| $expected = [datetime] "1/11/1111" |
There was a problem hiding this comment.
Why we think that is 'InvariantCulture' ? I think we should have explicit constructor and explicit check that "Explict type cast use 'InvariantCulture' by default". Also it is another test then "Verify implicit argument conversion uses InvariantCulture".
There was a problem hiding this comment.
Using invariant culture for a cast was a choice for consistency at the language level, casting a literal would always produce the same result in any culture.
If invariant culture conversions aren't appropriate, then one simply uses the appropriate code (constructor or whatever) and specifies how to do the conversion.
There was a problem hiding this comment.
Jason Shirk (@lzybkr) Thanks for useful comment! But my question was just about the test - what if we break internally and replace InvariantCulture with other culture accidentally? So the line and comment will be wrong.
There was a problem hiding this comment.
Ah, I see, I was thinking it was a test for the conversion. I agree then, we should not rely on the default when checking results.
If we don't already, we should have a test that the cast is culture invariant.
There was a problem hiding this comment.
I will change this line to LangaugePrimitives.ConvertTo explicitly with InvariantCulture. And also will add a test to validate if the explicit conversion in powershell language is using 'InvariantCulture'.
|
There are some test failures, I will investigate whether they are test or code issues. |
|
I see all test failures is culture-specific - the PR is really breaking change! :-) |
9cded32 to
4899334
Compare
|
The early failures were because I didn't set the culture back after setting it to 'ru-RU'. It's now fixed. |
| $csharpCmdlet, $scriptFunction, $scriptCmdlet = @( | ||
| $csharpCmdlet, $scriptFunction, $scriptCmdlet = try { | ||
| $oldCulture = [CultureInfo]::CurrentCulture | ||
| [CultureInfo]::CurrentCulture = 'ru-RU' |
There was a problem hiding this comment.
We use the 'ru-Ru' string in line 300 too - maybe put it in a variable?
|
If we have so many culture-sensitive test should we fix them too? |
| $dateString = "1/11/1111" | ||
| $expected = [System.Management.Automation.LanguagePrimitives]::ConvertTo($dateString, [datetime], [cultureinfo]::InvariantCulture) | ||
| $result = try { | ||
| $oldCulture = [CultureInfo]::CurrentCulture |
There was a problem hiding this comment.
Should it be outside of try block?
| # Implict parameter argument conversion should also use 'InvariantCulture' | ||
| $csharpCmdlet, $scriptFunction, $scriptCmdlet = @( | ||
| $csharpCmdlet, $scriptFunction, $scriptCmdlet = try { | ||
| $oldCulture = [CultureInfo]::CurrentCulture |
There was a problem hiding this comment.
Should it be outside of try block?
IMO, tests which valid only in certain culture, like this one: should explicitly set that culture for themselves. |
|
Some tests call #5128 is opened for the culture-sensitive tests. |
|
@PowerShell/powershell-committee reviewed this and due to the potential for breaking many existing modules, it's too risky to make this change so that c# and script cmdlets are alike for this case. We should keep the current behavior. |
|
Given the review decision of powershell committee, I'm closing this PR. |
Fix #3348
PowerShell uses
InvariantCulturefor explicit type conversion. The implicit argument conversion for the script function/cmdlet also usesInvariantCulture. However, the implicit parameter argument type conversion for the C# cmdlet usesCurrentCulture. Change it to use 'InvariantCulture' too.