-
-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend HostBuilderExtensions.RunCommandLineApplicationAsync to suppor…
…t injection of IConsole and IConvention. (#178)
- Loading branch information
1 parent
d72af41
commit a0e8d52
Showing
10 changed files
with
260 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
test/Hosting.CommandLine.Tests/HostBuilderExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using System.IO; | ||
using McMaster.Extensions.CommandLineUtils; | ||
using McMaster.Extensions.CommandLineUtils.Conventions; | ||
using McMaster.Extensions.Hosting.CommandLine.Tests.Utilities; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Moq; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace McMaster.Extensions.Hosting.CommandLine.Tests | ||
{ | ||
public class HostBuilderExtensionsTests | ||
{ | ||
private readonly ITestOutputHelper _output; | ||
|
||
public HostBuilderExtensionsTests(ITestOutputHelper output) | ||
{ | ||
_output = output; | ||
} | ||
|
||
[Fact] | ||
public void TestReturnCode() | ||
{ | ||
Assert.Equal(42, | ||
new HostBuilder() | ||
.ConfigureServices(collection => collection.AddSingleton<IConsole>(new TestConsole(_output))) | ||
.RunCommandLineApplicationAsync<Return42Command>(new string[0]) | ||
.GetAwaiter() | ||
.GetResult()); | ||
} | ||
|
||
[Fact] | ||
public async void TestConsoleInjection() | ||
{ | ||
var console = new Mock<IConsole>(); | ||
var textWriter = new Mock<TextWriter>(); | ||
textWriter.Setup(writer => writer.WriteLine("42")).Verifiable(); | ||
console.SetupGet(c => c.Out).Returns(textWriter.Object); | ||
await new HostBuilder() | ||
.ConfigureServices(collection => collection.AddSingleton<IConsole>(console.Object)) | ||
.RunCommandLineApplicationAsync<Write42Command>(new string[0]); | ||
Mock.Verify(console, textWriter); | ||
} | ||
|
||
[Fact] | ||
public async void TestConventionInjection() | ||
{ | ||
var valueHolder = new ValueHolder<string[]>(); | ||
var convention = new Mock<IConvention>(); | ||
convention.Setup(c => c.Apply(It.IsAny<ConventionContext>())) | ||
.Callback((ConventionContext c) => c.Application.ThrowOnUnexpectedArgument = false).Verifiable(); | ||
var args = new[] {"Capture", "some", "test", "arguments"}; | ||
await new HostBuilder() | ||
.ConfigureServices(collection => collection | ||
.AddSingleton<IConsole>(new TestConsole(_output)) | ||
.AddSingleton(valueHolder) | ||
.AddSingleton(convention.Object)) | ||
.RunCommandLineApplicationAsync<CaptureRemainingArgsCommand>(args); | ||
Assert.Equal(args, valueHolder.Value); | ||
Mock.Verify(convention); | ||
} | ||
|
||
public class Return42Command | ||
{ | ||
private int OnExecute() | ||
{ | ||
return 42; | ||
} | ||
} | ||
|
||
public class Write42Command | ||
{ | ||
private void OnExecute(CommandLineApplication<Write42Command> app) | ||
{ | ||
app.Out.WriteLine("42"); | ||
} | ||
} | ||
|
||
public class ValueHolder<T> | ||
{ | ||
public T Value { get; set; } | ||
} | ||
|
||
[Command("Capture")] | ||
public class CaptureRemainingArgsCommand | ||
{ | ||
public ValueHolder<string[]> ValueHolder { get; set; } | ||
|
||
public string[] RemainingArguments | ||
{ | ||
get => ValueHolder.Value; | ||
set => ValueHolder.Value = value; | ||
} | ||
|
||
public CaptureRemainingArgsCommand(ValueHolder<string[]> valueHolder) | ||
{ | ||
ValueHolder = valueHolder; | ||
} | ||
|
||
private void OnExecute(CommandLineApplication<CaptureRemainingArgsCommand> app) | ||
{ | ||
} | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
test/Hosting.CommandLine.Tests/McMaster.Extensions.Hosting.CommandLine.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>netcoreapp2.1;net461</TargetFrameworks> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Hosting.CommandLine\McMaster.Extensions.Hosting.CommandLine.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup Condition="'$(TargetFramework)' == 'net461'"> | ||
<Reference Include="System.ComponentModel.DataAnnotations" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" /> | ||
<PackageReference Include="Moq" Version="4.10.0" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Update="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) Nate McMaster. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.IO; | ||
using McMaster.Extensions.CommandLineUtils; | ||
using Xunit.Abstractions; | ||
|
||
namespace McMaster.Extensions.Hosting.CommandLine.Tests.Utilities | ||
{ | ||
public class TestConsole : IConsole | ||
{ | ||
public TestConsole(ITestOutputHelper output) | ||
{ | ||
Out = new XunitTextWriter(output); | ||
Error = new XunitTextWriter(output); | ||
} | ||
|
||
public TextWriter Out { get; set; } | ||
|
||
public TextWriter Error { get; set; } | ||
|
||
public TextReader In => throw new NotImplementedException(); | ||
|
||
public bool IsInputRedirected => throw new NotImplementedException(); | ||
|
||
public bool IsOutputRedirected => true; | ||
|
||
public bool IsErrorRedirected => true; | ||
|
||
public ConsoleColor ForegroundColor { get; set; } | ||
public ConsoleColor BackgroundColor { get; set; } | ||
|
||
public event ConsoleCancelEventHandler CancelKeyPress | ||
{ | ||
add { } | ||
remove { } | ||
} | ||
|
||
public void ResetColor() | ||
{ | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
test/Hosting.CommandLine.Tests/Utilities/XunitTextWriter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) Nate McMaster. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.IO; | ||
using System.Text; | ||
using Xunit.Abstractions; | ||
|
||
namespace McMaster.Extensions.Hosting.CommandLine.Tests.Utilities | ||
{ | ||
public class XunitTextWriter : TextWriter | ||
{ | ||
private readonly ITestOutputHelper _output; | ||
private readonly StringBuilder _sb = new StringBuilder(); | ||
|
||
public XunitTextWriter(ITestOutputHelper output) | ||
{ | ||
_output = output; | ||
} | ||
|
||
public override Encoding Encoding => Encoding.Unicode; | ||
|
||
public override void Write(char ch) | ||
{ | ||
if (ch == '\n') | ||
{ | ||
_output.WriteLine(_sb.ToString()); | ||
_sb.Clear(); | ||
} | ||
else | ||
{ | ||
_sb.Append(ch); | ||
} | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
{ | ||
if (_sb.Length > 0) | ||
{ | ||
_output.WriteLine(_sb.ToString()); | ||
_sb.Clear(); | ||
} | ||
} | ||
|
||
base.Dispose(disposing); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"methodDisplay": "method" | ||
} |