Skip to content

Commit

Permalink
fix(execution): resolve empty environment variables as empty arrays (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
matkoch committed Sep 9, 2024
1 parent 71ee501 commit dbae75d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
9 changes: 7 additions & 2 deletions source/Nuke.Build.Tests/ParameterServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public void TestNotSupplied(Type destinationType, object expectedValue)
[InlineData("arg2", typeof(string), "value3")]
[InlineData("switch2", typeof(bool), true)]
[InlineData("switch3", typeof(bool), false)]
[InlineData("array1", typeof(string[]), new[] { "element1", "element2" })]
[InlineData("array2", typeof(string[]), new string[0])]
[InlineData("array3", typeof(string[]), null)]
[InlineData("notsupplied1", typeof(bool), false)]
[InlineData("notsupplied2", typeof(int?), null)]
public void TestEnvironmentVariables(string parameter, Type destinationType, object expectedValue)
Expand All @@ -58,9 +61,11 @@ public void TestEnvironmentVariables(string parameter, Type destinationType, obj
{ "arg1", "value2" },
{ "arg2", "value3" },
{ "switch2", "true" },
{ "switch3", "false" }
{ "switch3", "false" },
{ "array1", "element1+element2" },
{ "array2", "" },
});
service.GetParameter(parameter, destinationType, separator: null).Should().Be(expectedValue);
service.GetParameter(parameter, destinationType, separator: '+').Should().BeEquivalentTo(expectedValue);
}

[Fact]
Expand Down
19 changes: 2 additions & 17 deletions source/Nuke.Build/Execution/ParameterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,26 +214,11 @@ static string GetTrimmedName(string name)

try
{
return ConvertValues(variableName, separator.HasValue ? value.Split(separator.Value) : new[] { value }, destinationType);
return Convert(value, destinationType, separator);
}
catch (Exception ex)
{
Assert.Fail(new[] { ex.Message, "Environment variable was:", value }.JoinNewLine());
// ReSharper disable once HeuristicUnreachableCode
return null;
}
}

[CanBeNull]
private object ConvertValues(string parameterName, IReadOnlyCollection<string> values, Type destinationType)
{
try
{
return Convert(values, destinationType);
}
catch (Exception ex)
{
Assert.Fail(new[] { $"Resolving parameter '{parameterName}' failed.", ex.Message }.JoinNewLine());
Assert.Fail(new[] { ex.Message, $"Resolving parameter '{variableName}' failed. Environment variable was:", value }.JoinNewLine());
// ReSharper disable once HeuristicUnreachableCode
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,16 @@ public void TestConversionCollections()
.Should().BeOfType<string[]>().Which
.Should().BeEmpty();

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

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

ReflectionUtility.Convert("1 2 3", typeof(int[]), separator: ' ')
.Should().BeOfType<int[]>().Which
.Should().HaveCount(3)
.And.Contain(2);
.Should().Equal(1, 2, 3);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public static object Convert(object value, Type destinationType)
[CanBeNull]
public static object Convert(string value, Type destinationType, char? separator)
{
return Convert(separator.HasValue ? value.Split(separator.Value) : new[] { value }, destinationType);
var values = (separator.HasValue ? value.Split(separator.Value) : new[] { value })
.Where(x => !x.IsNullOrWhiteSpace()).ToArray();
return Convert(values, destinationType);
}

[CanBeNull]
Expand Down

0 comments on commit dbae75d

Please sign in to comment.