System.CommandLine.Tests converting '123.456' fails in "fi-FI" culture on Windows
I built ec67fa283e6690c63731c7c51293bb21833761ec using .NET SDK 6.0.302 on Windows x64 and ran dotnet test --no-build in the Finnish (Finland) culture. Several tests failed, presumably because NumberFormatInfo.NumberDecimalSeparator is a comma , rather than a period .:
[xUnit.net 00:00:04.61] System.CommandLine.Tests.Binding.TypeConversionTests.Values_can_be_correctly_converted_to_nullable_float_without_the_parser_specifying_a_custom_converter [FAIL]
[xUnit.net 00:00:04.62] System.CommandLine.Tests.Binding.TypeConversionTests.Values_can_be_correctly_converted_to_double_without_the_parser_specifying_a_custom_converter [FAIL]
Failed System.CommandLine.Tests.Binding.TypeConversionTests.Values_can_be_correctly_converted_to_nullable_float_without_the_parser_specifying_a_custom_converter [5 ms]
Error Message:
System.InvalidOperationException : Cannot parse argument '123.456' for option '-x' as expected type 'System.Nullable`1[System.Single]'.
Stack Trace:
at System.CommandLine.Binding.ArgumentConverter.GetValueOrDefault[T](ArgumentConversionResult result) in [REDACTED]\command-line-api\src\System.CommandLine\Binding\ArgumentConverter.cs:line 211
at System.CommandLine.Parsing.OptionResult.GetValueOrDefault[T]() in [REDACTED]\command-line-api\src\System.CommandLine\Parsing\OptionResult.cs:line 55
at System.CommandLine.Parsing.SymbolResult.GetValueForOption[T](Option`1 option) in [REDACTED]\command-line-api\src\System.CommandLine\Parsing\SymbolResult.cs:line 158
at System.CommandLine.ParseResult.GetValueForOption[T](Option`1 option) in [REDACTED]\command-line-api\src\System.CommandLine\ParseResult.cs:line 168
at System.CommandLine.Tests.Binding.TypeConversionTests.Values_can_be_correctly_converted_to_nullable_float_without_the_parser_specifying_a_custom_converter() in [REDACTED]\command-line-api\src\System.CommandLine.Tests\Binding\TypeConversionTests.cs:line 545
Failed System.CommandLine.Tests.Binding.TypeConversionTests.Values_can_be_correctly_converted_to_double_without_the_parser_specifying_a_custom_converter [3 ms]
Error Message:
System.InvalidOperationException : Cannot parse argument '123.456' for option '-x' as expected type 'System.Double'.
Stack Trace:
at System.CommandLine.Binding.ArgumentConverter.GetValueOrDefault[T](ArgumentConversionResult result) in [REDACTED]\command-line-api\src\System.CommandLine\Binding\ArgumentConverter.cs:line 211
at System.CommandLine.Parsing.OptionResult.GetValueOrDefault[T]() in [REDACTED]\command-line-api\src\System.CommandLine\Parsing\OptionResult.cs:line 55
at System.CommandLine.Parsing.SymbolResult.GetValueForOption[T](Option`1 option) in [REDACTED]\command-line-api\src\System.CommandLine\Parsing\SymbolResult.cs:line 158
at System.CommandLine.ParseResult.GetValueForOption[T](Option`1 option) in [REDACTED]\command-line-api\src\System.CommandLine\ParseResult.cs:line 168
at System.CommandLine.Tests.Binding.TypeConversionTests.Values_can_be_correctly_converted_to_double_without_the_parser_specifying_a_custom_converter() in [REDACTED]\command-line-api\src\System.CommandLine.Tests\Binding\TypeConversionTests.cs:line 515
[xUnit.net 00:00:04.65] System.CommandLine.Tests.Binding.TypeConversionTests.Values_can_be_correctly_converted_to_nullable_double_without_the_parser_specifying_a_custom_converter [FAIL]
Failed System.CommandLine.Tests.Binding.TypeConversionTests.Values_can_be_correctly_converted_to_nullable_double_without_the_parser_specifying_a_custom_converter [3 ms]
Error Message:
System.InvalidOperationException : Cannot parse argument '123.456' for option '-x' as expected type 'System.Nullable`1[System.Double]'.
Stack Trace:
at System.CommandLine.Binding.ArgumentConverter.GetValueOrDefault[T](ArgumentConversionResult result) in [REDACTED]\command-line-api\src\System.CommandLine\Binding\ArgumentConverter.cs:line 211
at System.CommandLine.Parsing.OptionResult.GetValueOrDefault[T]() in [REDACTED]\command-line-api\src\System.CommandLine\Parsing\OptionResult.cs:line 55
at System.CommandLine.Parsing.SymbolResult.GetValueForOption[T](Option`1 option) in [REDACTED]\command-line-api\src\System.CommandLine\Parsing\SymbolResult.cs:line 158
at System.CommandLine.ParseResult.GetValueForOption[T](Option`1 option) in [REDACTED]\command-line-api\src\System.CommandLine\ParseResult.cs:line 168
at System.CommandLine.Tests.Binding.TypeConversionTests.Values_can_be_correctly_converted_to_nullable_double_without_the_parser_specifying_a_custom_converter() in [REDACTED]\command-line-api\src\System.CommandLine.Tests\Binding\TypeConversionTests.cs:line 525
[xUnit.net 00:00:04.67] System.CommandLine.Tests.Binding.TypeConversionTests.Values_can_be_correctly_converted_to_float_without_the_parser_specifying_a_custom_converter [FAIL]
Failed System.CommandLine.Tests.Binding.TypeConversionTests.Values_can_be_correctly_converted_to_float_without_the_parser_specifying_a_custom_converter [2 ms]
Error Message:
System.InvalidOperationException : Cannot parse argument '123.456' for option '-x' as expected type 'System.Single'.
Stack Trace:
at System.CommandLine.Binding.ArgumentConverter.GetValueOrDefault[T](ArgumentConversionResult result) in [REDACTED]\command-line-api\src\System.CommandLine\Binding\ArgumentConverter.cs:line 211
at System.CommandLine.Parsing.OptionResult.GetValueOrDefault[T]() in [REDACTED]\command-line-api\src\System.CommandLine\Parsing\OptionResult.cs:line 55
at System.CommandLine.Parsing.SymbolResult.GetValueForOption[T](Option`1 option) in [REDACTED]\command-line-api\src\System.CommandLine\Parsing\SymbolResult.cs:line 158
at System.CommandLine.ParseResult.GetValueForOption[T](Option`1 option) in [REDACTED]\command-line-api\src\System.CommandLine\ParseResult.cs:line 168
at System.CommandLine.Tests.Binding.TypeConversionTests.Values_can_be_correctly_converted_to_float_without_the_parser_specifying_a_custom_converter() in [REDACTED]\command-line-api\src\System.CommandLine.Tests\Binding\TypeConversionTests.cs:line 535
[xUnit.net 00:00:04.71] System.CommandLine.Tests.Binding.TypeConversionTests.Values_can_be_correctly_converted_to_nullable_decimal_without_the_parser_specifying_a_custom_converter [FAIL]
Failed System.CommandLine.Tests.Binding.TypeConversionTests.Values_can_be_correctly_converted_to_nullable_decimal_without_the_parser_specifying_a_custom_converter [6 ms]
Error Message:
System.InvalidOperationException : Cannot parse argument '123.456' for option '-x' as expected type 'System.Nullable`1[System.Decimal]'.
Stack Trace:
at System.CommandLine.Binding.ArgumentConverter.GetValueOrDefault[T](ArgumentConversionResult result) in [REDACTED]\command-line-api\src\System.CommandLine\Binding\ArgumentConverter.cs:line 211
at System.CommandLine.Parsing.OptionResult.GetValueOrDefault[T]() in [REDACTED]\command-line-api\src\System.CommandLine\Parsing\OptionResult.cs:line 55
at System.CommandLine.Parsing.SymbolResult.GetValueForOption[T](Option`1 option) in [REDACTED]\command-line-api\src\System.CommandLine\Parsing\SymbolResult.cs:line 158
at System.CommandLine.ParseResult.GetValueForOption[T](Option`1 option) in [REDACTED]\command-line-api\src\System.CommandLine\ParseResult.cs:line 168
at System.CommandLine.Tests.Binding.TypeConversionTests.Values_can_be_correctly_converted_to_nullable_decimal_without_the_parser_specifying_a_custom_converter() in [REDACTED]\command-line-api\src\System.CommandLine.Tests\Binding\TypeConversionTests.cs:line 505
[xUnit.net 00:00:04.72] System.CommandLine.Tests.Binding.TypeConversionTests.Values_can_be_correctly_converted_to_decimal_without_the_parser_specifying_a_custom_converter [FAIL]
Failed System.CommandLine.Tests.Binding.TypeConversionTests.Values_can_be_correctly_converted_to_decimal_without_the_parser_specifying_a_custom_converter [3 ms]
Error Message:
System.InvalidOperationException : Cannot parse argument '123.456' for option '-x' as expected type 'System.Decimal'.
Stack Trace:
at System.CommandLine.Binding.ArgumentConverter.GetValueOrDefault[T](ArgumentConversionResult result) in [REDACTED]\command-line-api\src\System.CommandLine\Binding\ArgumentConverter.cs:line 211
at System.CommandLine.Parsing.OptionResult.GetValueOrDefault[T]() in [REDACTED]\command-line-api\src\System.CommandLine\Parsing\OptionResult.cs:line 55
at System.CommandLine.Parsing.SymbolResult.GetValueForOption[T](Option`1 option) in [REDACTED]\command-line-api\src\System.CommandLine\Parsing\SymbolResult.cs:line 158
at System.CommandLine.ParseResult.GetValueForOption[T](Option`1 option) in [REDACTED]\command-line-api\src\System.CommandLine\ParseResult.cs:line 168
at System.CommandLine.Tests.Binding.TypeConversionTests.Values_can_be_correctly_converted_to_decimal_without_the_parser_specifying_a_custom_converter() in [REDACTED]\command-line-api\src\System.CommandLine.Tests\Binding\TypeConversionTests.cs:line 495
Test run for [REDACTED]\command-line-api\artifacts\bin\System.CommandLine.Hosting.Tests\Debug\net462\System.CommandLine.Hosting.Tests.dll (.NETFramework,Version=v4.6.2)
Failed! - Failed: 6, Passed: 836, Skipped: 7, Total: 849, Duration: 856 ms - System.CommandLine.Tests.dll (net6.0)
The test failures were previously reported in https://github.com/dotnet/command-line-api/issues/1733. I'm not marking this as a duplicate though, because here I'm primarily interested in getting the tests to pass so that it's easier to test some unrelated changes, rather than giving users or application developers a way to specify an IFormatProvider for the conversions.
https://github.com/dotnet/command-line-api/pull/951 is also related.
I tried to fix the test failures by replacing [Fact] with [CulturedFact("")] as suggested in https://github.com/dotnet/command-line-api/issues/1733#issuecomment-1121289510, but CulturedFactAttribute doesn't seem to be included in the NuGet packages of xUnit. https://github.com/xunit/xunit/discussions/2182
I guess it will instead need try…finally and assignments to CultureInfo.CurrentCulture. Or just disable these tests.
I guess we could consider copying the [UseCulture(...)] attribute from https://github.com/xunit/samples.xunit/blob/main/UseCulture/UseCultureAttribute.cs and using that (it isn't included as part of XUnit by default as per https://github.com/xunit/xunit/issues/290). I'm not really sure what the difference is between [CulturedFact] and [UseCulture].