From 40597ddf782e5fa6bc439148cf3b7f97593bc3b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ce=CC=81dric=20Luthi?= Date: Tue, 26 Dec 2023 22:22:29 +0100 Subject: [PATCH 1/2] chore: Invert #if NETSTANDARD* conditional compilation conditions While conceptually .NET 5, 6, 7, 8 are greater than .NET Standard 2.1 it does not apply to the `NETSTANDARD2_1_OR_GREATER` macro. So it's safer to conditionally compile on NETSTANDARD2_0 and assume that the #else branch targets a newer framework where the new bits are available in order not to use the old code path on the newest frameworks. --- .../Clients/TestcontainersClient.cs | 15 +++++------ .../Containers/ResourceReaper.cs | 27 +++++++++---------- .../Containers/TarOutputMemoryStream.cs | 9 +++---- src/Testcontainers/Images/IgnoreFile.cs | 6 ++--- 4 files changed, 26 insertions(+), 31 deletions(-) diff --git a/src/Testcontainers/Clients/TestcontainersClient.cs b/src/Testcontainers/Clients/TestcontainersClient.cs index d7b397c1b..fc4033edc 100644 --- a/src/Testcontainers/Clients/TestcontainersClient.cs +++ b/src/Testcontainers/Clients/TestcontainersClient.cs @@ -98,10 +98,10 @@ public Task GetContainerExitCodeAsync(string id, CancellationToken ct = de /// public Task<(string Stdout, string Stderr)> GetContainerLogsAsync(string id, DateTime since = default, DateTime until = default, bool timestampsEnabled = true, CancellationToken ct = default) { -#if NETSTANDARD2_1_OR_GREATER - var unixEpoch = DateTime.UnixEpoch; -#else +#if NETSTANDARD2_0 var unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); +#else + var unixEpoch = DateTime.UnixEpoch; #endif if (default(DateTime).Equals(since)) @@ -269,13 +269,12 @@ public async Task ReadFileAsync(string id, string filePath, Cancellation var readBytes = new byte[entry.Size]; -#if NETSTANDARD2_1_OR_GREATER - _ = await tarInputStream.ReadAsync(new Memory(readBytes), ct) - .ConfigureAwait(false); -#else +#if NETSTANDARD2_0 _ = await tarInputStream.ReadAsync(readBytes, 0, readBytes.Length, ct) - .ConfigureAwait(false); +#else + _ = await tarInputStream.ReadAsync(readBytes, ct) #endif + .ConfigureAwait(false); return readBytes; } diff --git a/src/Testcontainers/Containers/ResourceReaper.cs b/src/Testcontainers/Containers/ResourceReaper.cs index 617ee0c37..4a78c4dfe 100644 --- a/src/Testcontainers/Containers/ResourceReaper.cs +++ b/src/Testcontainers/Containers/ResourceReaper.cs @@ -306,13 +306,12 @@ await tcpClient.ConnectAsync(host, port) { using (var messageBuffer = new MemoryStream()) { -#if NETSTANDARD2_1_OR_GREATER - await stream.WriteAsync(new ReadOnlyMemory(sendBytes), ct) - .ConfigureAwait(false); -#else +#if NETSTANDARD2_0 await stream.WriteAsync(sendBytes, 0, sendBytes.Length, ct) - .ConfigureAwait(false); +#else + await stream.WriteAsync(sendBytes, ct) #endif + .ConfigureAwait(false); await stream.FlushAsync(ct) .ConfigureAwait(false); @@ -321,13 +320,12 @@ await stream.FlushAsync(ct) do { -#if NETSTANDARD2_1_OR_GREATER - var numberOfBytes = await stream.ReadAsync(new Memory(readBytes), ct) - .ConfigureAwait(false); -#else +#if NETSTANDARD2_0 var numberOfBytes = await stream.ReadAsync(readBytes, 0, readBytes.Length, ct) - .ConfigureAwait(false); +#else + var numberOfBytes = await stream.ReadAsync(readBytes, ct) #endif + .ConfigureAwait(false); if (numberOfBytes == 0) { @@ -367,13 +365,12 @@ await Task.Delay(TimeSpan.FromSeconds(RetryTimeoutInSeconds), ct) while (!_maintainConnectionCts.IsCancellationRequested) { // Keep the connection to Ryuk up. -#if NETSTANDARD2_1_OR_GREATER - _ = await stream.ReadAsync(new Memory(readBytes), _maintainConnectionCts.Token) - .ConfigureAwait(false); -#else +#if NETSTANDARD2_0 _ = await stream.ReadAsync(readBytes, 0, readBytes.Length, _maintainConnectionCts.Token) - .ConfigureAwait(false); +#else + _ = await stream.ReadAsync(readBytes, _maintainConnectionCts.Token) #endif + .ConfigureAwait(false); } } catch (OperationCanceledException) diff --git a/src/Testcontainers/Containers/TarOutputMemoryStream.cs b/src/Testcontainers/Containers/TarOutputMemoryStream.cs index 245362f98..b2b2043c2 100644 --- a/src/Testcontainers/Containers/TarOutputMemoryStream.cs +++ b/src/Testcontainers/Containers/TarOutputMemoryStream.cs @@ -74,13 +74,12 @@ public async Task AddAsync(IResourceMapping resourceMapping, CancellationToken c await PutNextEntryAsync(tarEntry, ct) .ConfigureAwait(false); -#if NETSTANDARD2_1_OR_GREATER - await WriteAsync(fileContent, ct) - .ConfigureAwait(false); -#else +#if NETSTANDARD2_0 await WriteAsync(fileContent, 0, fileContent.Length, ct) - .ConfigureAwait(false); +#else + await WriteAsync(fileContent, ct) #endif + .ConfigureAwait(false); await CloseEntryAsync(ct) .ConfigureAwait(false); diff --git a/src/Testcontainers/Images/IgnoreFile.cs b/src/Testcontainers/Images/IgnoreFile.cs index 7698787be..d5d3ec336 100644 --- a/src/Testcontainers/Images/IgnoreFile.cs +++ b/src/Testcontainers/Images/IgnoreFile.cs @@ -193,10 +193,10 @@ public string Replace(string input) } // Replace the last non recursive wildcard with a match-zero-or-one quantifier regular expression. -#if NETSTANDARD2_1_OR_GREATER - if (input.Contains('*') && index >= 0) -#else +#if NETSTANDARD2_0 if (input.Contains("*") && index >= 0) +#else + if (input.Contains('*') && index >= 0) #endif { input = input.Remove(index, 1).Insert(index, $"{MatchAllExceptPathSeparator}?"); From ed07855e02c463d6472c30fd129974a9bad62ea7 Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Wed, 27 Dec 2023 18:04:42 +0100 Subject: [PATCH 2/2] chore: Keep ConfigureAwait in macro if-else --- src/Testcontainers/Clients/TestcontainersClient.cs | 3 ++- src/Testcontainers/Containers/ResourceReaper.cs | 9 ++++++--- src/Testcontainers/Containers/TarOutputMemoryStream.cs | 3 ++- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Testcontainers/Clients/TestcontainersClient.cs b/src/Testcontainers/Clients/TestcontainersClient.cs index fc4033edc..4d54139cb 100644 --- a/src/Testcontainers/Clients/TestcontainersClient.cs +++ b/src/Testcontainers/Clients/TestcontainersClient.cs @@ -271,10 +271,11 @@ public async Task ReadFileAsync(string id, string filePath, Cancellation #if NETSTANDARD2_0 _ = await tarInputStream.ReadAsync(readBytes, 0, readBytes.Length, ct) + .ConfigureAwait(false); #else _ = await tarInputStream.ReadAsync(readBytes, ct) -#endif .ConfigureAwait(false); +#endif return readBytes; } diff --git a/src/Testcontainers/Containers/ResourceReaper.cs b/src/Testcontainers/Containers/ResourceReaper.cs index 4a78c4dfe..f941f2f92 100644 --- a/src/Testcontainers/Containers/ResourceReaper.cs +++ b/src/Testcontainers/Containers/ResourceReaper.cs @@ -308,10 +308,11 @@ await tcpClient.ConnectAsync(host, port) { #if NETSTANDARD2_0 await stream.WriteAsync(sendBytes, 0, sendBytes.Length, ct) + .ConfigureAwait(false); #else await stream.WriteAsync(sendBytes, ct) -#endif .ConfigureAwait(false); +#endif await stream.FlushAsync(ct) .ConfigureAwait(false); @@ -322,10 +323,11 @@ await stream.FlushAsync(ct) { #if NETSTANDARD2_0 var numberOfBytes = await stream.ReadAsync(readBytes, 0, readBytes.Length, ct) + .ConfigureAwait(false); #else var numberOfBytes = await stream.ReadAsync(readBytes, ct) -#endif .ConfigureAwait(false); +#endif if (numberOfBytes == 0) { @@ -367,10 +369,11 @@ await Task.Delay(TimeSpan.FromSeconds(RetryTimeoutInSeconds), ct) // Keep the connection to Ryuk up. #if NETSTANDARD2_0 _ = await stream.ReadAsync(readBytes, 0, readBytes.Length, _maintainConnectionCts.Token) + .ConfigureAwait(false); #else _ = await stream.ReadAsync(readBytes, _maintainConnectionCts.Token) -#endif .ConfigureAwait(false); +#endif } } catch (OperationCanceledException) diff --git a/src/Testcontainers/Containers/TarOutputMemoryStream.cs b/src/Testcontainers/Containers/TarOutputMemoryStream.cs index b2b2043c2..99ae0d99c 100644 --- a/src/Testcontainers/Containers/TarOutputMemoryStream.cs +++ b/src/Testcontainers/Containers/TarOutputMemoryStream.cs @@ -76,10 +76,11 @@ await PutNextEntryAsync(tarEntry, ct) #if NETSTANDARD2_0 await WriteAsync(fileContent, 0, fileContent.Length, ct) + .ConfigureAwait(false); #else await WriteAsync(fileContent, ct) -#endif .ConfigureAwait(false); +#endif await CloseEntryAsync(ct) .ConfigureAwait(false);