diff --git a/src/Aspire.Hosting.Azure.CognitiveServices/Aspire.Hosting.Azure.CognitiveServices.csproj b/src/Aspire.Hosting.Azure.CognitiveServices/Aspire.Hosting.Azure.CognitiveServices.csproj index 29f2a4f1c83..2988e306b47 100644 --- a/src/Aspire.Hosting.Azure.CognitiveServices/Aspire.Hosting.Azure.CognitiveServices.csproj +++ b/src/Aspire.Hosting.Azure.CognitiveServices/Aspire.Hosting.Azure.CognitiveServices.csproj @@ -10,6 +10,7 @@ + diff --git a/src/Aspire.Hosting.Azure.CognitiveServices/AzureOpenAIDeployment.cs b/src/Aspire.Hosting.Azure.CognitiveServices/AzureOpenAIDeployment.cs index e3b614ab5a9..da8d7cb4753 100644 --- a/src/Aspire.Hosting.Azure.CognitiveServices/AzureOpenAIDeployment.cs +++ b/src/Aspire.Hosting.Azure.CognitiveServices/AzureOpenAIDeployment.cs @@ -1,8 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; +using static Aspire.ArgumentExceptionExtensions; namespace Aspire.Hosting.ApplicationModel; @@ -53,10 +52,4 @@ public class AzureOpenAIDeployment(string name, string modelName, string modelVe /// The default value is . /// public int SkuCapacity { get; set; } = skuCapacity ?? DefaultSkuCapacity; - - private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) - { - ArgumentException.ThrowIfNullOrEmpty(argument, paramName); - return argument; - } } diff --git a/src/Aspire.Hosting.Azure.CosmosDB/Aspire.Hosting.Azure.CosmosDB.csproj b/src/Aspire.Hosting.Azure.CosmosDB/Aspire.Hosting.Azure.CosmosDB.csproj index 47bcafe9987..ed3defcf180 100644 --- a/src/Aspire.Hosting.Azure.CosmosDB/Aspire.Hosting.Azure.CosmosDB.csproj +++ b/src/Aspire.Hosting.Azure.CosmosDB/Aspire.Hosting.Azure.CosmosDB.csproj @@ -1,4 +1,4 @@ - + $(DefaultTargetFramework) @@ -12,6 +12,7 @@ + diff --git a/src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBContainerResource.cs b/src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBContainerResource.cs index 87d86eedcea..fd963d99a21 100644 --- a/src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBContainerResource.cs +++ b/src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBContainerResource.cs @@ -1,9 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; using Aspire.Hosting.ApplicationModel; +using static Aspire.ArgumentExceptionExtensions; namespace Aspire.Hosting.Azure; @@ -17,15 +16,27 @@ namespace Aspire.Hosting.Azure; public class AzureCosmosDBContainerResource(string name, string containerName, string partitionKeyPath, AzureCosmosDBDatabaseResource parent) : Resource(name), IResourceWithParent, IResourceWithConnectionString, IResourceWithAzureFunctionsConfig { + private string _containerName = ThrowIfNullOrEmpty(containerName); + /// /// Gets or sets the container name. /// - public string ContainerName { get; set; } = ThrowIfNullOrEmpty(containerName); + public string ContainerName + { + get => _containerName; + set => _containerName = ThrowIfNullOrEmpty(value, nameof(containerName)); + } + + private string _partitionKeyPath = ThrowIfNullOrEmpty(partitionKeyPath); /// /// Gets or sets the partition key path. /// - public string PartitionKeyPath { get; set; } = ThrowIfNullOrEmpty(partitionKeyPath); + public string PartitionKeyPath + { + get => _partitionKeyPath; + set => _partitionKeyPath = ThrowIfNullOrEmpty(value, nameof(partitionKeyPath)); + } /// /// Gets the parent Azure Cosmos DB database resource. @@ -54,10 +65,4 @@ void IResourceWithAzureFunctionsConfig.ApplyAzureFunctionsConfiguration(IDiction target[$"Aspire__Microsoft__Azure__Cosmos__{connectionName}__ContainerName"] = ContainerName; } } - - private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) - { - ArgumentException.ThrowIfNullOrEmpty(argument, paramName); - return argument; - } } diff --git a/src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBDatabaseResource.cs b/src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBDatabaseResource.cs index c1e3a358299..eabb4f8a411 100644 --- a/src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBDatabaseResource.cs +++ b/src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBDatabaseResource.cs @@ -1,9 +1,9 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; using Aspire.Hosting.ApplicationModel; +using static Aspire.ArgumentExceptionExtensions; + namespace Aspire.Hosting.Azure; /// @@ -16,10 +16,16 @@ namespace Aspire.Hosting.Azure; public class AzureCosmosDBDatabaseResource(string name, string databaseName, AzureCosmosDBResource parent) : Resource(name), IResourceWithParent, IResourceWithConnectionString, IResourceWithAzureFunctionsConfig { + private string _databaseName = ThrowIfNullOrEmpty(databaseName); + /// /// Gets or sets the database name. /// - public string DatabaseName { get; set; } = ThrowIfNullOrEmpty(databaseName); + public string DatabaseName + { + get => _databaseName; + set => _databaseName = ThrowIfNullOrEmpty(value, nameof(databaseName)); + } /// /// The containers for this database. @@ -51,10 +57,4 @@ void IResourceWithAzureFunctionsConfig.ApplyAzureFunctionsConfiguration(IDiction target[$"Aspire__Microsoft__Azure__Cosmos__{connectionName}__DatabaseName"] = DatabaseName; } } - - private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) - { - ArgumentException.ThrowIfNullOrEmpty(argument, paramName); - return argument; - } } diff --git a/src/Aspire.Hosting.Azure.EventHubs/Aspire.Hosting.Azure.EventHubs.csproj b/src/Aspire.Hosting.Azure.EventHubs/Aspire.Hosting.Azure.EventHubs.csproj index d5e7389cdac..d73dbb852c3 100644 --- a/src/Aspire.Hosting.Azure.EventHubs/Aspire.Hosting.Azure.EventHubs.csproj +++ b/src/Aspire.Hosting.Azure.EventHubs/Aspire.Hosting.Azure.EventHubs.csproj @@ -10,6 +10,7 @@ + diff --git a/src/Aspire.Hosting.Azure.EventHubs/AzureEventHubConsumerGroupResource.cs b/src/Aspire.Hosting.Azure.EventHubs/AzureEventHubConsumerGroupResource.cs index a61cef7f1f7..2c0db7f957b 100644 --- a/src/Aspire.Hosting.Azure.EventHubs/AzureEventHubConsumerGroupResource.cs +++ b/src/Aspire.Hosting.Azure.EventHubs/AzureEventHubConsumerGroupResource.cs @@ -1,11 +1,10 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; using System.Text.Json; using Aspire.Hosting.ApplicationModel; using Azure.Provisioning; +using static Aspire.ArgumentExceptionExtensions; namespace Aspire.Hosting.Azure; @@ -19,10 +18,16 @@ namespace Aspire.Hosting.Azure; public class AzureEventHubConsumerGroupResource(string name, string consumerGroupName, AzureEventHubResource parent) : Resource(name), IResourceWithParent, IResourceWithConnectionString, IResourceWithAzureFunctionsConfig { + private string _consumerGroupName = ThrowIfNullOrEmpty(consumerGroupName); + /// /// The event hub consumer group name. /// - public string ConsumerGroupName { get; set; } = ThrowIfNullOrEmpty(consumerGroupName); + public string ConsumerGroupName + { + get => _consumerGroupName; + set => _consumerGroupName = ThrowIfNullOrEmpty(value, nameof(consumerGroupName)); + } /// /// Gets the parent Azure Event Hub resource. @@ -60,10 +65,4 @@ internal void WriteJsonObjectProperties(Utf8JsonWriter writer) { writer.WriteString(nameof(Name), ConsumerGroupName); } - - private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) - { - ArgumentException.ThrowIfNullOrEmpty(argument, paramName); - return argument; - } } diff --git a/src/Aspire.Hosting.Azure.EventHubs/AzureEventHubResource.cs b/src/Aspire.Hosting.Azure.EventHubs/AzureEventHubResource.cs index 82e3baa673b..49bc80b13fa 100644 --- a/src/Aspire.Hosting.Azure.EventHubs/AzureEventHubResource.cs +++ b/src/Aspire.Hosting.Azure.EventHubs/AzureEventHubResource.cs @@ -1,11 +1,10 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; using System.Text.Json; using Aspire.Hosting.ApplicationModel; using Azure.Provisioning; +using static Aspire.ArgumentExceptionExtensions; namespace Aspire.Hosting.Azure; @@ -19,10 +18,16 @@ namespace Aspire.Hosting.Azure; public class AzureEventHubResource(string name, string hubName, AzureEventHubsResource parent) : Resource(name), IResourceWithParent, IResourceWithConnectionString, IResourceWithAzureFunctionsConfig { + private string _hubName = ThrowIfNullOrEmpty(hubName); + /// /// The event hub name. /// - public string HubName { get; set; } = ThrowIfNullOrEmpty(hubName); + public string HubName + { + get => _hubName; + set => _hubName = ThrowIfNullOrEmpty(value, nameof(hubName)); + } /// /// Number of partitions created for the Event Hub, allowed values are from @@ -109,10 +114,4 @@ internal void WriteJsonObjectProperties(Utf8JsonWriter writer) writer.WriteEndArray(); } #pragma warning restore CA1507 // Use nameof to express symbol names - - private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) - { - ArgumentException.ThrowIfNullOrEmpty(argument, paramName); - return argument; - } } diff --git a/src/Aspire.Hosting.Azure.PostgreSQL/Aspire.Hosting.Azure.PostgreSQL.csproj b/src/Aspire.Hosting.Azure.PostgreSQL/Aspire.Hosting.Azure.PostgreSQL.csproj index 2693bea4c60..e2c0c2ed415 100644 --- a/src/Aspire.Hosting.Azure.PostgreSQL/Aspire.Hosting.Azure.PostgreSQL.csproj +++ b/src/Aspire.Hosting.Azure.PostgreSQL/Aspire.Hosting.Azure.PostgreSQL.csproj @@ -10,6 +10,7 @@ + diff --git a/src/Aspire.Hosting.Azure.PostgreSQL/AzurePostgresFlexibleServerDatabaseResource.cs b/src/Aspire.Hosting.Azure.PostgreSQL/AzurePostgresFlexibleServerDatabaseResource.cs index e7f82496a4a..b63dd6c31fe 100644 --- a/src/Aspire.Hosting.Azure.PostgreSQL/AzurePostgresFlexibleServerDatabaseResource.cs +++ b/src/Aspire.Hosting.Azure.PostgreSQL/AzurePostgresFlexibleServerDatabaseResource.cs @@ -1,9 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; using Aspire.Hosting.ApplicationModel; +using static Aspire.ArgumentExceptionExtensions; namespace Aspire.Hosting.Azure; @@ -41,12 +40,6 @@ public class AzurePostgresFlexibleServerDatabaseResource(string name, string dat /// public override ResourceAnnotationCollection Annotations => InnerResource?.Annotations ?? base.Annotations; - private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) - { - ArgumentException.ThrowIfNullOrEmpty(argument, paramName); - return argument; - } - internal void SetInnerResource(PostgresDatabaseResource innerResource) { // Copy the annotations to the inner resource before making it the inner resource diff --git a/src/Aspire.Hosting.Azure.ServiceBus/Aspire.Hosting.Azure.ServiceBus.csproj b/src/Aspire.Hosting.Azure.ServiceBus/Aspire.Hosting.Azure.ServiceBus.csproj index 2b03599b6a8..f051ed29f91 100644 --- a/src/Aspire.Hosting.Azure.ServiceBus/Aspire.Hosting.Azure.ServiceBus.csproj +++ b/src/Aspire.Hosting.Azure.ServiceBus/Aspire.Hosting.Azure.ServiceBus.csproj @@ -10,6 +10,7 @@ + diff --git a/src/Aspire.Hosting.Azure.ServiceBus/AzureServiceBusQueueResource.cs b/src/Aspire.Hosting.Azure.ServiceBus/AzureServiceBusQueueResource.cs index d9e3a3bd5c5..95ccdd2d32f 100644 --- a/src/Aspire.Hosting.Azure.ServiceBus/AzureServiceBusQueueResource.cs +++ b/src/Aspire.Hosting.Azure.ServiceBus/AzureServiceBusQueueResource.cs @@ -1,12 +1,11 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; using System.Text.Json; using System.Xml; using Aspire.Hosting.ApplicationModel; using Azure.Provisioning; +using static Aspire.ArgumentExceptionExtensions; namespace Aspire.Hosting.Azure; @@ -198,10 +197,4 @@ internal void WriteJsonObjectProperties(Utf8JsonWriter writer) } writer.WriteEndObject(); } - - private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) - { - ArgumentException.ThrowIfNullOrEmpty(argument, paramName); - return argument; - } } diff --git a/src/Aspire.Hosting.Azure.ServiceBus/AzureServiceBusRule.cs b/src/Aspire.Hosting.Azure.ServiceBus/AzureServiceBusRule.cs index 362cd821317..1e623b34d13 100644 --- a/src/Aspire.Hosting.Azure.ServiceBus/AzureServiceBusRule.cs +++ b/src/Aspire.Hosting.Azure.ServiceBus/AzureServiceBusRule.cs @@ -1,10 +1,9 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; using System.Text.Json; using Azure.Provisioning; +using static Aspire.ArgumentExceptionExtensions; namespace Aspire.Hosting.Azure; @@ -181,10 +180,4 @@ internal void WriteJsonObjectProperties(Utf8JsonWriter writer) writer.WriteEndObject(); // Properties } - - private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) - { - ArgumentException.ThrowIfNullOrEmpty(argument, paramName); - return argument; - } } diff --git a/src/Aspire.Hosting.Azure.ServiceBus/AzureServiceBusSubscriptionResource.cs b/src/Aspire.Hosting.Azure.ServiceBus/AzureServiceBusSubscriptionResource.cs index be49c2340ef..9fdf6648cf1 100644 --- a/src/Aspire.Hosting.Azure.ServiceBus/AzureServiceBusSubscriptionResource.cs +++ b/src/Aspire.Hosting.Azure.ServiceBus/AzureServiceBusSubscriptionResource.cs @@ -1,12 +1,11 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; using System.Text.Json; using System.Xml; using Aspire.Hosting.ApplicationModel; using Azure.Provisioning; +using static Aspire.ArgumentExceptionExtensions; namespace Aspire.Hosting.Azure; @@ -183,10 +182,4 @@ internal void WriteJsonObjectProperties(Utf8JsonWriter writer) writer.WriteEndObject(); } - - private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) - { - ArgumentException.ThrowIfNullOrEmpty(argument, paramName); - return argument; - } } diff --git a/src/Aspire.Hosting.Azure.ServiceBus/AzureServiceBusTopicResource.cs b/src/Aspire.Hosting.Azure.ServiceBus/AzureServiceBusTopicResource.cs index 45cc12bc664..96a7989457a 100644 --- a/src/Aspire.Hosting.Azure.ServiceBus/AzureServiceBusTopicResource.cs +++ b/src/Aspire.Hosting.Azure.ServiceBus/AzureServiceBusTopicResource.cs @@ -1,12 +1,11 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; using System.Text.Json; using System.Xml; using Aspire.Hosting.ApplicationModel; using Azure.Provisioning; +using static Aspire.ArgumentExceptionExtensions; namespace Aspire.Hosting.Azure; @@ -126,10 +125,4 @@ internal void WriteJsonObjectProperties(Utf8JsonWriter writer) writer.WriteEndObject(); } - - private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) - { - ArgumentException.ThrowIfNullOrEmpty(argument, paramName); - return argument; - } } diff --git a/src/Aspire.Hosting.Azure.Sql/Aspire.Hosting.Azure.Sql.csproj b/src/Aspire.Hosting.Azure.Sql/Aspire.Hosting.Azure.Sql.csproj index 7dfc5b09eb3..bc8f870d77e 100644 --- a/src/Aspire.Hosting.Azure.Sql/Aspire.Hosting.Azure.Sql.csproj +++ b/src/Aspire.Hosting.Azure.Sql/Aspire.Hosting.Azure.Sql.csproj @@ -10,6 +10,7 @@ + diff --git a/src/Aspire.Hosting.Azure.Sql/AzureSqlDatabaseResource.cs b/src/Aspire.Hosting.Azure.Sql/AzureSqlDatabaseResource.cs index d8250a73c64..e42a59bd535 100644 --- a/src/Aspire.Hosting.Azure.Sql/AzureSqlDatabaseResource.cs +++ b/src/Aspire.Hosting.Azure.Sql/AzureSqlDatabaseResource.cs @@ -1,9 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; using Aspire.Hosting.ApplicationModel; +using static Aspire.ArgumentExceptionExtensions; namespace Aspire.Hosting.Azure; @@ -52,12 +51,6 @@ public class AzureSqlDatabaseResource(string name, string databaseName, AzureSql /// public override ResourceAnnotationCollection Annotations => InnerResource?.Annotations ?? base.Annotations; - private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) - { - ArgumentException.ThrowIfNullOrEmpty(argument, paramName); - return argument; - } - internal void SetInnerResource(SqlServerDatabaseResource innerResource) { // Copy the annotations to the inner resource before making it the inner resource diff --git a/src/Aspire.Hosting.Garnet/GarnetBuilderExtensions.cs b/src/Aspire.Hosting.Garnet/GarnetBuilderExtensions.cs index 17ae0fe0a6b..1c4851f70b5 100644 --- a/src/Aspire.Hosting.Garnet/GarnetBuilderExtensions.cs +++ b/src/Aspire.Hosting.Garnet/GarnetBuilderExtensions.cs @@ -54,7 +54,7 @@ public static class GarnetBuilderExtensions public static IResourceBuilder AddGarnet(this IDistributedApplicationBuilder builder, [ResourceName] string name, int? port) { - return builder.AddGarnet(name, port, password: null); + return AddGarnet(builder, name, port, password: null); } /// diff --git a/src/Aspire.Hosting.Milvus/Aspire.Hosting.Milvus.csproj b/src/Aspire.Hosting.Milvus/Aspire.Hosting.Milvus.csproj index 5c7f70f8828..f14fbb1d233 100644 --- a/src/Aspire.Hosting.Milvus/Aspire.Hosting.Milvus.csproj +++ b/src/Aspire.Hosting.Milvus/Aspire.Hosting.Milvus.csproj @@ -11,6 +11,8 @@ + + diff --git a/src/Aspire.Hosting.Milvus/MilvusDatabaseResource.cs b/src/Aspire.Hosting.Milvus/MilvusDatabaseResource.cs index 69adf9f4743..85ec2c48bc2 100644 --- a/src/Aspire.Hosting.Milvus/MilvusDatabaseResource.cs +++ b/src/Aspire.Hosting.Milvus/MilvusDatabaseResource.cs @@ -1,9 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; using Aspire.Hosting.Milvus; +using static Aspire.ArgumentExceptionExtensions; namespace Aspire.Hosting.ApplicationModel; @@ -30,10 +29,4 @@ public class MilvusDatabaseResource(string name, string databaseName, MilvusServ /// Gets the database name. /// public string DatabaseName { get; } = ThrowIfNullOrEmpty(databaseName); - - private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) - { - ArgumentException.ThrowIfNullOrEmpty(argument, paramName); - return argument; - } } diff --git a/src/Aspire.Hosting.MongoDB/Aspire.Hosting.MongoDB.csproj b/src/Aspire.Hosting.MongoDB/Aspire.Hosting.MongoDB.csproj index 93fc96cb9b8..a3ad1df04b6 100644 --- a/src/Aspire.Hosting.MongoDB/Aspire.Hosting.MongoDB.csproj +++ b/src/Aspire.Hosting.MongoDB/Aspire.Hosting.MongoDB.csproj @@ -11,6 +11,7 @@ + diff --git a/src/Aspire.Hosting.MongoDB/MongoDBDatabaseResource.cs b/src/Aspire.Hosting.MongoDB/MongoDBDatabaseResource.cs index 2c66493fc82..7824434664b 100644 --- a/src/Aspire.Hosting.MongoDB/MongoDBDatabaseResource.cs +++ b/src/Aspire.Hosting.MongoDB/MongoDBDatabaseResource.cs @@ -1,8 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; +using static Aspire.ArgumentExceptionExtensions; namespace Aspire.Hosting.ApplicationModel; @@ -29,10 +28,4 @@ public class MongoDBDatabaseResource(string name, string databaseName, MongoDBSe /// Gets the database name. /// public string DatabaseName { get; } = ThrowIfNullOrEmpty(databaseName); - - private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) - { - ArgumentException.ThrowIfNullOrEmpty(argument, paramName); - return argument; - } } diff --git a/src/Aspire.Hosting.MySql/Aspire.Hosting.MySql.csproj b/src/Aspire.Hosting.MySql/Aspire.Hosting.MySql.csproj index 4070ac50ab7..cd7b795497d 100644 --- a/src/Aspire.Hosting.MySql/Aspire.Hosting.MySql.csproj +++ b/src/Aspire.Hosting.MySql/Aspire.Hosting.MySql.csproj @@ -10,6 +10,7 @@ + diff --git a/src/Aspire.Hosting.MySql/MySqlDatabaseResource.cs b/src/Aspire.Hosting.MySql/MySqlDatabaseResource.cs index 71de49ba54d..59139327200 100644 --- a/src/Aspire.Hosting.MySql/MySqlDatabaseResource.cs +++ b/src/Aspire.Hosting.MySql/MySqlDatabaseResource.cs @@ -1,8 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; +using static Aspire.ArgumentExceptionExtensions; using MySqlConnector; namespace Aspire.Hosting.ApplicationModel; @@ -40,10 +39,4 @@ public ReferenceExpression ConnectionStringExpression /// Gets the database name. /// public string DatabaseName { get; } = ThrowIfNullOrEmpty(databaseName); - - private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) - { - ArgumentException.ThrowIfNullOrEmpty(argument, paramName); - return argument; - } } diff --git a/src/Aspire.Hosting.Oracle/Aspire.Hosting.Oracle.csproj b/src/Aspire.Hosting.Oracle/Aspire.Hosting.Oracle.csproj index 75c7631f739..d7d4ffe9fac 100644 --- a/src/Aspire.Hosting.Oracle/Aspire.Hosting.Oracle.csproj +++ b/src/Aspire.Hosting.Oracle/Aspire.Hosting.Oracle.csproj @@ -9,6 +9,7 @@ + diff --git a/src/Aspire.Hosting.Oracle/OracleDatabaseResource.cs b/src/Aspire.Hosting.Oracle/OracleDatabaseResource.cs index 872443a66eb..03703b44213 100644 --- a/src/Aspire.Hosting.Oracle/OracleDatabaseResource.cs +++ b/src/Aspire.Hosting.Oracle/OracleDatabaseResource.cs @@ -1,8 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; +using static Aspire.ArgumentExceptionExtensions; namespace Aspire.Hosting.ApplicationModel; @@ -30,10 +29,4 @@ public class OracleDatabaseResource(string name, string databaseName, OracleData /// Gets the database name. /// public string DatabaseName { get; } = ThrowIfNullOrEmpty(databaseName); - - private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) - { - ArgumentException.ThrowIfNullOrEmpty(argument, paramName); - return argument; - } } diff --git a/src/Aspire.Hosting.PostgreSQL/Aspire.Hosting.PostgreSQL.csproj b/src/Aspire.Hosting.PostgreSQL/Aspire.Hosting.PostgreSQL.csproj index fdc2dd9df05..6b107b79249 100644 --- a/src/Aspire.Hosting.PostgreSQL/Aspire.Hosting.PostgreSQL.csproj +++ b/src/Aspire.Hosting.PostgreSQL/Aspire.Hosting.PostgreSQL.csproj @@ -10,6 +10,7 @@ + diff --git a/src/Aspire.Hosting.PostgreSQL/PostgresDatabaseResource.cs b/src/Aspire.Hosting.PostgreSQL/PostgresDatabaseResource.cs index 5ae94ad5575..7d6443057f7 100644 --- a/src/Aspire.Hosting.PostgreSQL/PostgresDatabaseResource.cs +++ b/src/Aspire.Hosting.PostgreSQL/PostgresDatabaseResource.cs @@ -2,8 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Data.Common; -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; +using static Aspire.ArgumentExceptionExtensions; namespace Aspire.Hosting.ApplicationModel; @@ -40,10 +39,4 @@ public ReferenceExpression ConnectionStringExpression /// Gets the database name. /// public string DatabaseName { get; } = ThrowIfNullOrEmpty(databaseName); - - private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) - { - ArgumentException.ThrowIfNullOrEmpty(argument, paramName); - return argument; - } } diff --git a/src/Aspire.Hosting.Python/Aspire.Hosting.Python.csproj b/src/Aspire.Hosting.Python/Aspire.Hosting.Python.csproj index 367683a5155..1606889137a 100644 --- a/src/Aspire.Hosting.Python/Aspire.Hosting.Python.csproj +++ b/src/Aspire.Hosting.Python/Aspire.Hosting.Python.csproj @@ -9,6 +9,7 @@ + diff --git a/src/Aspire.Hosting.Python/PythonAppResourceBuilderExtensions.cs b/src/Aspire.Hosting.Python/PythonAppResourceBuilderExtensions.cs index d93f5912606..a0f9a37b962 100644 --- a/src/Aspire.Hosting.Python/PythonAppResourceBuilderExtensions.cs +++ b/src/Aspire.Hosting.Python/PythonAppResourceBuilderExtensions.cs @@ -4,6 +4,7 @@ using Aspire.Hosting.ApplicationModel; using Aspire.Hosting.Python; using Aspire.Hosting.Utils; +using static Aspire.ArgumentExceptionExtensions; namespace Aspire.Hosting; @@ -173,21 +174,4 @@ private static void AddOpenTelemetryArguments(CommandLineArgsCallbackContext con context.Args.Add("--metrics_exporter"); context.Args.Add("otlp"); } - - private static void ThrowIfNullOrContainsIsNullOrEmpty(string[] scriptArgs) - { - ArgumentNullException.ThrowIfNull(scriptArgs); - foreach (var scriptArg in scriptArgs) - { - if (string.IsNullOrEmpty(scriptArg)) - { - var values = string.Join(", ", scriptArgs); - if (scriptArg is null) - { - throw new ArgumentNullException(nameof(scriptArgs), $"Array params contains null item: [{values}]"); - } - throw new ArgumentException($"Array params contains empty item: [{values}]", nameof(scriptArgs)); - } - } - } } diff --git a/src/Aspire.Hosting.Redis/RedisBuilderExtensions.cs b/src/Aspire.Hosting.Redis/RedisBuilderExtensions.cs index c98e403e3a5..c8fb1510111 100644 --- a/src/Aspire.Hosting.Redis/RedisBuilderExtensions.cs +++ b/src/Aspire.Hosting.Redis/RedisBuilderExtensions.cs @@ -33,7 +33,7 @@ public static class RedisBuilderExtensions /// public static IResourceBuilder AddRedis(this IDistributedApplicationBuilder builder, [ResourceName] string name, int? port) { - return builder.AddRedis(name, port, null); + return AddRedis(builder, name, port, null); } /// diff --git a/src/Aspire.Hosting.Redis/RedisResource.cs b/src/Aspire.Hosting.Redis/RedisResource.cs index 2d3769de7ba..b4a903f23f9 100644 --- a/src/Aspire.Hosting.Redis/RedisResource.cs +++ b/src/Aspire.Hosting.Redis/RedisResource.cs @@ -16,6 +16,8 @@ public class RedisResource(string name) : ContainerResource(name), IResourceWith /// A parameter that contains the Redis server password. public RedisResource(string name, ParameterResource password) : this(name) { + ArgumentNullException.ThrowIfNull(password); + PasswordParameter = password; } diff --git a/src/Aspire.Hosting.SqlServer/Aspire.Hosting.SqlServer.csproj b/src/Aspire.Hosting.SqlServer/Aspire.Hosting.SqlServer.csproj index f488e5e241b..873a9f5e947 100644 --- a/src/Aspire.Hosting.SqlServer/Aspire.Hosting.SqlServer.csproj +++ b/src/Aspire.Hosting.SqlServer/Aspire.Hosting.SqlServer.csproj @@ -10,6 +10,7 @@ + diff --git a/src/Aspire.Hosting.SqlServer/SqlServerDatabaseResource.cs b/src/Aspire.Hosting.SqlServer/SqlServerDatabaseResource.cs index e87343e2627..799746a83a0 100644 --- a/src/Aspire.Hosting.SqlServer/SqlServerDatabaseResource.cs +++ b/src/Aspire.Hosting.SqlServer/SqlServerDatabaseResource.cs @@ -1,9 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; using Microsoft.Data.SqlClient; +using static Aspire.ArgumentExceptionExtensions; namespace Aspire.Hosting.ApplicationModel; @@ -41,10 +40,4 @@ public ReferenceExpression ConnectionStringExpression /// Gets the database name. /// public string DatabaseName { get; } = ThrowIfNullOrEmpty(databaseName); - - private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) - { - ArgumentException.ThrowIfNullOrEmpty(argument, paramName); - return argument; - } } diff --git a/src/Aspire.Hosting.Testing/Aspire.Hosting.Testing.csproj b/src/Aspire.Hosting.Testing/Aspire.Hosting.Testing.csproj index c65eb272a8c..59469c53476 100644 --- a/src/Aspire.Hosting.Testing/Aspire.Hosting.Testing.csproj +++ b/src/Aspire.Hosting.Testing/Aspire.Hosting.Testing.csproj @@ -1,4 +1,4 @@ - + $(AllTargetFrameworks) @@ -14,6 +14,7 @@ + diff --git a/src/Aspire.Hosting.Testing/DistributedApplicationFactory.cs b/src/Aspire.Hosting.Testing/DistributedApplicationFactory.cs index adaaf28a3f1..7b91696989f 100644 --- a/src/Aspire.Hosting.Testing/DistributedApplicationFactory.cs +++ b/src/Aspire.Hosting.Testing/DistributedApplicationFactory.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; +using static Aspire.ArgumentExceptionExtensions; namespace Aspire.Hosting.Testing; @@ -149,24 +150,6 @@ protected virtual void OnBuilt(DistributedApplication application) { } - private static string[] ThrowIfNullOrContainsIsNullOrEmpty(string[] args) - { - ArgumentNullException.ThrowIfNull(args); - foreach (var arg in args) - { - if (string.IsNullOrEmpty(arg)) - { - var values = string.Join(", ", args); - if (arg is null) - { - throw new ArgumentNullException(nameof(args), $"Array params contains null item: [{values}]"); - } - throw new ArgumentException($"Array params contains empty item: [{values}]", nameof(args)); - } - } - return args; - } - private void OnBuiltCore(DistributedApplication application) { _shutdownTimeout = application.Services.GetService>()?.Value.ShutdownTimeout ?? _shutdownTimeout; diff --git a/src/Aspire.Hosting.Testing/DistributedApplicationTestingBuilder.cs b/src/Aspire.Hosting.Testing/DistributedApplicationTestingBuilder.cs index de9e7405ed5..ed2158afaed 100644 --- a/src/Aspire.Hosting.Testing/DistributedApplicationTestingBuilder.cs +++ b/src/Aspire.Hosting.Testing/DistributedApplicationTestingBuilder.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using static Aspire.ArgumentExceptionExtensions; namespace Aspire.Hosting.Testing; @@ -135,23 +136,6 @@ public static IDistributedApplicationTestingBuilder Create(string[] args, Action return new TestingBuilder(args, configureBuilder); } - private static void ThrowIfNullOrContainsIsNullOrEmpty(string[] args) - { - ArgumentNullException.ThrowIfNull(args); - foreach (var arg in args) - { - if (string.IsNullOrEmpty(arg)) - { - var values = string.Join(", ", args); - if (arg is null) - { - throw new ArgumentNullException(nameof(args), $"Array params contains null item: [{values}]"); - } - throw new ArgumentException($"Array params contains empty item: [{values}]", nameof(args)); - } - } - } - private sealed class SuspendingDistributedApplicationFactory(Type entryPoint, string[] args, Action configureBuilder) : DistributedApplicationFactory(entryPoint, args) { diff --git a/src/Aspire.Hosting.Valkey/ValkeyResource.cs b/src/Aspire.Hosting.Valkey/ValkeyResource.cs index 15b64e5c396..85a268d3a1e 100644 --- a/src/Aspire.Hosting.Valkey/ValkeyResource.cs +++ b/src/Aspire.Hosting.Valkey/ValkeyResource.cs @@ -20,6 +20,8 @@ public class ValkeyResource(string name) : ContainerResource(name), IResourceWit /// A parameter that contains the Valkey server password. public ValkeyResource(string name, ParameterResource password) : this(name) { + ArgumentNullException.ThrowIfNull(password); + PasswordParameter = password; } diff --git a/src/Aspire.Hosting/ApplicationModel/ExecutableResource.cs b/src/Aspire.Hosting/ApplicationModel/ExecutableResource.cs index e536a73a041..2f26ca0c575 100644 --- a/src/Aspire.Hosting/ApplicationModel/ExecutableResource.cs +++ b/src/Aspire.Hosting/ApplicationModel/ExecutableResource.cs @@ -3,8 +3,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; +using static Aspire.ArgumentExceptionExtensions; namespace Aspire.Hosting.ApplicationModel; @@ -32,11 +31,5 @@ public class ExecutableResource(string name, string command, string workingDirec /// /// Gets the working directory for the executable resource. /// - public string WorkingDirectory { get; } = workingDirectory ?? throw new ArgumentNullException(nameof(workingDirectory)); - - private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) - { - ArgumentException.ThrowIfNullOrEmpty(argument, paramName); - return argument; - } + public string WorkingDirectory { get; } = ThrowIfNullOrEmpty(workingDirectory); } diff --git a/src/Aspire.Hosting/Aspire.Hosting.csproj b/src/Aspire.Hosting/Aspire.Hosting.csproj index 945f36b91c0..806d10051f0 100644 --- a/src/Aspire.Hosting/Aspire.Hosting.csproj +++ b/src/Aspire.Hosting/Aspire.Hosting.csproj @@ -1,4 +1,4 @@ - + $(DefaultTargetFramework) @@ -41,6 +41,7 @@ + diff --git a/src/Components/Aspire.OpenAI/Aspire.OpenAI.csproj b/src/Components/Aspire.OpenAI/Aspire.OpenAI.csproj index 80805ec79d3..925ff1718af 100644 --- a/src/Components/Aspire.OpenAI/Aspire.OpenAI.csproj +++ b/src/Components/Aspire.OpenAI/Aspire.OpenAI.csproj @@ -12,6 +12,7 @@ + diff --git a/src/Components/Aspire.OpenAI/AspireOpenAIClientBuilder.cs b/src/Components/Aspire.OpenAI/AspireOpenAIClientBuilder.cs index 440adf96ff0..6e4ec3ce61f 100644 --- a/src/Components/Aspire.OpenAI/AspireOpenAIClientBuilder.cs +++ b/src/Components/Aspire.OpenAI/AspireOpenAIClientBuilder.cs @@ -2,11 +2,10 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Data.Common; -using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using OpenAI; +using static Aspire.ArgumentExceptionExtensions; namespace Aspire.OpenAI; @@ -87,10 +86,4 @@ internal string GetRequiredDeploymentName() private static string? ConnectionStringValue(DbConnectionStringBuilder connectionString, string key) => connectionString.TryGetValue(key, out var value) ? value as string : null; - - private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) - { - ArgumentException.ThrowIfNullOrEmpty(argument, paramName); - return argument; - } } diff --git a/src/Components/Common/ArgumentExceptionExtensions.cs b/src/Components/Common/ArgumentExceptionExtensions.cs new file mode 100644 index 00000000000..16463748426 --- /dev/null +++ b/src/Components/Common/ArgumentExceptionExtensions.cs @@ -0,0 +1,45 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; + +namespace Aspire; + +internal static class ArgumentExceptionExtensions +{ + /// + /// Adds a check Throw ArgumentException if argument is null or empty. + /// And return argument + /// + /// + /// + /// argument not null + public static string ThrowIfNullOrEmpty( + [NotNull] string? argument, + [CallerArgumentExpression(nameof(argument))] string? paramName = null) + { + ArgumentException.ThrowIfNullOrEmpty(argument, paramName); + return argument; + } + + public static string[] ThrowIfNullOrContainsIsNullOrEmpty( + [NotNull] string[] args, + [CallerArgumentExpression(nameof(args))] string? paramName = null) + { + ArgumentNullException.ThrowIfNull(args, paramName); + foreach (var arg in args) + { + if (string.IsNullOrEmpty(arg)) + { + var values = string.Join(", ", args); + if (arg is null) + { + throw new ArgumentNullException(paramName, $"Array params contains null item: [{values}]"); + } + throw new ArgumentException($"Array params contains empty item: [{values}]", paramName); + } + } + return args; + } +} diff --git a/tests/Aspire.Hosting.Garnet.Tests/GarnetPublicApiTests.cs b/tests/Aspire.Hosting.Garnet.Tests/GarnetPublicApiTests.cs index 160671a44df..b9264d96b64 100644 --- a/tests/Aspire.Hosting.Garnet.Tests/GarnetPublicApiTests.cs +++ b/tests/Aspire.Hosting.Garnet.Tests/GarnetPublicApiTests.cs @@ -9,27 +9,45 @@ namespace Aspire.Hosting.Garnet.Tests; public class GarnetPublicApiTests { - [Fact] - public void AddGarnetShouldThrowWhenBuilderIsNull() + [Theory] + [InlineData(0)] + [InlineData(1)] + public void AddGarnetShouldThrowWhenBuilderIsNull(int overrideIndex) { IDistributedApplicationBuilder builder = null!; const string name = "garnet"; + int? port = null; + IResourceBuilder? password = null; - var action = () => builder.AddGarnet(name); + Action action = overrideIndex switch + { + 0 => () => builder.AddGarnet(name, port), + 1 => () => builder.AddGarnet(name, port, password), + _ => throw new InvalidOperationException() + }; var exception = Assert.Throws(action); Assert.Equal(nameof(builder), exception.ParamName); } [Theory] - [InlineData(true)] - [InlineData(false)] - public void AddGarnetShouldThrowWhenNameIsNullOrEmpty(bool isNull) + [InlineData(0, false)] + [InlineData(0, true)] + [InlineData(1, false)] + [InlineData(1, true)] + public void AddGarnetShouldThrowWhenNameIsNullOrEmpty(int overrideIndex, bool isNull) { - var builder = TestDistributedApplicationBuilder.Create(); + using var builder = TestDistributedApplicationBuilder.Create(); var name = isNull ? null! : string.Empty; + int? port = null; + IResourceBuilder? password = null; - var action = () => builder.AddGarnet(name); + Action action = overrideIndex switch + { + 0 => () => builder.AddGarnet(name, port), + 1 => () => builder.AddGarnet(name, port, password), + _ => throw new InvalidOperationException() + }; var exception = isNull ? Assert.Throws(action) @@ -65,8 +83,8 @@ public void WithDataBindMountShouldThrowWhenBuilderIsNull() [InlineData(false)] public void WithDataBindMountShouldThrowWhenSourceIsNullOrEmpty(bool isNull) { - var builder = TestDistributedApplicationBuilder.Create() - .AddGarnet("garnet"); + using var testBuilder = TestDistributedApplicationBuilder.Create(); + var builder = testBuilder.AddGarnet("garnet"); var source = isNull ? null! : string.Empty; var action = () => builder.WithDataBindMount(source); diff --git a/tests/Aspire.Hosting.Redis.Tests/RedisPublicApiTests.cs b/tests/Aspire.Hosting.Redis.Tests/RedisPublicApiTests.cs index 4241bf31001..5e4f9183850 100644 --- a/tests/Aspire.Hosting.Redis.Tests/RedisPublicApiTests.cs +++ b/tests/Aspire.Hosting.Redis.Tests/RedisPublicApiTests.cs @@ -9,27 +9,45 @@ namespace Aspire.Hosting.Redis.Tests; public class RedisPublicApiTests { - [Fact] - public void AddRedisShouldThrowWhenBuilderIsNull() + [Theory] + [InlineData(0)] + [InlineData(1)] + public void AddRedisShouldThrowWhenBuilderIsNull(int overrideIndex) { IDistributedApplicationBuilder builder = null!; const string name = "Redis"; + int? port = null; + IResourceBuilder? password = null; - var action = () => builder.AddRedis(name); + Action action = overrideIndex switch + { + 0 => () => builder.AddRedis(name, port), + 1 => () => builder.AddRedis(name, port, password), + _ => throw new InvalidOperationException() + }; var exception = Assert.Throws(action); Assert.Equal(nameof(builder), exception.ParamName); } [Theory] - [InlineData(true)] - [InlineData(false)] - public void AddRedisShouldThrowWhenNameIsNullOrEmpty(bool isNull) + [InlineData(0, false)] + [InlineData(0, true)] + [InlineData(1, false)] + [InlineData(1, true)] + public void AddRedisShouldThrowWhenNameIsNullOrEmpty(int overrideIndex, bool isNull) { - var builder = TestDistributedApplicationBuilder.Create(); + using var builder = TestDistributedApplicationBuilder.Create(); var name = isNull ? null! : string.Empty; + int? port = null; + IResourceBuilder? password = null; - var action = () => builder.AddRedis(name); + Action action = overrideIndex switch + { + 0 => () => builder.AddRedis(name, port), + 1 => () => builder.AddRedis(name, port, password), + _ => throw new InvalidOperationException() + }; var exception = isNull ? Assert.Throws(action) @@ -111,7 +129,7 @@ public void WithDataBindMountShouldThrowWhenBuilderIsNull() [InlineData(false)] public void WithDataBindMountShouldThrowWhenNameIsNullOrEmpty(bool isNull) { - var builder = TestDistributedApplicationBuilder.Create(); + using var builder = TestDistributedApplicationBuilder.Create(); var redis = builder.AddRedis("Redis"); var source = isNull ? null! : string.Empty; @@ -162,7 +180,7 @@ public void RedisInsightWithDataBindMountShouldThrowWhenBuilderIsNull() [InlineData(false)] public void RedisInsightWithDataBindMountShouldThrowWhenNameIsNullOrEmpty(bool isNull) { - var builder = TestDistributedApplicationBuilder.Create(); + using var builder = TestDistributedApplicationBuilder.Create(); IResourceBuilder? redisInsightBuilder = null; var redis = builder.AddRedis("Redis").WithRedisInsight(resource => { redisInsightBuilder = resource; }); var source = isNull ? null! : string.Empty; @@ -206,14 +224,34 @@ public void CtorRedisInsightResourceShouldThrowWhenNameIsNullOrEmpty(bool isNull Assert.Equal(nameof(name), exception.ParamName); } + [Fact] + public void CtorRedisResourceShouldThrowWhenPasswordIsNull() + { + const string name = "redis"; + ParameterResource password = null!; + + var action = () => new RedisResource(name, password); + + var exception = Assert.Throws(action); + Assert.Equal(nameof(password), exception.ParamName); + } + [Theory] - [InlineData(true)] - [InlineData(false)] - public void CtorRedisResourceShouldThrowWhenNameIsNullOrEmpty(bool isNull) + [InlineData(0, false)] + [InlineData(0, true)] + [InlineData(1, false)] + [InlineData(1, true)] + public void CtorRedisResourceShouldThrowWhenNameIsNullOrEmpty(int overrideIndex, bool isNull) { var name = isNull ? null! : string.Empty; - - var action = () => new RedisResource(name); + var password = new ParameterResource("password", (_) => "password"); + + Action action = overrideIndex switch + { + 0 => () => new RedisResource(name), + 1 => () => new RedisResource(name, password), + _ => throw new InvalidOperationException() + }; var exception = isNull ? Assert.Throws(action) diff --git a/tests/Aspire.Hosting.Tests/ExecutableResourceTests.cs b/tests/Aspire.Hosting.Tests/ExecutableResourceTests.cs index 409e2f49349..e10d5bc8713 100644 --- a/tests/Aspire.Hosting.Tests/ExecutableResourceTests.cs +++ b/tests/Aspire.Hosting.Tests/ExecutableResourceTests.cs @@ -96,10 +96,7 @@ public void ExecutableResourceNullWorkingDirectory() [Fact] public void ExecutableResourceEmptyWorkingDirectory() - { - var er = new ExecutableResource("name", command: "cmd", workingDirectory: ""); - Assert.Empty(er.WorkingDirectory); - } + => Assert.Throws("workingDirectory", () => new ExecutableResource("name", command: "cmd", workingDirectory: "")); private sealed class TestResource(string name, string connectionString) : Resource(name), IResourceWithConnectionString {