Skip to content

Commit

Permalink
Fix requirement check of parameters defined in interfaces (#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
tunger authored May 27, 2020
1 parent 3baa3f3 commit c40f197
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,38 @@ public void TestMultipleInheritance()
b.AllDependencies.Should().NotBeEmpty();
}

[Fact]
public void TestRequirementValidation()
{
EnvironmentInfo.SetVariable("StringParameter", "hello");
var build = new ParameterBuild();
var targets = ExecutableTargetFactory.CreateAll(build, x => ((IParameterInterface)x).HelloWorld);

// must not throw
RequirementService.ValidateRequirements(build, targets);
}

[Fact]
public void TestInvalidDependencyType()
{
var build = new InvalidDependencyTypeTestBuild();
Assert.Throws<InvalidCastException>(() => ExecutableTargetFactory.CreateAll(build, x => x.E));
}

private interface IParameterInterface
{
[Parameter] string StringParameter => InjectionUtility.GetInjectionValue(() => StringParameter);

public Target HelloWorld => _ => _
.Requires(() => StringParameter)
.Executes(() =>
{
Logger.Info(StringParameter);
});
}

private class ParameterBuild : NukeBuild, IParameterInterface { }

private class TestBuild : NukeBuild, ITestBuild
{
public Target E => _ => _
Expand Down
8 changes: 5 additions & 3 deletions source/Nuke.Common/Execution/RequirementService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ public static void ValidateRequirements(NukeBuild build, IReadOnlyCollection<Exe

private static bool IsMemberNull(MemberInfo member, NukeBuild build, ExecutableTarget target = null)
{
member = member.DeclaringType != build.GetType()
? build.GetType().GetMember(member.Name).Single()
: member;
if (member.DeclaringType != build.GetType())
{
// Can happen when a derived class overrides a member
member = build.GetType().GetMember(member.Name).SingleOrDefault() ?? member;
}

var from = target != null ? $"from target '{target.Name}' " : string.Empty;
ControlFlow.Assert(member.HasCustomAttribute<InjectionAttributeBase>(),
Expand Down

0 comments on commit c40f197

Please sign in to comment.