Skip to content

Commit 51b2629

Browse files
committed
first command alias searching, add test
1 parent b5e2f30 commit 51b2629

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

src/MicroBatchFramework/BatchEngineHostBuilderExtensions.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -211,28 +211,27 @@ static List<Type> GetBatchTypes()
211211
MethodInfo foundMethod = null;
212212
foreach (var baseType in batchBaseTypes)
213213
{
214-
if (split.Length == 2)
214+
bool isFound = false;
215+
foreach (var (method, cmdattr) in baseType.GetMethods().
216+
Select(m => (MethodInfo: m, Attr: m.GetCustomAttribute<CommandAttribute>())).Where(x => x.Attr != null))
215217
{
216-
if (baseType.Name.Equals(split[0], StringComparison.OrdinalIgnoreCase))
218+
if (cmdattr.CommandNames.Any(x => arg0.Equals(x, StringComparison.OrdinalIgnoreCase)))
217219
{
218-
if (foundType != null)
219-
{
220-
throw new InvalidOperationException("Duplicate BatchBase TypeName is not allowed, " + foundType.FullName + " and " + baseType.FullName);
221-
}
222220
foundType = baseType;
223-
foundMethod = baseType.GetMethod(split[1]);
221+
foundMethod = method;
222+
isFound = true;
224223
}
225224
}
226-
else
225+
if (!isFound && split.Length == 2)
227226
{
228-
foreach (var (method, cmdattr) in baseType.GetMethods().
229-
Select(m => (MethodInfo: m, Attr: m.GetCustomAttribute<CommandAttribute>())).Where(x => x.Attr != null))
227+
if (baseType.Name.Equals(split[0], StringComparison.OrdinalIgnoreCase))
230228
{
231-
if (cmdattr.CommandNames.Any(x => arg0.Equals(x, StringComparison.OrdinalIgnoreCase)))
229+
if (foundType != null)
232230
{
233-
foundType = baseType;
234-
foundMethod = method;
231+
throw new InvalidOperationException("Duplicate BatchBase TypeName is not allowed, " + foundType.FullName + " and " + baseType.FullName);
235232
}
233+
foundType = baseType;
234+
foundMethod = baseType.GetMethod(split[1]);
236235
}
237236
}
238237
}

tests/MicroBatchFramework.Tests/CommandAttributeTest.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ class ResultContainer
3131
[Fact]
3232
public async Task TestCommandName()
3333
{
34-
var hostBuilder = BatchHost.CreateDefaultBuilder()
34+
var host = BatchHost.CreateDefaultBuilder()
3535
.ConfigureServices((c, services) =>
3636
{
3737
services.AddSingleton<ResultContainer>();
38-
});
39-
await hostBuilder.RunBatchEngineAsync(new string[]{ "test", "-value", "1" });
38+
})
39+
.UseBatchEngine<CommandAttributeTestCommand>(new string[]{ "test", "-value", "1" })
40+
.Build();
41+
var result = host.Services.GetService<ResultContainer>();
42+
await host.RunAsync();
43+
result.X.Should().Be(1);
4044
}
4145

4246
}

0 commit comments

Comments
 (0)