Skip to content

Commit

Permalink
Merge pull request #15 from itn3000/fix-alias-skip
Browse files Browse the repository at this point in the history
Fix CommandAttribute's name not working
  • Loading branch information
neuecc authored Sep 17, 2019
2 parents 178ede0 + e4f3656 commit beb4583
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 21 deletions.
50 changes: 29 additions & 21 deletions src/MicroBatchFramework/BatchEngineHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,43 +200,51 @@ static List<Type> GetBatchTypes()

static (Type, MethodInfo) GetTypeFromAssemblies(string arg0)
{
var split = arg0.Split('.');
if (split.Length != 2)
{
return (null, null);
}
var typeName = split[0];
var methodName = split[1];

var batchBaseTypes = GetBatchTypes();
if (batchBaseTypes == null)
{
return (null, null);
}

var split = arg0.Split('.');
Type foundType = null;
foreach (var item in batchBaseTypes)
MethodInfo foundMethod = null;
foreach (var baseType in batchBaseTypes)
{
if (item.Name == typeName)
bool isFound = false;
foreach (var (method, cmdattr) in baseType.GetMethods().
Select(m => (MethodInfo: m, Attr: m.GetCustomAttribute<CommandAttribute>())).Where(x => x.Attr != null))
{
if (foundType != null)
if (cmdattr.CommandNames.Any(x => arg0.Equals(x, StringComparison.OrdinalIgnoreCase)))
{
throw new InvalidOperationException("Duplicate BatchBase TypeName is not allowed, " + foundType.FullName + " and " + item.FullName);
if(foundType != null && foundMethod != null)
{
throw new InvalidOperationException($"Duplicate BatchBase Command name is not allowed, {foundType.FullName}.{foundMethod.Name} and {baseType.FullName}.{method.Name}");
}
foundType = baseType;
foundMethod = method;
isFound = true;
}
foundType = item;
}
}

if (foundType != null)
{
var method = foundType.GetMethod(methodName);
if (method != null)
if (!isFound && split.Length == 2)
{
return (foundType, method);
if (baseType.Name.Equals(split[0], StringComparison.OrdinalIgnoreCase))
{
if (foundType != null)
{
throw new InvalidOperationException("Duplicate BatchBase TypeName is not allowed, " + foundType.FullName + " and " + baseType.FullName);
}
foundType = baseType;
foundMethod = baseType.GetMethod(split[1]);
}
}
}

if(foundType != null && foundMethod != null)
{
return (foundType, foundMethod);
}
return (null, null);

}
}
}
47 changes: 47 additions & 0 deletions tests/MicroBatchFramework.Tests/CommandAttributeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using FluentAssertions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace MicroBatchFramework.Tests
{
public class CommandAttributeTest
{
class CommandAttributeTestCommand : BatchBase
{
ResultContainer _Result;
public CommandAttributeTestCommand(ResultContainer r)
{
_Result = r;
}
[Command("test")]
public void TestCommand(int value)
{
_Result.X = value;
}
}
class ResultContainer
{
public int X;
}
[Fact]
public async Task TestCommandName()
{
var host = BatchHost.CreateDefaultBuilder()
.ConfigureServices((c, services) =>
{
services.AddSingleton<ResultContainer>();
})
.UseBatchEngine<CommandAttributeTestCommand>(new string[]{ "test", "-value", "1" })
.Build();
var result = host.Services.GetService<ResultContainer>();
await host.RunAsync();
result.X.Should().Be(1);
}

}
}

0 comments on commit beb4583

Please sign in to comment.