generated from frankhaugen/DotnetRepoTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Testcontainers support and enhance output table testing
Added a new project for supporting Testcontainers which includes interface for running Testcontainers and a builder for configuring them. Enhanced the TestOutputExtensions project to support console tables in test outputs. Additionally, refactored tests for better structure and organization.
- Loading branch information
1 parent
b0354e1
commit c43c04e
Showing
10 changed files
with
196 additions
and
4 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
13 changes: 13 additions & 0 deletions
13
Frank.Testing.TestOutputExtensions/TestOutputTableExtensions.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,13 @@ | ||
using ConsoleTableExt; | ||
|
||
namespace Xunit.Abstractions; | ||
|
||
public static class TestOutputTableExtensions | ||
{ | ||
public static void WriteTable<T>(this ITestOutputHelper outputHelper, IEnumerable<T> source, ConsoleTableBuilderFormat format = ConsoleTableBuilderFormat.Minimal) where T : class => | ||
outputHelper.WriteLine(ConsoleTableBuilder | ||
.From(source.ToList()) | ||
.WithFormat(format) | ||
.Export() | ||
.ToString()); | ||
} |
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,64 @@ | ||
using DotNet.Testcontainers.Containers; | ||
|
||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
|
||
namespace Frank.Testing.Testcontainers; | ||
|
||
public class ContainerRunner<T> : ITestcontainerRunner where T : class, IContainer | ||
{ | ||
private readonly ILogger<T> _logger; | ||
private readonly T _container; | ||
private readonly CancellationToken _cancellationToken; | ||
private readonly TimeSpan _timeout; | ||
|
||
private CancellationTokenSource? _cancellationTokenSource; | ||
|
||
internal ContainerRunner(ILogger<T>? logger, T container, TimeSpan timeout, CancellationToken cancellationToken) | ||
{ | ||
_logger = logger ??= new NullLogger<T>(); | ||
_container = container ?? throw new ArgumentNullException(nameof(container)); | ||
_cancellationToken = cancellationToken; | ||
_timeout = timeout; | ||
} | ||
|
||
public async Task StartAsync() | ||
{ | ||
_cancellationTokenSource = new CancellationTokenSource(_timeout); | ||
_cancellationToken.Register(() => _cancellationTokenSource.Cancel()); | ||
await _container.StartAsync(_cancellationTokenSource.Token); | ||
} | ||
|
||
public async Task StopAsync() | ||
{ | ||
await _container.StopAsync(_cancellationToken); | ||
await DisposeAsync(); | ||
} | ||
|
||
public TestcontainersStates GetState() => _container.State; | ||
|
||
/// <inheritdoc /> | ||
public async Task ExecuteCommandAsync(string command, CancellationToken cancellationToken = default) | ||
{ | ||
await _container.ExecAsync(new[] { command }, _cancellationToken); | ||
} | ||
|
||
public async Task ExecuteAsync(Func<Task> actionAsync) | ||
{ | ||
try | ||
{ | ||
await actionAsync(); | ||
} | ||
catch (Exception? exception) | ||
{ | ||
_logger.LogError(exception, "Failed to execute action in container {ContainerName}", _container.Name); | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async ValueTask DisposeAsync() | ||
{ | ||
await _container.DisposeAsync(); | ||
_cancellationTokenSource?.Dispose(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
Frank.Testing.Testcontainers/Frank.Testing.Testcontainers.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,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Testcontainers" Version="3.7.0" /> | ||
</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,16 @@ | ||
using DotNet.Testcontainers.Containers; | ||
|
||
namespace Frank.Testing.Testcontainers; | ||
|
||
public interface ITestcontainerRunner : IAsyncDisposable | ||
{ | ||
Task StartAsync(); | ||
|
||
Task StopAsync(); | ||
|
||
TestcontainersStates GetState(); | ||
|
||
Task ExecuteCommandAsync(string command, CancellationToken cancellationToken = default); | ||
|
||
Task ExecuteAsync(Func<Task> actionAsync); | ||
} |
51 changes: 51 additions & 0 deletions
51
Frank.Testing.Testcontainers/TestContainerRunnerBuilder.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,51 @@ | ||
using DotNet.Testcontainers.Containers; | ||
|
||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
|
||
namespace Frank.Testing.Testcontainers; | ||
|
||
public class TestContainerRunnerBuilder<T> where T : class, IContainer | ||
{ | ||
private ILogger<T>? _logger; | ||
private TimeSpan _maxLifetime; | ||
private CancellationToken _cancellationToken; | ||
private Func<IContainer>? _containerFactory; | ||
|
||
public TestContainerRunnerBuilder<T> WithLogger(ILogger<T>? logger) | ||
{ | ||
_logger = logger; | ||
return this; | ||
} | ||
|
||
public TestContainerRunnerBuilder<T> WithMaxLifetime(TimeSpan maxLifetime) | ||
{ | ||
_maxLifetime = maxLifetime; | ||
return this; | ||
} | ||
|
||
public TestContainerRunnerBuilder<T> WithCancellationToken(CancellationToken cancellationToken) | ||
{ | ||
_cancellationToken = cancellationToken; | ||
return this; | ||
} | ||
|
||
public TestContainerRunnerBuilder<T> WithContainerFactory(Func<IContainer>? containerFactory) | ||
{ | ||
_containerFactory = containerFactory; | ||
return this; | ||
} | ||
|
||
public ITestcontainerRunner Build() | ||
{ | ||
_logger ??= new NullLogger<T>(); | ||
if (_containerFactory == null) | ||
throw new ArgumentNullException(nameof(_containerFactory)); | ||
if (_maxLifetime == default) | ||
_maxLifetime = TimeSpan.FromMinutes(1); | ||
if (_cancellationToken == default) | ||
_cancellationToken = CancellationToken.None; | ||
|
||
return new ContainerRunner<IContainer>(_logger, _containerFactory(), _maxLifetime, _cancellationToken); | ||
} | ||
} |
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
27 changes: 27 additions & 0 deletions
27
Frank.Testing.Tests/TestOutputExtensionsTests/TestOutputTableExtensionsTests.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,27 @@ | ||
using Xunit.Abstractions; | ||
|
||
namespace Frank.Testing.Tests.TestOutputExtensionsTests; | ||
|
||
public class TestOutputTableExtensionsTests | ||
{ | ||
private readonly ITestOutputHelper _outputHelper; | ||
|
||
public TestOutputTableExtensionsTests(ITestOutputHelper outputHelper) | ||
{ | ||
_outputHelper = outputHelper; | ||
} | ||
|
||
[Fact] | ||
public void ToTable_WithEnumerable_ReturnsTable() | ||
{ | ||
// Arrange | ||
var passwords = new[] | ||
{ | ||
new { Sha1Hash = "5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8", Sha1Prefix = "5BAA6", Sha2Suffix = "1E4C9B93F3F0682250B6CF8331B7EE68FD8", TimesPwned = 3645844 }, new { Sha1Hash = "5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8", Sha1Prefix = "5BAA6", Sha2Suffix = "1E4C9B93F3F0682250B6CF8331B7EE68FD8", TimesPwned = 3645844 }, new { Sha1Hash = "5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8", Sha1Prefix = "5BAA6", Sha2Suffix = "1E4C9B93F3F0682250B6CF8331B7EE68FD8", TimesPwned = 3645844 }, | ||
new { Sha1Hash = "5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8", Sha1Prefix = "5BAA6", Sha2Suffix = "1E4C9B93F3F0682250B6CF8331B7EE68FD8", TimesPwned = 364584 }, | ||
}; | ||
|
||
// Act | ||
_outputHelper.WriteTable(passwords); | ||
} | ||
} |
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