Skip to content

Commit b39a9f1

Browse files
authored
Improve DI registration (#121)
* DefaultResolver should not throw NotImplementedException * Should use interface for extension methods instead of concrete type fixed #120 * DefaultResolver should not throw NotImplementedExceptions * Mark implementation as obsolete and introduce new method that uses factory method to register * Make AddCommandLineParser extension method work * Add new constructor for #120 * Create new instance when options are null * Create new instance when options are null * Add ICommandLineParser interface * Make both generic and normal AddCommandLineParser DI extensions work #120 * Replace new keyword with dependency injected CommandLineParsers * Update tests to use new DI method
1 parent 3884ae8 commit b39a9f1

29 files changed

+570
-191
lines changed

CommandLineParser.Tests/BasicDITests.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ public void CommandLineParserUsesInjectedServiceCorrectly()
2323

2424
Services.AddSingleton(mockedService.Object);
2525

26-
var parser = new CommandLineParser(Services);
26+
Services.AddCommandLineParser();
27+
28+
var parser = ResolveParser();
2729

2830
parser.RegisterCommand<MyCommandThatUsesService>();
2931

@@ -41,7 +43,9 @@ public void CommandLineParserServiceResolvesCorrectly()
4143

4244
Services.AddSingleton(mockedService);
4345

44-
var parser = new CommandLineParser(Services);
46+
Services.AddCommandLineParser();
47+
48+
var parser = ResolveParser();
4549

4650
var resolved = parser.Services.GetRequiredService<MySerice>();
4751

CommandLineParser.Tests/Command/CommandDiscoveryTests.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ public void DiscoverCommandFromAssemblyContainsCorrectTypes()
4141
[Fact]
4242
public void DiscoveredCommandsAreRegisteredCorrectly()
4343
{
44-
var parser = new CommandLineParser<SomeBaseType>(Services);
44+
Services.AddCommandLineParser<SomeBaseType>();
45+
46+
var parser = ResolveParser<SomeBaseType>();
4547

4648
parser.DiscoverCommands(Assembly.GetExecutingAssembly());
4749

@@ -64,7 +66,8 @@ public async Task CommandDiscoveryWithInjectedServices()
6466
Services.AddSingleton(envMock.Object);
6567
Services.AddSingleton(myServiceMock.Object);
6668

67-
var parser = new CommandLineParser<MyCommandWithInjectionsOptions>(Services);
69+
Services.AddCommandLineParser<MyCommandWithInjectionsOptions>();
70+
var parser = ResolveParser<MyCommandWithInjectionsOptions>();
6871

6972
parser.DiscoverCommands(Assembly.GetExecutingAssembly());
7073

@@ -83,7 +86,8 @@ public async Task NonGenericCommandCanBeDiscovered()
8386

8487
Services.AddSingleton(argResolverMock.Object);
8588

86-
var parser = new CommandLineParser(Services);
89+
Services.AddCommandLineParser();
90+
var parser = ResolveParser();
8791

8892
parser.DiscoverCommands(typeof(NonGenericDiscoverableCommand).Assembly);
8993

@@ -110,7 +114,7 @@ public class ValidCommand2 : Command<SomeBaseType, object>
110114
{
111115
}
112116

113-
public class MyCommandWithInjectionsOptions
117+
public class MyCommandWithInjectionsOptions
114118
{
115119
}
116120

CommandLineParser.Tests/Command/CommandInModelTests.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using MatthiWare.CommandLine.Core.Attributes;
33
using Xunit;
44
using Xunit.Abstractions;
5+
using Microsoft.Extensions.DependencyInjection;
56

67
namespace MatthiWare.CommandLine.Tests.Command
78
{
@@ -13,10 +14,13 @@ public CommandInModelTests(ITestOutputHelper testOutputHelper) : base(testOutput
1314

1415
#region FindCommandsInModel
1516

16-
[Fact]
17+
[Fact(Timeout = 1000)]
1718
public void FindCommandsInModel()
1819
{
19-
var parser = new CommandLineParser<ModelWithCommands>(Services);
20+
Services.AddLogging();
21+
Services.AddCommandLineParser<ModelWithCommands>();
22+
23+
var parser = ResolveParser<ModelWithCommands>();
2024

2125
Assert.Equal(3, parser.Commands.Count);
2226
}
@@ -65,7 +69,9 @@ public override void OnConfigure(ICommandConfigurationBuilder builder)
6569
[Fact]
6670
public void FindCommandsInCommandModel()
6771
{
68-
var parser = new CommandLineParser(Services);
72+
Services.AddCommandLineParser();
73+
74+
var parser = ResolveParser();
6975

7076
parser.RegisterCommand<GenericSubCommandWithOwnOptions, SubCommandModelWithCommands>();
7177

CommandLineParser.Tests/Command/MultipleCommandTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ public MultipleCommandTests(ITestOutputHelper testOutputHelper) : base(testOutpu
1616
[InlineData(new string[] { }, false)]
1717
public void NonRequiredCommandShouldNotSetResultInErrorStateWhenRequiredOptionsAreMissing(string[] args, bool _)
1818
{
19-
var parser = new CommandLineParser(Services);
19+
Services.AddCommandLineParser();
20+
21+
var parser = ResolveParser();
2022

2123
parser.AddCommand<MultipleCommandTestsOptions>()
2224
.Name("cmd1")

CommandLineParser.Tests/Command/SubCommandTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public async Task TestSubCommandWorksCorrectlyInModelAsync(bool autoExecute, str
2727
Services.AddSingleton(new MainCommand(lock1, autoExecute, bla, i, n));
2828
Services.AddSingleton(new SubCommand(lock2, autoExecute, bla, i, n));
2929

30-
var parser = new CommandLineParser<MainModel>(Services);
30+
Services.AddCommandLineParser<MainModel>();
31+
var parser = ResolveParser<MainModel>();
3132

3233
var result = await parser.ParseAsync(new[] { "main", "-b", bla, "sub", "-i", i.ToString(), "-n", n.ToString() });
3334

CommandLineParser.Tests/CommandLineModelTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public CommandLineModelTests(ITestOutputHelper testOutputHelper) : base(testOutp
1414
[Fact]
1515
public void TestBasicModel()
1616
{
17-
var parser = new CommandLineParser<Model>(Services);
17+
Services.AddCommandLineParser<Model>();
18+
var parser = ResolveParser<Model>();
1819

1920
Assert.Equal(1, parser.Options.Count);
2021

@@ -36,7 +37,8 @@ public void TestBasicModel()
3637
[Fact]
3738
public void TestBasicModelWithOverwritingUsingFluentApi()
3839
{
39-
var parser = new CommandLineParser<Model>(Services);
40+
Services.AddCommandLineParser<Model>();
41+
var parser = ResolveParser<Model>();
4042

4143
parser.Configure(_ => _.Message)
4244
.Required(false)

0 commit comments

Comments
 (0)