Skip to content

Commit

Permalink
fix(utilities): mandatory boolean default for conversion from string …
Browse files Browse the repository at this point in the history
…values
  • Loading branch information
matkoch committed Oct 13, 2024
1 parent 6895f4c commit 1b0de60
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 10 deletions.
7 changes: 7 additions & 0 deletions source/Nuke.Build.Tests/ParameterServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ public void TestNotSupplied(Type destinationType, object expectedValue)
[Theory]
[InlineData("arg1", typeof(string), "value1")]
[InlineData("arg2", typeof(string), "value3")]
[InlineData("arg3", typeof(string), "value4")]
[InlineData("switch1", typeof(bool), true)]
[InlineData("switch2", typeof(bool), true)]
[InlineData("switch3", typeof(bool), false)]
[InlineData("switch4", typeof(bool), false)]
[InlineData("switch5", typeof(bool), true)]
[InlineData("array1", typeof(string[]), new[] { "element1", "element2" })]
[InlineData("array2", typeof(string[]), new string[0])]
[InlineData("array3", typeof(string[]), null)]
Expand All @@ -60,8 +64,11 @@ public void TestEnvironmentVariables(string parameter, Type destinationType, obj
{
{ "arg1", "value2" },
{ "arg2", "value3" },
{ "nuke_arg_3", "value4" },
{ "switch2", "true" },
{ "switch3", "false" },
{ "switch4", "" },
{ "nuke_switch_5", "true" },
{ "array1", "element1+element2" },
{ "array2", "" },
});
Expand Down
2 changes: 1 addition & 1 deletion source/Nuke.Build/Execution/ParameterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ static string GetTrimmedName(string name)

try
{
return Convert(value, destinationType, separator);
return Convert(value, destinationType, separator, booleanDefault: false);
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ public void TestConversionCollections()
.Should().BeOfType<string[]>().Which
.Should().BeEmpty();

ReflectionUtility.Convert(string.Empty, typeof(string[]), separator: '+')
ReflectionUtility.Convert(string.Empty, typeof(string[]), separator: '+', booleanDefault: true)
.Should().BeOfType<string[]>().Which
.Should().BeEmpty();

ReflectionUtility.Convert("A+B+C", typeof(string[]), separator: '+')
ReflectionUtility.Convert("A+B+C", typeof(string[]), separator: '+', booleanDefault: true)
.Should().BeOfType<string[]>().Which
.Should().Equal("A", "B", "C");

ReflectionUtility.Convert("1 2 3", typeof(int[]), separator: ' ')
ReflectionUtility.Convert("1 2 3", typeof(int[]), separator: ' ', booleanDefault: true)
.Should().BeOfType<int[]>().Which
.Should().Equal(1, 2, 3);
}
Expand Down
2 changes: 1 addition & 1 deletion source/Nuke.Utilities/ArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private object ConvertValues(string argumentName, IReadOnlyCollection<string> va
{
try
{
return ReflectionUtility.Convert(values, destinationType);
return ReflectionUtility.Convert(values, destinationType, booleanDefault: true);
}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion source/Nuke.Utilities/EnvironmentInfo.Variables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static T GetVariable<T>(string name, char? separator = null)
if (value == null)
return default;

return (T)ReflectionUtility.Convert(value, typeof(T), separator);
return (T)ReflectionUtility.Convert(value, typeof(T), separator, booleanDefault: false);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ public static object Convert(object value, Type destinationType)

// TODO: rename overloads?
[CanBeNull]
public static object Convert(string value, Type destinationType, char? separator)
public static object Convert(string value, Type destinationType, char? separator, bool booleanDefault)
{
var values = (separator.HasValue ? value.Split(separator.Value) : new[] { value })
.Where(x => !x.IsNullOrWhiteSpace()).ToArray();
return Convert(values, destinationType);
return Convert(values, destinationType, booleanDefault);
}

[CanBeNull]
public static object Convert(IReadOnlyCollection<string> values, Type destinationType)
public static object Convert(IReadOnlyCollection<string> values, Type destinationType, bool booleanDefault)
{
Assert.True(!destinationType.IsArray || destinationType.GetArrayRank() == 1, "Arrays must have a rank of 1");
var elementType = (destinationType.IsArray ? destinationType.GetElementType() : destinationType).NotNull();
Expand All @@ -62,7 +62,7 @@ public static object Convert(IReadOnlyCollection<string> values, Type destinatio
return Array.CreateInstance(elementType, length: 0);

if (destinationType == typeof(bool) || destinationType == typeof(bool?))
return true;
return booleanDefault;

return null;
}
Expand Down

0 comments on commit 1b0de60

Please sign in to comment.