Skip to content

Commit

Permalink
refactor(#603): Add default console logger
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn committed Nov 21, 2022
1 parent 446f002 commit 950a3f2
Show file tree
Hide file tree
Showing 32 changed files with 96 additions and 121 deletions.
2 changes: 1 addition & 1 deletion build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Task("Tests")
Verbosity = param.Verbosity,
NoRestore = true,
NoBuild = true,
Loggers = new[] { "trx" },
Loggers = new[] { "console;verbosity=normal", "trx" },
Filter = param.TestFilter,
ResultsDirectory = param.Paths.Directories.TestResultsDirectoryPath,
ArgumentCustomization = args => args
Expand Down
3 changes: 1 addition & 2 deletions src/Testcontainers/Configurations/TestcontainersSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace DotNet.Testcontainers.Configurations
using DotNet.Testcontainers.Images;
using JetBrains.Annotations;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

/// <summary>
/// This class represents the Testcontainers settings.
Expand Down Expand Up @@ -83,7 +82,7 @@ static TestcontainersSettings()
[PublicAPI]
[NotNull]
public static ILogger Logger { get; set; }
= NullLogger.Instance;
= new Logger();

/// <summary>
/// Gets or sets the host operating system.
Expand Down
88 changes: 88 additions & 0 deletions src/Testcontainers/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
namespace DotNet.Testcontainers
{
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using Microsoft.Extensions.Logging;

/// <summary>
/// An <see cref="ILogger" /> implementation that forwards messages to the console.
/// Not every test framework or environment supports this approach.
/// Developers may still need to configure their own logging implementation.
/// If VSTest.Console.exe loads the test adapter in a deterministic order,
/// we can write our own test adapter and intercept the IMessageLogger instance:
/// https://github.com/microsoft/vstest/issues/4125#issuecomment-1320880502.
/// To debug the test host and runner set the environment variables VSTEST_HOST_DEBUG and VSTEST_RUNNER_DEBUG to 1.
/// To enable VSTest logging set VSTEST_DIAG to 1 and VSTEST_DIAG_VERBOSITY to verbose.
/// The following example contains the ITestExecutor implementations. It is important that the assembly ends with TestAdapter.dll.
/// <code>
/// [FileExtension(DllFileExtension)]
/// [FileExtension(ExeFileExtension)]
/// [DefaultExecutorUri(ExecutorUri)]
/// [ExtensionUri(ExecutorUri)]
/// [Category(Category)]
/// internal sealed class UssDiscovery : ITestDiscoverer, ITestExecutor
/// {
/// private const string DllFileExtension = &quot;.dll&quot;;
///
/// private const string ExeFileExtension = &quot;.exe&quot;;
///
/// private const string ExecutorUri = &quot;executor://testcontainers.org/v1&quot;;
///
/// private const string Category = &quot;managed&quot;;
///
/// public void DiscoverTests(IEnumerable&lt;string&gt; sources, IDiscoveryContext discoveryContext, IMessageLogger logger, ITestCaseDiscoverySink discoverySink)
/// {
/// }
///
/// public void RunTests(IEnumerable&lt;TestCase&gt; tests, IRunContext runContext, IFrameworkHandle frameworkHandle)
/// {
/// SetLogger(frameworkHandle);
/// }
///
/// public void RunTests(IEnumerable&lt;string&gt; sources, IRunContext runContext, IFrameworkHandle frameworkHandle)
/// {
/// SetLogger(frameworkHandle);
/// }
///
/// public void Cancel()
/// {
/// }
///
/// private static void SetLogger(IMessageLogger logger)
/// {
/// // Set the TestcontainersSettings.Logger. Use a semaphore to block the test execution until the logger is set.
/// }
/// }
/// </code>
/// </summary>
internal sealed class Logger : ILogger, IDisposable
{
private readonly TextWriter stderr = Console.Error;

private readonly TextWriter stdout = Console.Out;

private readonly Stopwatch stopwatch = Stopwatch.StartNew();

public void Dispose()
{
}

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
var message = string.Format(CultureInfo.CurrentCulture, "[testcontainers.org {0:hh\\:mm\\:ss\\.ff}] {1}", this.stopwatch.Elapsed, formatter.Invoke(state, exception));
this.stdout.WriteLine(message);
}

public bool IsEnabled(LogLevel logLevel)
{
return true;
}

public IDisposable BeginScope<TState>(TState state)
{
return this;
}
}
}
2 changes: 0 additions & 2 deletions src/Testcontainers/Networks/NonExistingDockerNetwork.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
namespace DotNet.Testcontainers.Networks
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;
using Docker.DotNet.Models;
Expand Down
19 changes: 0 additions & 19 deletions tests/Testcontainers.Tests/CustomSerilogLoggerFactory.cs

This file was deleted.

28 changes: 0 additions & 28 deletions tests/Testcontainers.Tests/Initialization.cs

This file was deleted.

39 changes: 0 additions & 39 deletions tests/Testcontainers.Tests/Initialized.cs

This file was deleted.

6 changes: 6 additions & 0 deletions tests/Testcontainers.Tests/SkipOnLinuxEngineAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ namespace DotNet.Testcontainers.Tests
{
using System;
using System.Diagnostics;
using DotNet.Testcontainers.Configurations;
using Xunit;

public sealed class SkipOnLinuxEngineAttribute : FactAttribute
{
private static readonly bool IsLinuxEngineEnabled = GetIsLinuxEngineEnabled();

static SkipOnLinuxEngineAttribute()
{
TestcontainersSettings.ResourceReaperEnabled = IsLinuxEngineEnabled;
}

public SkipOnLinuxEngineAttribute()
{
if (IsLinuxEngineEnabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace DotNet.Testcontainers.Tests.Unit

public static class TestcontainersAccessInformationTest
{
[Collection(nameof(Testcontainers))]
public sealed class AccessDockerInformation
{
private const string DoesNotExist = nameof(TestcontainersAccessInformationTest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ private static async Task EstablishConnection(AzuriteFixture.AzuriteDefaultFixtu
Assert.Contains(TableServiceDataFileName, execResult.Stdout);
}

[Collection(nameof(Testcontainers))]
public sealed class CommonContainerPorts : IClassFixture<AzuriteFixture.AzuriteDefaultFixture>
{
private readonly AzuriteFixture.AzuriteDefaultFixture commonContainerPorts;
Expand All @@ -109,7 +108,6 @@ public async Task ConnectionEstablished()
}
}

[Collection(nameof(Testcontainers))]
public sealed class CustomContainerPorts : IClassFixture<AzuriteFixture.AzuriteWithCustomContainerPortsFixture>
{
private readonly AzuriteFixture.AzuriteDefaultFixture customContainerPorts;
Expand All @@ -128,7 +126,6 @@ public async Task ConnectionEstablished()
}
}

[Collection(nameof(Testcontainers))]
public sealed class BlobServiceEnabled : IClassFixture<AzuriteFixture.AzuriteWithBlobOnlyFixture>
{
private readonly AzuriteFixture.AzuriteDefaultFixture azurite;
Expand Down Expand Up @@ -171,7 +168,6 @@ await Assert.ThrowsAsync<RequestFailedException>(() => tableServiceClient.GetPro
}
}

[Collection(nameof(Testcontainers))]
public sealed class QueueServiceEnabled : IClassFixture<AzuriteFixture.AzuriteWithQueueOnlyFixture>
{
private readonly AzuriteFixture.AzuriteDefaultFixture azurite;
Expand Down Expand Up @@ -214,7 +210,6 @@ await Assert.ThrowsAsync<RequestFailedException>(() => tableServiceClient.GetPro
}
}

[Collection(nameof(Testcontainers))]
public sealed class TableServiceEnabled : IClassFixture<AzuriteFixture.AzuriteWithTableOnlyFixture>
{
private readonly AzuriteFixture.AzuriteDefaultFixture azurite;
Expand Down Expand Up @@ -257,7 +252,6 @@ await Assert.ThrowsAsync<RequestFailedException>(() => queueServiceClient.GetPro
}
}

[Collection(nameof(Testcontainers))]
public sealed class CustomLocation : IClassFixture<AzuriteFixture.AzuriteWithCustomWorkspaceFixture>
{
private readonly IEnumerable<string> dataFiles;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace DotNet.Testcontainers.Tests.Unit

public static class CosmosDbTestcontainerTest
{
[Collection(nameof(Testcontainers))]
public sealed class ConnectionTests : IClassFixture<CosmosDbFixture>, IDisposable
{
private const string SkipReason = "The Cosmos DB Linux Emulator Docker image does not run on every CI environment."; // https://github.com/Azure/azure-cosmos-db-emulator-docker/issues/45.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace DotNet.Testcontainers.Tests.Unit
using DotNet.Testcontainers.Tests.Fixtures;
using Xunit;

[Collection(nameof(Testcontainers))]
public sealed class CouchDbTestcontainerTest : IClassFixture<CouchDbFixture>
{
private readonly CouchDbFixture couchDbFixture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace DotNet.Testcontainers.Tests.Unit
using DotNet.Testcontainers.Tests.Fixtures;
using Xunit;

[Collection(nameof(Testcontainers))]
public sealed class CouchbaseTestcontainerTest : IClassFixture<CouchbaseFixture>
{
private readonly CouchbaseFixture couchbaseFixture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace DotNet.Testcontainers.Tests.Unit
using DotNet.Testcontainers.Tests.Fixtures;
using Xunit;

[Collection(nameof(Testcontainers))]
public sealed class ElasticsearchTestcontainerTest : IClassFixture<ElasticsearchFixture>
{
private readonly ElasticsearchFixture elasticsearchFixture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace DotNet.Testcontainers.Tests.Unit
using DotNet.Testcontainers.Tests.Fixtures;
using Xunit;

[Collection(nameof(Testcontainers))]
public sealed class MariaDbTestcontainerTest : IClassFixture<MariaDbFixture>
{
private readonly MariaDbFixture mariaDbFixture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using MongoDB.Bson;
using Xunit;

[Collection(nameof(Testcontainers))]
public sealed class MongoDbTestcontainerTest : IClassFixture<MongoDbFixture>, IClassFixture<MongoDbNoAuthFixture>
{
private const string MongoDbScheme = "mongodb";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace DotNet.Testcontainers.Tests.Unit
using DotNet.Testcontainers.Tests.Fixtures;
using Xunit;

[Collection(nameof(Testcontainers))]
public sealed class MsSqlTestcontainerTest : IClassFixture<MsSqlFixture>
{
private readonly MsSqlFixture msSqlFixture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace DotNet.Testcontainers.Tests.Unit

public static class MySqlTestcontainerTest
{
[Collection(nameof(Testcontainers))]
public sealed class MySqlCustomUsernameTest : IClassFixture<MySqlFixture>
{
private readonly MySqlFixture mySqlFixture;
Expand Down Expand Up @@ -69,7 +68,6 @@ public async Task ThrowErrorInRunningContainerWithInvalidScript()
}
}

[Collection(nameof(Testcontainers))]
public sealed class MySqlRootUsernameTest : IClassFixture<MySqlRootUsernameFixture>
{
private readonly MySqlFixture mySqlFixture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using DotNet.Testcontainers.Tests.Fixtures;
using Xunit;

[Collection(nameof(Testcontainers))]
public sealed class Neo4jTestcontainerTest : IClassFixture<Neo4jFixture>
{
private readonly Neo4jFixture neo4jFixture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace DotNet.Testcontainers.Tests.Unit
using DotNet.Testcontainers.Tests.Fixtures;
using Xunit;

[Collection(nameof(Testcontainers))]
public sealed class OracleTestcontainerTest : IClassFixture<OracleFixture>
{
private readonly OracleFixture oracleFixture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace DotNet.Testcontainers.Tests.Unit
using DotNet.Testcontainers.Tests.Fixtures;
using Xunit;

[Collection(nameof(Testcontainers))]
public sealed class PostgreSqlTestcontainerTest : IClassFixture<PostgreSqlFixture>
{
private readonly PostgreSqlFixture postgreSqlFixture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace DotNet.Testcontainers.Tests.Unit
using DotNet.Testcontainers.Tests.Fixtures;
using Xunit;

[Collection(nameof(Testcontainers))]
public sealed class RedisTestcontainerTest : IClassFixture<RedisFixture>
{
private readonly RedisFixture redisFixture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace DotNet.Testcontainers.Tests.Unit
using DotNet.Testcontainers.Tests.Fixtures;
using Xunit;

[Collection(nameof(Testcontainers))]
public sealed class KafkaTestcontainerTest : IClassFixture<KafkaFixture>
{
private readonly KafkaFixture kafkaFixture;
Expand Down
Loading

0 comments on commit 950a3f2

Please sign in to comment.