Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<PackageId>$(AssemblyName)</PackageId>
<Version>4.17.0</Version>
<Version>4.18.0</Version>
<AssemblyVersion>$(Version)</AssemblyVersion>
<FileVersion>$(Version)</FileVersion>
<Product>Testcontainers</Product>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public async Task<X509Certificate2> GetCertificateAsync(CancellationToken ct = d
var certificateBytes = await ReadFileAsync(ElasticsearchBuilder.ElasticsearchHttpCaCertificateFilePath, ct)
.ConfigureAwait(false);

#if NET5_0_OR_GREATER
#if NET6_0_OR_GREATER
return X509Certificate2.CreateFromPem(Encoding.Default.GetString(certificateBytes));
#else
return new X509Certificate2(certificateBytes);
Expand Down
6 changes: 3 additions & 3 deletions src/Testcontainers.WebDriver/WebDriverContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ public async Task ExportVideoAsync(string target, CancellationToken ct = default
var bytes = await _ffmpegContainer.ReadFileAsync(WebDriverBuilder.VideoFilePath, ct)
.ConfigureAwait(false);

#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
#if NETSTANDARD2_0
File.WriteAllBytes(target, bytes);
#else
await File.WriteAllBytesAsync(target, bytes, ct)
.ConfigureAwait(false);
#else
File.WriteAllBytes(target, bytes);
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers.Xunit/DbContainerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ await base.DisposeAsyncCore()
/// <inheritdoc />
public DbConnection CreateConnection() => _testMethods.CreateConnection();

#if NET8_0_OR_GREATER
#if NET7_0_OR_GREATER
/// <inheritdoc />
public DbConnection OpenConnection() => _testMethods.OpenConnection();

Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers.Xunit/DbContainerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ await base.DisposeAsyncCore()
/// <inheritdoc />
public DbConnection CreateConnection() => _testMethods.CreateConnection();

#if NET8_0_OR_GREATER
#if NET7_0_OR_GREATER
/// <inheritdoc />
public DbConnection OpenConnection() => _testMethods.OpenConnection();

Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers.Xunit/DbContainerTestMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ internal sealed class DbContainerTestMethods(DbProviderFactory dbProviderFactory
private readonly DbProviderFactory _dbProviderFactory = dbProviderFactory ?? throw new ArgumentNullException(nameof(dbProviderFactory));
private readonly Lazy<string> _connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString));

#if NET8_0_OR_GREATER
#if NET7_0_OR_GREATER
[CanBeNull]
private DbDataSource _dbDataSource;
private DbDataSource DbDataSource
Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers.Xunit/IDbContainerTestMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal interface IDbContainerTestMethods
/// <returns>A new, closed connection to the database.</returns>
DbConnection CreateConnection();

#if NET8_0_OR_GREATER
#if NET7_0_OR_GREATER
/// <summary>
/// Returns a new, open connection to the database.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ protected override X509Certificate2 GetClientCertificate()

// The certificate must be exported to PFX on Windows to avoid "No credentials are available in the security package":
// https://stackoverflow.com/questions/72096812/loading-x509certificate2-from-pem-file-results-in-no-credentials-are-available/72101855#72101855.
#if NETSTANDARD
return Polyfills.X509Certificate2.CreateFromPemFile(clientCertificateFilePath, clientCertificateKeyFilePath);
#elif NET9_0_OR_GREATER
#if NET9_0_OR_GREATER
var certificate = X509Certificate2.CreateFromPemFile(clientCertificateFilePath, clientCertificateKeyFilePath);
return OperatingSystem.IsWindows() ? X509CertificateLoader.LoadPkcs12(certificate.Export(X509ContentType.Pfx), null) : certificate;
#elif NET6_0_OR_GREATER
#elif NET5_0_OR_GREATER
var certificate = X509Certificate2.CreateFromPemFile(clientCertificateFilePath, clientCertificateKeyFilePath);
return OperatingSystem.IsWindows() ? new X509Certificate2(certificate.Export(X509ContentType.Pfx)) : certificate;
#else
return Polyfills.X509Certificate2.CreateFromPemFile(clientCertificateFilePath, clientCertificateKeyFilePath);
#endif
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/Testcontainers/Clients/TestcontainersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,8 @@ public async Task<byte[]> ReadFileAsync(string id, string filePath, Cancellation

var readBytes = new byte[entry.Size];

#if NETSTANDARD2_0
_ = await tarInputStream.ReadAsync(readBytes, 0, readBytes.Length, ct)
.ConfigureAwait(false);
#else
_ = await tarInputStream.ReadAsync(readBytes, ct)
.ConfigureAwait(false);
#endif

return readBytes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public virtual string GetReuseHash()

var jsonUtf8Bytes = JsonSerializer.SerializeToUtf8Bytes(this, GetType(), DefaultJsonSerializerOptions.Instance);

#if NET6_0_OR_GREATER
#if NET5_0_OR_GREATER
return Convert.ToBase64String(SHA1.HashData(jsonUtf8Bytes));
#else
using (var sha1 = SHA1.Create())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public async Task<byte[]> GetAllBytesAsync(CancellationToken ct = default)
{
using (var httpClient = new HttpClient())
{
#if NET6_0_OR_GREATER
#if NET5_0_OR_GREATER
return await httpClient.GetByteArrayAsync(_uri, ct)
.ConfigureAwait(false);
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,12 @@ await waitTask
{
// Stop the task that is still pending. Either the timeout, or the
// evaluation of the condition that did not complete in time.
#if NET8_0_OR_GREATER
await linkedCts.CancelAsync()
.ConfigureAwait(false);
#else
linkedCts.Cancel();
#endif
}
}

Expand Down
33 changes: 5 additions & 28 deletions src/Testcontainers/Containers/ResourceReaper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public async ValueTask DisposeAsync()

try
{
#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
await _maintainConnectionCts.CancelAsync()
.ConfigureAwait(false);
#else
Expand Down Expand Up @@ -314,13 +314,8 @@ private static async Task<bool> TryRegisterFilterAsync(NetworkStream stream, str

var readBytes = new byte[AcknowledgementBufferSizeInBytes];

#if NETSTANDARD2_0
await stream.WriteAsync(sendBytes, 0, sendBytes.Length, ct)
.ConfigureAwait(false);
#else
await stream.WriteAsync(sendBytes, ct)
.ConfigureAwait(false);
#endif

await stream.FlushAsync(ct)
.ConfigureAwait(false);
Expand All @@ -331,13 +326,8 @@ await stream.FlushAsync(ct)

do
{
#if NETSTANDARD2_0
var numberOfBytes = await stream.ReadAsync(readBytes, 0, readBytes.Length, ct)
.ConfigureAwait(false);
#else
var numberOfBytes = await stream.ReadAsync(readBytes, ct)
.ConfigureAwait(false);
#endif

if (numberOfBytes == 0)
{
Expand All @@ -352,25 +342,15 @@ await stream.FlushAsync(ct)
if (indexOfNewLine == -1)
{
// We have not received the entire message yet. Read from stream again.
#if NETSTANDARD2_0
await messageBuffer.WriteAsync(readBytes, 0, numberOfBytes, ct)
.ConfigureAwait(false);
#else
await messageBuffer.WriteAsync(readBytes.AsMemory(0, numberOfBytes), ct)
.ConfigureAwait(false);
#endif

hasAcknowledge = false;
}
else
{
#if NETSTANDARD2_0
await messageBuffer.WriteAsync(readBytes, 0, indexOfNewLine, ct)
.ConfigureAwait(false);
#else
await messageBuffer.WriteAsync(readBytes.AsMemory(0, indexOfNewLine), ct)
.ConfigureAwait(false);
#endif

var buffer = messageBuffer.GetBuffer();

Expand Down Expand Up @@ -408,7 +388,7 @@ private async Task<bool> TryConnectAndRegisterFilterAsync(string filter, Cancell

try
{
#if NET6_0_OR_GREATER
#if NET5_0_OR_GREATER
await tcpClient.ConnectAsync(host, port, ct)
.ConfigureAwait(false);
#else
Expand Down Expand Up @@ -485,10 +465,12 @@ await Task.Delay(TimeSpan.FromSeconds(RetryTimeoutInSeconds), CancellationToken.

try
{
#if NET6_0_OR_GREATER
#if NET5_0_OR_GREATER
await tcpClient.ConnectAsync(host, port, ct)
.ConfigureAwait(false);
#else
// The overload that takes a cancellation token is not available.
// The loop checks the cancellation token before each connection attempt.
await tcpClient.ConnectAsync(host, port)
.ConfigureAwait(false);
#endif
Expand Down Expand Up @@ -520,13 +502,8 @@ await Task.Delay(TimeSpan.FromSeconds(RetryTimeoutInSeconds), ct)
while (!_maintainConnectionCts.IsCancellationRequested)
{
// Keep the connection to Ryuk up.
#if NETSTANDARD2_0
var numberOfBytes = await stream.ReadAsync(readBytes, 0, readBytes.Length, _maintainConnectionCts.Token)
.ConfigureAwait(false);
#else
var numberOfBytes = await stream.ReadAsync(readBytes, _maintainConnectionCts.Token)
.ConfigureAwait(false);
#endif

if (numberOfBytes == 0)
{
Expand Down
9 changes: 3 additions & 6 deletions src/Testcontainers/Containers/TarOutputMemoryStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,10 @@ public async Task AddAsync(IResourceMapping resourceMapping, CancellationToken c
await PutNextEntryAsync(tarEntry, ct)
.ConfigureAwait(false);

#if NETSTANDARD2_0
await WriteAsync(fileContent, 0, fileContent.Length, ct)
// Keep the explicit `this`. C# binds extension methods only to an explicit
// receiver, and .NET Standard 2.0 resolves `WriteAsync` to the polyfill.
await this.WriteAsync(fileContent, ct)
.ConfigureAwait(false);
#else
await WriteAsync(fileContent, ct)
.ConfigureAwait(false);
#endif

await CloseEntryAsync(ct)
.ConfigureAwait(false);
Expand Down
67 changes: 67 additions & 0 deletions src/Testcontainers/Polyfills/StreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#if NETSTANDARD2_0
namespace DotNet.Testcontainers
{
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;

/// <summary>
/// Provides extension methods for the <see cref="Stream" /> class
/// that .NET Standard 2.0 does not support.
/// </summary>
internal static class StreamExtensions
{
/// <summary>
/// Asynchronously reads a sequence of bytes from the stream into the buffer.
/// </summary>
/// <param name="stream">The stream to read from.</param>
/// <param name="buffer">The buffer to write the data into.</param>
/// <param name="ct">Cancellation token.</param>
/// <returns>
/// Task that completes when the bytes have been read,
/// returning the number of bytes read into the buffer.
/// </returns>
public static async ValueTask<int> ReadAsync(this Stream stream, Memory<byte> buffer, CancellationToken ct = default)
{
if (MemoryMarshal.TryGetArray<byte>(buffer, out var segment))
{
return await stream.ReadAsync(segment.Array, segment.Offset, segment.Count, ct)
.ConfigureAwait(false);
}

var bytes = new byte[buffer.Length];

var numberOfBytes = await stream.ReadAsync(bytes, 0, bytes.Length, ct)
.ConfigureAwait(false);

bytes.AsSpan(0, numberOfBytes).CopyTo(buffer.Span);
return numberOfBytes;
}

/// <summary>
/// Asynchronously writes a sequence of bytes from the buffer to the stream.
/// </summary>
/// <param name="stream">The stream to write to.</param>
/// <param name="buffer">The buffer to read the data from.</param>
/// <param name="ct">Cancellation token.</param>
/// <returns>Task that completes when the bytes have been written.</returns>
public static async ValueTask WriteAsync(this Stream stream, ReadOnlyMemory<byte> buffer, CancellationToken ct = default)
{
if (MemoryMarshal.TryGetArray(buffer, out var segment))
{
await stream.WriteAsync(segment.Array, segment.Offset, segment.Count, ct)
.ConfigureAwait(false);
}
else
{
var bytes = buffer.ToArray();

await stream.WriteAsync(bytes, 0, bytes.Length, ct)
.ConfigureAwait(false);
}
}
}
}
#endif
Loading