diff --git a/src/Aspire.Hosting/ApplicationModel/ExecutableResource.cs b/src/Aspire.Hosting/ApplicationModel/ExecutableResource.cs index 5a3a7297553..8d7748e14e1 100644 --- a/src/Aspire.Hosting/ApplicationModel/ExecutableResource.cs +++ b/src/Aspire.Hosting/ApplicationModel/ExecutableResource.cs @@ -11,7 +11,7 @@ namespace Aspire.Hosting.ApplicationModel; /// /// The name of the resource. /// The command to execute. -/// The working directory of the executable. +/// The working directory of the executable. Can be empty. public class ExecutableResource(string name, string command, string workingDirectory) : Resource(name), IResourceWithEnvironment, IResourceWithArgs, IResourceWithEndpoints, IResourceWithWaitSupport { @@ -23,7 +23,7 @@ public class ExecutableResource(string name, string command, string workingDirec /// /// Gets the working directory for the executable resource. /// - public string WorkingDirectory { get; } = ThrowIfNullOrEmpty(workingDirectory); + public string WorkingDirectory { get; } = workingDirectory ?? throw new ArgumentNullException(nameof(workingDirectory)); private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null) { diff --git a/tests/Aspire.Hosting.NodeJs.Tests/NodeJsPublicApiTests.cs b/tests/Aspire.Hosting.NodeJs.Tests/NodeJsPublicApiTests.cs index 1e2dc132f44..91acc3afefe 100644 --- a/tests/Aspire.Hosting.NodeJs.Tests/NodeJsPublicApiTests.cs +++ b/tests/Aspire.Hosting.NodeJs.Tests/NodeJsPublicApiTests.cs @@ -42,20 +42,16 @@ public void CtorNodeAppResourceShouldThrowWhenCommandIsNullOrEmpty(bool isNull) Assert.Equal(nameof(command), exception.ParamName); } - [Theory] - [InlineData(true)] - [InlineData(false)] - public void CtorNodeAppResourceShouldThrowWhenWorkingDirectoryIsNullOrEmpty(bool isNull) + [Fact] + public void CtorNodeAppResourceShouldThrowWhenWorkingDirectoryIsNull() { const string name = "NodeApp"; const string command = "npm"; - var workingDirectory = isNull ? null! : string.Empty; + string workingDirectory = null!; var action = () => new NodeAppResource(name, command, workingDirectory); - var exception = isNull - ? Assert.Throws(action) - : Assert.Throws(action); + var exception = Assert.Throws(action); Assert.Equal(nameof(workingDirectory), exception.ParamName); } @@ -136,20 +132,16 @@ public void AddNpmAppShouldThrowWhenNameIsNullOrEmpty(bool isNull) Assert.Equal(nameof(name), exception.ParamName); } - [Theory] - [InlineData(true)] - [InlineData(false)] - public void AddNpmAppShouldThrowWhenWorkingDirectoryIsNullOrEmpty(bool isNull) + [Fact] + public void AddNpmAppShouldThrowWhenWorkingDirectoryIsNull() { var builder = TestDistributedApplicationBuilder.Create(); const string name = "NpmApp"; - var workingDirectory = isNull ? null! : string.Empty; + string workingDirectory = null!; var action = () => builder.AddNpmApp(name, workingDirectory); - var exception = isNull - ? Assert.Throws(action) - : Assert.Throws(action); + var exception = Assert.Throws(action); Assert.Equal(nameof(workingDirectory), exception.ParamName); } diff --git a/tests/Aspire.Hosting.Python.Tests/PythonPublicApiTests.cs b/tests/Aspire.Hosting.Python.Tests/PythonPublicApiTests.cs index a3f36495212..97af7245035 100644 --- a/tests/Aspire.Hosting.Python.Tests/PythonPublicApiTests.cs +++ b/tests/Aspire.Hosting.Python.Tests/PythonPublicApiTests.cs @@ -42,20 +42,15 @@ public void CtorPythonAppResourceShouldThrowWhenExecutablePathIsNullOrEmpty(bool Assert.Equal("command", exception.ParamName); } - [Theory] - [InlineData(true)] - [InlineData(false)] - public void CtorPythonAppResourceShouldThrowWhenAppDirectoryIsNullOrEmpty(bool isNull) + [Fact] + public void CtorPythonAppResourceShouldThrowWhenAppDirectoryIsNull() { const string name = "Python"; const string executablePath = "/src/python"; - var appDirectory = isNull ? null! : string.Empty; - var action = () => new PythonAppResource(name, executablePath, appDirectory); + var action = () => new PythonAppResource(name, executablePath, appDirectory: null!); - var exception = isNull - ? Assert.Throws(action) - : Assert.Throws(action); + var exception = Assert.Throws(action); Assert.Equal("workingDirectory", exception.ParamName); } @@ -379,21 +374,16 @@ public void CtorPythonProjectResourceShouldThrowWhenExecutablePathIsNullOrEmpty( Assert.Equal("command", exception.ParamName); } - [Theory] - [InlineData(true)] - [InlineData(false)] + [Fact] [Obsolete("PythonProjectResource is deprecated. Please use PythonAppResource instead.")] - public void CtorPythonProjectResourceShouldThrowWhenAppDirectoryIsNullOrEmpty(bool isNull) + public void CtorPythonProjectResourceShouldThrowWhenAppDirectoryIsNull() { const string name = "Python"; const string executablePath = "/src/python"; - var projectDirectory = isNull ? null! : string.Empty; - var action = () => new PythonProjectResource(name, executablePath, projectDirectory); + var action = () => new PythonProjectResource(name, executablePath, projectDirectory: null!); - var exception = isNull - ? Assert.Throws(action) - : Assert.Throws(action); + var exception = Assert.Throws(action); Assert.Equal("workingDirectory", exception.ParamName); } diff --git a/tests/Aspire.Hosting.Tests/ExecutableResourceTests.cs b/tests/Aspire.Hosting.Tests/ExecutableResourceTests.cs index 6cfb9936feb..409e2f49349 100644 --- a/tests/Aspire.Hosting.Tests/ExecutableResourceTests.cs +++ b/tests/Aspire.Hosting.Tests/ExecutableResourceTests.cs @@ -82,6 +82,25 @@ 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() + => Assert.Throws("command", () => new ExecutableResource("name", command: "", workingDirectory: ".")); + + [Fact] + public void ExecutableResourceNullWorkingDirectory() + => Assert.Throws("workingDirectory", () => new ExecutableResource("name", command: "cmd", workingDirectory: null!)); + + [Fact] + public void ExecutableResourceEmptyWorkingDirectory() + { + var er = new ExecutableResource("name", command: "cmd", workingDirectory: ""); + Assert.Empty(er.WorkingDirectory); + } + private sealed class TestResource(string name, string connectionString) : Resource(name), IResourceWithConnectionString { public ReferenceExpression ConnectionStringExpression =>