azure-webjobs-sdk
azure-webjobs-sdk copied to clipboard
BindingParameterResolverTests fails in most locales
The unit test BindingParameterResolverTests.DateTimeResolver_ReturnsExpectedValue
is locale-dependent and shouldn't be.
Repro steps
- Switch to a locale such as en-GB.
- Set the date to a date on the 13th or later of the month.
- Run the unit tests.
Expected behavior
The unit tests should pass.
Actual behavior
DateTimeResolver_ReturnsExpectedValue
fails with a FormatException
.
Microsoft.Azure.WebJobs.Host.UnitTests.Bindings.Path.BindingParameterResolverTests.DateTimeResolver_ReturnsExpectedValue Source: BindingParameterResolverTests.cs line 68 Duration: 1 ms Message: System.FormatException : String '02/16/2023 08:35:41' was not recognized as a valid DateTime. Stack Trace: DateTimeParse.Parse(ReadOnlySpan`1 s, DateTimeFormatInfo dtfi, DateTimeStyles styles) DateTime.Parse(String s) BindingParameterResolverTests.DateTimeResolver_ReturnsExpectedValue() line 77
The problem is that resolvedValue = resolver.Resolve("datetime:G")
uses CultureInfo.InvariantCulture
, but the basic DateTime.Parse(resolvedValue)
of the following assert uses whatever the current culture of the test computer is. For historical reasons CultureInfo.InvariantCulture
is, if not an alias of en-US, very close to it, but the majority of cultures don't use middle-endian dates.
Known workarounds
Setting the computer's locale to en-US is a workaround, but somewhat antithetical to the desire to welcome outside contributions.
I see this as a bug in the test rather than the code tested, so I think the correct fix is to change DateTime.Parse(resolvedValue)
to DateTime.Parse(resolvedValue, CultureInfo.InvariantCulture)
.
Related information
Present in e210142b674d1324fbbe49ffe5767f834e15c719 , which is the current dev.