Skip to content

Commit

Permalink
fix: Differentiate message format for test output helper and message …
Browse files Browse the repository at this point in the history
…sink

Test output helper: include timestamp
Message sink: don't include timestamp since it's already included by xUnit
  • Loading branch information
0xced committed Nov 22, 2024
1 parent 250d6a4 commit 5448078
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 108 deletions.
2 changes: 1 addition & 1 deletion src/Testcontainers.Xunit/ContainerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ namespace Testcontainers.Xunit;
/// <typeparam name="TContainerEntity">The container entity.</typeparam>
[PublicAPI]
public class ContainerFixture<TBuilderEntity, TContainerEntity>(IMessageSink messageSink)
: ContainerLifetime<TBuilderEntity, TContainerEntity>(new XunitLoggerProvider(messageSink).CreateLogger("testcontainers.org"))
: ContainerLifetime<TBuilderEntity, TContainerEntity>(new MessageSinkLogger(messageSink))
where TBuilderEntity : IContainerBuilder<TBuilderEntity, TContainerEntity>, new()
where TContainerEntity : IContainer;
2 changes: 1 addition & 1 deletion src/Testcontainers.Xunit/ContainerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Testcontainers.Xunit;
/// <typeparam name="TContainerEntity">The container entity.</typeparam>
[PublicAPI]
public abstract class ContainerTest<TBuilderEntity, TContainerEntity>(ITestOutputHelper testOutputHelper, Func<TBuilderEntity, TBuilderEntity> configure = null)
: ContainerLifetime<TBuilderEntity, TContainerEntity>(new XunitLoggerProvider(testOutputHelper).CreateLogger("testcontainers.org"))
: ContainerLifetime<TBuilderEntity, TContainerEntity>(new TestOutputLogger(testOutputHelper))
where TBuilderEntity : IContainerBuilder<TBuilderEntity, TContainerEntity>, new()
where TContainerEntity : IContainer
{
Expand Down
20 changes: 20 additions & 0 deletions src/Testcontainers.Xunit/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Testcontainers.Xunit;

internal abstract class Logger : ILogger
{
protected static string GetMessage<TState>(TState state, Exception exception, Func<TState, Exception, string> formatter)
{
return exception == null ? formatter(state, null) : $"{formatter(state, exception)}{Environment.NewLine}{exception}";
}

protected abstract void Log<TState>(TState state, Exception exception, Func<TState, Exception, string> formatter);

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
Log(state, exception, formatter);
}

public bool IsEnabled(LogLevel logLevel) => logLevel != LogLevel.None;

public IDisposable BeginScope<TState>(TState state) => new NullScope();
}
21 changes: 21 additions & 0 deletions src/Testcontainers.Xunit/MessageSinkLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Testcontainers.Xunit;

internal sealed class MessageSinkLogger(IMessageSink messageSink) : Logger
{
protected override void Log<TState>(TState state, Exception exception, Func<TState, Exception, string> formatter)
{
if (messageSink == null)
{
return;
}

var message = GetMessage(state, exception, formatter);
messageSink.OnMessage(new DiagnosticMessage($"[testcontainers.org] {message}"));
}

/// <returns>
/// The hash code of the underlying message sink, because <see cref="DotNet.Testcontainers.Clients.DockerApiClient.LogContainerRuntimeInfoAsync" />
/// logs the runtime information once per Docker Engine API client and logger.
/// </returns>
public override int GetHashCode() => messageSink?.GetHashCode() ?? 0;
}
8 changes: 8 additions & 0 deletions src/Testcontainers.Xunit/NullScope.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Testcontainers.Xunit;

internal sealed class NullScope : IDisposable
{
public void Dispose()
{
}
}
17 changes: 17 additions & 0 deletions src/Testcontainers.Xunit/TestOutputLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Testcontainers.Xunit;

internal sealed class TestOutputLogger(ITestOutputHelper testOutputHelper) : Logger
{
private readonly Stopwatch _stopwatch = Stopwatch.StartNew();

protected override void Log<TState>(TState state, Exception exception, Func<TState, Exception, string> formatter)
{
if (testOutputHelper == null)
{
return;
}

var message = GetMessage(state, exception, formatter);
testOutputHelper.WriteLine($@"[testcontainers.org {_stopwatch.Elapsed:hh\:mm\:ss\.fff}] {message}");
}
}
106 changes: 0 additions & 106 deletions src/Testcontainers.Xunit/XunitLoggerProvider.cs

This file was deleted.

0 comments on commit 5448078

Please sign in to comment.