From dbf8b306f52dc7df5a95357e4ae28fc91b450584 Mon Sep 17 00:00:00 2001 From: bell-nat Date: Wed, 24 Jul 2024 20:58:45 +0300 Subject: [PATCH 1/2] Fix CA1062 --- .../ValkeyBuilderExtensions.cs | 17 ++- .../ValkeyPublicApiTests.cs | 123 ++++++++++++++++++ 2 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 tests/Aspire.Hosting.Valkey.Tests/ValkeyPublicApiTests.cs diff --git a/src/Aspire.Hosting.Valkey/ValkeyBuilderExtensions.cs b/src/Aspire.Hosting.Valkey/ValkeyBuilderExtensions.cs index edbbe7c9c9..0852c8aae1 100644 --- a/src/Aspire.Hosting.Valkey/ValkeyBuilderExtensions.cs +++ b/src/Aspire.Hosting.Valkey/ValkeyBuilderExtensions.cs @@ -52,6 +52,9 @@ public static IResourceBuilder AddValkey(this IDistributedApplic string name, int? port = null) { + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(name); + var valkey = new ValkeyResource(name); return builder.AddResource(valkey) .WithEndpoint(port: port, targetPort: 6379, name: ValkeyResource.PrimaryEndpointName) @@ -80,6 +83,8 @@ public static IResourceBuilder AddValkey(this IDistributedApplic public static IResourceBuilder WithDataVolume(this IResourceBuilder builder, string? name = null, bool isReadOnly = false) { + ArgumentNullException.ThrowIfNull(builder); + builder.WithVolume(name ?? VolumeNameGenerator.CreateVolumeName(builder, "data"), ValkeyContainerDataDirectory, isReadOnly); if (!isReadOnly) @@ -111,6 +116,9 @@ public static IResourceBuilder WithDataVolume(this IResourceBuil public static IResourceBuilder WithDataBindMount(this IResourceBuilder builder, string source, bool isReadOnly = false) { + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(source); + builder.WithBindMount(source, ValkeyContainerDataDirectory, isReadOnly); if (!isReadOnly) { @@ -138,11 +146,16 @@ public static IResourceBuilder WithDataBindMount(this IResourceB /// The . public static IResourceBuilder WithPersistence(this IResourceBuilder builder, TimeSpan? interval = null, long keysChangedThreshold = 1) - => builder.WithAnnotation(new CommandLineArgsCallbackAnnotation(context => + { + ArgumentNullException.ThrowIfNull(builder); + + return builder.WithAnnotation(new CommandLineArgsCallbackAnnotation(context => { context.Args.Add("--save"); - context.Args.Add((interval ?? TimeSpan.FromSeconds(60)).TotalSeconds.ToString(CultureInfo.InvariantCulture)); + context.Args.Add( + (interval ?? TimeSpan.FromSeconds(60)).TotalSeconds.ToString(CultureInfo.InvariantCulture)); context.Args.Add(keysChangedThreshold.ToString(CultureInfo.InvariantCulture)); return Task.CompletedTask; }), ResourceAnnotationMutationBehavior.Replace); + } } diff --git a/tests/Aspire.Hosting.Valkey.Tests/ValkeyPublicApiTests.cs b/tests/Aspire.Hosting.Valkey.Tests/ValkeyPublicApiTests.cs new file mode 100644 index 0000000000..7431948c0b --- /dev/null +++ b/tests/Aspire.Hosting.Valkey.Tests/ValkeyPublicApiTests.cs @@ -0,0 +1,123 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using Aspire.Hosting.ApplicationModel; +using Xunit; + +namespace Aspire.Hosting.Valkey.Tests; + +public class ValkeyPublicApiTests +{ + #region ValkeyBuilderExtensions + + [Fact] + public void AddValkeyContainerShouldThrowsWhenBuilderIsNull() + { + IDistributedApplicationBuilder builder = null!; + const string name = "Valkey"; + + var action = () => builder.AddValkey(name); + + Assert.Multiple(() => + { + var exception = Assert.Throws(action); + Assert.Equal(nameof(builder), exception.ParamName); + }); + } + + [Fact] + public void AddValkeyContainerShouldThrowsWhenNameIsNull() + { + IDistributedApplicationBuilder builder = new DistributedApplicationBuilder([]); + string name = null!; + + var action = () => builder.AddValkey(name); + + Assert.Multiple(() => + { + var exception = Assert.Throws(action); + Assert.Equal(nameof(name), exception.ParamName); + }); + } + + [Fact] + public void WithDataVolumeShouldThrowsWhenBuilderIsNull() + { + IResourceBuilder builder = null!; + + var action = () => builder.WithDataVolume(); + + Assert.Multiple(() => + { + var exception = Assert.Throws(action); + Assert.Equal(nameof(builder), exception.ParamName); + }); + } + + [Fact] + public void WithDataBindMountShouldThrowsWhenBuilderIsNull() + { + IResourceBuilder builder = null!; + const string source = "Valkey"; + + var action = () => builder.WithDataBindMount(source); + + Assert.Multiple(() => + { + var exception = Assert.Throws(action); + Assert.Equal(nameof(builder), exception.ParamName); + }); + } + + [Fact] + public void WithDataBindMountShouldThrowsWhenSourceIsNull() + { + var distributedApplicationBuilder = new DistributedApplicationBuilder([]); + const string name = "Valkey"; + var resource = new ValkeyResource(name); + var builder = distributedApplicationBuilder.AddResource(resource); + string source = null!; + + var action = () => builder.WithDataBindMount(source); + + Assert.Multiple(() => + { + var exception = Assert.Throws(action); + Assert.Equal(nameof(source), exception.ParamName); + }); + } + + [Fact] + public void WithPersistenceShouldThrowsWhenBuilderIsNull() + { + IResourceBuilder builder = null!; + + var action = () => builder.WithPersistence(); + + Assert.Multiple(() => + { + var exception = Assert.Throws(action); + Assert.Equal(nameof(builder), exception.ParamName); + }); + } + + #endregion + + #region ValkeyResource + + [Fact] + public void CtorValkeyResourceShouldThrowsWhenNameIsNull() + { + string name = null!; + + var action = () => new ValkeyResource(name); + + Assert.Multiple(() => + { + var exception = Assert.Throws(action); + Assert.Equal(nameof(name), exception.ParamName); + }); + } + + #endregion +} From 863ce14ec140333a4c7e4713c241fb48e2f1f558 Mon Sep 17 00:00:00 2001 From: bell-nat Date: Tue, 30 Jul 2024 23:32:39 +0300 Subject: [PATCH 2/2] Fix PR by feedback --- .../ValkeyPublicApiTests.cs | 63 +++++++------------ 1 file changed, 21 insertions(+), 42 deletions(-) diff --git a/tests/Aspire.Hosting.Valkey.Tests/ValkeyPublicApiTests.cs b/tests/Aspire.Hosting.Valkey.Tests/ValkeyPublicApiTests.cs index 7431948c0b..30cf8552e5 100644 --- a/tests/Aspire.Hosting.Valkey.Tests/ValkeyPublicApiTests.cs +++ b/tests/Aspire.Hosting.Valkey.Tests/ValkeyPublicApiTests.cs @@ -11,66 +11,54 @@ public class ValkeyPublicApiTests #region ValkeyBuilderExtensions [Fact] - public void AddValkeyContainerShouldThrowsWhenBuilderIsNull() + public void AddValkeyContainerShouldThrowWhenBuilderIsNull() { IDistributedApplicationBuilder builder = null!; const string name = "Valkey"; var action = () => builder.AddValkey(name); - Assert.Multiple(() => - { - var exception = Assert.Throws(action); - Assert.Equal(nameof(builder), exception.ParamName); - }); + var exception = Assert.Throws(action); + Assert.Equal(nameof(builder), exception.ParamName); } [Fact] - public void AddValkeyContainerShouldThrowsWhenNameIsNull() + public void AddValkeyContainerShouldThrowWhenNameIsNull() { IDistributedApplicationBuilder builder = new DistributedApplicationBuilder([]); string name = null!; var action = () => builder.AddValkey(name); - Assert.Multiple(() => - { - var exception = Assert.Throws(action); - Assert.Equal(nameof(name), exception.ParamName); - }); + var exception = Assert.Throws(action); + Assert.Equal(nameof(name), exception.ParamName); } [Fact] - public void WithDataVolumeShouldThrowsWhenBuilderIsNull() + public void WithDataVolumeShouldThrowWhenBuilderIsNull() { IResourceBuilder builder = null!; var action = () => builder.WithDataVolume(); - Assert.Multiple(() => - { - var exception = Assert.Throws(action); - Assert.Equal(nameof(builder), exception.ParamName); - }); + var exception = Assert.Throws(action); + Assert.Equal(nameof(builder), exception.ParamName); } [Fact] - public void WithDataBindMountShouldThrowsWhenBuilderIsNull() + public void WithDataBindMountShouldThrowWhenBuilderIsNull() { IResourceBuilder builder = null!; const string source = "Valkey"; var action = () => builder.WithDataBindMount(source); - Assert.Multiple(() => - { - var exception = Assert.Throws(action); - Assert.Equal(nameof(builder), exception.ParamName); - }); + var exception = Assert.Throws(action); + Assert.Equal(nameof(builder), exception.ParamName); } [Fact] - public void WithDataBindMountShouldThrowsWhenSourceIsNull() + public void WithDataBindMountShouldThrowWhenSourceIsNull() { var distributedApplicationBuilder = new DistributedApplicationBuilder([]); const string name = "Valkey"; @@ -80,25 +68,19 @@ public void WithDataBindMountShouldThrowsWhenSourceIsNull() var action = () => builder.WithDataBindMount(source); - Assert.Multiple(() => - { - var exception = Assert.Throws(action); - Assert.Equal(nameof(source), exception.ParamName); - }); + var exception = Assert.Throws(action); + Assert.Equal(nameof(source), exception.ParamName); } [Fact] - public void WithPersistenceShouldThrowsWhenBuilderIsNull() + public void WithPersistenceShouldThrowWhenBuilderIsNull() { IResourceBuilder builder = null!; var action = () => builder.WithPersistence(); - Assert.Multiple(() => - { - var exception = Assert.Throws(action); - Assert.Equal(nameof(builder), exception.ParamName); - }); + var exception = Assert.Throws(action); + Assert.Equal(nameof(builder), exception.ParamName); } #endregion @@ -106,17 +88,14 @@ public void WithPersistenceShouldThrowsWhenBuilderIsNull() #region ValkeyResource [Fact] - public void CtorValkeyResourceShouldThrowsWhenNameIsNull() + public void CtorValkeyResourceShouldThrowWhenNameIsNull() { string name = null!; var action = () => new ValkeyResource(name); - Assert.Multiple(() => - { - var exception = Assert.Throws(action); - Assert.Equal(nameof(name), exception.ParamName); - }); + var exception = Assert.Throws(action); + Assert.Equal(nameof(name), exception.ParamName); } #endregion