From bcf2ec4ccaa270ede088445be69851256e4f6295 Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Tue, 15 Apr 2025 15:03:30 -0600 Subject: [PATCH 1/3] Fix AE in ExecutableResource --- .../ApplicationModel/ExecutableResource.cs | 13 ++--------- .../ExecutableResourceTests.cs | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/Aspire.Hosting/ApplicationModel/ExecutableResource.cs b/src/Aspire.Hosting/ApplicationModel/ExecutableResource.cs index 5a3a7297553..165f874da05 100644 --- a/src/Aspire.Hosting/ApplicationModel/ExecutableResource.cs +++ b/src/Aspire.Hosting/ApplicationModel/ExecutableResource.cs @@ -1,9 +1,6 @@ // 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.Hosting.ApplicationModel; /// @@ -18,16 +15,10 @@ public class ExecutableResource(string name, string command, string workingDirec /// /// Gets the command associated with this executable resource. /// - public string Command { get; } = ThrowIfNullOrEmpty(command); + public string Command { get; } = command ?? throw new ArgumentNullException(nameof(command)); /// /// Gets the working directory for the executable resource. /// - public string WorkingDirectory { get; } = ThrowIfNullOrEmpty(workingDirectory); - - private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) - { - ArgumentException.ThrowIfNullOrEmpty(argument, paramName); - return argument; - } + public string WorkingDirectory { get; } = workingDirectory ?? throw new ArgumentNullException(nameof(workingDirectory)); } diff --git a/tests/Aspire.Hosting.Tests/ExecutableResourceTests.cs b/tests/Aspire.Hosting.Tests/ExecutableResourceTests.cs index 6cfb9936feb..265d062c962 100644 --- a/tests/Aspire.Hosting.Tests/ExecutableResourceTests.cs +++ b/tests/Aspire.Hosting.Tests/ExecutableResourceTests.cs @@ -82,6 +82,28 @@ public async Task AddExecutableWithArgs() Assert.Equal(expectedManifest, manifest.ToString()); } + [Fact] + public void ExecutableResourceNullCommand() + => Assert.Throws("command", () => new ExecutableResource("name", command: null!, workingDirectory: ".")); + + [Fact] + public void ExecutableResourceEmptyCommand() + { + var er = new ExecutableResource("name", command: "", workingDirectory: "."); + Assert.Empty(er.Command); + } + + [Fact] + public void ExecutableResourceNullWorkingDirectory() + => Assert.Throws("workingDirectory", () => new ExecutableResource("name", command: "cmd", workingDirectory: null!)); + + [Fact] + public void ExecutableResourceEmptyWorkingDirectory() + { + var er = new ExecutableResource("name", command: "", workingDirectory: ""); + Assert.Empty(er.Command); + } + private sealed class TestResource(string name, string connectionString) : Resource(name), IResourceWithConnectionString { public ReferenceExpression ConnectionStringExpression => From 40d8bfb29cf27bc9a13e33e4675bff2d80e27f9f Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Tue, 15 Apr 2025 15:04:53 -0600 Subject: [PATCH 2/3] doc --- src/Aspire.Hosting/ApplicationModel/ExecutableResource.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Aspire.Hosting/ApplicationModel/ExecutableResource.cs b/src/Aspire.Hosting/ApplicationModel/ExecutableResource.cs index 165f874da05..13bde827923 100644 --- a/src/Aspire.Hosting/ApplicationModel/ExecutableResource.cs +++ b/src/Aspire.Hosting/ApplicationModel/ExecutableResource.cs @@ -7,8 +7,8 @@ namespace Aspire.Hosting.ApplicationModel; /// A resource that represents a specified executable process. /// /// The name of the resource. -/// The command to execute. -/// The working directory of the executable. +/// The command to execute. Can be empty. +/// The working directory of the executable. Can be empty. public class ExecutableResource(string name, string command, string workingDirectory) : Resource(name), IResourceWithEnvironment, IResourceWithArgs, IResourceWithEndpoints, IResourceWithWaitSupport { From 90ae1389598241f1d2ee63f9871594ac9d4f0622 Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Tue, 15 Apr 2025 15:13:54 -0600 Subject: [PATCH 3/3] not command --- .../ApplicationModel/ExecutableResource.cs | 13 +++++++++++-- .../Aspire.Hosting.Tests/ExecutableResourceTests.cs | 9 +++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Aspire.Hosting/ApplicationModel/ExecutableResource.cs b/src/Aspire.Hosting/ApplicationModel/ExecutableResource.cs index 13bde827923..8d7748e14e1 100644 --- a/src/Aspire.Hosting/ApplicationModel/ExecutableResource.cs +++ b/src/Aspire.Hosting/ApplicationModel/ExecutableResource.cs @@ -1,13 +1,16 @@ // 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.Hosting.ApplicationModel; /// /// A resource that represents a specified executable process. /// /// The name of the resource. -/// The command to execute. Can be empty. +/// The command to execute. /// The working directory of the executable. Can be empty. public class ExecutableResource(string name, string command, string workingDirectory) : Resource(name), IResourceWithEnvironment, IResourceWithArgs, IResourceWithEndpoints, IResourceWithWaitSupport @@ -15,10 +18,16 @@ public class ExecutableResource(string name, string command, string workingDirec /// /// Gets the command associated with this executable resource. /// - public string Command { get; } = command ?? throw new ArgumentNullException(nameof(command)); + public string Command { get; } = ThrowIfNullOrEmpty(command); /// /// 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; + } } diff --git a/tests/Aspire.Hosting.Tests/ExecutableResourceTests.cs b/tests/Aspire.Hosting.Tests/ExecutableResourceTests.cs index 265d062c962..409e2f49349 100644 --- a/tests/Aspire.Hosting.Tests/ExecutableResourceTests.cs +++ b/tests/Aspire.Hosting.Tests/ExecutableResourceTests.cs @@ -88,10 +88,7 @@ public void ExecutableResourceNullCommand() [Fact] public void ExecutableResourceEmptyCommand() - { - var er = new ExecutableResource("name", command: "", workingDirectory: "."); - Assert.Empty(er.Command); - } + => Assert.Throws("command", () => new ExecutableResource("name", command: "", workingDirectory: ".")); [Fact] public void ExecutableResourceNullWorkingDirectory() @@ -100,8 +97,8 @@ public void ExecutableResourceNullWorkingDirectory() [Fact] public void ExecutableResourceEmptyWorkingDirectory() { - var er = new ExecutableResource("name", command: "", workingDirectory: ""); - Assert.Empty(er.Command); + var er = new ExecutableResource("name", command: "cmd", workingDirectory: ""); + Assert.Empty(er.WorkingDirectory); } private sealed class TestResource(string name, string connectionString) : Resource(name), IResourceWithConnectionString