Skip to content

Commit cc32ebd

Browse files
authored
Extract Aspire.Hosting.Python.Tests project (dotnet#4915)
Contributes to dotnet#4294
1 parent 6de43ed commit cc32ebd

File tree

5 files changed

+51
-19
lines changed

5 files changed

+51
-19
lines changed

Aspire.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.0.31903.59
@@ -514,6 +514,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.Hosting.Qdrant.Tests
514514
EndProject
515515
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.Hosting.Kafka.Tests", "tests\Aspire.Hosting.Kafka.Tests\Aspire.Hosting.Kafka.Tests.csproj", "{0A83AA67-221E-44B4-9BA9-DC64DC17949E}"
516516
EndProject
517+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.Hosting.Python.Tests", "tests\Aspire.Hosting.Python.Tests\Aspire.Hosting.Python.Tests.csproj", "{72F5A6F3-3516-402B-8F8D-50A7BC2E4BD4}"
518+
EndProject
517519
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aspire.Hosting.Valkey.Tests", "tests\Aspire.Hosting.Valkey.Tests\Aspire.Hosting.Valkey.Tests.csproj", "{1C16DC2D-3B79-4081-AC1E-F3F965C61216}"
518520
EndProject
519521
Global
@@ -1334,6 +1336,10 @@ Global
13341336
{1BC02557-B78B-48CE-9D3C-488A6B7672F4}.Debug|Any CPU.Build.0 = Debug|Any CPU
13351337
{1BC02557-B78B-48CE-9D3C-488A6B7672F4}.Release|Any CPU.ActiveCfg = Release|Any CPU
13361338
{1BC02557-B78B-48CE-9D3C-488A6B7672F4}.Release|Any CPU.Build.0 = Release|Any CPU
1339+
{72F5A6F3-3516-402B-8F8D-50A7BC2E4BD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1340+
{72F5A6F3-3516-402B-8F8D-50A7BC2E4BD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
1341+
{72F5A6F3-3516-402B-8F8D-50A7BC2E4BD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
1342+
{72F5A6F3-3516-402B-8F8D-50A7BC2E4BD4}.Release|Any CPU.Build.0 = Release|Any CPU
13371343
{7425E5B2-BC47-4521-AC40-B8CECA329E08}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
13381344
{7425E5B2-BC47-4521-AC40-B8CECA329E08}.Debug|Any CPU.Build.0 = Debug|Any CPU
13391345
{7425E5B2-BC47-4521-AC40-B8CECA329E08}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -1593,6 +1599,7 @@ Global
15931599
{C424395C-1235-41A4-BF55-07880A04368C} = {4981B3A5-4AFD-4191-BF7D-8692D9783D60}
15941600
{830A89EC-4029-4753-B25A-068BAE37DEC7} = {4981B3A5-4AFD-4191-BF7D-8692D9783D60}
15951601
{1BC02557-B78B-48CE-9D3C-488A6B7672F4} = {830A89EC-4029-4753-B25A-068BAE37DEC7}
1602+
{72F5A6F3-3516-402B-8F8D-50A7BC2E4BD4} = {830A89EC-4029-4753-B25A-068BAE37DEC7}
15961603
{7425E5B2-BC47-4521-AC40-B8CECA329E08} = {830A89EC-4029-4753-B25A-068BAE37DEC7}
15971604
{8E2AA85E-C351-47B4-AF91-58557FAD5840} = {830A89EC-4029-4753-B25A-068BAE37DEC7}
15981605
{0A83AA67-221E-44B4-9BA9-DC64DC17949E} = {830A89EC-4029-4753-B25A-068BAE37DEC7}

tests/Aspire.Hosting.Tests/Python/AddPythonProjectTests.cs renamed to tests/Aspire.Hosting.Python.Tests/AddPythonProjectTests.cs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
using System.Diagnostics;
99
using Aspire.Components.Common.Tests;
1010
using Xunit.Abstractions;
11+
using Aspire.Hosting.ApplicationModel;
12+
using System.Runtime.CompilerServices;
13+
using Microsoft.Extensions.Logging;
1114

12-
namespace Aspire.Hosting.Tests.Python;
15+
namespace Aspire.Hosting.Python.Tests;
1316

1417
public class AddPythonProjectTests(ITestOutputHelper outputHelper)
1518
{
@@ -21,9 +24,9 @@ public async Task AddPythonProjectProducesDockerfileResourceInManifest()
2124

2225
var manifestPath = Path.Combine(projectDirectory, "aspire-manifest.json");
2326

24-
using var builder = TestDistributedApplicationBuilder.Create(options =>
27+
using var builder = CreateTestDistributedApplicationBuilder(options =>
2528
{
26-
options.ProjectDirectory = Path.GetFullPath(projectDirectory);
29+
GetProjectDirectoryRef(options) = Path.GetFullPath(projectDirectory);
2730
options.Args = ["--publisher", "manifest", "--output-path", manifestPath];
2831
});
2932

@@ -51,9 +54,9 @@ public async Task AddInstrumentedPythonProjectProducesDockerfileResourceInManife
5154

5255
var manifestPath = Path.Combine(projectDirectory, "aspire-manifest.json");
5356

54-
using var builder = TestDistributedApplicationBuilder.Create(options =>
57+
using var builder = CreateTestDistributedApplicationBuilder(options =>
5558
{
56-
options.ProjectDirectory = Path.GetFullPath(projectDirectory);
59+
GetProjectDirectoryRef(options) = Path.GetFullPath(projectDirectory);
5760
options.Args = ["--publisher", "manifest", "--output-path", manifestPath];
5861
});
5962

@@ -76,13 +79,16 @@ public async Task AddInstrumentedPythonProjectProducesDockerfileResourceInManife
7679
Directory.Delete(projectDirectory, true);
7780
}
7881

82+
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_projectDirectory")]
83+
static extern ref string? GetProjectDirectoryRef(DistributedApplicationOptions? @this);
84+
7985
[Fact]
8086
[RequiresTools(["python"])]
8187
public async Task PythonResourceFinishesSuccessfully()
8288
{
8389
var (projectDirectory, _, scriptName) = CreateTempPythonProject(outputHelper);
8490

85-
using var builder = TestDistributedApplicationBuilder.Create();
91+
using var builder = CreateTestDistributedApplicationBuilder();
8692
builder.AddPythonProject("pyproj", projectDirectory, scriptName);
8793

8894
using var app = builder.Build();
@@ -103,10 +109,10 @@ public async Task PythonResourceFinishesSuccessfully()
103109
[RequiresTools(["python"])]
104110
public async Task AddPythonProject_SetsResourcePropertiesCorrectly()
105111
{
106-
using var builder = TestDistributedApplicationBuilder.Create();
112+
using var builder = CreateTestDistributedApplicationBuilder();
107113

108114
var (projectDirectory, pythonExecutable, scriptName) = CreateTempPythonProject(outputHelper);
109-
115+
110116
builder.AddPythonProject("pythonProject", projectDirectory, scriptName);
111117

112118
var app = builder.Build();
@@ -118,7 +124,7 @@ public async Task AddPythonProject_SetsResourcePropertiesCorrectly()
118124
Assert.Equal("pythonProject", pythonProjectResource.Name);
119125
Assert.Equal(projectDirectory, pythonProjectResource.WorkingDirectory);
120126

121-
if(OperatingSystem.IsWindows())
127+
if (OperatingSystem.IsWindows())
122128
{
123129
Assert.Equal(Path.Join(projectDirectory, ".venv", "Scripts", "python.exe"), pythonProjectResource.Command);
124130
}
@@ -139,10 +145,10 @@ public async Task AddPythonProject_SetsResourcePropertiesCorrectly()
139145
[RequiresTools(["python"])]
140146
public async Task AddPythonProjectWithInstrumentation_SwitchesExecutableToInstrumentationExecutable()
141147
{
142-
using var builder = TestDistributedApplicationBuilder.Create();
148+
using var builder = CreateTestDistributedApplicationBuilder();
143149

144150
var (projectDirectory, pythonExecutable, scriptName) = CreateTempPythonProject(outputHelper, instrument: true);
145-
151+
146152
builder.AddPythonProject("pythonProject", projectDirectory, scriptName, virtualEnvironmentPath: ".venv");
147153

148154
var app = builder.Build();
@@ -169,7 +175,7 @@ public async Task AddPythonProjectWithInstrumentation_SwitchesExecutableToInstru
169175
Assert.Equal("otlp", commandArguments[5]);
170176
Assert.Equal(pythonExecutable, commandArguments[6]);
171177
Assert.Equal(scriptName, commandArguments[7]);
172-
178+
173179
// If we don't throw, clean up the directories.
174180
Directory.Delete(projectDirectory, true);
175181
}
@@ -178,7 +184,7 @@ public async Task AddPythonProjectWithInstrumentation_SwitchesExecutableToInstru
178184
[RequiresTools(["python"])]
179185
public async Task AddPythonProjectWithScriptArgs_IncludesTheArguments()
180186
{
181-
using var builder = TestDistributedApplicationBuilder.Create();
187+
using var builder = CreateTestDistributedApplicationBuilder();
182188

183189
var (projectDirectory, pythonExecutable, scriptName) = CreateTempPythonProject(outputHelper);
184190

@@ -211,6 +217,13 @@ public async Task AddPythonProjectWithScriptArgs_IncludesTheArguments()
211217
Directory.Delete(projectDirectory, true);
212218
}
213219

220+
private TestDistributedApplicationBuilder CreateTestDistributedApplicationBuilder(Action<DistributedApplicationOptions>? configureOptions = null)
221+
{
222+
var builder = TestDistributedApplicationBuilder.Create(configureOptions);
223+
builder.Services.AddXunitLogging(outputHelper);
224+
return builder;
225+
}
226+
214227
private static (string projectDirectory, string pythonExecutable, string scriptName) CreateTempPythonProject(ITestOutputHelper outputHelper, bool instrument = false)
215228
{
216229
var projectDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>$(NetCurrent)</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<ProjectReference Include="..\..\src\Aspire.Hosting.AppHost\Aspire.Hosting.AppHost.csproj" />
9+
<ProjectReference Include="..\..\src\Aspire.Hosting.Python\Aspire.Hosting.Python.csproj" />
10+
<ProjectReference Include="..\Aspire.Hosting.Tests\Aspire.Hosting.Tests.csproj" />
11+
</ItemGroup>
12+
13+
</Project>

tests/Aspire.Hosting.Tests/Aspire.Hosting.Tests.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
<ProjectReference Include="..\..\src\Aspire.Hosting.MySql\Aspire.Hosting.MySql.csproj" IsAspireProjectResource="false" />
3636
<ProjectReference Include="..\..\src\Aspire.Hosting.Nats\Aspire.Hosting.Nats.csproj" IsAspireProjectResource="false" />
3737
<ProjectReference Include="..\..\src\Aspire.Hosting.Testing\Aspire.Hosting.Testing.csproj" />
38-
<ProjectReference Include="..\..\src\Aspire.Hosting.Python\Aspire.Hosting.Python.csproj" IsAspireProjectResource="false" />
3938
<ProjectReference Include="..\Aspire.Components.Common.Tests\Aspire.Components.Common.Tests.csproj" IsAspireProjectResource="false" />
4039
<ProjectReference Include="..\testproject\TestProject.AppHost\TestProject.AppHost.csproj" IsAspireProjectResource="false" />
4140
<ProjectReference Include="..\Aspire.Hosting.Tests.SharedShim\Aspire.Hosting.Tests.SharedShim.csproj" IsAspireProjectResource="false" Aliases="AspireHostingShared" />
@@ -44,7 +43,7 @@
4443
<ProjectReference Include="..\..\src\Components\Aspire.MySqlConnector\Aspire.MySqlConnector.csproj" IsAspireProjectResource="false" />
4544
<ProjectReference Include="..\..\src\Components\Aspire.Npgsql\Aspire.Npgsql.csproj" IsAspireProjectResource="false" />
4645
<ProjectReference Include="..\..\src\Components\Aspire.Oracle.EntityFrameworkCore\Aspire.Oracle.EntityFrameworkCore.csproj" IsAspireProjectResource="false" />
47-
46+
4847

4948
<ProjectReference Include="..\Aspire.Components.Common.Tests\Aspire.Components.Common.Tests.csproj" />
5049

tests/Aspire.Hosting.Tests/Utils/TestDistributedApplicationBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ public static TestDistributedApplicationBuilder Create(params string[] args)
4141
return new TestDistributedApplicationBuilder(options => options.Args = args);
4242
}
4343

44-
public static TestDistributedApplicationBuilder Create(Action<DistributedApplicationOptions> configureOptions)
44+
public static TestDistributedApplicationBuilder Create(Action<DistributedApplicationOptions>? configureOptions)
4545
{
4646
return new TestDistributedApplicationBuilder(configureOptions);
4747
}
4848

4949
public static TestDistributedApplicationBuilder CreateWithTestContainerRegistry() =>
5050
Create(o => o.ContainerRegistryOverride = TestConstants.AspireTestContainerRegistry);
5151

52-
private TestDistributedApplicationBuilder(Action<DistributedApplicationOptions> configureOptions)
52+
private TestDistributedApplicationBuilder(Action<DistributedApplicationOptions>? configureOptions)
5353
{
5454
var appAssembly = typeof(TestDistributedApplicationBuilder).Assembly;
5555
var assemblyName = appAssembly.FullName;
@@ -80,7 +80,7 @@ void Configure(DistributedApplicationOptions applicationOptions, HostApplication
8080
["DcpPublisher:ResourceNameSuffix"] = $"{Random.Shared.Next():x}",
8181
});
8282

83-
configureOptions(applicationOptions);
83+
configureOptions?.Invoke(applicationOptions);
8484
}
8585
}
8686

0 commit comments

Comments
 (0)