Skip to content

Commit 4ec29fe

Browse files
authored
Add cancellation token support (#19)
1 parent 32905f4 commit 4ec29fe

13 files changed

Lines changed: 83 additions & 19 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- Added cancellation token support for commands
1213
- Added a built-in `verify-dependencies` command that validates the application's Dependency Injection setup by ensuring all required services are fully and correctly registered in the composition root.
1314

1415
### Changed

Neolution.DotNet.Console.SampleAsync/Commands/Echo/EchoCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public EchoCommand(ILogger<EchoCommand> logger, IHostEnvironment environment, IC
4141
}
4242

4343
/// <inheritdoc />
44-
public Task RunAsync(EchoOptions options)
44+
public Task RunAsync(EchoOptions options, CancellationToken cancellationToken)
4545
{
4646
ArgumentNullException.ThrowIfNull(options);
4747

Neolution.DotNet.Console.SampleAsync/Commands/GuidGen/GuidGenCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public GuidGenCommand(ILogger<GuidGenCommand> logger)
2626
}
2727

2828
/// <inheritdoc />
29-
public Task RunAsync(GuidGenOptions options)
29+
public Task RunAsync(GuidGenOptions options, CancellationToken cancellationToken)
3030
{
3131
if (options == null)
3232
{
@@ -42,6 +42,7 @@ public Task RunAsync(GuidGenOptions options)
4242
}
4343

4444
System.Console.WriteLine(result);
45+
4546
this.logger.LogTrace("Wrote result to console");
4647
return Task.CompletedTask;
4748
}

Neolution.DotNet.Console.SampleAsync/Commands/Start/StartCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ public StartCommand(ILogger<StartCommand> logger, IHttpClientFactory httpClientF
3232
}
3333

3434
/// <inheritdoc />
35-
public async Task RunAsync(StartOptions options)
35+
public async Task RunAsync(StartOptions options, CancellationToken cancellationToken)
3636
{
3737
this.logger.LogDebug("Check if Google is online");
38-
var response = await this.httpClient.GetAsync(new Uri("https://www.google.com"));
38+
var response = await this.httpClient.GetAsync(new Uri("https://www.google.com"), cancellationToken);
3939
if (response.IsSuccessStatusCode)
4040
{
4141
this.logger.LogTrace("Internet connection is available");

Neolution.DotNet.Console.UnitTests/DotNetConsoleBuilderTests.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
/// </summary>
1515
public class DotNetConsoleBuilderTests
1616
{
17+
/// <summary>
18+
/// The argument string for the internal verify-dependencies command
19+
/// </summary>
20+
private static readonly string[] VerifyDependenciesArgs = { "verify-dependencies" };
21+
1722
/// <summary>
1823
/// Given a mistyped verb, when a default verb is defined, then should throw on console building.
1924
/// </summary>
@@ -98,7 +103,7 @@ public void GivenInvalidArguments_WhenNoDefaultVerbIsDefined_ThenShouldNotThrowO
98103
public void GivenVerifyDependenciesCommand_WhenRegistrationIsMissing_ThenShouldThrow()
99104
{
100105
// Arrange
101-
var builder = DotNetConsole.CreateBuilderWithReference(Assembly.GetAssembly(typeof(DefaultCommand))!, new[] { "verify-dependencies" });
106+
var builder = DotNetConsole.CreateBuilderWithReference(Assembly.GetAssembly(typeof(DefaultCommand))!, VerifyDependenciesArgs);
102107

103108
// Intentionally only registering the transient service and not the scoped and singleton services.
104109
builder.Services.AddTransient<ITransientServiceStub, TransientServiceStub>();

Neolution.DotNet.Console.UnitTests/DotNetConsoleRunTests.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@ public void GivenServicesWithVariousServiceLifetimes_WhenRunningConsoleApp_ThenS
5757
[InlineData("verify-dependencies")]
5858
public void GivenValidArguments_WhenNoDefaultVerbIsDefined_ThenShouldNotThrow([NotNull] string args)
5959
{
60-
if (args == null)
61-
{
62-
throw new ArgumentNullException(nameof(args));
63-
}
60+
ArgumentNullException.ThrowIfNull(args);
6461

6562
// Arrange
6663
var builder = DotNetConsole.CreateBuilderWithReference(Assembly.GetAssembly(typeof(DefaultCommand))!, args.Split(" "));

Neolution.DotNet.Console.UnitTests/Fakes/DefaultCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace Neolution.DotNet.Console.UnitTests.Fakes
22
{
3+
using System.Threading;
34
using System.Threading.Tasks;
45
using Microsoft.Extensions.Hosting;
56
using Neolution.DotNet.Console.Abstractions;
@@ -33,7 +34,7 @@ public DefaultCommand(IUnitTestLogger logger, IHostEnvironment environment)
3334
}
3435

3536
/// <inheritdoc />
36-
public async Task RunAsync(DefaultOptions options)
37+
public async Task RunAsync(DefaultOptions options, CancellationToken cancellationToken)
3738
{
3839
this.logger.Log("options", options);
3940
this.logger.Log("environment", this.environment);

Neolution.DotNet.Console.UnitTests/Fakes/EchoCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace Neolution.DotNet.Console.UnitTests.Fakes
22
{
3+
using System.Threading;
34
using System.Threading.Tasks;
45
using Neolution.DotNet.Console.Abstractions;
56
using Neolution.DotNet.Console.UnitTests.Spies;
@@ -25,7 +26,7 @@ public EchoCommand(IUnitTestLogger logger)
2526
}
2627

2728
/// <inheritdoc />
28-
public async Task RunAsync(EchoOptions options)
29+
public async Task RunAsync(EchoOptions options, CancellationToken cancellationToken)
2930
{
3031
this.logger.Log("options", options);
3132
await Task.CompletedTask;

Neolution.DotNet.Console.UnitTests/Neolution.DotNet.Console.UnitTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<PrivateAssets>all</PrivateAssets>
1818
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1919
</PackageReference>
20-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
20+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
2121
<PackageReference Include="Neolution.CodeAnalysis.TestsRuleset" Version="3.2.1">
2222
<PrivateAssets>all</PrivateAssets>
2323
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

Neolution.DotNet.Console.UnitTests/Stubs/InjectServicesWithVariousLifetimesCommandStub.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace Neolution.DotNet.Console.UnitTests.Stubs
22
{
3+
using System.Threading;
34
using System.Threading.Tasks;
45
using Neolution.DotNet.Console.Abstractions;
56

@@ -38,7 +39,7 @@ public InjectServicesWithVariousLifetimesCommandStub(ITransientServiceStub trans
3839
}
3940

4041
/// <inheritdoc />
41-
public async Task RunAsync(InjectServicesWithVariousLifetimesOptionsStub options)
42+
public async Task RunAsync(InjectServicesWithVariousLifetimesOptionsStub options, CancellationToken cancellationToken)
4243
{
4344
await this.transientService.DoSomethingAsync();
4445
await this.scopedService.DoSomethingAsync();

0 commit comments

Comments
 (0)