Skip to content

Commit 4b42283

Browse files
authored
Merge pull request #8864 from dotnet/backport/pr-8797-to-release/9.2
[release/9.2] Fix AE in ExecutableResource
2 parents 3bb6930 + ef6f857 commit 4b42283

File tree

4 files changed

+37
-36
lines changed

4 files changed

+37
-36
lines changed

src/Aspire.Hosting/ApplicationModel/ExecutableResource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Aspire.Hosting.ApplicationModel;
1111
/// </summary>
1212
/// <param name="name">The name of the resource.</param>
1313
/// <param name="command">The command to execute.</param>
14-
/// <param name="workingDirectory">The working directory of the executable.</param>
14+
/// <param name="workingDirectory">The working directory of the executable. Can be empty.</param>
1515
public class ExecutableResource(string name, string command, string workingDirectory)
1616
: Resource(name), IResourceWithEnvironment, IResourceWithArgs, IResourceWithEndpoints, IResourceWithWaitSupport
1717
{
@@ -23,7 +23,7 @@ public class ExecutableResource(string name, string command, string workingDirec
2323
/// <summary>
2424
/// Gets the working directory for the executable resource.
2525
/// </summary>
26-
public string WorkingDirectory { get; } = ThrowIfNullOrEmpty(workingDirectory);
26+
public string WorkingDirectory { get; } = workingDirectory ?? throw new ArgumentNullException(nameof(workingDirectory));
2727

2828
private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
2929
{

tests/Aspire.Hosting.NodeJs.Tests/NodeJsPublicApiTests.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,16 @@ public void CtorNodeAppResourceShouldThrowWhenCommandIsNullOrEmpty(bool isNull)
4242
Assert.Equal(nameof(command), exception.ParamName);
4343
}
4444

45-
[Theory]
46-
[InlineData(true)]
47-
[InlineData(false)]
48-
public void CtorNodeAppResourceShouldThrowWhenWorkingDirectoryIsNullOrEmpty(bool isNull)
45+
[Fact]
46+
public void CtorNodeAppResourceShouldThrowWhenWorkingDirectoryIsNull()
4947
{
5048
const string name = "NodeApp";
5149
const string command = "npm";
52-
var workingDirectory = isNull ? null! : string.Empty;
50+
string workingDirectory = null!;
5351

5452
var action = () => new NodeAppResource(name, command, workingDirectory);
5553

56-
var exception = isNull
57-
? Assert.Throws<ArgumentNullException>(action)
58-
: Assert.Throws<ArgumentException>(action);
54+
var exception = Assert.Throws<ArgumentNullException>(action);
5955
Assert.Equal(nameof(workingDirectory), exception.ParamName);
6056
}
6157

@@ -136,20 +132,16 @@ public void AddNpmAppShouldThrowWhenNameIsNullOrEmpty(bool isNull)
136132
Assert.Equal(nameof(name), exception.ParamName);
137133
}
138134

139-
[Theory]
140-
[InlineData(true)]
141-
[InlineData(false)]
142-
public void AddNpmAppShouldThrowWhenWorkingDirectoryIsNullOrEmpty(bool isNull)
135+
[Fact]
136+
public void AddNpmAppShouldThrowWhenWorkingDirectoryIsNull()
143137
{
144138
var builder = TestDistributedApplicationBuilder.Create();
145139
const string name = "NpmApp";
146-
var workingDirectory = isNull ? null! : string.Empty;
140+
string workingDirectory = null!;
147141

148142
var action = () => builder.AddNpmApp(name, workingDirectory);
149143

150-
var exception = isNull
151-
? Assert.Throws<ArgumentNullException>(action)
152-
: Assert.Throws<ArgumentException>(action);
144+
var exception = Assert.Throws<ArgumentNullException>(action);
153145
Assert.Equal(nameof(workingDirectory), exception.ParamName);
154146
}
155147

tests/Aspire.Hosting.Python.Tests/PythonPublicApiTests.cs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,15 @@ public void CtorPythonAppResourceShouldThrowWhenExecutablePathIsNullOrEmpty(bool
4242
Assert.Equal("command", exception.ParamName);
4343
}
4444

45-
[Theory]
46-
[InlineData(true)]
47-
[InlineData(false)]
48-
public void CtorPythonAppResourceShouldThrowWhenAppDirectoryIsNullOrEmpty(bool isNull)
45+
[Fact]
46+
public void CtorPythonAppResourceShouldThrowWhenAppDirectoryIsNull()
4947
{
5048
const string name = "Python";
5149
const string executablePath = "/src/python";
52-
var appDirectory = isNull ? null! : string.Empty;
5350

54-
var action = () => new PythonAppResource(name, executablePath, appDirectory);
51+
var action = () => new PythonAppResource(name, executablePath, appDirectory: null!);
5552

56-
var exception = isNull
57-
? Assert.Throws<ArgumentNullException>(action)
58-
: Assert.Throws<ArgumentException>(action);
53+
var exception = Assert.Throws<ArgumentNullException>(action);
5954
Assert.Equal("workingDirectory", exception.ParamName);
6055
}
6156

@@ -379,21 +374,16 @@ public void CtorPythonProjectResourceShouldThrowWhenExecutablePathIsNullOrEmpty(
379374
Assert.Equal("command", exception.ParamName);
380375
}
381376

382-
[Theory]
383-
[InlineData(true)]
384-
[InlineData(false)]
377+
[Fact]
385378
[Obsolete("PythonProjectResource is deprecated. Please use PythonAppResource instead.")]
386-
public void CtorPythonProjectResourceShouldThrowWhenAppDirectoryIsNullOrEmpty(bool isNull)
379+
public void CtorPythonProjectResourceShouldThrowWhenAppDirectoryIsNull()
387380
{
388381
const string name = "Python";
389382
const string executablePath = "/src/python";
390-
var projectDirectory = isNull ? null! : string.Empty;
391383

392-
var action = () => new PythonProjectResource(name, executablePath, projectDirectory);
384+
var action = () => new PythonProjectResource(name, executablePath, projectDirectory: null!);
393385

394-
var exception = isNull
395-
? Assert.Throws<ArgumentNullException>(action)
396-
: Assert.Throws<ArgumentException>(action);
386+
var exception = Assert.Throws<ArgumentNullException>(action);
397387
Assert.Equal("workingDirectory", exception.ParamName);
398388
}
399389

tests/Aspire.Hosting.Tests/ExecutableResourceTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,25 @@ public async Task AddExecutableWithArgs()
8282
Assert.Equal(expectedManifest, manifest.ToString());
8383
}
8484

85+
[Fact]
86+
public void ExecutableResourceNullCommand()
87+
=> Assert.Throws<ArgumentNullException>("command", () => new ExecutableResource("name", command: null!, workingDirectory: "."));
88+
89+
[Fact]
90+
public void ExecutableResourceEmptyCommand()
91+
=> Assert.Throws<ArgumentException>("command", () => new ExecutableResource("name", command: "", workingDirectory: "."));
92+
93+
[Fact]
94+
public void ExecutableResourceNullWorkingDirectory()
95+
=> Assert.Throws<ArgumentNullException>("workingDirectory", () => new ExecutableResource("name", command: "cmd", workingDirectory: null!));
96+
97+
[Fact]
98+
public void ExecutableResourceEmptyWorkingDirectory()
99+
{
100+
var er = new ExecutableResource("name", command: "cmd", workingDirectory: "");
101+
Assert.Empty(er.WorkingDirectory);
102+
}
103+
85104
private sealed class TestResource(string name, string connectionString) : Resource(name), IResourceWithConnectionString
86105
{
87106
public ReferenceExpression ConnectionStringExpression =>

0 commit comments

Comments
 (0)