From eedbad75c3c7d40010da944d05aac5313ca101ad Mon Sep 17 00:00:00 2001 From: Jonathan Gilbert Date: Tue, 10 Sep 2024 06:11:22 -0500 Subject: [PATCH 1/2] Updated the protected Storage constructor in Storage.cs to coalesce the supplied logger value to NullLoggerFactory.Instance, so that if a caller constructs an instance of BackblazeClient using the default logger value (which is null) then it still works. Bumped the LangVersion in Backblaze.Client.csproj to 8.0 to support the null-coalescing operator. --- src/Client/Backblaze.Client.csproj | 2 +- src/Client/Client/Storage/Storage.cs | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Client/Backblaze.Client.csproj b/src/Client/Backblaze.Client.csproj index 640a322..e4f968c 100644 --- a/src/Client/Backblaze.Client.csproj +++ b/src/Client/Backblaze.Client.csproj @@ -10,7 +10,7 @@ Microcompiler Bytewizer Inc. Backblaze B2 Cloud Storage - 7.1 + 8.0 Bytewizer.Backblaze 1.1.0 $(VersionPrefix)-$(VersionSuffix) diff --git a/src/Client/Client/Storage/Storage.cs b/src/Client/Client/Storage/Storage.cs index 25a25ac..d70d3d1 100644 --- a/src/Client/Client/Storage/Storage.cs +++ b/src/Client/Client/Storage/Storage.cs @@ -28,6 +28,8 @@ protected Storage(HttpClient client, IClientOptions options, ILoggerFactory logg try { // Initialize components + logger ??= NullLoggerFactory.Instance; + _client = new ApiClient(client, options, logger.CreateLogger(), cache); _logger = logger.CreateLogger(); } From 65d11ee24b24affde4cda3afb430468f81897539 Mon Sep 17 00:00:00 2001 From: Jonathan Gilbert Date: Thu, 10 Oct 2024 11:45:57 -0500 Subject: [PATCH 2/2] Added unit test class BackblazeClientTests.cs to Backblaze.Tests.Unit.csproj with tests that passing null for the ILoggerFactory parameter does not cause a crash. Added a reference to NSubstitute to Backblaze.Tests.Unit.csproj. --- test/Unit/Backblaze.Tests.Unit.csproj | 1 + test/Unit/BackblazeClientTests.cs | 41 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 test/Unit/BackblazeClientTests.cs diff --git a/test/Unit/Backblaze.Tests.Unit.csproj b/test/Unit/Backblaze.Tests.Unit.csproj index 321dd10..4de1c14 100644 --- a/test/Unit/Backblaze.Tests.Unit.csproj +++ b/test/Unit/Backblaze.Tests.Unit.csproj @@ -26,6 +26,7 @@ + all diff --git a/test/Unit/BackblazeClientTests.cs b/test/Unit/BackblazeClientTests.cs new file mode 100644 index 0000000..c25caa9 --- /dev/null +++ b/test/Unit/BackblazeClientTests.cs @@ -0,0 +1,41 @@ +using System.Net.Http; + +using Bytewizer.Backblaze.Client; + +using Microsoft.Extensions.Caching.Memory; + +using Xunit; + +using NSubstitute; + +namespace Backblaze.Tests.Unit +{ + public class BackblazeClientTests + { + [Fact] + public void BackblazeClient_constructor_does_not_crash_on_null_loggerFactory() + { + // Arrange + var clientOptions = new ClientOptions(); + + var dummyMemoryCache = Substitute.For(); + + // Act & Assert + new BackblazeClient(clientOptions, logger: null, dummyMemoryCache); + } + + [Fact] + public void BackblazeClient_constructor_with_HttpClient_does_not_crash_on_null_loggerFactory() + { + // Arrange + var clientOptions = new ClientOptions(); + + var httpClient = new HttpClient(); + + var dummyMemoryCache = Substitute.For(); + + // Act & Assert + new BackblazeClient(httpClient, clientOptions, logger: null, dummyMemoryCache); + } + } +}