Skip to content

Commit e4ef146

Browse files
authored
Adding public API test coverage for Aspire.Hosting.Python (#5110)
* CA1062#Aspire.Hosting.Python * Fix PR by feedback * Remove Assert.Multiple by feedback * builder replace to TestDistributedApplicationBuilder Fix PR by feedback * To expression body
1 parent 32ba296 commit e4ef146

File tree

3 files changed

+282
-2
lines changed

3 files changed

+282
-2
lines changed

src/Aspire.Hosting.Python/PythonProjectResource.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
using System.Runtime.CompilerServices;
46
using Aspire.Hosting.ApplicationModel;
57

68
namespace Aspire.Hosting.Python;
@@ -12,7 +14,8 @@ namespace Aspire.Hosting.Python;
1214
/// <param name="executablePath">The path to the executable used to run the python project.</param>
1315
/// <param name="projectDirectory">The path to the directory containing the python project.</param>
1416
public class PythonProjectResource(string name, string executablePath, string projectDirectory)
15-
: ExecutableResource(name, executablePath, projectDirectory), IResourceWithServiceDiscovery
17+
: ExecutableResource(ThrowIfNull(name), ThrowIfNull(executablePath), ThrowIfNull(projectDirectory)), IResourceWithServiceDiscovery
1618
{
17-
19+
private static string ThrowIfNull([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
20+
=> argument ?? throw new ArgumentNullException(paramName);
1821
}

src/Aspire.Hosting.Python/PythonProjectResourceBuilderExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public static class PythonProjectResourceBuilderExtensions
6060
public static IResourceBuilder<PythonProjectResource> AddPythonProject(
6161
this IDistributedApplicationBuilder builder, string name, string projectDirectory, string scriptPath, params string[] scriptArgs)
6262
{
63+
ArgumentNullException.ThrowIfNull(builder);
64+
6365
return builder.AddPythonProject(name, projectDirectory, scriptPath, ".venv", scriptArgs);
6466
}
6567

@@ -108,7 +110,12 @@ public static IResourceBuilder<PythonProjectResource> AddPythonProject(
108110
this IDistributedApplicationBuilder builder, string name, string projectDirectory, string scriptPath,
109111
string virtualEnvironmentPath, params string[] scriptArgs)
110112
{
113+
ArgumentNullException.ThrowIfNull(builder);
114+
ArgumentNullException.ThrowIfNull(name);
115+
ArgumentNullException.ThrowIfNull(projectDirectory);
116+
ArgumentNullException.ThrowIfNull(scriptPath);
111117
ArgumentNullException.ThrowIfNull(virtualEnvironmentPath);
118+
ArgumentNullException.ThrowIfNull(scriptArgs);
112119

113120
projectDirectory = PathNormalizer.NormalizePathForCurrentPlatform(Path.Combine(builder.AppHostDirectory, projectDirectory));
114121
var virtualEnvironment = new VirtualEnvironment(Path.IsPathRooted(virtualEnvironmentPath)
Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Aspire.Hosting.Utils;
5+
using Xunit;
6+
7+
namespace Aspire.Hosting.Python.Tests;
8+
9+
public class PythonPublicApiTests
10+
{
11+
[Fact]
12+
public void CtorPythonProjectResourceShouldThrowWhenNameIsNull()
13+
{
14+
string name = null!;
15+
const string executablePath = "/src/python";
16+
const string projectDirectory = "/data/python";
17+
18+
var action = () => new PythonProjectResource(name, executablePath, projectDirectory);
19+
20+
var exception = Assert.Throws<ArgumentNullException>(action);
21+
Assert.Equal(nameof(name), exception.ParamName);
22+
}
23+
24+
[Fact]
25+
public void CtorPythonProjectResourceShouldThrowWhenExecutablePathIsNull()
26+
{
27+
const string name = "Python";
28+
string executablePath = null!;
29+
const string projectDirectory = "/data/python";
30+
31+
var action = () => new PythonProjectResource(name, executablePath, projectDirectory);
32+
33+
var exception = Assert.Throws<ArgumentNullException>(action);
34+
Assert.Equal(nameof(executablePath), exception.ParamName);
35+
}
36+
37+
[Fact]
38+
public void CtorPythonProjectResourceShouldThrowWhenProjectDirectoryIsNull()
39+
{
40+
const string name = "Python";
41+
const string executablePath = "/src/python";
42+
string projectDirectory = null!;
43+
44+
var action = () => new PythonProjectResource(name, executablePath, projectDirectory);
45+
46+
var exception = Assert.Throws<ArgumentNullException>(action);
47+
Assert.Equal(nameof(projectDirectory), exception.ParamName);
48+
}
49+
50+
[Fact]
51+
public void AddPythonProjectShouldThrowWhenBuilderIsNull()
52+
{
53+
IDistributedApplicationBuilder builder = null!;
54+
const string name = "Python";
55+
const string projectDirectory = "/src/python";
56+
const string scriptPath = "scripts";
57+
string[] scriptArgs = ["--traces"];
58+
59+
var action = () => builder.AddPythonProject(
60+
name,
61+
projectDirectory,
62+
scriptPath,
63+
scriptArgs);
64+
65+
var exception = Assert.Throws<ArgumentNullException>(action);
66+
Assert.Equal(nameof(builder), exception.ParamName);
67+
}
68+
69+
[Fact]
70+
public void AddPythonProjectShouldThrowWhenNameIsNull()
71+
{
72+
var builder = TestDistributedApplicationBuilder.Create();
73+
string name = null!;
74+
const string projectDirectory = "/src/python";
75+
const string scriptPath = "scripts";
76+
string[] scriptArgs = ["--traces"];
77+
78+
var action = () => builder.AddPythonProject(
79+
name,
80+
projectDirectory,
81+
scriptPath,
82+
scriptArgs);
83+
84+
var exception = Assert.Throws<ArgumentNullException>(action);
85+
Assert.Equal(nameof(name), exception.ParamName);
86+
}
87+
88+
[Fact]
89+
public void AddPythonProjectShouldThrowWhenProjectDirectoryIsNull()
90+
{
91+
var builder = TestDistributedApplicationBuilder.Create();
92+
const string name = "Python";
93+
string projectDirectory = null!;
94+
const string scriptPath = "scripts";
95+
string[] scriptArgs = ["--traces"];
96+
97+
var action = () => builder.AddPythonProject(
98+
name,
99+
projectDirectory,
100+
scriptPath,
101+
scriptArgs);
102+
103+
var exception = Assert.Throws<ArgumentNullException>(action);
104+
Assert.Equal(nameof(projectDirectory), exception.ParamName);
105+
}
106+
107+
[Fact]
108+
public void AddPythonProjectShouldThrowWhenScriptPathIsNull()
109+
{
110+
var builder = TestDistributedApplicationBuilder.Create();
111+
const string name = "Python";
112+
const string projectDirectory = "/src/python";
113+
string scriptPath = null!;
114+
string[] scriptArgs = ["--traces"];
115+
116+
var action = () => builder.AddPythonProject(
117+
name,
118+
projectDirectory,
119+
scriptPath,
120+
scriptArgs);
121+
122+
var exception = Assert.Throws<ArgumentNullException>(action);
123+
Assert.Equal(nameof(scriptPath), exception.ParamName);
124+
}
125+
126+
[Fact]
127+
public void AddPythonProjectShouldThrowWhenScriptArgsIsNull()
128+
{
129+
var builder = TestDistributedApplicationBuilder.Create();
130+
const string name = "Python";
131+
const string projectDirectory = "/src/python";
132+
const string scriptPath = "scripts";
133+
string[] scriptArgs = null!;
134+
135+
var action = () => builder.AddPythonProject(
136+
name,
137+
projectDirectory,
138+
scriptPath,
139+
scriptArgs);
140+
141+
var exception = Assert.Throws<ArgumentNullException>(action);
142+
Assert.Equal(nameof(scriptArgs), exception.ParamName);
143+
}
144+
145+
[Fact]
146+
public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenBuilderIsNull()
147+
{
148+
IDistributedApplicationBuilder builder = null!;
149+
const string name = "Python";
150+
const string projectDirectory = "/src/python";
151+
const string scriptPath = "scripts";
152+
var virtualEnvironmentPath = ".venv";
153+
string[] scriptArgs = ["--traces"]; ;
154+
155+
var action = () => builder.AddPythonProject(
156+
name,
157+
projectDirectory,
158+
scriptPath,
159+
virtualEnvironmentPath,
160+
scriptArgs);
161+
162+
var exception = Assert.Throws<ArgumentNullException>(action);
163+
Assert.Equal(nameof(builder), exception.ParamName);
164+
}
165+
166+
[Fact]
167+
public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenNameIsNull()
168+
{
169+
var builder = TestDistributedApplicationBuilder.Create();
170+
string name = null!;
171+
const string projectDirectory = "/src/python";
172+
const string scriptPath = "scripts";
173+
const string virtualEnvironmentPath = ".venv";
174+
string[] scriptArgs = ["--traces"]; ;
175+
176+
var action = () => builder.AddPythonProject(
177+
name,
178+
projectDirectory,
179+
scriptPath,
180+
virtualEnvironmentPath,
181+
scriptArgs);
182+
183+
var exception = Assert.Throws<ArgumentNullException>(action);
184+
Assert.Equal(nameof(name), exception.ParamName);
185+
}
186+
187+
[Fact]
188+
public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenProjectDirectoryIsNull()
189+
{
190+
var builder = TestDistributedApplicationBuilder.Create();
191+
const string name = "Python";
192+
string projectDirectory = null!;
193+
const string scriptPath = "scripts";
194+
const string virtualEnvironmentPath = ".venv";
195+
string[] scriptArgs = ["--traces"]; ;
196+
197+
var action = () => builder.AddPythonProject(
198+
name,
199+
projectDirectory,
200+
scriptPath,
201+
virtualEnvironmentPath,
202+
scriptArgs);
203+
204+
var exception = Assert.Throws<ArgumentNullException>(action);
205+
Assert.Equal(nameof(projectDirectory), exception.ParamName);
206+
}
207+
208+
[Fact]
209+
public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenScriptPathIsNull()
210+
{
211+
var builder = TestDistributedApplicationBuilder.Create();
212+
const string name = "Python";
213+
const string projectDirectory = "/src/python";
214+
string scriptPath = null!;
215+
const string virtualEnvironmentPath = ".venv";
216+
string[] scriptArgs = ["--traces"]; ;
217+
218+
var action = () => builder.AddPythonProject(
219+
name,
220+
projectDirectory,
221+
scriptPath,
222+
virtualEnvironmentPath,
223+
scriptArgs);
224+
225+
var exception = Assert.Throws<ArgumentNullException>(action);
226+
Assert.Equal(nameof(scriptPath), exception.ParamName);
227+
}
228+
229+
[Fact]
230+
public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenVirtualEnvironmentPathIsNull()
231+
{
232+
var builder = TestDistributedApplicationBuilder.Create();
233+
const string name = "Python";
234+
const string projectDirectory = "/src/python";
235+
const string scriptPath = "scripts";
236+
string virtualEnvironmentPath = null!;
237+
string[] scriptArgs = ["--traces"]; ;
238+
239+
var action = () => builder.AddPythonProject(
240+
name,
241+
projectDirectory,
242+
scriptPath,
243+
virtualEnvironmentPath,
244+
scriptArgs);
245+
246+
var exception = Assert.Throws<ArgumentNullException>(action);
247+
Assert.Equal(nameof(virtualEnvironmentPath), exception.ParamName);
248+
}
249+
250+
[Fact]
251+
public void AddPythonProjectWithVirtualEnvironmentPathShouldThrowWhenScriptArgsIsNull()
252+
{
253+
var builder = TestDistributedApplicationBuilder.Create();
254+
const string name = "Python";
255+
const string projectDirectory = "/src/python";
256+
const string scriptPath = "scripts";
257+
const string virtualEnvironmentPath = ".venv";
258+
string[] scriptArgs = null!;
259+
260+
var action = () => builder.AddPythonProject(
261+
name,
262+
projectDirectory,
263+
scriptPath,
264+
virtualEnvironmentPath,
265+
scriptArgs);
266+
267+
var exception = Assert.Throws<ArgumentNullException>(action);
268+
Assert.Equal(nameof(scriptArgs), exception.ParamName);
269+
}
270+
}

0 commit comments

Comments
 (0)